PyBrain是一个Python库,提供了易于使用的人工神经网络等功能。
我试图使用pickle或cPickle对PyBrain网络进行适当的序列化/反序列化,但失败了。
请看下面的例子:
from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
import cPickle as pickle
import numpy as np
#generate some data
np.random.seed(93939393)
data = SupervisedDataSet(2, 1)
for x in xrange(10):
y = x * 3
z = x + y + 0.2 * np.random.randn()
data.addSample((x, y), (z,))
#build a network and train it
net1 = buildNetwork( data.indim, 2, data.outdim )
trainer1 = BackpropTrainer(net1, dataset=data, verbose=True)
for i in xrange(4):
trainer1.trainEpochs(1)
print '\tvalue after %d epochs: %.2f'%(i, net1.activate((1, 4))[0])
这是上述代码的输出:
Total error: 201.501998476
value after 0 epochs: 2.79
Total error: 152.487616382
value after 1 epochs: 5.44
Total error: 120.48092561
value after 2 epochs: 7.56
Total error: 97.9884043452
value after 3 epochs: 8.41
正如您所看到的,随着训练的进行,网络总误差逐渐减少。您还可以看到预测值逐渐接近期望值12。
现在我们将进行类似的练习,但会涉及序列化/反序列化:
print 'creating net2'
net2 = buildNetwork(data.indim, 2, data.outdim)
trainer2 = BackpropTrainer(net2, dataset=data, verbose=True)
trainer2.trainEpochs(1)
print '\tvalue after %d epochs: %.2f'%(1, net2.activate((1, 4))[0])
#So far, so good. Let's test pickle
pickle.dump(net2, open('testNetwork.dump', 'w'))
net2 = pickle.load(open('testNetwork.dump'))
trainer2 = BackpropTrainer(net2, dataset=data, verbose=True)
print 'loaded net2 using pickle, continue training'
for i in xrange(1, 4):
trainer2.trainEpochs(1)
print '\tvalue after %d epochs: %.2f'%(i, net2.activate((1, 4))[0])
这是该块的输出:
creating net2
Total error: 176.339378639
value after 1 epochs: 5.45
loaded net2 using pickle, continue training
Total error: 123.392181859
value after 1 epochs: 5.45
Total error: 94.2867637623
value after 2 epochs: 5.45
Total error: 78.076711114
value after 3 epochs: 5.45
正如您所看到的,训练似乎对网络产生了一些影响(报告的总误差值继续下降),但网络的输出值冻结在第一次训练迭代时相关的值上。是否有任何缓存机制需要我注意,导致这种错误行为?是否有更好的方法来序列化/反序列化pybrain网络?相关版本号:Python 2.6.5(r265:79096,2010年3月19日,21:48:26)[MSC v.1500 32位(Intel)],Numpy 1.5.1,cPickle 1.71,pybrain 0.3。附注:我已在项目网站上创建了一个错误报告,并将保持SO和错误跟踪器更新。
net2
后,您确定不应该再次执行trainer2 = BackpropTrainer(net2, dataset=data, verbose=True)
吗? - Seth Johnson