Beispiel #1
0
def createAndTrainNetworkFromFile(curs_filename,
                                  count_input_samples,
                                  count_samples,
                                  net_filename,
                                  count_layers=33,
                                  count_outputs=1,
                                  max_epochs=15000,
                                  min_epochs=300):
    net = buildNetwork(count_input_samples, count_layers, count_outputs)
    ds = SupervisedDataSet(count_input_samples, count_outputs)
    wb = load_workbook(filename=curs_filename)
    ws = wb.active
    for i in range(0, count_samples):
        loaded_data = []
        for j in range(0, count_input_samples + 1):
            loaded_data.append(
                round(float(ws.cell(row=i + 1, column=j + 1).value), 4))
            #ds.addSample(loaded_data[:-1], loaded_data[-1])
        #print loaded_data[:-1], loaded_data[-1]
        ds.addSample(loaded_data[:-1], loaded_data[-1])
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.setData(ds)
    a = trainer.trainUntilConvergence(maxEpochs=max_epochs,
                                      continueEpochs=min_epochs,
                                      validationProportion=0.15)
    net_filename = net_filename[:-4] + str(a[0][-1]) + '.xml'
    NetworkWriter.writeToFile(net, net_filename)
    result_list = [a, net_filename]
    return result_list
Beispiel #2
0
def trainNetwork(net, sample_list, validate_list, net_filename, max_epochs=5500, min_epochs=300):
    count_input_samples = len(sample_list)
    count_outputs = len(validate_list)
    ds = SupervisedDataSet(count_input_samples, count_outputs)
    ds.addSample(sample_list, validate_list)
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.setData(ds)
    trainer.trainUntilConvergence(maxEpochs=max_epochs, continueEpochs=min_epochs)
    NetworkWriter.writeToFile(net, net_filename)
    return net
Beispiel #3
0
def createAndTrainNetworkFromList(train_list, count_input_samples, net_filename, count_layers=33,
                          count_outputs=1, max_epochs=15000, min_epochs=300):
    net = buildNetwork(count_input_samples, count_layers, count_outputs)
    ds = SupervisedDataSet(count_input_samples, count_outputs)
    count_samples = len(train_list)
    for i in range(0, count_samples):
        ds.addSample(train_list[i][:-count_outputs], train_list[i][-count_outputs])
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.setData(ds)
    a = trainer.trainUntilConvergence(maxEpochs=max_epochs, continueEpochs=min_epochs, validationProportion=0.15)
    net_filename = net_filename[:-4]+str(a[0][-1])+'.xml'
    NetworkWriter.writeToFile(net, net_filename)
    result_list = [a, net_filename]
    return result_list
Beispiel #4
0
def trainNetwork(net,
                 sample_list,
                 validate_list,
                 net_filename,
                 max_epochs=5500,
                 min_epochs=300):
    count_input_samples = len(sample_list)
    count_outputs = len(validate_list)
    ds = SupervisedDataSet(count_input_samples, count_outputs)
    ds.addSample(sample_list, validate_list)
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.setData(ds)
    trainer.trainUntilConvergence(maxEpochs=max_epochs,
                                  continueEpochs=min_epochs)
    NetworkWriter.writeToFile(net, net_filename)
    return net
Beispiel #5
0
def createAndTrainNetworkFromFile(curs_filename, count_input_samples, count_samples, net_filename, count_layers=33,
                          count_outputs=1, max_epochs=15000, min_epochs=300):
    net = buildNetwork(count_input_samples, count_layers, count_outputs)
    ds = SupervisedDataSet(count_input_samples, count_outputs)
    wb = load_workbook(filename=curs_filename)
    ws = wb.active
    for i in range(0, count_samples):
        loaded_data = []
        for j in range(0, count_input_samples + 1):
            loaded_data.append(round(float(ws.cell(row=i+1, column=j+1).value), 4))
            #ds.addSample(loaded_data[:-1], loaded_data[-1])
        #print loaded_data[:-1], loaded_data[-1]
        ds.addSample(loaded_data[:-1], loaded_data[-1])
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.setData(ds)
    a = trainer.trainUntilConvergence(maxEpochs=max_epochs, continueEpochs=min_epochs, validationProportion=0.15)
    net_filename = net_filename[:-4]+str(a[0][-1])+'.xml'
    NetworkWriter.writeToFile(net, net_filename)
    result_list = [a, net_filename]
    return result_list
