在使用神经网络模块时,应该设置pybrain中的哪些参数以确保结果的精确复制?
对于每次新运行,尽管随机种子设置为相同值,但输出不同。每次运行的权重和偏差也相同(归因于np.random.seed(0)
)。
也可以使用net._setParameters()
设置权重和偏差,但遵循此方法时结果也不同。
#!/usr/bin/python
#Python 2.7
from __future__ import division
import numpy as np
from pybrain.structure import SigmoidLayer, LinearLayer, TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
np.random.seed(0)
net = buildNetwork(1,2,1,hiddenclass=SigmoidLayer,outclass=LinearLayer,bias=True)
N = 10
t = np.arange(0,N,1)/N
x = np.cos(2*np.pi*0.1*t)
y = np.cos(2*np.pi*0.1*t)
ds = SupervisedDataSet(1, 1)
for c in range(N):
ds.addSample(x[c], y[c])
#net._setParameters(np.random.normal(0,1,(len(net.params))))
#net._setParameters(np.array([1.0]*len(net.params)))
trainer = BackpropTrainer(net, ds)
print 'NN parameters after setup:'
print net.params
for c in range(2):
e1 = trainer.train()
print 'Epoch %d Error: %f'%(c,e1)
print 'NN parameters after training:'
print net.params
p=np.zeros(N)
for c in range(N):
p[c] = net.activate([x[c]])
err = np.sum((y-p)**2)/N
print 'Prediction error = %2.4f'%err
连续两次运行的代码输出:
运行1:
NN parameters after setup:
[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799 -0.97727788
0.95008842]
Epoch 0 Error: 0.258780
Epoch 1 Error: 0.149163
NN parameters after training:
[ 1.63888191 0.40916677 0.97224621 2.24929727 1.8615028 -1.09298541
0.83265293]
Prediction error = 0.2179
运行2:
NN parameters after setup:
[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799 -0.97727788
0.95008842]
Epoch 0 Error: 0.258757
Epoch 1 Error: 0.148765
NN parameters after training:
[ 1.6384432 0.40916969 0.97225458 2.24931834 1.86149186 -1.09343599
0.83221073]
Prediction error = 0.2167
显然,训练前的NN参数对于两种情况都是相同的。训练后,NN参数不同(因此训练期间的预测结果和误差)。