如何保存和恢复PyBrain培训? [英] How to save and recover PyBrain training?
本文介绍了如何保存和恢复PyBrain培训?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以在PyBrain中保存和恢复经过训练的神经网络,这样就不必在每次运行脚本时都对其进行重新训练?
Is there a way to save and recover a trained Neural Network in PyBrain, so that I don't have to retrain it each time I run the script?
推荐答案
可以使用python的内置pickle/cPickle模块或使用PyBrain的XML NetworkWriter来保存和加载PyBrain的神经网络.
PyBrain's Neural Networks can be saved and loaded using either python's built in pickle/cPickle module, or by using PyBrain's XML NetworkWriter.
# Using pickle
from pybrain.tools.shortcuts import buildNetwork
import pickle
net = buildNetwork(2,4,1)
fileObject = open('filename', 'w')
pickle.dump(net, fileObject)
fileObject.close()
fileObject = open('filename','r')
net = pickle.load(fileObject)
注意cPickle是用C实现的,因此应该比pickle快得多.用法应与泡菜基本相同,因此只需导入并使用cPickle.
Note cPickle is implemented in C, and therefore should be much faster than pickle. Usage should mostly be the same as pickle, so just import and use cPickle instead.
# Using NetworkWriter
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.customxml.networkwriter import NetworkWriter
from pybrain.tools.customxml.networkreader import NetworkReader
net = buildNetwork(2,4,1)
NetworkWriter.writeToFile(net, 'filename.xml')
net = NetworkReader.readFrom('filename.xml')
这篇关于如何保存和恢复PyBrain培训?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文