Beispiel #6
0
class Brain():
    def __init__(self): 
        self.inputs = 3
        self.outputs = 1
        self.n = buildNetwork(self.inputs, 200,200,200,200,self.outputs, bias=True,hiddenclass=TanhLayer)
        self.n.sortModules()
        self.ds = SupervisedDataSet(self.inputs, self.outputs)
        self.trainer = RPropMinusTrainer(self.n)
        self.trainer.setData(self.ds)
    def wipedataset(self):
        self.ds = SupervisedDataSet(self.inputs, self.outputs)
        pass
    def cycle(self,action,state):
        return self.n.activate([action,state[0],state[1]])
    def AddToTrainingSet(self,action,state,output):
        out= "New Set","Action: ",action,"State: ", state,"Output: ", output
        f.write(str(out)+"\n")
        self.ds.addSample((action,state[0],state[1]),output)
    def train(self):
        return "ERROR",self.trainer.train()
    def traintoconverg(self):
        x = 10000
        y=0
        z=100
        print len(self.ds),"DS SIZE"
        while x > 0.0001 and y < z:
            print len(self.ds)
            x = self.trainer.train()
            print x,"ERROR",y
            y+=1
        f = open('brains/brain2000.ann','w')
        pickle.dump(self.n,f)
    def trainfinal(self):
        x = 10000
        y=0
        z=25
        while x > 0.00001 and y < z:
            x = self.trainer.train()
            print x,"ERROR",y
            y+=1
Beispiel #7
0
def createAndTrainNetworkFromList(train_list,
                                  count_input_samples,
                                  net_filename,
                                  count_layers=33,
                                  count_outputs=1,
                                  max_epochs=15000,
                                  min_epochs=300):
    net = buildNetwork(count_input_samples, count_layers, count_outputs)
    ds = SupervisedDataSet(count_input_samples, count_outputs)
    count_samples = len(train_list)
    for i in range(0, count_samples):
        ds.addSample(train_list[i][:-count_outputs],
                     train_list[i][-count_outputs])
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.setData(ds)
    a = trainer.trainUntilConvergence(maxEpochs=max_epochs,
                                      continueEpochs=min_epochs,
                                      validationProportion=0.15)
    net_filename = net_filename[:-4] + str(a[0][-1]) + '.xml'
    NetworkWriter.writeToFile(net, net_filename)
    result_list = [a, net_filename]
    return result_list
n.addConnection(FullConnection(inLayer, hiddenLayers[0]))

for i in range(1, len(hiddenLayers)):
    n.addConnection(FullConnection(hiddenLayers[i - 1], hiddenLayers[i]))

n.addConnection(FullConnection(hiddenLayers[len(hiddenLayers) - 1], outLayer))
n.sortModules()


# training set
DS = SupervisedDataSet(10, 5)

trainer = RPropMinusTrainer(n, verbose=True, batchlearning=True, learningrate=0.01, lrdecay=0.0, momentum=0.0,
                            weightdecay=0.0)
trainer.setData(DS)

data = days('btceUSD.days.csv')


def normalize(v, _max, _min):
    return 2.0 / (_max - _min) * (v - _min) - 1.0


def denormalize(v, _max, _min):
    return (v + 1.0) / 2.0 * (_max - _min) + _min


def ticks_to_inputs_outputs(ticks, ticks_forecast):
    window_prices = map(lambda (x): x[0], ticks)
    window_volumes = map(lambda (x): x[1], ticks)
                    nr.append(ratio)    
                print ratio, column   
            else:
                print column, "not an int or long"         
    return np.array(nr[:-1]), nr[-1]

