【问题标题】:Python OOP functionPython OOP 函数
【发布时间】:2026-01-27 03:20:10
【问题描述】:

我需要编写一个具有功能性的类

x = np.linspace(-10., 10., num=100)
sig = Sigmoid()(x)
sig_prime = Sigmoid().prime(x)

这就是模板:

class Sigmoid:
    def __call__(self, z):
        """
        Compute the sigmoid of z
        Arguments:
        z -- scalar or numpy array of any size.
        Return:
        sigmoid(z)
        """
        sigmoid = 1/np.exp(-z)
        return sigmoid
    
    def prime(self, z):
        """
        Compute the derivative of sigmoid of z
        Arguments:
        z -- scalar or numpy array of any size.
        Return:
        Sigmoid prime
        """
        ###return sigmoid * (1 - sigmoid)

拜托,你能帮我写函数 def prime(self, z) 吗?因为我不知道该怎么做。

【问题讨论】:

    标签: python class oop call


    【解决方案1】:
    def prime(self, z):
      s = 1/np.exp(-z)
      ds = s*(1-s)
      return ds
    

    找到它here

    【讨论】:

    • 非常感谢,我想我应该以某种方式使用函数“call”非常感谢!