31

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.

5
  • 5
    Looks like you re-assigned np to a float value. Commented Sep 1, 2013 at 10:13
  • 6
    Would X or T happen to have accidentally been created with a dtype of object instead of float64? Commented Sep 1, 2013 at 10:13
  • 2
    No, re-assigned was not happen. type(X) is numpy ndarray, type(X[0][0]) is float Commented Sep 1, 2013 at 12:45
  • 3
    Hm. But when i implement X = X.astype(float), everything works fine. Commented Sep 1, 2013 at 13:09
  • 2
    Try print X.dtype, T.dtype and see what you get. You most likely have the wrong dtype. Commented Sep 2, 2013 at 6:50

2 Answers 2

22

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

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:

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
Sign up to request clarification or add additional context in comments.

3 Comments

Yes thanks. I didn't know about dtype and used just type(X). I did X = X.astype(float) and it is worked.
so array operations like mean() could not be used in arrays with dype=object? I wonder why?
this error message is very misleading: the problem is that the dtype is in fact numpy.object, but the message says numpy.float64 has no attribute log10, or whatever arithmetic method
9

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

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.