为了快速计算,我必须在Numpy中实现我的sigmoid函数,这是下面的代码
def sigmoid(Z):
"""
Implements the sigmoid activation in bumpy
Arguments:
Z -- numpy array of any shape
Returns:
A -- output of sigmoid(z), same shape as Z
cache -- returns Z, useful during backpropagation
"""
cache=Z
print(type(Z))
print(Z)
A=1/(1+(np.exp((-Z))))
return A, cache
还提供了一些相关信息:
Z=(np.matmul(W,A)+b)
Z的类型是:
<class 'numpy.ndarray'>
不幸的是,我得到了一个:“一元-的坏操作数类型:'tuple‘”,我一直试图解决这个问题,没有任何运气,我很感激任何建议。最好的
发布于 2020-03-18 15:24:40
这对我有用。我认为不需要使用缓存,因为你已经初始化了它。试试下面的代码。
import matplotlib.pyplot as plt
import numpy as np
z = np.linspace(-10, 10, 100)
def sigmoid(z):
return 1/(1 + np.exp(-z))
a = sigmoid(z)
plt.plot(z, a)
plt.xlabel("z")
plt.ylabel("sigmoid(z)")
发布于 2022-11-06 13:55:41
您很可能会看到此错误,因为您的Z
(类型为numpy.ndarray
)包含一个元组。例如,考虑Z
的这个定义。
Z = np.array([1, (1, 2, 3), 3])
如果您在这个sigmoid
上运行您的Z
函数,它将打印Z
是一个numpy.ndarray
,但是生成您得到的TypeError
,因为numpy在Z
的成员之间广播一元否定,其中一个是元组,而tuple不实现一元否定。
https://stackoverflow.com/questions/60746851
复制相似问题