本文整理汇总了Python中pybrain.supervised.trainers.BackpropTrainer类的典型用法代码示例。如果您正苦于以下问题:Python BackpropTrainer类的具体用法?Python BackpropTrainer怎么用?Python BackpropTrainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BackpropTrainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
def train(self):
trainer = BackpropTrainer(self.network, self.data_set)
trainer.trainUntilConvergence(
verbose=False, validationProportion=0.15, maxEpochs=1000, continueEpochs=10)
return trainer
示例2: train
def train(self, data, LRATE, MOMENTUM, ITERATIONS):
trainer = BackpropTrainer(module=self.net, dataset=data, learningrate=LRATE,
momentum=MOMENTUM, lrdecay=0.99999, verbose=True)
# for i in xrange(0, self.initialization_periods):
# self.net.activate(data.getSequence(i)[0])
print "Training..."
return trainer.trainUntilConvergence(maxEpochs=ITERATIONS)
示例3: move_function
def move_function(board):
global net
best_max_move = None
max_value = -1000
best_min_move = None
min_value = 1000
#value is the chance of black winning
for m in board.get_moves():
nextboard = board.peek_move(m)
value = net.activate(board_to_input(nextboard))
if value > max_value:
max_value = value
best_max_move = m
if value < min_value:
min_value = value
best_min_move = m
ds = SupervisedDataSet(97, 1)
best_move = None
#active player
if board.active == BLACK:
ds.addSample(board_to_input(board), max_value)
best_move = best_max_move
elif board.active == WHITE:
ds.addSample(board_to_input(board), min_value)
best_move = best_min_move
trainer = BackpropTrainer(net, ds)
trainer.train()
NetworkWriter.writeToFile(net, 'CheckersMini/synapsemon_random_black_mini_140.xml')
NetworkWriter.writeToFile(net, 'SynapsemonPie/synapsemon_random_black_mini_140_copy.xml')
return best_move
示例4: train
def train(data):
"""
See http://www.pybrain.org/docs/tutorial/fnn.html
Returns a neural network trained on the test data.
Parameters:
data - A ClassificationDataSet for training.
Should not include the test data.
"""
network = buildNetwork(
# This is where we specify the architecture of
# the network. We can play around with different
# parameters here.
# http://www.pybrain.org/docs/api/tools.html
data.indim, 5, data.outdim,
hiddenclass=SigmoidLayer,
outclass=SoftmaxLayer
)
# We can fiddle around with this guy's options as well.
# http://www.pybrain.org/docs/api/supervised/trainers.html
trainer = BackpropTrainer(network, dataset=data)
trainer.trainUntilConvergence(maxEpochs=20)
return network
示例5: measuredLearning
def measuredLearning(ds):
trndata,tstdata = splitData(ds,.025)
#build network
###
# This network has no hidden layters, you might need to add some
###
fnn = buildNetwork( trndata.indim, 22, trndata.outdim, outclass=SoftmaxLayer )
trainer = BackpropTrainer( fnn, verbose=True,dataset=trndata)
####
# Alter this to figure out how many runs you want. Best to start small and be sure that you see learning.
# Before you ramp it up.
###
for i in range(150):
trainer.trainEpochs(5)
trnresult = percentError(trainer.testOnClassData(),trndata['class'] )
tstresult = percentError( trainer.testOnClassData(
dataset=tstdata ), tstdata['class'] )
print "epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
" test error: %5.2f%%" % tstresult
if(trnresult<.5):
return
示例6: nnTest
def nnTest(tx, ty, rx, ry, iterations):
print "NN start"
print strftime("%a, %d %b %Y %H:%M:%S", localtime())
resultst = []
resultsr = []
positions = range(iterations)
network = buildNetwork(16, 16, 1, bias=True)
ds = ClassificationDataSet(16, 1, class_labels=["1", "0"])
for i in xrange(len(tx)):
ds.addSample(tx[i], [ty[i]])
trainer = BackpropTrainer(network, ds, learningrate=0.05)
validator = CrossValidator(trainer, ds, n_folds=10)
print validator.validate()
for i in positions:
print trainer.train()
resultst.append(sum((np.array([round(network.activate(test)) for test in tx]) - ty)**2)/float(len(ty)))
resultsr.append(sum((np.array([round(network.activate(test)) for test in rx]) - ry)**2)/float(len(ry)))
print i, resultst[i], resultsr[i]
plt.plot(positions, resultst, 'g-', positions, resultsr, 'r-')
plt.axis([0, iterations, 0, 1])
plt.ylabel("Percent Error")
plt.xlabel("Network Epoch")
plt.title("Neural Network Error")
plt.savefig('nn.png', dpi=500)
print "NN end"
print strftime("%a, %d %b %Y %H:%M:%S", localtime())
示例7: run
def run(self, fold, X_train, y_train, X_test, y_test):
DS_train, DS_test = ClassificationData.convert_to_DS(
X_train,
y_train,
X_test,
y_test)
NHiddenUnits = self.__get_best_hu(DS_train)
fnn = buildNetwork(
DS_train.indim,
NHiddenUnits,
DS_train.outdim,
outclass=SoftmaxLayer,
bias=True)
trainer = BackpropTrainer(
fnn,
dataset=DS_train,
momentum=0.1,
verbose=False,
weightdecay=0.01)
trainer.trainEpochs(self.epochs)
tstresult = percentError(
trainer.testOnClassData(dataset=DS_test),
DS_test['class'])
print "NN fold: %4d" % fold, "; test error: %5.2f%%" % tstresult
return tstresult / 100.0
示例8: train
def train(self):
'''
Perform batch regression
'''
self.getTrainingData2()
trainer = BackpropTrainer(self.net, self.ds)
trainer.train()
示例9: run
def run(self, ds_train, ds_test):
"""
This function both trains the ANN and evaluates the ANN using a specified training and testing set
Args:
:param ds_train (TweetClassificationDatasetFactory): the training dataset the neural network is trained with.
:param ds_test (TweetClassificationDatasetFactory): the test dataset evaluated.
:returns: error (float): the percent error of the test dataset, tested on the neural network.
"""
ds_train._convertToOneOfMany()
ds_test._convertToOneOfMany()
trainer = BackpropTrainer(
self.network,
dataset=ds_train,
momentum=0.1,
verbose=True,
weightdecay=0.01)
trainer.trainUntilConvergence(
dataset=ds_train,
maxEpochs=self.max_epochs,
continueEpochs=self.con_epochs)
result = trainer.testOnClassData(dataset=ds_test)
error = percentError(result, ds_test['class'])
return error
示例10: big_training
def big_training(np_data, num_nets=1, num_epoch=20, net_builder=net_full, train_size=.1, testing=False):
sss = cross_validation.StratifiedShuffleSplit(np_data[:,:1].ravel(), n_iter=num_nets , test_size=1-train_size, random_state=3476)
nets=[None for net_ind in range(num_nets)]
trainaccu=[[0 for i in range(num_epoch)] for net_ind in range(num_nets)]
testaccu=[[0 for i in range(num_epoch)] for net_ind in range(num_nets)]
net_ind=0
for train_index, test_index in sss:
print ('%s Building %d. network.' %(time.ctime(), net_ind+1))
#print("TRAIN:", len(train_index), "TEST:", len(test_index))
trainset = ClassificationDataSet(np_data.shape[1] - 1, 1)
trainset.setField('input', np_data[train_index,1:]/100-.6)
trainset.setField('target', np_data[train_index,:1])
trainset._convertToOneOfMany( )
trainlabels = trainset['class'].ravel().tolist()
if testing:
testset = ClassificationDataSet(np_data.shape[1] - 1, 1)
testset.setField('input', np_data[test_index,1:]/100-.6)
testset.setField('target', np_data[test_index,:1])
testset._convertToOneOfMany( )
testlabels = testset['class'].ravel().tolist()
nets[net_ind] = net_builder()
trainer = BackpropTrainer(nets[net_ind], trainset)
for i in range(num_epoch):
for ii in range(3):
err = trainer.train()
print ('%s Epoch %d: Network trained with error %f.' %(time.ctime(), i+1, err))
trainaccu[net_ind][i]=accuracy_score(trainlabels,trainer.testOnClassData())
print ('%s Epoch %d: Train accuracy is %f' %(time.ctime(), i+1, trainaccu[net_ind][i]))
print ([sum([trainaccu[y][i]>tres for y in range(net_ind+1)]) for tres in [0,.1,.2,.3,.4,.5,.6]])
if testing:
testaccu[net_ind][i]=accuracy_score(testlabels,trainer.testOnClassData(testset))
print ('%s Epoch %d: Test accuracy is %f' %(time.ctime(), i+1, testaccu[net_ind][i]))
NetworkWriter.writeToFile(nets[net_ind], 'nets/'+net_builder.__name__+str(net_ind)+'.xml')
net_ind +=1
return [nets, trainaccu, testaccu]
示例11: computeModel
def computeModel(self, path, user):
# Create a supervised dataset for training.
trndata = SupervisedDataSet(24, 1)
tstdata = SupervisedDataSet(24, 1)
#Fill the dataset.
for number in range(0,10):
for variation in range(0,7):
# Pass all the features as inputs.
trndata.addSample(self.getSample(user, number, variation),(user.key,))
for variation in range(7,10):
# Pass all the features as inputs.
tstdata.addSample(self.getSample(user, number, variation),(user.key,))
# Build the LSTM.
n = buildNetwork(24, 50, 1, hiddenclass=LSTMLayer, recurrent=True, bias=True)
# define a training method
trainer = BackpropTrainer(n, dataset = trndata, momentum=0.99, learningrate=0.00002)
# carry out the training
trainer.trainOnDataset(trndata, 2000)
valueA = trainer.testOnData(tstdata)
print '\tMSE -> {0:.2f}'.format(valueA)
self.saveModel(n, '.\NeuralNets\SavedNet_%d' %(user.key))
return n
示例12: Classifier
class Classifier():
def __init__(self, testing = False):
self.training_set, self.test_set = split_samples(0.5 if testing else 1.0)
self.net = buildNetwork( self.training_set.indim, self.training_set.outdim, outclass=SoftmaxLayer )
self.trainer = BackpropTrainer( self.net, dataset=self.training_set, momentum=0.1, verbose=True, weightdecay=0.01)
self.train()
def train(self):
self.trainer.trainEpochs( EPOCHS )
trnresult = percentError( self.trainer.testOnClassData(),
self.training_set['class'] )
print " train error: %5.2f%%" % trnresult
def classify(self, file):
strengths = self.net.activate(process_sample(*load_sample(file)))
print strengths
best_match = None
strength = 0.0
for i,s in enumerate(strengths):
if s > strength:
best_match = i
strength = s
return SOUNDS[best_match]
def test(self):
tstresult = percentError( self.trainer.testOnClassData(
dataset=self.test_set ), self.test_set['class'] )
print " test error: %5.2f%%" % tstresult
示例13: trainNetwork
def trainNetwork(epochs, rate, trndata, tstdata, network=None):
'''
epochs: number of iterations to run on dataset
trndata: pybrain ClassificationDataSet
tstdat: pybrain ClassificationDataSet
network: filename of saved pybrain network, or None
'''
if network is None:
net = buildNetwork(400, 25, 25, 9, bias=True, hiddenclass=SigmoidLayer, outclass=SigmoidLayer)
else:
net = NetworkReader.readFrom(network)
print "Number of training patterns: ", len(trndata)
print "Input and output dimensions: ", trndata.indim, trndata.outdim
print "First sample input:"
print trndata['input'][0]
print ""
print "First sample target:", trndata['target'][0]
print "First sample class:", trndata.getClass(int(trndata['class'][0]))
print ""
trainer = BackpropTrainer(net, dataset=trndata, learningrate=rate)
for i in range(epochs):
trainer.trainEpochs(1)
trnresult = percentError(trainer.testOnClassData(), trndata['class'])
tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
print "epoch: %4d" % trainer.totalepochs, " train error: %5.2f%%" % trnresult, " test error: %5.2f%%" % tstresult
return net
示例14: train_neural_network
def train_neural_network():
start = time.clock()
ds = get_ds()
# split main data to train and test parts
train, test = ds.splitWithProportion(0.75)
# build nn with 10 inputs, 3 hidden layers, 1 output neuron
net = buildNetwork(10,3,1, bias=True)
# use backpropagation algorithm
trainer = BackpropTrainer(net, train, momentum = 0.1, weightdecay = 0.01)
# plot error
trnError, valError = trainer.trainUntilConvergence(dataset = train, maxEpochs = 50)
plot_error(trnError, valError)
print "train the model..."
trainer.trainOnDataset(train, 500)
print "Total epochs: %s" % trainer.totalepochs
print "activate..."
out = net.activateOnDataset(test).argmax(axis = 1)
percent = 100 - percentError(out, test['target'])
print "%s" % percent
end = time.clock()
print "Time: %s" % str(end-start)
开发者ID:zhzhussupovkz,项目名称:forest-cover-type-prediction,代码行数:29,代码来源:forest_cover_type_neural_network.py
示例15: __init__
class Brain:
def __init__(self, hiddenNodes = 30):
# construct neural network
self.myClassifierNet = buildNetwork(12, hiddenNodes, 1, bias=True, hiddenclass=TanhLayer) #parameters to buildNetwork are inputs, hidden, output
# set up dataset
self.myDataset = SupervisedDataSet(12, 1)
self.myClassifierTrainer = BackpropTrainer(self.myClassifierNet, self.myDataset)
def addSampleImageFromFile(self, imageFile, groupId):
"adds a data sample from an image file, including needed processing"
myImage = Image.open(imageFile)
self.myDataset.addSample(twelveToneParallel(myImage), (groupId,))
def train(self):
#myClassifierTrainer.trainUntilConvergence() #this will take forever (possibly literally in the pathological case)
for i in range(0, 15):
self.myClassifierTrainer.train() #this may result in an inferior network, but in practice seems to work fine
def save(self, saveFileName="recognizernet.brain"):
saveFile = open(saveFileName, 'w')
pickle.dump(self.myClassifierNet, saveFile)
saveFile.close()
def load(self, saveFileName="recognizernet.brain"):
saveFile = open(saveFileName, 'r')
myClassifierNet = pickle.load(saveFile)
saveFile.close()
def classify(self, fileName):
myImage = Image.open(fileName)
if self.myClassifierNet.activate(twelveToneParallel(myImage)) < 0.5:
return 0
else:
return 1