本文整理汇总了Python中pybrain.supervised.BackpropTrainer类的典型用法代码示例。如果您正苦于以下问题:Python BackpropTrainer类的具体用法?Python BackpropTrainer怎么用?Python BackpropTrainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BackpropTrainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: trainedANN
def trainedANN():
n = FeedForwardNetwork()
n.addInputModule(LinearLayer(4, name='in'))
n.addModule(SigmoidLayer(6, name='hidden'))
n.addOutputModule(LinearLayer(2, name='out'))
n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))
n.sortModules()
draw_connections(n)
# d = generateTrainingData()
d = getDatasetFromFile(root.path()+"/res/dataSet")
t = BackpropTrainer(n, d, learningrate=0.001, momentum=0.75)
t.trainOnDataset(d)
# FIXME: I'm not sure the recurrent ANN is going to converge
# so just training for fixed number of epochs
count = 0
while True:
globErr = t.train()
print globErr
if globErr < 0.01:
break
count += 1
if count == 20:
return trainedANN()
exportANN(n)
draw_connections(n)
return n
示例2: EightBitBrain
class EightBitBrain(object):
def __init__(self, dataset, inNodes, outNodes, hiddenNodes, classes):
self.__dataset = ClassificationDataSet(inNodes, classes-1)
for element in dataset:
self.addDatasetSample(self._binaryList(element[0]), element[1])
self.__dataset._convertToOneOfMany()
self.__network = buildNetwork(inNodes, hiddenNodes, self.__dataset.outdim, recurrent=True)
self.__trainer = BackpropTrainer(self.__network, learningrate = 0.01, momentum = 0.99, verbose = True)
self.__trainer.setData(self.__dataset)
def _binaryList(self, n):
return [int(c) for c in "{0:08b}".format(n)]
def addDatasetSample(self, argument, target):
self.__dataset.addSample(argument, target)
def train(self, epochs):
self.__trainer.trainEpochs(epochs)
def activate(self, information):
result = self.__network.activate(self._binaryList(information))
highest = (0,0)
for resultClass in range(len(result)):
if result[resultClass] > highest[0]:
highest = (result[resultClass], resultClass)
return highest[1]
示例3: trained_cat_dog_ANN
def trained_cat_dog_ANN():
n = FeedForwardNetwork()
d = get_cat_dog_trainset()
input_size = d.getDimension('input')
n.addInputModule(LinearLayer(input_size, name='in'))
n.addModule(SigmoidLayer(input_size+1500, name='hidden'))
n.addOutputModule(LinearLayer(2, name='out'))
n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))
n.sortModules()
n.convertToFastNetwork()
print 'successful converted to fast network'
t = BackpropTrainer(n, d, learningrate=0.0001)#, momentum=0.75)
count = 0
while True:
globErr = t.train()
print globErr
count += 1
if globErr < 0.01:
break
if count == 30:
break
exportCatDogANN(n)
return n
示例4: training
def training(d):
# net = buildNetwork(d.indim, 55, d.outdim, bias=True,recurrent=False, hiddenclass =SigmoidLayer , outclass = SoftmaxLayer)
net = FeedForwardNetwork()
inLayer = SigmoidLayer(d.indim)
hiddenLayer1 = SigmoidLayer(d.outdim)
hiddenLayer2 = SigmoidLayer(d.outdim)
outLayer = SigmoidLayer(d.outdim)
net.addInputModule(inLayer)
net.addModule(hiddenLayer1)
net.addModule(hiddenLayer2)
net.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer, hiddenLayer1)
hidden_to_hidden = FullConnection(hiddenLayer1, hiddenLayer2)
hidden_to_out = FullConnection(hiddenLayer2, outLayer)
net.addConnection(in_to_hidden)
net.addConnection(hidden_to_hidden)
net.addConnection(hidden_to_out)
net.sortModules()
print net
t = BackpropTrainer(net, d, learningrate = 0.9,momentum=0.9, weightdecay=0.01, verbose = True)
t.trainUntilConvergence(continueEpochs=1200, maxEpochs=1000)
NetworkWriter.writeToFile(net, 'myNetwork'+str(time.time())+'.xml')
return t
示例5: trained_cat_dog_RFCNN
def trained_cat_dog_RFCNN():
n = RecurrentNetwork()
d = get_cat_dog_trainset()
input_size = d.getDimension('input')
n.addInputModule(LinearLayer(input_size, name='in'))
n.addModule(SigmoidLayer(input_size+1500, name='hidden'))
n.addOutputModule(LinearLayer(2, name='out'))
n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))
n.addRecurrentConnection(FullConnection(n['out'], n['hidden'], name='nmc'))
n.sortModules()
t = BackpropTrainer(n, d, learningrate=0.0001)#, momentum=0.75)
count = 0
while True:
globErr = t.train()
print globErr
count += 1
if globErr < 0.01:
break
if count == 30:
break
exportCatDogRFCNN(n)
return n
示例6: train
def train(self, **kwargs):
if "verbose" in kwargs:
verbose = kwargs["verbose"]
else:
verbose = False
"""t = BackpropTrainer(self.rnn, dataset=self.trndata, learningrate = 0.1, momentum = 0.0, verbose = True)
for i in range(1000):
t.trainEpochs(5)
"""
# pdb.set_trace()
#print self.nn.outdim, " nn | ", self.trndata.outdim, " trndata "
trainer = BackpropTrainer(self.nn, self.trndata, learningrate = 0.0005, momentum = 0.99)
assert (self.tstdata is not None)
assert (self.trndata is not None)
b1, b2 = trainer.trainUntilConvergence(verbose=verbose,
trainingData=self.trndata,
validationData=self.tstdata,
maxEpochs=10)
#print b1, b2
#print "new parameters are: "
#self.print_connections()
return b1, b2
示例7: train
def train(self, params):
"""
Train TDNN network on buffered dataset history
:param params:
:return:
"""
# self.net = buildNetwork(params['encoding_num'] * params['num_lags'],
# params['num_cells'],
# params['encoding_num'],
# bias=True,
# outputbias=True)
ds = SupervisedDataSet(params['encoding_num'] * params['num_lags'],
params['encoding_num'])
history = self.window(self.history, params['learning_window'])
n = params['encoding_num']
for i in xrange(params['num_lags'], len(history)):
targets = numpy.zeros((1, n))
targets[0, :] = self.encoder.encode(history[i])
features = numpy.zeros((1, n * params['num_lags']))
for lags in xrange(params['num_lags']):
features[0, lags * n:(lags + 1) * n] = self.encoder.encode(
history[i - (lags + 1)])
ds.addSample(features, targets)
trainer = BackpropTrainer(self.net,
dataset=ds,
verbose=params['verbosity'] > 0)
if len(history) > 1:
trainer.trainEpochs(params['num_epochs'])
示例8: trainedRNN
def trainedRNN():
n = RecurrentNetwork()
n.addInputModule(LinearLayer(4, name='in'))
n.addModule(SigmoidLayer(6, name='hidden'))
n.addOutputModule(LinearLayer(2, name='out'))
n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))
n.addRecurrentConnection(NMConnection(n['out'], n['out'], name='nmc'))
# n.addRecurrentConnection(FullConnection(n['out'], n['hidden'], inSliceFrom = 0, inSliceTo = 1, outSliceFrom = 0, outSliceTo = 3))
n.sortModules()
draw_connections(n)
d = getDatasetFromFile(root.path()+"/res/dataSet")
t = BackpropTrainer(n, d, learningrate=0.001, momentum=0.75)
t.trainOnDataset(d)
count = 0
while True:
globErr = t.train()
print globErr
if globErr < 0.01:
break
count += 1
if count == 50:
return trainedRNN()
# exportRNN(n)
draw_connections(n)
return n
示例9: gradientCheck
def gradientCheck(module, tolerance=0.0001, dataset=None):
""" check the gradient of a module with a randomly generated dataset,
(and, in the case of a network, determine which modules contain incorrect derivatives). """
if module.paramdim == 0:
print('Module has no parameters')
return True
if dataset:
d = dataset
else:
d = buildAppropriateDataset(module)
b = BackpropTrainer(module)
res = b._checkGradient(d, True)
# compute average precision on every parameter
precision = zeros(module.paramdim)
for seqres in res:
for i, p in enumerate(seqres):
if p[0] == 0 and p[1] == 0:
precision[i] = 0
else:
precision[i] += abs((p[0] + p[1]) / (p[0] - p[1]))
precision /= len(res)
if max(precision) < tolerance:
print('Perfect gradient')
return True
else:
print('Incorrect gradient', precision)
if isinstance(module, Network):
index = 0
for m in module._containerIterator():
if max(precision[index:index + m.paramdim]) > tolerance:
print('Incorrect module:', m, res[-1][index:index + m.paramdim])
index += m.paramdim
else:
print(res)
return False
示例10: initializeNetwork
def initializeNetwork(self):
can1 = NNTrainData.NNTrainData(cv2.imread('NNTrain/can1.png'), self.encodingDict["can"])
can2 = NNTrainData.NNTrainData(cv2.imread('NNTrain/can2.png'), self.encodingDict["can"])
can3 = NNTrainData.NNTrainData(cv2.imread('NNTrain/can3.png'), self.encodingDict["can"])
stain1 = NNTrainData.NNTrainData(cv2.imread('NNTrain/stain1.png'), self.encodingDict["stain"])
stain2 = NNTrainData.NNTrainData(cv2.imread('NNTrain/stain2.png'), self.encodingDict["stain"])
stain3 = NNTrainData.NNTrainData(cv2.imread('NNTrain/stain3.png'), self.encodingDict["stain"])
dirt1 = NNTrainData.NNTrainData(cv2.imread('NNTrain/dirt1.png'), self.encodingDict["dirt"])
dirt2 = NNTrainData.NNTrainData(cv2.imread('NNTrain/dirt2.png'), self.encodingDict["dirt"])
dirt3 = NNTrainData.NNTrainData(cv2.imread('NNTrain/dirt3.png'), self.encodingDict["dirt"])
self.trainData.append(can1)
self.trainData.append(can2)
self.trainData.append(can3)
self.trainData.append(stain1)
self.trainData.append(stain2)
self.trainData.append(stain3)
self.trainData.append(dirt1)
self.trainData.append(dirt2)
self.trainData.append(dirt3)
for x in self.trainData:
x.prepareTrainData()
self.net = buildNetwork(4, 3, 3, hiddenclass=TanhLayer, outclass=SoftmaxLayer)
ds = SupervisedDataSet(4, 3)
for x in self.trainData:
ds.addSample((x.contours/100.0, x.color[0]/1000.0, x.color[1]/1000.0, x.color[2]/1000.0), x.output)
trainer = BackpropTrainer(self.net, momentum=0.1, verbose=True, weightdecay=0.01)
trainer.trainOnDataset(ds, 1000)
trainer.testOnData(verbose=True)
print "\nSiec nauczona\n"
示例11: generate_and_test_nn
def generate_and_test_nn():
d = load_training_set()
n = buildNetwork(d.indim, 13, d.outdim, hiddenclass=LSTMLayer, outclass=SoftmaxLayer, outputbias=False, recurrent=True)
t = BackpropTrainer(n, learningrate=0.01, momentum=0.99, verbose=True)
t.trainOnDataset(d, 1000)
t.testOnData(verbose=True)
return (n, d)
示例12: testOldTraining
def testOldTraining(hidden=15, n=None):
d = XORDataSet()
if n is None:
n = buildNetwork(d.indim, hidden, d.outdim, recurrent=False)
t = BackpropTrainer(n, learningrate=0.01, momentum=0., verbose=False)
t.trainOnDataset(d, 250)
t.testOnData(verbose=True)
示例13: execute
def execute(self):
network = self.networkFactoryMethod()
trainer = BackpropTrainer(network, learningrate = self.learningrate, momentum = self.momentum)
trainer.trainOnDataset(self.datasetForTraining, self.epochs)
averageError = trainer.testOnData(self.datasetForTest)
self.collectedErrors.append(averageError)
return averageError
示例14: main
def main():
print '----- loading train/test datasets -----'
train_ds, test_ds = create_datasets()
print '----- building the network -----'
net = ann_network()
trainer = BackpropTrainer(net, learningrate=0.1, momentum=0.1, verbose=True)
print '----- training the model -----'
trainer.trainOnDataset(train_ds)
示例15: training
def training(d):
"""
Builds a network and trains it.
"""
n = buildNetwork(d.indim, 4, d.outdim,recurrent=True)
t = BackpropTrainer(n, d, learningrate = 0.01, momentum = 0.99, verbose = True)
for epoch in range(0,500):
t.train()
return t