1

在过去的几天里,我一直在调试我的神经网络,但我找不到问题。

我创建了用于识别 MNIST 数据集图像的多层感知器的完整原始实现。

网络似乎在学习,因为经过训练周期测试数据的准确率超过 94%。我的损失函数有问题 - 它会在一段时间后开始增加,当测试/验证准确度达到 ~76% 时。

有人可以检查我的前向/反向传播数学并告诉我我的损失函数是否正确实现,或者建议可能有什么问题?

神经网络结构:

  • 输入层:758 个节点,(每个像素 1 个节点)
  • 隐藏层 1:300 个节点
  • 隐藏层 2:75 个节点
  • 输出层:10个节点

NN激活函数:

  • 输入层 -> 隐藏层 1:ReLU
  • 隐藏层 1 -> 隐藏层 2:ReLU
  • 隐藏层 2 -> 输出层 3:Softmax

NN损失函数:

  • 分类交叉熵

完整的 CLEAN 代码可在此处作为 Jupyter Notebook 获得。

神经网络前向/后向传播:

def train(self, features, targets):
        n_records = features.shape[0]

        # placeholders for weights and biases change values
        delta_weights_i_h1 = np.zeros(self.weights_i_to_h1.shape)
        delta_weights_h1_h2 = np.zeros(self.weights_h1_to_h2.shape)
        delta_weights_h2_o = np.zeros(self.weights_h2_to_o.shape)
        delta_bias_i_h1 = np.zeros(self.bias_i_to_h1.shape)
        delta_bias_h1_h2 = np.zeros(self.bias_h1_to_h2.shape)
        delta_bias_h2_o = np.zeros(self.bias_h2_to_o.shape)

        for X, y in zip(features, targets):
            ### forward pass
            # input to hidden 1
            inputs_to_h1_layer = np.dot(X, self.weights_i_to_h1) + self.bias_i_to_h1
            inputs_to_h1_layer_activated = self.activation_ReLU(inputs_to_h1_layer)

            # hidden 1 to hidden 2
            h1_to_h2_layer = np.dot(inputs_to_h1_layer_activated, self.weights_h1_to_h2) + self.bias_h1_to_h2
            h1_to_h2_layer_activated = self.activation_ReLU(h1_to_h2_layer)

            # hidden 2 to output
            h2_to_output_layer = np.dot(h1_to_h2_layer_activated, self.weights_h2_to_o) + self.bias_h2_to_o
            h2_to_output_layer_activated = self.softmax(h2_to_output_layer)

            # output
            final_outputs = h2_to_output_layer_activated 

            ### backpropagation
            # output to hidden2
            error = y - final_outputs
            output_error_term = error.dot(self.dsoftmax(h2_to_output_layer_activated))

            h2_error = np.dot(output_error_term, self.weights_h2_to_o.T)
            h2_error_term = h2_error * self.activation_dReLU(h1_to_h2_layer_activated)

            # hidden2 to hidden1
            h1_error = np.dot(h2_error_term, self.weights_h1_to_h2.T) 
            h1_error_term = h1_error * self.activation_dReLU(inputs_to_h1_layer_activated)

            # weight & bias step (input to hidden)
            delta_weights_i_h1 += h1_error_term * X[:, None]
            delta_bias_i_h1 = np.sum(h1_error_term, axis=0)

            # weight & bias step (hidden1 to hidden2)
            delta_weights_h1_h2 += h2_error_term * inputs_to_h1_layer_activated[:, None]
            delta_bias_h1_h2 = np.sum(h2_error_term, axis=0)

            # weight & bias step (hidden2 to output)
            delta_weights_h2_o += output_error_term * h1_to_h2_layer_activated[:, None]
            delta_bias_h2_o = np.sum(output_error_term, axis=0)

        # update the weights and biases     
        self.weights_i_to_h1 += self.lr * delta_weights_i_h1 / n_records
        self.weights_h1_to_h2 += self.lr * delta_weights_h1_h2 / n_records
        self.weights_h2_to_o += self.lr * delta_weights_h2_o / n_records
        self.bias_i_to_h1 += self.lr * delta_bias_i_h1 / n_records
        self.bias_h1_to_h2 += self.lr * delta_bias_h1_h2 / n_records
        self.bias_h2_to_o += self.lr * delta_bias_h2_o / n_records

激活函数实现:

def activation_ReLU(self, x):
    return x * (x > 0)

def activation_dReLU(self, x):
    return 1. * (x > 0)

def softmax(self, x):
    z = x - np.max(x)
    return np.exp(z) / np.sum(np.exp(z))

def dsoftmax(self, x):
    # TODO: vectorise math
    vec_len = len(x)
    J = np.zeros((vec_len, vec_len))
    for i in range(vec_len):
        for j in range(vec_len):
            if i == j:
                J[i][j] = x[i] * (1 - x[j])
            else:
                J[i][j] = -x[i] * x[j]
    return J

损失函数实现:

def categorical_cross_entropy(pred, target): 
    return (1/len(pred)) * -np.sum(target * np.log(pred))
4

1 回答 1

0

我设法找到了问题。

神经网络很大,所以我不能把所有东西都放在这个问题上。虽然如果你查看我的 Jupiter Notebook,你可以看到我的 Softmax 激活函数的实现以及我如何在训练周期中使用它。

损失计算错误的问题是由于我的 Softmax 实现仅适用于 ndarray dim == 1

在训练步骤中,我只将带有 dim 1 的 ndarray 放入了激活函数,因此 NN 学得很好,但是我的run()函数返回了错误的预测,因为我已将整个测试数据插入其中,而不仅仅是 for 循环中的单行。因此,它计算 Softmax “矩阵方式”而不是“行方式”。

这是非常快速的解决方法:

   def softmax(self, x):
        # TODO: vectorise math to speed up computation
        softmax_result = None
        if x.ndim == 1:
            z = x - np.max(x)
            softmax_result = np.exp(z) / np.sum(np.exp(z))
            return softmax_result
        else:
            softmax_result = []
            for row in x:
                z = row - np.max(row)
                row_softmax_result = np.exp(z) / np.sum(np.exp(z))
                softmax_result.append(row_softmax_result)
            return np.array(softmax_result)

然而,这段代码应该被向量化以避免for循环和如果可能的话,因为目前它很丑陋并且占用了太多的PC资源。

于 2017-06-27T20:23:06.670 回答