loop of ufunc does not support argument 0 of type float which has no callable exp method, in some cases

Viewed 3315

When I give following command it works:

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)))

Output is:

[[0.51897047 0.4531795  0.55224658]]

but when I give it following it generates error:

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

where A, parameters['W2], parameters['b2'] are numpy array and have float values. Output is:

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

In Both cases I've given float numpy arrays, but it generates error in second case. Why???

1 Answers

I encountered the same error message because my array was of type object (dtype=object) I am assuming it is because I juggled between deltatimes and floats when calculating daily rates. I suppose your parameter structured array fields are not of type float. I am not sure, just guessing. I solved the issue by specifying the type of the array.

Try:

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

You might also check that your fields type are properly set in your array

Related