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

时间:2021-08-07 18:19:37

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。

2 个解决方案

#1


14  

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)
print
# 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 "/[...]/*_sigmoid.py", line 24, in <module>
    print sigmoid(X, T)
  File "/[...]/*_sigmoid.py", line 14, in sigmoid
    return 1.0 / (1.0 + np.exp(-1.0 * np.dot(X, T)))
AttributeError: exp

#2


4  

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

你把np类型。点(X, T)到浮点32如下:

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

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

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

Hopefully it will finally work!

希望它最终能成功!

#1


14  

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)
print
# 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 "/[...]/*_sigmoid.py", line 24, in <module>
    print sigmoid(X, T)
  File "/[...]/*_sigmoid.py", line 14, in sigmoid
    return 1.0 / (1.0 + np.exp(-1.0 * np.dot(X, T)))
AttributeError: exp

#2


4  

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

你把np类型。点(X, T)到浮点32如下:

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

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

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

Hopefully it will finally work!

希望它最终能成功!