开发神经网络时如何在python中使用sigmoid函数

How to use sigmoid function in python when developing a NeuralNetwork

我正在尝试创建一个神经网络,但是当我尝试实现 sigmoid 函数(像本例一样导入或手动创建它)时,它只是说变量 sigmoid 不存在

这是我的代码

此外,我正在使用 visual studio 代码与 Anaconda

from matplotlib import pylab
import pylab as plt
import numpy as np

class NeuralNetwork:
    def __init__(self,x,y):
        self.input = x
        self.weights1 = np.random.rand(self.input.shape[1],4)
        self.weights2 = np.random.rand(4,1)
        self.y = y
        self.output = np.zeros(self.y.shape)


    def sigmoid(self,x):
        return (1/(1+np.exp(-x)))      



    def feedforward(self):
        self.layer1 = sigmoid(np.dot(self.input, self.weights1))
        self.output = sigmoid(np.dot(self.layer1, self.weights2))```

您已将 sigmoid 定义为 class 方法,因此您需要像这样使用 self 关键字

self.layer1 = self.sigmoid(np.dot(self.input, self.weights1))
self.output = self.sigmoid(np.dot(self.layer1, self.weights2))