我目前正在尝试使用 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