data = cursor.execute("select %s from adult_data" % columns).fetchall()

dataset = SupervisedDataSet(8, 1)
for row in data:
    xd, yd = createNPRow(row)       
    dataset.addSample(xd, yd)

nn = buildNetwork(8, 3, 1)
trainer = RPropMinusTrainer(nn)
trainer.setData(dataset)

for x in range(5):
    error = trainer.train()
    print error
  
errors, success = 0,0
for row in cursor.execute("select %s from adult_test" % columns).fetchall():    
    xd, yd = createNPRow(row)    
    check = int(round(nn.activate(xd[:8])[0]))
    if check > 1: check = 1
    prediction = possibilities['relation_to_50k_plus'][check]
    actual = possibilities['relation_to_50k_plus'][yd]
    if prediction == actual:
        match = "match"
        success += 1
Beispiel #10
0
		testset.append((a,random.uniform(-math.pi,-math.pi/2),-1))
		testset.append((a,random.uniform(math.pi/2,math.pi),-1))

	elif a == 1:
		testset.append((a,random.uniform(-math.pi,0),1))
		testset.append((a,random.uniform(0,math.pi),-1))
	else:
		testset.append((a,random.uniform(0,math.pi),1))
		testset.append((a,random.uniform(-math.pi,0),-1))


ann = buildNetwork(2,20,1,bias=True,hiddenclass=TanhLayer)
ds = SupervisedDataSet(2,1)
ann.sortModules()
trainer = RPropMinusTrainer(ann)
trainer.setData(ds)
for i in dataset:
	ds.addSample((i[0],i[1]),i[2])

i=10000
x=0
z=100
while i > 0.0001 and x<z:
	i = trainer.train()
	print i
	x+=1

for a in testset:
	result = ann.activate([a[0],a[1]])
	print str(a[2]) +" actual vs. output " +str(result)
                print ratio, column
            else:
                print column, "not an int or long"
    return np.array(nr[:-1]), nr[-1]


data = cursor.execute("select %s from adult_data" % columns).fetchall()

dataset = SupervisedDataSet(8, 1)
for row in data:
    xd, yd = createNPRow(row)
    dataset.addSample(xd, yd)

nn = buildNetwork(8, 3, 1)
trainer = RPropMinusTrainer(nn)
trainer.setData(dataset)

for x in range(5):
    error = trainer.train()
    print error

errors, success = 0, 0
for row in cursor.execute("select %s from adult_test" % columns).fetchall():
    xd, yd = createNPRow(row)
    check = int(round(nn.activate(xd[:8])[0]))
    if check > 1: check = 1
    prediction = possibilities['relation_to_50k_plus'][check]
    actual = possibilities['relation_to_50k_plus'][yd]
    if prediction == actual:
        match = "match"
        success += 1
Beispiel #12
0
for i in range(len(files)):
    img = Image.open("img/" + files[i])
    data = np.array(img)
    data = data.reshape(-1)
    ds.addSample((data), (files[i][0:6]))

img1 = Image.open("test_img/152830.png")

data1 = np.array(img1)
data1 = data1.reshape(-1)

net = buildNetwork(200 * 60 * 3, 1)

trainer = RPropMinusTrainer(net)
trainer.setData(ds)

trainer.trainEpochs(100)


def calculation(a, b):
    i = 0
    if ((int(a) // 100000) % 10 == (int(b) // 100000) % 10):
        i = i + 1
    if ((int(a) // 10000) % 10 == (int(b) // 10000) % 10):
        i = i + 1
    if ((int(a) // 1000) % 10 == (int(b) // 1000) % 10):
        i = i + 1
    if ((int(a) // 100) % 10 == (int(b) // 100) % 10):
        i = i + 1
    if ((int(a) // 10) % 10 == (int(b) // 10) % 10):