本文整理汇总了Python中pybrain.datasets.SupervisedDataSet类的典型用法代码示例。如果您正苦于以下问题:Python SupervisedDataSet类的具体用法?Python SupervisedDataSet怎么用?Python SupervisedDataSet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SupervisedDataSet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: entrenarSomnolencia
def entrenarSomnolencia(red):
#Se inicializa el dataset
ds = SupervisedDataSet(4096,1)
"""Se crea el dataset, para ello procesamos cada una de las imagenes obteniendo los rostros,
luego se le asignan los valores deseados del resultado la red neuronal."""
print "Somnolencia - cara"
for i,c in enumerate(os.listdir(os.path.dirname('/home/taberu/Imágenes/img_tesis/somnoliento/'))):
try:
im = cv2.imread('/home/taberu/Imágenes/img_tesis/somnoliento/'+c)
pim = pi.procesarImagen(im)
cara = d.deteccionFacial(pim)
if cara == None:
print "No hay cara"
else:
print i
ds.appendLinked(cara.flatten(),10)
except:
pass
trainer = BackpropTrainer(red, ds)
print "Entrenando hasta converger"
trainer.trainUntilConvergence()
NetworkWriter.writeToFile(red, 'rna_somnolencia.xml')
示例2: getErrorPercent
def getErrorPercent(training_dataset, eval_dataset_list, num_hidden, num_epochs):
num_datapoints = len(training_dataset)
num_inputs = len(training_dataset[0][0])
num_outputs = len(training_dataset[0][1])
# print "Num Inputs:", num_inputs
# print "Num Outputs:", num_outputs
# print "Num Hidden Nodes:", num_hidden
NN = buildNetwork(num_inputs, num_hidden, num_outputs, bias=True, hiddenclass=SigmoidLayer, outclass=SigmoidLayer)
dataset = SupervisedDataSet(num_inputs, num_outputs)
for datapoint in training_dataset:
dataset.addSample(datapoint[0], datapoint[1])
trainer = BackpropTrainer(NN, dataset=dataset, momentum=0.0, verbose=False, weightdecay=0.0)
for epoch in range(0, num_epochs):
#print epoch
trainer.train()
errors = []
for eval_set in eval_dataset_list:
total_percent_errors = [0]*num_outputs
for jj in range(0, len(eval_set)):
nn_out = NN.activate(eval_set[jj][0])
percent_error = computeError(eval_set[jj][1], nn_out)
#print percent_error
total_percent_errors = map(operator.add, percent_error, total_percent_errors)
#print total_percent_errors
errors.append(map(operator.div, total_percent_errors, [len(dataset)]*num_outputs))
#print errors
return errors
示例3: main
def main():
inputs = ReadCSV('./data/input.csv')
outputs = ReadCSV('./data/output.csv')
test_set = test.keys()
train_set = []
for k in inputs.keys():
if k not in test_set:
train_set.append(k)
print "Number of training samples", len(train_set)
print "Number of testing samples", len(test_set)
net = buildNetwork(178, 6, 5)
ds=SupervisedDataSet(178,5)
for id in train_set:
ds.addSample(inputs[id],outputs[id])
trainer = BackpropTrainer(net, ds, learningrate=0.001, momentum = 0.001)
trainer.trainUntilConvergence(maxEpochs=1000, validationProportion = 0.5)
for id in test_set:
predicted = net.activate(inputs[id])
actual = outputs[id]
print '-----------------------------'
print test[id]
print '-----------------------------'
print 'Trait\t\tPredicted\tActual\tError'
for i in range(0,5):
error = abs(predicted[i] - actual[i])*100/4.0
print traits[i], '\t', predicted[i], '\t', actual[i], '\t', error,"%"
示例4: __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
示例5: loadDataSet
def loadDataSet(ds_file):
global X, Y
BB = set()
aaa = {}
ds = SupervisedDataSet(400, 10)
#ds = SupervisedDataSet(1024, 5)
with open(ds_file,"rb") as f:
lines = f.readlines()
for line in lines:
l = [float(a) for a in line.strip().split(',')]
#A = [float(1.0)] + l[:-1]
A = l[:-1]
X.append(A)
B = int(l[-1])
#BB.update([B])
#for aa,bb in enumerate(BB):
# aaa[bb] = aa
#print aaa
#Y.append(aaa[bb])
Y.append(B)
C = []
for i in range(10):
C.append(int(1) if i==B or (i==0 and B==10) else int(0))
ds.addSample(tuple(A), tuple(C))
return ds
示例6: run_data
def run_data():
with open('new_data2.txt') as data_file:
data = json.load(data_file)
ds = SupervisedDataSet(1316, 1316)
for i in xrange(0, 51):
print "Adding {}th data sample".format(i),
input = tuple(data[str(i)]['input'])
output = tuple(data[str(i)]['output'])
# print len(input), len(output)
ds.addSample( input, output)
print ":: Done"
print "Train"
net = buildNetwork( 1316, 100, 1316, bias=True, )
trainer = BackpropTrainer(net, ds)
terrors = trainer.trainUntilConvergence(verbose = True, validationProportion = 0.33, maxEpochs = 20, continueEpochs = 10 )
# print terrors[0][-1],terrors[1][-1]
fo = open("results2.txt", "w")
for input, expectedOutput in ds:
output = net.activate(input)
count = 0
for q in xrange(0, 1316):
print output[q], expectedOutput[q]
if math.floor(output[q]) == math.floor(expectedOutput[q]):
count+=1
m = float(count)/1316.00
print "{0} :: {1}".format(count, m)
fo.write("{0} :: {1}\n".format(count, m))
示例7: learn
def learn(self):
# convert reinforcement dataset to NFQ supervised dataset
supervised = SupervisedDataSet(self.module.network.indim, 1)
for seq in self.dataset:
lastexperience = None
for state, action, reward in seq:
if not lastexperience:
# delay each experience in sequence by one
lastexperience = (state, action, reward)
continue
# use experience from last timestep to do Q update
(state_, action_, reward_) = lastexperience
Q = self.module.getValue(state_, action_[0])
inp = r_[state_, one_to_n(action_[0], self.module.numActions)]
tgt = Q + 0.5*(reward_ + self.gamma * max(self.module.getActionValues(state)) - Q)
supervised.addSample(inp, tgt)
# update last experience with current one
lastexperience = (state, action, reward)
# train module with backprop/rprop on dataset
trainer = RPropMinusTrainer(self.module.network, dataset=supervised, batchlearning=True, verbose=False)
trainer.trainUntilConvergence(maxEpochs=self.maxEpochs)
示例8: NeuralKinect
class NeuralKinect():
def __init__(self):
# Softmax layer -> great for classification networks
#self.neuralNet = buildNetwork(60, 60, 5, outclass=SoftmaxLayer)
#self.neuralNet = buildNetwork(60, 60, 5, hiddenclass=TanhLayer)
#self.neuralNet = buildNetwork(60, 60, 5, bias=True)
self.neuralNet = buildNetwork(60, 60, 5)
self.dataSet = SupervisedDataSet(60, 5)
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()))
def loadDataSet(self):
points = []
for csvFile in glob.iglob("TrainData/*.csv"):
with open(csvFile, 'rt') as letterSet:
reader = csv.reader(letterSet)
header = str(reader.next())
letter = header[2:3]
targetStr = header[4:9]
print("Processing Dataset for letter -> " + letter)
target = []
for digit in targetStr:
target.append(digit)
rows = 1
for row in reader:
for col in row:
points.append(col)
if rows % 20 == 0:
self.dataSet.addSample(points, target)
points = []
rows += 1
def processResults(self, output):
result = ""
for digit in output:
if digit > 0.5:
result += "1"
else:
result += "0"
print("Network result -> " + chr(64+int(result,2)))
def testNetwork(self):
points = []
for csvFile in glob.iglob("TestData/*.csv"):
with open(csvFile, 'rt') as testPose:
reader = csv.reader(testPose)
rows = 1
for row in reader:
for col in row:
points.append(col)
if rows % 20 == 0:
self.processResults(self.neuralNet.activate(points))
points = []
rows += 1
示例9: neural_network
def neural_network(data, target, network):
DS = SupervisedDataSet(len(data[0]), 1)
nn = buildNetwork(len(data[0]), 7, 1, bias = True)
kf = KFold(len(target), 10, shuffle = True);
RMSE_NN = []
for train_index, test_index in kf:
data_train, data_test = data[train_index], data[test_index]
target_train, target_test = target[train_index], target[test_index]
for d,t in zip(data_train, target_train):
DS.addSample(d, t)
bpTrain = BackpropTrainer(nn,DS, verbose = True)
#bpTrain.train()
bpTrain.trainUntilConvergence(maxEpochs = 10)
p = []
for d_test in data_test:
p.append(nn.activate(d_test))
rmse_nn = sqrt(np.mean((p - target_test)**2))
RMSE_NN.append(rmse_nn)
DS.clear()
time = range(1,11)
plt.figure()
plt.plot(time, RMSE_NN)
plt.xlabel('cross-validation time')
plt.ylabel('RMSE')
plt.show()
print(np.mean(RMSE_NN))
示例10: get_dataset_for_pybrain_regression
def get_dataset_for_pybrain_regression(X,y):
ds = SupervisedDataSet(250,1)
tuples_X = [tuple(map(float,tuple(x))) for x in X.values]
tuples_y = [tuple(map(float,(y,))) for y in y.values]
for X,y in zip(tuples_X,tuples_y):
ds.addSample(X,y)
return ds
示例11: learn
def learn(self,dataset):
"""
This function trains network
Input:
dataset - Dataset to train network
Returns:
Nothing
"""
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.datasets import SupervisedDataSet
from neuraltrainer import NeuralTrainer
if self._net == None: raise NeuralBrainException("Brain is not configured!")
if dataset == {}: raise NeuralBrainException("Dataset for learning is empty.")
data = SupervisedDataSet(self._input,self._output)
for input,output in dataset.items():
input = self._normalize(input,self._input)
output = self._normalize(output,self._output)
data.addSample(input,output)
data.addSample(input,output)# For better learning 2x
trainer = NeuralTrainer(self._net, data)
trainer.simpleTrain()
示例12: get_train_samples
def get_train_samples(input_num,output_num):
'''
从new_samples文件夹中读图,根据输入数和输出数制作样本,每一原始样本加入随机噪音生成100个样本
'''
print 'getsample start.'
sam_path='./new_samples'
samples = SupervisedDataSet(input_num,output_num)
nlist = os.listdir(sam_path)
t=int(np.sqrt(input_num))
for n in nlist:
file = os.path.join(sam_path,n)
im = Image.open(file)
im = im.convert('L')
im = im.resize((t,t),Image.BILINEAR)
buf = np.array(im).reshape(input_num,1)
buf = buf<200
buf = tuple(buf)
buf1=int(n.split('.')[0])
buf2=range(output_num)
for i in range(len(buf2)):
buf2[i] = 0
buf2[buf1]=1
buf2 = tuple(buf2)
samples.addSample(buf,buf2)
for i in range(100):
buf3 = list(buf)
for j in range(len(buf)/20):
buf3[np.random.randint(len(buf))] = bool(np.random.randint(2))
samples.addSample(tuple(buf3),buf2)
return samples
示例13: convertDataNeuralNetwork
def convertDataNeuralNetwork(x, y):
data = SupervisedDataSet(x.shape[1], 1)
for xIns, yIns in zip(x, y):
data.addSample(xIns, yIns)
return data
def NN(xTrain, yTrain, xTest, yTest):
trainData = convertDataNeuralNetwork(xTrain, yTrain)
testData = convertDataNeuralNetwork(xTest, yTest)
fnn = FeedForwardNetwork()
inLayer = SigmoidLayer(trainData.indim)
hiddenLayer = SigmoidLayer(5)
outLayer = LinearLayer(trainData.outdim)
fnn.addInputModule(inLayer)
fnn.addModule(hiddenLayer)
fnn.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)
fnn.addConnection(in_to_hidden)
fnn.addConnection(hidden_to_out)
fnn.sortModules()
trainer = BackpropTrainer(fnn, dataset = trainData, momentum = 0.1, verbose = True, weightdecay = 0.01)
for i in xrange(10):
trainer.trainEpochs(500)
rmse = percentError(trainer.testOnClassData(dataset = testData), yTest)
return rmse/100
def main():
rmse = NN(xTrain, yTrain, xTest, yTest)
print rmse
if __name__=="__main__":
main()
示例14: run_try
def run_try(self, rand_chance=0, rand_count=0, rand_count_ref=0, render=False):
ds = SupervisedDataSet(env_size, 1)
observation = env.reset()
random_indexes = []
while len(random_indexes) < rand_count:
random_index = math.floor(random() * rand_count_ref)
if random_index not in random_indexes:
random_indexes.append(random_index)
for t in range(max_frames):
if render:
env.render()
# print(observation)
action = 0 if net.activate(observation)[0] < 0 else 1
if t in random_indexes or random() < rand_chance:
action = (action + 1) % 1
ds.addSample(observation, (action,))
observation, reward, done, info = env.step(action)
if done:
print("Episode finished after {} timesteps".format(t + 1))
break
if t == max_frames - 1:
print("Passed!!")
self.run_try(render=True)
return t, ds
示例15: __init__
class dataset:
# Initialize the dataset with input and label size
def __init__(self, inputsize, labelsize):
self.inputsize = inputsize
self.labelsize = labelsize
self.DS = SupervisedDataSet(self.inputsize, self.labelsize)
# Adds data to existing training dataset
def addTrainingData(self,inputdata, labeldata):
try:
if inputdata.size == self.inputsize and labeldata.size == self.labelsize:
self.DS.appendLinked(inputdata, labeldata)
return 1
except AttributeError:
print "Input error."
return 0
def getTrainingDataset(self):
return self.DS
def generateDataSet(self):
for line in fileinput.input(['data/inputdata3.txt']):
x = line.split(':')
# print ft.feature.getImageFeatureVector(x[0]),np.array([int(x[1])])
self.addTrainingData(ft.feature.getImageFeatureVector(x[0]),np.array([int(x[1])]))
return 1