python - 如何在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 中的使用,我已经到达了这里。我的预测输出与神经网络预测的结果大不相同。而我使用的是来自同一个训练模型的完全相同的值。
解决方案
您的输出将是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
.
推荐阅读
- react-native - 如何根据反应导航中的身份验证动态设置抽屉项目?
- java - 如何在java中使用unirest编写条件查询字符串
- c# - 在 OpenXML 中导致问题的“ProofError”元素
- javascript - 如何使用 Jquery 在输入文本框中验证允许减号(-)和仅数字?
- python - Python 非本地和全局如何协同工作?
- java - 即使使用前台服务,测距和监控也会在一段时间后停止
- php - Laravel snappy 文件未创建
- c - 使用联合计算 log2
- laravel - 如何在 Laravel 中搜索 MIN 和 MAX
- android - android sinch 后台音频和视频通话