首页 > 解决方案 > Numpy 中的 Sigmoid 函数

问题描述

为了快速计算,我必须在 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'”我试图解决这个问题,但没有任何运气。我感谢任何建议。最好的

标签: numpysigmoid

解决方案


这对我有用。我认为不需要使用缓存,因为您已经初始化了它。试试下面的代码。

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

推荐阅读