【问题标题】:How to use sigmoid function in python when developing a NeuralNetwork开发神经网络时如何在python中使用sigmoid函数
【发布时间】:2020-09-27 04:46:33
【问题描述】:

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

这是我的代码

另外,我在 Anaconda 中使用 Visual Studio 代码

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

【问题讨论】:

  • 你必须使用 self.sigmoid() 因为 sigmoid 是类的方法
  • 如果我在下面的回答对您有所帮助,请考虑支持并接受它。它会帮助我、你和其他试图回答类似问题的人。

标签: python neural-network sigmoid


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2017-12-03
    • 2017-07-28
    • 2012-07-25
    • 2020-08-30
    • 2014-03-26
    • 2017-07-11
    相关资源
    最近更新 更多