【问题标题】:Where is the sigmoid derivative used in the backpropagation algorithm and how are weights updated反向传播算法中使用的 sigmoid 导数在哪里以及权重如何更新
【发布时间】:2021-11-24 00:27:35
【问题描述】:

我是一名 10 年级的学生,正在尝试学习神经网络如何在 Python 代码中工作。我没有多少微积分知识,只是对导数以及如何找到它们的理解有限。

我使用 numpy 在 python 中制作了一个简单的前馈网络。我已经设置了带有 feed_forward 函数的层类,以矩阵将层输入与层权重相乘,并将 sigmoid 函数应用于它们以实现我理解的输出,该输出通过网络传递到下一层,依此类推。我相信我对前馈有正确的了解,但我不明白错误是如何通过网络反向传播的,以及权重是如何更新的。互联网上的资源似乎深入到微积分中,然后我失去了很多理解。

这是我目前的代码:

class Layer:
    def __init__(self, neurons, connections, type):
        self.neurons = neurons
        self.weights = np.random.randn(neurons, connections)
        self.type = type
        self.learning_rate = .1

    def sigmoid(self, value):
        return 1.0 / (1.0 + np.exp(-value))

    def sigmoid_derr(self, value):
        return value * (1.0 - value)

    def feed_forward(self, input):
        self.input = input
        self.output = self.sigmoid(np.dot(input, self.weights))
        return self.output
 l1.feed_forward([0.15, 0.895])
 l2.feed_forward(l1.output)
 l3.feed_forward(l2.output)

我尝试在前馈之后计算误差,并对其应用平方误差,然后通过类似于前馈的网络反向传播,但使用转置权重通过网络反向传播。然后,我尝试将每一层的输出乘以 2 作为导数,并将该导出误差添加到每个神经元连接的权重中。但是,当我遍历输出时,就接近了一个。感谢所有帮助。

更新: 我在 youtube 上找到了一段视频,它在一定程度上解释了我想要实现的目标,并且我尝试自己实现它,但是在某些运行中,网络试图让自己尽可能接近 0.519。我不确定为什么。 这是代码:

import numpy as np

INPUT = 'INPUT'
HIDDEN = 'HIDDEN'
OUTPUT = 'OUTPUT'


class Layer:
    def __init__(self, neurons, connections, type):
        self.neurons = neurons
        self.weights = np.random.randn(neurons, connections)
        self.type = type
        self.learning_rate = .1

    def sigmoid(self, value):
        return 1.0 / (1.0 + np.exp(-value))

    def sigmoid_derr(self, value):
        return value * (1-value)
        #return self.sigmoid(value) * (1.0 - self.sigmoid(value))

    def cost_derivative(self, expected, actual):
        return np.multiply(np.power(np.subtract(expected, actual),2),2)

    def feed_forward(self, input):
        self.input = input
        self.output = self.sigmoid(np.dot(input, self.weights))
        return self.output

    def backprop(self, expected=[], prevError=None):  # fowardLayerWeights is the previous backpropagated layers weights
        if self.type == OUTPUT:
            self.error = np.subtract(expected, self.output)
            self.delta = np.multiply(self.error, self.sigmoid_derr(self.output))

            self.error = np.dot(self.error, self.weights.T)
        else:
            self.delta = np.multiply(prevError, self.sigmoid_derr(self.output))
            self.error = np.dot(prevError, self.weights.T)



l1 = Layer(2, 3, type=INPUT)
l2 = Layer(3, 4, type=HIDDEN)
l3 = Layer(4, 2, type=OUTPUT)

for i in range(10000):
    l1.feed_forward([0.26, 0.87])
    l2.feed_forward(l1.output)
    l3.feed_forward(l2.output)

    l3.backprop(expected=[0.12, 0.92])
    l2.backprop(prevError=l3.error)
    l1.backprop(prevError=l2.error)

    #print(l3.output)

    l3.weights += np.dot(l3.output.T, l3.delta)
    l2.weights += np.dot(l2.output.T, l2.delta)
    l1.weights += np.dot(l1.output.T, l1.delta)

    print(l3.output)

如果你想要,这就是视频:https://www.youtube.com/watch?v=h3l4qz76JhQ

【问题讨论】:

    标签: python numpy deep-learning backpropagation


    【解决方案1】:

    我假设你的反向传播是 sigmoid_derr 函数,但实现不适合 sigmoid 的导数。

    S`(x) = S(x)[1-S(x)]

    其中 S(x) 是 sigmoid 推导。更多信息请查看this link

    【讨论】:

    • 之所以这样,是因为我传递给函数的值已经通过 sigmoid。我看过的其他来源似乎使用了这个实现。我应该再次通过 sigmoid 吗?
    • 在一张纸上试一试。导数不会一样...
    猜你喜欢
    • 2016-04-14
    • 2012-07-22
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2013-01-22
    • 2017-12-10
    • 1970-01-01
    相关资源
    最近更新 更多