Search code examples
pythonneural-networkpybrain

How do you make a prediction (forecast) from a trained network for a given input?


Below is my code for a neural network,with 3 inputs and 1 hidden layer and 1 output:

#Data 
ds = SupervisedDataSet(3,1)

myfile = open('my_file.csv','r')

for data in tf.myfile ():
   indata =  tuple(data[:3])
   outdata = tuple(data[3])
   ds.addSample(indata,outdata)

net = FeedForwardNetwork() 
inp = LinearLayer(3) 
h1 = SigmoidLayer(1) 
outp = LinearLayer(1)

# add modules 
net.addOutputModule(outp) 
net.addInputModule(inp) 
net.addModule(h1)

# create connections 
net.addConnection(FullConnection(inp, h1))  
net.addConnection(FullConnection(h1, outp))

# finish up 
net.sortModules()

# initialize the backprop trainer and train 
trainer = BackpropTrainer(net, ds)
trainer.trainOnDataset(ds,1000) trainer.testOnData(verbose=True)

print 'Final weights:',net.params

My question is,if you want to use this trained neural network to make a forecast based on specific inputs,how do you do it?


Solution

  • According to the documentation, you can test specific inputs with the activate method on your network. Assuming your input looks something like (1 2 3) your code would look like

    net.activate((1,2,3))