首页 > 解决方案 > 如何在python中找到分类神经网络的预测输出?

问题描述

我是 python 和学习神经网络的新手。我有一个训练有素的 3 层前馈神经网络,隐藏层有 2 个神经元,输出层有 3 个神经元。我想知道如何计算输出层值/预测输出

我从网络中提取了权重和偏差,并计算了隐藏层的激活值。我只是想确认如何使用softmax函数来计算输出层神经元的输出?

我的实现如下:

weights_from_hiddenLayer_to_OutputLayer = [
    [x, y],  # two weights connected to the output neuron 1 from hidden neurons 1 and 2
    [a, b],  # two weights connected to the output neuron 2 from hidden neurons 1 and 2
    [c, d]   # two weights connected to the output neuron 3 from hidden neurons 1 and 2I
    ]

# output layer biases extracted from the neural network
biases_output_layer = [a, b, c]

act1 = m  # activation value of hidden neuron 1
act2 = n  # activation value of hidden neuron 2
arr = []
for i, weights in enumerate(weights_from_hiddenLayer_to_OutputLayer):
            arr.append(m*weights[0]+n*weights[1] +
                       biases_output_layer[i])
# i believe this will be the brightest neuron / predicted neural networks output ?  
print(np.argmax(arr))

我已经在互联网上搜索了softmax在 python 中的使用,我已经到达了这里。我的预测输出与神经网络预测的结果大不相同。而我使用的是来自同一个训练模型的完全相同的值。

标签: pythonneural-networksoftmax

解决方案


您的输出将是weights_from_hiddenLayer_to_OutputLayer之前激活的矩阵乘法。然后你可以通过softmax 函数来得到一个概率分布,并使用argmax你猜到的来得到相应的类。

weights_from_hiddenLayer_to_OutputLayer = np.array([
    [x, y],  # two weights connected to the output neuron 1 from hidden neurons 1 and 2
    [a, b],  # two weights connected to the output neuron 2 from hidden neurons 1 and 2
    [c, d]   # two weights connected to the output neuron 3 from hidden neurons 1 and 2I
    ])

act = np.array([m, n])
biases_output_layer = [a, b, c]
arr = np.dot(weights_from_hiddenLayer_to_OutputLayer, act)    # matrix multiplication of weights and activations
arr = arr + biases_output_layer
     
probability = np.exp(arr) / np.sum(np.exp(arr), axis=0)       # softmax
print(np.argmax(probability))

请注意,从技术上讲,您不需要使用 softmax,除非您正在反向传播或尝试评估输出的置信度,因为np.argmax()无论您是否传入arr或相应的probability.


推荐阅读