本文整理汇总了Python中pybrain.supervised.trainers.BackpropTrainer.trainEpochs方法的典型用法代码示例。如果您正苦于以下问题:Python BackpropTrainer.trainEpochs方法的具体用法?Python BackpropTrainer.trainEpochs怎么用?Python BackpropTrainer.trainEpochs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybrain.supervised.trainers.BackpropTrainer
的用法示例。
在下文中一共展示了BackpropTrainer.trainEpochs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def train(self):
print "Enter the number of times to train, -1 means train until convergence:"
t = int(raw_input())
print "Training the Neural Net"
print "self.net.indim = "+str(self.net.indim)
print "self.train_data.indim = "+str(self.train_data.indim)
trainer = BackpropTrainer(self.net, dataset=self.train_data, momentum=0.1, verbose=True, weightdecay=0.01)
if t == -1:
trainer.trainUntilConvergence()
else:
for i in range(t):
trainer.trainEpochs(1)
trnresult = percentError( trainer.testOnClassData(), self.train_data['class'])
# print self.test_data
tstresult = percentError( trainer.testOnClassData(dataset=self.test_data), self.test_data['class'] )
print "epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
" test error: %5.2f%%" % tstresult
if i % 10 == 0 and i > 1:
print "Saving Progress... Writing to a file"
NetworkWriter.writeToFile(self.net, self.path)
print "Done training... Writing to a file"
NetworkWriter.writeToFile(self.net, self.path)
return trainer
示例2: main
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def main():
trndata, tstdata = createDS()
for repeat in xrange(repeats):
iter_trn_results = []
iter_tst_results = []
nn = createNNLong(trndata)
hiddenAstrocyteLayer, outputAstrocyteLayer = associateAstrocyteLayers(nn)
trainer = BackpropTrainer(nn, dataset=trndata, learningrate=0.01,
momentum=0.1, verbose=False, weightdecay=0.0)
for grand_iter in xrange(iterations):
trainer.trainEpochs(1)
trnresult = percentError(trainer.testOnClassData(), trndata['class'])
iter_trn_results.append(trnresult)
tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
iter_tst_results.append(tstresult)
if not grand_iter%20:
print 'epoch %4d' %trainer.totalepochs, 'train error %5.2f%%' %trnresult, \
'test error %5.2f%%' %tstresult
inputs = list(trndata['input'])
random.shuffle(inputs)
for inpt in trndata['input']:
nn.activate(inpt)
for minor_iter in range(hiddenAstrocyteLayer.astrocyte_processing_iters):
hiddenAstrocyteLayer.update()
outputAstrocyteLayer.update()
hiddenAstrocyteLayer.reset()
outputAstrocyteLayer.reset()
all_trn_results.append(iter_trn_results)
all_tst_results.append(iter_tst_results)
plotResults(all_trn_results)
plotResults(all_tst_results)
plt.show()
示例3: train_network
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def train_network():
start = timeit.default_timer()
# Read data
race_data = pd.read_csv('../data/half_ironman_data_v1.csv')
race_factors = pd.read_csv('../data/half_ironman_race_factors.csv')
# Prepare input data
supervised_dataset = get_supervised_dataset(race_data, race_factors)
# Create network
network = create_feedforward_network(supervised_dataset)
train_data, test_data = supervised_dataset.splitWithProportion(0.9)
trainer = BackpropTrainer(network, dataset=train_data)
# Train our network
trainer.trainEpochs(1)
# check network accuracy
print _sum_square_error(network.activateOnDataset(dataset=train_data), train_data['target'])
print _sum_square_error(network.activateOnDataset(dataset=test_data), test_data['target'])
print 'Execution time =>', timeit.default_timer() - start, 'secs'
示例4: test_train
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def test_train(self, epochs=1):
print("Training...")
split = int(len(self.samples) * 0.7)
train_samples = self.samples[0:split]
train_labels = self.labels[0:split]
test_samples = self.samples[split:]
test_labels = self.labels[split:]
net = buildNetwork(300, 300, 1)
ds = SupervisedDataSet(300, 1)
for i in range(len(train_samples)):
ds.addSample(tuple(np.array(train_samples[i], dtype='float64')), (train_labels[i],))
trainer = BackpropTrainer(net, ds, verbose=True)
trainer.trainEpochs(epochs)
self.totalEpochs = epochs
error = 0
counter = 0
for i in range(0, 100):
output = net.activate(tuple(np.array(test_samples[i], dtype='float64')))
if round(output[0]) != test_labels[i]:
counter += 1
print(counter, " : output : ", output[0], " real answer : ", test_labels[i])
error += 1
else:
counter += 1
print(counter, " : output : ", output[0], " real answer : ", test_labels[i])
print("Trained with " + str(epochs) + " epochs; Total: " + str(self.totalEpochs) + ";")
return error
示例5: trainNetwork
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def trainNetwork(net, ds, epochs, learningrate = 0.01, momentum=0.4, weightdecay = 0.0):
trainer = BackpropTrainer(net,
dataset=ds,
learningrate=learningrate,
momentum=momentum,
weightdecay=weightdecay)
trainer.trainEpochs(epochs)
示例6: createnetwork
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def createnetwork(n_hoglist,n_classlist,n_classnum,n_hiddensize=100):
n_inputdim=len(n_hoglist[0])
n_alldata = ClassificationDataSet(n_inputdim,1, nb_classes=n_classnum)
for i in range(len(n_hoglist)):
n_input = n_hoglist[i]
n_class = n_classlist[i]
n_alldata.addSample(n_input, [n_class])
n_tstdata, n_trndata = n_alldata.splitWithProportion( 0.25 )
n_trndata._convertToOneOfMany( )
n_tstdata._convertToOneOfMany( )
print "Number of training patterns: ", len(n_trndata)
print "Input and output dimensions: ", n_trndata.indim, n_trndata.outdim
print "First sample (input, target, class):"
print n_trndata['input'][0], n_trndata['target'][0], n_trndata['class'][0]
n_fnn = buildNetwork(n_trndata.indim,n_hiddensize, n_trndata.outdim, outclass=SoftmaxLayer)
n_trainer = BackpropTrainer(n_fnn, dataset=n_trndata, momentum=0.1, verbose=True, weightdecay=0.01)
n_result = 1
while n_result > 0.1:
print n_result
n_trainer.trainEpochs(1)
n_trnresult = percentError(n_trainer.testOnClassData(),
n_trndata['class'])
n_tstresult = percentError(n_trainer.testOnClassData(
dataset=n_tstdata), n_tstdata['class'])
print "epoch: %4d" % n_trainer.totalepochs, \
" train error: %5.2f%%" % n_trnresult, \
" test error: %5.2f%%" % n_tstresult
n_result = n_tstresult
示例7: fit
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def fit(self, Xtrain, Ytrain):
""" Use entirety of provided X, Y to predict
Arguments
Xtrain -- Training data
Ytrain -- Training prediction
n_hidden -- each entry in the list n_hidden tells how many hidden nodes at that layer
epocs_to_train -- number of iterations to train the NN for
Returns
classifier -- a classifier fitted to Xtrain and Ytrain
"""
self.Xt = Xtrain
self.Yt = Ytrain
n_hidden = self.params['n_hidden']
epochs_to_train = self.params['epochs_to_train']
# PyBrain expects data in its DataSet format
trndata = convert_to_pybrain_dataset(Xtrain, Ytrain)
# build neural net and train it
net = buildNetwork(trndata.indim, *(n_hidden + [trndata.outdim]), outclass=SoftmaxLayer)
trainer = BackpropTrainer(net, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
with open('nn_progress_report.txt', 'a') as f:
f.write('training %s for %d epochs\n' % (self.params, epochs_to_train))
#trainer.trainUntilConvergence()
trainer.trainEpochs(epochs_to_train)
# Return a functor that wraps calling predict
self.trainer = trainer
示例8: train
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def train(args):
inputs, ys, gc = args
row_length = len(inputs[0])
d = ds.ClassificationDataSet(
row_length, nb_classes=2, class_labels=['Poisonous',
'Edible'])
d.setField('input', inputs)
d.setField('target', ys)
test, train = d.splitWithProportion(.25)
test._convertToOneOfMany()
train._convertToOneOfMany()
hidden = row_length // 2
print "indim:", train.indim
net = buildNetwork(train.indim,
hidden,
train.outdim,
outclass=SoftmaxLayer)
trainer = BackpropTrainer(net,
dataset=train,
momentum=0.0,
learningrate=0.1,
verbose=True,
weightdecay=0.0)
for i in xrange(20):
trainer.trainEpochs(1)
trnresult = percentError(trainer.testOnClassData(),
train['class'])
tstresult = percentError(
trainer.testOnClassData(dataset=test),
test['class'])
print "epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
" test error: %5.2f%%" % tstresult
return net, gc
示例9: process_symbol
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def process_symbol(net, symbol):
print "processing ", symbol
#zuerst train_data prüfen, wenn keine Trainingsdaten da sind, dann brauchen wir nicht weitermachen
train_data = load(symbol+'.train')
if (len(train_data) == 0):
print "--no training data, skip", symbol
return
print "-traing data loaded"
data = load_stockdata(symbol)
if (len(data) == 0):
print "--no data, skip", symbol
return
print "-stock data loaded"
settings = load_settings(symbol,data)
if(len(settings) == 0):
print "--no settings, skip", symbol
return
print "-settings loaded"
#jetzt sind alle Daten vorhanden
ds = build_dataset(data, train_data, settings)
print "-train"
trainer = BackpropTrainer(net, ds)
trainer.trainEpochs(epochs)
print "-saving network"
NetworkWriter.writeToFile(net, 'network.xml')
return net
示例10: ann
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def ann(training_filename , testing_filename,itr,epoch,model_type):
training_start_time = "The generation of data set and training started at :%s" % datetime.datetime.now()
training_dataset = np.genfromtxt(training_filename, skip_header=0,dtype="int", delimiter='\t' )
data = ClassificationDataSet(len(training_dataset[0])-1, 2, nb_classes=2)
for aSample in training_dataset:
data.addSample(aSample[0:len(aSample)-1],[aSample[len(aSample)-1]] );
#
data._convertToOneOfMany( )
fann = buildNetwork(314,2,outclass=SoftmaxLayer);
trainer = BackpropTrainer( fann, dataset=data, momentum=0.1, verbose=False, weightdecay=0.01)
counter = 0;
print training_start_time
while(counter < itr):
trainer.trainEpochs( epoch );
counter = counter + 1;
trnresult = percentError( trainer.testOnClassData(),data['class'] )
trained_result_log = "epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult;
training_time_end = "The training and result logging ended at %s :" % datetime.datetime.now()
filename = working_dir + "\models\\"+model_type + ".obj"
save_trained_model(fann, filename)
log_file.write("\n" + training_start_time+"\n")
log_file.write(str(trained_result_log)+"\n")
log_file.write(training_time_end+"\n")
示例11: pybrain_high
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def pybrain_high():
back=[]
alldate=New_stock.objects.filter().exclude(name='CIHKY')[0:100]
wholelen=len(alldate)
test=New_stock.objects.filter(name__contains="CIHKY")
testlen=len(test)
# test dateset
testdata= SupervisedDataSet(5, 1)
testwhole=newalldate(test,testlen)
for i in testwhole:
testdata.addSample((i[0],i[2],i[3],i[4],i[5]), (0,))
# 实验 dateset
data= SupervisedDataSet(5, 1)
wholedate=newalldate(alldate,wholelen)
for i in wholedate:
data.addSample((i[0],i[2],i[3],i[4],i[5]), (i[1]))
#print testwhole
# 建立bp神经网络
net = buildNetwork(5, 3, 1,bias=True,hiddenclass=TanhLayer, outclass=SoftmaxLayer)
trainer = BackpropTrainer(net,data)
trainer.trainEpochs(epochs=100)
# train and test the network
# print trainer.train()
trainer.train()
print 'ok'
out=net.activateOnDataset(testdata)
for j in test:
back.append((j.high))
print back
print out
backout=backnormal(back,out)
print 'okokokoko'
print backout # 输出22的测试集合
return out
示例12: trainNetwork
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
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
示例13: Classifier
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
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
示例14: trainBackProp
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def trainBackProp(self):
trainer = BackpropTrainer(self.neuralNet, self.dataSet)
start = time.time()
trainer.trainEpochs(EPOCHS)
end = time.time()
print("Training time -> " + repr(end-start))
print(repr(trainer.train()))
示例15: trainByImg
# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import trainEpochs [as 别名]
def trainByImg(net, test, target, trainNum):
h, l = test.shape[:2]
for i in range(h):
for j in range(l):
if target[i, j] != 0:
target[i, j] = 255
ds = SupervisedDataSet(data_size, 10, data_size)
# ds = ClassificationDataSet(block_size, nb_classes=2, class_labels=[0,255])
"""
for i in range(trainNum):
x = random.uniform(block_width, data_width-block_width-1)
y = random.uniform(block_length,data_length-block_length-1)
if target[y,x] == 255:
targetValue = 1
else:
targetValue = 0
ds.addSample(np.ravel(test[y-block_length:y+block_length+1,x-block_width:x+block_width+1]), [targetValue])
for i in range(0,data_length,block_width):
for j in range(0,data_width,block_length):
ds.addSample(np.ravel(test[i:i+block_length,j:j+block_width]), np.ravel(target[i:i+block_length,j:j+block_width]))
#ds._convertToOneOfMany()
"""
# ds.addSample(np.ravel(test), np.ravel(target))
trainer = BackpropTrainer(net, ds)
for i in range(1):
trainer.trainEpochs(1)