【问题标题】:Deep learning: the code for backpropagation in Python深度学习:Python 中的反向传播代码
【发布时间】:2021-04-19 10:13:02
【问题描述】:

我正在阅读free online book,但我正在努力处理某些代码。

(代码来源于 Michael Nielsen)

class Network(object):

def update_mini_batch(self, mini_batch, eta):
        """Update the network's weights and biases by applying
        gradient descent using backpropagation to a single mini batch.
        The "mini_batch" is a list of tuples "(x, y)", and "eta"
        is the learning rate."""
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        for x, y in mini_batch:
            delta_nabla_b, delta_nabla_w = self.backprop(x, y)
            nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
            nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
        self.weights = [w-(eta/len(mini_batch))*nw 
                        for w, nw in zip(self.weights, nabla_w)]
        self.biases = [b-(eta/len(mini_batch))*nb 
                       for b, nb in zip(self.biases, nabla_b)]


def backprop(self, x, y):
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        # feedforward
        activation = x
        activations = [x] # list to store all the activations, layer by layer
        zs = [] # list to store all the z vectors, layer by layer
        for b, w in zip(self.biases, self.weights):
            z = np.dot(w, activation)+b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)
        # backward pass

因为它说mini_batch 是一个元组列表(x, y),所以函数backpropx 的参数是一个标量,对吧?如果是这样,因为w(权重)是一个矩阵(比如它的维度是n*p),其行在lth 层中有n 神经元,而在l-1 层中有p 神经元.那么,x 必须是 n x 1 向量。我感到困惑。

在本书的示例中,它使用了[2,3,1],即三层,分别有 2,3 和 1 个神经元。因为第一层输入,它有两个元素。所以第 2 层的权重矩阵有 3*2 维。看来x 应该是一个长度为2 的向量来与w 进行矩阵乘法。

另外,C_x 对激活 a 的偏导代码如下:

def cost_derivative(self, output_activations, y):
        """Return the vector of partial derivatives \partial C_x /
        \partial a for the output activations."""
        return (output_activations-y) 

我检查了我理解的公式,(output_activations-y)表示成本的变化。但这应该除以激活的变化吗?

你能帮帮我吗?

【问题讨论】:

    标签: python deep-learning backpropagation


    【解决方案1】:

    因为它说“mini_batch”是一个元组列表“(x, y)”,所以函数backprop中x的参数是一个标量,对吧?

    没有。单词“batch”对应一个python列表。在批处理/python 列表中有像(x, y) 这样的对,其中x 代表输入向量,y 是标签。 x 的形状取决于您创建网络对象的方式。在[2, 3, 1] 的情况下,x 应该是形状为(2,) 的向量。

    但这应该除以激活的变化吗?

    没有。

    首先,您所考虑的称为“数值微分”。既然你没有成本的变化,你不应该用激活的变化来分割它。

    其次,作者使用的东西叫做“解析微分”。比如说,你有一个函数f(x, y) = 0.5*(x-y)^2f w.r.t 的偏导数。 xx-y。因此,您不需要将其除以x 的变化。但是,您需要注意使用的实际成本函数才能得出正确的导数。有时,如何计算这些值并不明显。在这种情况下,损失是均方误差,如在线书籍中所述。


    回复评论:

    输入为vector_x = (1,2,3)的训练集

    训练集应该是一个包含一组训练样本的容器,其中每个训练样本由一个输入和相应的标签组成。因此,小批量的一个示例可能是一个 python 列表,例如:[([0, 1], [1, 2]), ([2, 1], [3, 2])],它表示有两个训练样本。第一个是([0, 1], [1, 2]),第二个是([2, 1], [3, 2])。以第一个为例,它的输入是[0, 1](形状为(2,)的向量),它的输出是[1, 2],这意味着单个训练样本的输入和期望输出都可以是向量。您的问题 ([(1,a),(2,b),(3,c)]) 有一些模棱两可之处,所以我更喜欢我的解释。

    【讨论】:

    • 你救我!非常感谢。关于 mini_batch,假设有一个训练集,其输入为 vector_x = (1,2,3),其对应的输出为 vector_y = (a,b,c)。比 mini_batch 的形式是 [(vector_x, vector_y)] 而不是 [(1,a),(2,b),(3,c)],对吧?
    • 感谢您的快速回复。它非常清晰和有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2013-03-09
    • 2018-11-03
    相关资源
    最近更新 更多