python - Python中的神经网络概率计算
问题描述
我目前正在尝试使用 python 库中的 mlpclassifier 提供的权重矩阵和偏差向量手动计算我的神经网络的输出概率。目标是从 mlp.predict_proba 获得相同的输出。不幸的是,由于未知原因,我无法计算它。首先我执行测试数据和第一个权重矩阵之间的内积,添加来自同一层的偏置向量,然后计算激活函数(在这种情况下为'relu')......等等直到输出层. 您可以在下面找到我正在使用的代码以及一些附加说明。
# compute predictions using matrixes of weights
import numpy as np
# matrixes of weights and bias
theta1 = mlp.coefs_[0] # 13 x 14 matrix
bias1 = mlp.intercepts_[0] # 14 x 1 vector
theta2 = mlp.coefs_[1] # 14 x 13 matrix
bias2 = mlp.intercepts_[1] # 13 x 1 vector
theta3 = mlp.coefs_[2] # 13 x 12 matrix
bias3 = mlp.intercepts_[2] # 12 x 1 matrix
theta4 = mlp.coefs_[3] # 12 x 3 matrix
bias4 = mlp.intercepts_[3] # 3 x 1 vector
def relu(X):
return np.maximum(0,X)
def probCalc(X_test): # X_test with attributes along columns 45x13 matrix,values between 0 - 1.
# number of layers calculation
nLayer = len(mlp.hidden_layer_sizes)
j = True
# weights matrixes and layers calculation
for i in range(nLayer+1):
if j == True:
theta = mlp.coefs_[i]
bias = mlp.intercepts_[i]
a = relu(np.dot(X_test, theta) + bias)
j = False
else:
theta = mlp.coefs_[i]
bias = mlp.intercepts_[i]
a = relu(np.dot(a, theta) + bias)
return a
myProbCalc = probCalc(X_test)
提前谢谢你:) Joao
解决方案
您的最后一层很可能实际上没有 relu 激活。没有理由通过 relu 激活获得 0 和 1 之间的数字(其总和为 1)。
用 softmax 函数代替最后一层激活。
推荐阅读
- assembly - 堆栈指针下的值会消失吗
- python - 尝试将 1 添加到引号的末尾,以便在循环时更改链接
- amazon-web-services - 获取 AWS 安全组规则 ID
- node.js - 从 pcap NodeJS 解密 SSL 数据
- r - 有没有办法在 R 中添加两个连续列?
- python - 不允许我将整数放入变量中(python 以下教程的新手)
- pyspark - 如何使用结构化流从数据库中读取传入数据
- seaborn - Seaborn 热图仅适用于列的子集
- azure - Power BI 和 Azure 密钥保管库
- jquery - ASP.NET MVC:关闭后对话框不起作用