【问题标题】:loop of ufunc does not support argument 0 of type float which has no callable exp method, in some cases在某些情况下,ufunc 的循环不支持没有可调用 exp 方法的 float 类型的参数 0
【发布时间】:2021-01-21 07:51:37
【问题描述】:

当我给出以下命令时,它会起作用:

W2 = np.array( [[ 0.00874286,-0.01293537,-0.00079741,0.00564486,0.01233471]])
b2=np.array([[0.]])
a=np.array([[11.2,11.0,12.7],[10.1,34.4,4.0],[9.0,7.9,6.6],[8.5,8.0,9.45,],[5.5,9.9,8.3]])

Z = np.dot(W2,a)+b2
print(1/(1+np.exp(-Z)))

输出为:

[[0.51897047 0.4531795  0.55224658]]

但是当我按照它给出它会产生错误:

Z = np.dot(parameters['W2'],A)+parameters['b2']
print(1/(1+np.exp(-Z)))

其中 A, parameters['W2], parameters['b2'] 是 numpy 数组并且具有浮点值。 输出为:

AttributeError                            Traceback (most recent call last)
AttributeError: 'float' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-26-dc868b05af37> in <module>()
      3 Z = np.dot(parameters['W2'],A)+parameters['b2']
      4 #A1,C=linear_sigmoid_forward(A, parameters['W2'],parameters['b2'])
----> 5 print(1/(1+np.exp(-Z)))

TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method

在这两种情况下,我都给出了 float numpy 数组,但在第二种情况下会产生错误。为什么???

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    我遇到了同样的错误消息,因为我的数组是对象类型 (dtype=object) 我假设这是因为我在计算每日费率时在 deltatimes 和 floats 之间进行了权衡。我想您的 parameter 结构化数组字段不是浮点类型。我不确定,只是猜测。我通过指定数组的类型解决了这个问题。

    试试:

    Z = np.dot(parameters['W2'],A)+parameters['b2']
    print(1/(1+np.exp(-Z.astype(np.float)))
    

    您还可以检查您的字段类型是否在您的数组中正确设置

    【讨论】:

    • 您介意将答案标记为有效吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2021-02-14
    • 2021-12-13
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多