本文整理汇总了Python中pybrain.datasets.SequentialDataSet.splitWithProportion方法的典型用法代码示例。如果您正苦于以下问题:Python SequentialDataSet.splitWithProportion方法的具体用法?Python SequentialDataSet.splitWithProportion怎么用?Python SequentialDataSet.splitWithProportion使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybrain.datasets.SequentialDataSet
的用法示例。
在下文中一共展示了SequentialDataSet.splitWithProportion方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: simple_rnn_classifier
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import splitWithProportion [as 别名]
class simple_rnn_classifier():
def __init__(self, ds):
self.alldata = SequentialDataSet(ds.num_features, 1)
# Now add the samples to the data set.
idx = 1
self.alldata.newSequence()
for sample in ds.all_moves:
self.alldata.addSample(sample.get_features(), [ds.get_classes().index(sample.class_)])
idx += 1
if (idx%6 == 0):
self.alldata.newSequence()
self.tstdata, self.trndata = self.alldata.splitWithProportion(0.25)
#print "Number of training patterns: ", len(self.trndata)
#print "Input and output dimensions: ", self.trndata.indim, self.trndata.outdim
#print "First sample (input, target, class):"
#print self.trndata['input'][0], self.trndata['target'][0], self.trndata['class'][0]
# 5 hidden layers.
self.rnn = buildNetwork(self.trndata.indim,
3,
self.trndata.outdim,
hiddenclass=LSTMLayer,
outclass=SigmoidLayer,
recurrent=True)
self.rnn.randomize()
self.trainer = BackpropTrainer(self.nn, dataset=self.trndata, momentum=0.1, verbose=True, weightdecay=0.01)
def start_training(self):
f = open("./results/rnn_perf.txt", "w");
for i in range(200):
print "training step: " , i
self.trainer.trainEpochs(1)
err = self.evaluate()
f.write(str(err) + ",")
f.flush()
f.close()
def evaluate(self):
print "epoch:" , self.trainer.totalepochs
correct = 0
wrong = 0
self.fnn.sortModules()
for Idx in range (len(self.tstdata)):
out = self.fnn.activate(self.tstdata['input'][Idx])
if argmax(out) == argmax(self.tstdata['target'][Idx]) :
correct += 1
else:
wrong += 1
correct_ratio = correct*1.0/(wrong + correct)
self.correct_perc.append(correct_ratio)
print "Wrong Predictions: " , wrong , "Ratio = ", wrong*100.0/(wrong+correct) , "%"
print "Correct Predictions: ", correct, "Ratio = ", correct*100.0/(wrong+correct) , "%"
if (self.max_ratio < correct_ratio):
print "Found new max, saving network"
self.write_out("best_perfrming_")
self.max_ratio = correct_ratio
return 1 - correct_ratio
def write_out(self, name=""):
NetworkWriter.writeToFile(self.fnn, "./results/" + name + "rnn.xml")
示例2: evalSensor
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import splitWithProportion [as 别名]
def evalSensor(sensorIndex, featureSpaceIndices):
#reset data structure
allByTime = {}
f = open(fileName, 'r')
headers = f.readline().split()
for line in f:
splited = line.split()
timeStamp = int(splited[0])
allByTime[timeStamp] = {}
f.close()
allByJoint = {}
for inputIndices in featureSpaceIndices:
allByJoint[inputIndices] = {}
clfs = {}
grades = {}
for inputIndices in featureSpaceIndices:
allByTime, allByJoint = angleExtraction.prepareAnglesFromInput(fileName, inputIndices, sensorIndex, True, allByTime, allByJoint)
#normalizing allByJoint
timeSet = Set([])
for inputIndices in featureSpaceIndices:
vec = []
for timeStamp in allByTime.keys():
if(timeStamp in allByJoint[inputIndices].keys()):
timeSet.add(timeStamp)
x = allByJoint[inputIndices][timeStamp]
vec.append(x)
if(len(vec) > 0):
vec = angleExtraction.normelizeVector(vec)
i=0
for timeStamp in allByTime.keys():
if(timeStamp in allByJoint[inputIndices].keys()):
allByJoint[inputIndices][timeStamp] = vec[i]
i = i + 1
#time set to list, output dict to list
time = []
for timeStamp in timeSet:
time.append(timeStamp)
time.sort()
allOutput = []
tmpTime = []
#clean zeros, create time ordered output vector
for timeStamp in time:
out = allByTime[timeStamp]['output']
if(out != 0 and len(allByTime[timeStamp]) == featureNum + 1):
tmpTime.append(timeStamp)
allOutput.append(out)
time = tmpTime
#normalize allOutput
allOutput = normalByPercentile(allOutput)
#create a net
hiddenSize = (featureNum + 2)/2
net = buildNetwork(featureNum, hiddenSize, 1, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True, bias=True)
#build dataset
ds = SequentialDataSet(featureNum, 1)
i=0
lastTimeStamp = time[0]
for timeStamp in time:
if(len(allByTime[timeStamp]) == featureNum+1):#it is a full vector
if(timeStamp - lastTimeStamp > 100):
ds.newSequence()
sample = []
for inputIndices in featureSpaceIndices:
sample.append(allByTime[timeStamp][inputIndices])
ds.appendLinked(sample, allOutput[i])
i = i + 1
lastTimeStamp = timeStamp
#train
net.randomize()
tstdata, trndata = ds.splitWithProportion( 0.25 )
trainer = BackpropTrainer(net, trndata)
print len(ds)
min = 100
trainNum = 100
bestTrainer = None
for i in range(trainNum):
res = trainer.train()
if(res < min):
min = res
bestTrainer = trainer
net.randomize()
print min
"""
res = 100
while(res > min):
net.randomize()
res = trainer.train()
"""
trainer = bestTrainer
for i in range(trainNum):
res = trainer.train()
if(i % (trainNum/10) == 0):
print res
print 'trndata.evaluateModuleMSE ' + str(trndata.evaluateModuleMSE(net))
print 'tstdata.evaluateModuleMSE ' + str(tstdata.evaluateModuleMSE(net))
#print net.activateOnDataset(tstdata)
hits = 0.0
total = 0.0
#.........这里部分代码省略.........