【问题标题】:Sigmoid derivative in gradient descent梯度下降中的 Sigmoid 导数
【发布时间】:2020-10-31 08:22:32
【问题描述】:

This 是 James Loy 编写的神经网络。

问题是在调整权重时,旧的权重被添加到梯度向量中而不是减去:self.weights1 += d_weights1

this 的帖子中,它表明 sigmoid 导数缺少一个将被补偿的负号。

缺失的地方在哪里,如果没有缺失,sigmoid 导数应该是什么?

Sigmoid 导数实现:

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

def sigmoid_derivative(x):
    return x * (1.0 - x)

整个实现:

import numpy as np

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

def sigmoid_derivative(x):
    return x * (1.0 - x)

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 feedforward(self):
        self.layer1 = sigmoid(np.dot(self.input, self.weights1))
        self.output = sigmoid(np.dot(self.layer1, self.weights2))

    def backprop(self):
        # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1
        d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
        d_weights1 = np.dot(self.input.T,  (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))

        # update the weights with the derivative (slope) of the loss function
        self.weights1 += d_weights1
        self.weights2 += d_weights2


if __name__ == "__main__":
    X = np.array([[0,0,1],
                  [0,1,1],
                  [1,0,1],
                  [1,1,1]])
    y = np.array([[0],[1],[1],[0]])
    nn = NeuralNetwork(X,y)

    for i in range(1500):
        nn.feedforward()
        nn.backprop()

    print(nn.output)

【问题讨论】:

    标签: neural-network gradient-descent derivative sigmoid


    【解决方案1】:
    def sigmoid_derivative(x):
        return x * (1.0 - x)
    

    应该改为

    def sigmoid_derivative(x):
        return sigmoid(x) * (1.0 - sigmoid(x))
    

    希望这能解决您的问题。导出的方程来自简单的微分。你可以在这里查看导数:Sigmoid

    【讨论】:

      猜你喜欢
      • 2019-08-29
      • 2021-12-18
      • 2016-09-25
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 2017-06-23
      相关资源
      最近更新 更多