Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在两个train/val数据集上,当acc增加时,神经网络损失开始增加_Python_Neural Network_Softmax_Cross Entropy - Fatal编程技术网

Python 在两个train/val数据集上,当acc增加时,神经网络损失开始增加

Python 在两个train/val数据集上,当acc增加时,神经网络损失开始增加,python,neural-network,softmax,cross-entropy,Python,Neural Network,Softmax,Cross Entropy,在过去的几天里,我一直在调试我的NN,但我找不到问题 我创建了多层感知器的完整原始实现,用于识别MNIST数据集图像 网络似乎在学习,因为列车循环测试后的数据精度高于94%的精度。我有损失函数的问题-它开始增加一段时间后,当测试/val精度达到约76% 有人能检查我的正向/反向道具数学,告诉我我的损失函数是否正确执行,或者建议可能的错误吗? NN结构: 输入层:758个节点(每像素1个节点) 隐藏层1:300节点 隐藏层2:75个节点 输出层:10个节点 NN激活功能: 输入层->隐藏层1

在过去的几天里,我一直在调试我的NN,但我找不到问题

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

网络似乎在学习,因为列车循环测试后的数据精度高于94%的精度。我有损失函数的问题-它开始增加一段时间后,当测试/val精度达到约76%

有人能检查我的正向/反向道具数学,告诉我我的损失函数是否正确执行,或者建议可能的错误吗?

NN结构:

  • 输入层:758个节点(每像素1个节点)
  • 隐藏层1:300节点
  • 隐藏层2:75个节点
  • 输出层:10个节点
NN激活功能:

  • 输入层->隐藏层1:ReLU
  • 隐藏层1->隐藏层2:ReLU
  • 隐藏层2->输出层3:Softmax
NN损失函数:

  • 范畴交叉熵

神经网络正向/反向传递:

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))
损失函数实现:

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

我设法找到了问题

神经网络很大,所以我不能完全坚持这个问题。不过,若你们查看我的Jupiter笔记本,你们可以看到我的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循环和ifs,因为目前它很难看并且占用了太多的PC资源

我设法找到了问题

神经网络很大,所以我不能完全坚持这个问题。不过,若你们查看我的Jupiter笔记本,你们可以看到我的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循环和ifs,因为目前它很难看并且占用了太多的PC资源

一个想法可能是在Tensorflow中实现这一点并检查渐变是否匹配一个想法可能是在Tensorflow中实现这一点并检查渐变是否匹配