numpy AttributeError:"float"对象没有属性"exp" [英] Numpy AttributeError: 'float' object has no attribute 'exp'

查看:139
本文介绍了numpy AttributeError:"float"对象没有属性"exp"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

这行给我错误"AttributeError:"float"对象没有属性"exp"". X,t是Numpy ndarray.

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

推荐答案

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

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

结果:

[[15 16]
 [ 5 10]]

[0.9933071490757153, 0.9999546021312976, 0.999999694097773, 0.9999998874648379]

[[ 0.99999969  0.99999989]
 [ 0.99330715  0.9999546 ]]

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

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

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

赠予:

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

这篇关于numpy AttributeError:"float"对象没有属性"exp"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