Python Numpy AttributeError: 'float' 对象没有属性 'exp'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18557337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:05:38  来源:igfitidea点击:

Numpy AttributeError: 'float' object has no attribute 'exp'

pythonnumpy

提问by Il'ya Zhenin

Here is my code:

这是我的代码:

def sigmoid(X, T): return (1.0 / (1.0 + np.exp(-1.0*np.dot(X, T))))

And this line gives me error "AttributeError: 'float' object has no attribute 'exp'". X, t are Numpy ndarray.

这一行给了我错误“AttributeError:'float'对象没有属性'exp'”。X, t 是 Numpy ndarray。

回答by H.D.

Probably there's something wrong with the input values for X and/or T. The function from the question works ok:

X 和/或 T 的输入值可能有问题。问题中的函数工作正常:

import numpy as np
from math import e

def sigmoid(X, T):
  return 1.0 / (1.0 + np.exp(-1.0 * np.dot(X, T)))

X = np.array([[1, 2, 3], [5, 0, 0]])
T = np.array([[1, 2], [1, 1], [4, 4]])

print(X.dot(T))
# Just to see if values are ok
print([1. / (1. + e ** el) for el in [-5, -10, -15, -16]])
print()
print(sigmoid(X, T))

Result:

结果:

[[15 16]
 [ 5 10]]

[0.9933071490757153, 0.9999546021312976, 0.999999694097773, 0.9999998874648379]

[[ 0.99999969  0.99999989]
 [ 0.99330715  0.9999546 ]]

Probably it's the dtype of your input arrays. Changing X to:

可能是您输入数组的 dtype。将 X 更改为:

X = np.array([[1, 2, 3], [5, 0, 0]], dtype=object)

Gives:

给出:

Traceback (most recent call last):
  File "/[...]/stackoverflow_sigmoid.py", line 24, in <module>
    print sigmoid(X, T)
  File "/[...]/stackoverflow_sigmoid.py", line 14, in sigmoid
    return 1.0 / (1.0 + np.exp(-1.0 * np.dot(X, T)))
AttributeError: exp

回答by mehtab rai

You convert type np.dot(X, T)to float32 like this:

您可以np.dot(X, T)像这样将类型转换为 float32:

z=np.array(np.dot(X, T),dtype=np.float32)

z=np.array(np.dot(X, T),dtype=np.float32)

def sigmoid(X, T):
    return (1.0 / (1.0 + np.exp(-z)))

Hopefully it will finally work!

希望它最终会起作用!