Python 在其他编程语言上使用Matlab中导出的神经网络权重
我在使用python复制matlab中创建的神经网络时遇到问题。它是一个Python 在其他编程语言上使用Matlab中导出的神经网络权重,python,matlab,pattern-matching,neural-network,Python,Matlab,Pattern Matching,Neural Network,我在使用python复制matlab中创建的神经网络时遇到问题。它是一个{9,8,4}网络。下面分别是matlab和python的原始输出 0.00187283763854096 0.00280257304094145 0.00709416898379967 0.00474275971385824 0.000545071722266366 0.0520122170317888 0.0402746073491970 0.0179208146529717 0.0245726107168336
{9,8,4}
网络。下面分别是matlab和python的原始输出
0.00187283763854096 0.00280257304094145 0.00709416898379967 0.00474275971385824 0.000545071722266366
0.0520122170317888 0.0402746073491970 0.0179208146529717 0.0245726107168336 0.230693355244371
0.430695009441386 0.434492291029203 0.410151021812136 0.416871471927059 0.469873849186641
0.562954025662924 0.539410486293765 0.666336481449288 0.637779009735872 0.284564488176231
[1.0, -1.0, -0.6875955603907775, -0.9999999426232321]
[1.0, -1.0, 0.5569364789701737, -0.9994593106654553]
[1.0, -1.0, 0.5022468075847347, -0.999780120038859]
[1.0, -1.0, 0.4924691499951816, -0.9997110849203137]
[1.0, -1.0, 0.5945295051094253, -0.9991584098381949]
我使用net2.IW{1}
,net2.LW{2}
获得输入和分层权重。得出的偏差如下:net2.b{1}
和net2.b{2}
没有使用偏见,我得到了一些看起来很接近的东西:
[-0.6296705512038354, 0.9890465283687858, 0.1368924025968622, 0.5426776395855755]
[-0.05171165478856995, 0.2973298654798701, 0.02897695903082293, 0.0499820714219222]
[-0.10046933055782481, 0.40531232885083035, 0.033067381241777244, 0.06585830703439044]
[0.03167268710874907, 0.5485036035542894, 0.10579223668518502, 0.015475934153332364]
[0.006502829360007152, 0.22928662468119648, 0.03788967208701787, 0.012868192806301859]
因此,我认为问题可能在于偏见;不过我不太确定
从Matlab获取权重的Python实现
def sigmoid(x):
return math.tanh(x)
def NN(inputs, bias1, bias2):
wsum=[sum([(x*y) for x,y in zip(inputs[0],row)])for row in inputweights]
wsbias=[(x + y) for x,y in zip(wsum,bias1)]
inputactivation=[sigmoid(k) for k in wsbias]
wsoutput=[sum([(x*y) for x,y in zip(inputactivtion,row)])for row in hiddenweights]
wsbias2=[(x + y) for x,y in zip(wsoutput,bias2)]
outputactivation=[sigmoid(k) for k in wsbias2]
return 'output' outputactivation
我非常感谢任何有效的解决方案。
下面是获得的输入和分层权重以及输入和分层偏差
IW=[[-9.1964, -2.3015, 0.2493, 3.3648, -2.6015, -0.0795, -11.2356, 4.6861,-0.8360],
[6.0201, -1.8708, 2.7844, 0.2419, -1.1808, -8.6800, 5.8519, -5.2958, 5.3233],
[0.8597, 0.8644, -0.6913, -0.0397, 0.0619, 0.4506, 1.0687, 0.4090, -0.2874],
[2.9459, 3.2596, 2.2859, 1.1933, 2.9675, -9.6017, 3.5893, 1.4808, -7.5311],
[-0.1533, -1.4806, -2.3748, 0.8059, -0.5502, -1.0447, -0.5920, -1.1667, -1.1447],
[4.7185, -9.2097, 1.1001, -0.0173, 1.4929, 0.3884, 3.7674, 6.3459, -4.2845],
[-16.4031, 8.1351, 2.0689, 2.1267, 6.2093, -8.3875, -15.8493, -0.6096, 2.9214],
[1.7329, 0.1797, 0.1500, 9.1616, -1.7226, 0.9479, 3.2542, -24.4003, -4.2790]]
LW=[[-18.5985, 12.2366, -0.8833, -1.6382, 4.6281, 8.1221, -23.7587, -0.8589],
[12.0462, -11.5464, 6.9612, -10.8562, -7.0647, 5.6653, 16.2527, -7.6119],
[12.4176, 0.9808, 0.7650, -2.9434, -0.2765, -3.0689, -3.1528, 3.0389],
[5.7570, 7.7584, -6.9550, -2.3679, -1.4884, -11.0668, 2.6764, 26.5427]]
bias1=[-1.7639, -1.2599, -0.7560, 0.2520,-0.2520,0.7560, -1.2599, -1.7639]
bias2= [0.2129,-8.1812, 0.0202,4.4512]
我的投入
[[0.0, 0.0, 0.0414444125526, 0.0, 0.0, 0.00670501464516, 0.0, 0.0, 0.0313140652051], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00398243636152, 0.0, 0.0, 0.000863557858377, 0.0, 0.0, 0.00356406423776], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00440892765754, 0.0, 0.0, 0.000725737283104, 0.0, 0.0, 0.00543503005753], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00565322288091, 0.0, 0.0, 0.00236630383341, 0.0, 0.0, 0.00642911490856], [0.0, 0.0, 0.0, 1.0]]
[[0.0, 0.0, 0.00250332223564, 0.0, 0.0, 0.000926998841251, 0.0, 0.0, 0.00241792804103], [0.0, 0.0, 0.0, 1.0]]
感谢您的建议。偏差有时作为权重矩阵的第一列或最后一列添加,有时作为自己的向量添加。但这并不依赖于语言,而是依赖于库。你能说出图书馆的名字吗?还有一些关于如何在Python中创建网络/权重的代码(显示您在何处使用了先前提取的值。另外,您确定使用的是相同的传递/激活函数吗?@Neil Slater,谢谢您的回答。我使用了nprtool和tansig transfor函数。@NeilSlater,我使用了从matlab导出的权重和偏差在python中实现。您的偏差是否存在于输入中?