简体   繁体   English

TypeError: ufunc 的循环不支持没有可调用日志方法的 ArrayBox 类型的参数 0

[英]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.

相关问题 TypeError: ufunc 的循环不支持 Symbol 类型的参数 0,它没有可调用的 sqrt 方法 - TypeError: loop of ufunc does not support argument 0 of type Symbol which has no callable sqrt method 类型错误:ufunc 的循环不支持没有可调用 sin 方法的 Add 类型的参数 0 - TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method TypeError:ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method ufunc 的到期循环中的错误不支持 str 类型的参数 0,该参数没有可调用的日志方法 - Error in due loop of ufunc does not support argument 0 of type str which has no callable log method ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - loop of ufunc does not support argument 0 of type float which has no callable exp method python3 np.exp(matrix1 * matrix2)中的错误-“ufunc循环不支持float类型的参数0,它没有可调用的exp方法” - Error in python3 np.exp(matrix1 * matrix2) - “loop of ufunc does not support argument 0 of type float which has no callable exp method” 为什么我会收到日志方法的“ufunc 循环不支持 numpy.ndarray 类型的参数 0”错误? - Why do I get the 'loop of ufunc does not support argument 0 of type numpy.ndarray' error for log method? nplog“TypeError:ufunc循环不支持int类型的参数0”后的PYTHON错误 - PYTHON Error after nplog "TypeError: loop of ufunc does not support argument 0 of type int " 错误类型 ufunc 的错误循环不支持参数 0 pyomo - Error TypeError loop of ufunc does not support argument 0 pyomo “ufunc循环不支持Mul类型的参数0”是什么意思? - What does "loop of ufunc does not support argument 0 of type Mul" mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM