[英]TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable log method
I am using auto grad library of python to calculate gradient of cost function of logistic regression.我正在使用 python 的自动毕业库来计算逻辑回归成本 function 的梯度。
Here is the code for fit method of LR:下面是 LR 的 fit 方法的代码:
import numpy as np
import pandas as pd
class LogisticRegression:
def __init__(self) -> None:
self.num_of_iterations = None
self.thetas = None
self.tolerance = None
self.thetas_history = []
self.cost_func_history = []
def fit_autograd(self, X, y, tol, n_iter=100, lr=0.01, fit_intercept=True):
from autograd import grad
from autograd import elementwise_grad as egrad
import autograd.numpy as npa
from math import e
def training_loss(weights):
# Training loss is the negative log-likelihood of the training labels.
preds = (1/(1 + e**( np.dot(X_il, weights) )) - 0.00001)
label_probabilities = preds * y_il + (1 - preds) * (1 - y_il)
return -np.sum(np.log(label_probabilities))
# handling fit intercept param
if (fit_intercept == True):
self.num_of_thetas = len(list(X.columns))+1
thetas = pd.Series(np.random.randn(self.num_of_thetas))
bias = pd.DataFrame(pd.Series([1.0 for i in range(len(X))]))
X = pd.concat([bias,X],axis=1)
else:
self.num_of_thetas = len(list(X.columns))
self.thetas = pd.Series(np.random.randn(self.num_of_thetas))
self.num_of_samples = len(X)
self.num_of_iterations = n_iter
self.learning_rate = lr
self.tolerance = tol
self.fit_intercept = fit_intercept
mygrad = grad(training_loss)
X_il,y_il = X.to_numpy(),y.to_numpy()
theta_il = thetas.to_numpy()
for it in range(self.num_of_iterations):
temp_grad = mygrad(theta_il)
print(temp_grad)
print("thetas:",thetas)
thetas -= (lr/self.num_of_samples) * temp_grad
self.thetas_history.append(thetas)
self.cost_func_history.append(temp_grad)
return thetas
It is throwing following error.它抛出以下错误。
TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable log method
I have checked following issue on github using which i was able to resolve for np.exp().我已经检查了github上的以下问题,使用它我能够解决 np.exp()。 I am not sure how to do it for np.log().我不确定如何为 np.log() 执行此操作。
Thanks谢谢
I believe you need to use import autograd.numpy as np
instead of import numpy as np
.我相信您需要使用import autograd.numpy as np
而不是import numpy as np
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.