def exec_algo(xml_file, output_location): rootObj=ml.parse(xml_file) file_name=rootObj.MachineLearning.prediction.datafile file=open(file_name) var_input=rootObj.MachineLearning.prediction.input var_output=rootObj.MachineLearning.prediction.output var_classes=rootObj.MachineLearning.prediction.classes DS=ClassificationDataSet(var_input,var_output,nb_classes=var_classes) #DS1=ClassificationDataSet(13,1,nb_classes=10) for line in file.readlines(): data=[float(x) for x in line.strip().split(',') if x != ''] inp=tuple(data[:var_input]) output=tuple(data[var_input:]) DS.addSample(inp,output) tstdata,trndata=DS.splitWithProportion(0) #trndatatest,tstdatatest=DS1.splitWithProportion(0) trdata=ClassificationDataSet(trndata.indim,1,nb_classes=10) #tsdata=ClassificationDataSet(DS1.indim,1,nb_classes=10) #tsdata1=ClassificationDataSet(DS1.indim,1,nb_classes=10) for i in xrange(trndata.getLength()): if (trndata.getSample(i)[1][0]!=100): trdata.addSample(trndata.getSample(i)[0],trndata.getSample(i)[1]) trdata._convertToOneOfMany() #tsdata._convertToOneOfMany() #tsdata1._convertToOneOfMany() print "%d" % (trdata.getLength()) rnn=RecurrentNetwork() inputLayer=LinearLayer(trdata.indim) hiddenLayer=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.hiddenLayerActivation hiddenNeurons=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.hiddenNeurons if hiddenLayer=='Sigmoid': hiddenLayer=SigmoidLayer(hiddenNeurons) elif hiddenLayer=='Softmax': hiddenLayer=SoftmaxLayer(hiddenNeurons) else: hiddenLayer=LinearLayer(hiddenNeurons) outputLayer=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.outputLayerActivation if outputLayer=='Sigmoid': outputLayer=SigmoidLayer(trdata.outdim) elif outputLayer=='Softmax': outputLayer=SoftmaxLayer(trdata.outdim) else: outputLayer=LinearLayer(trdata.outdim) rnn.addInputModule(inputLayer) rnn.addModule(hiddenLayer) rnn.addOutputModule(outputLayer) rnn_type=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.RNN_Type in_to_hidden=FullConnection(inputLayer,hiddenLayer) hidden_to_outputLayer=FullConnection(hiddenLayer,outputLayer) rnn.addConnection(in_to_hidden) rnn.addConnection(hidden_to_outputLayer) if rnn_type=='Elman': hidden_to_hidden=FullConnection(hiddenLayer,hiddenLayer, name='c3') rnn.addRecurrentConnection(hidden_to_hidden) #hidden_to_hidden=FullConnection(hiddenLayer,hiddenLayer, name='c3') if rnn_type=='Jordan': output_to_hidden=FullConnection(outputLayer,hiddenLayer, name='c3') rnn.addRecurrentConnection(output_to_hidden) #rnn.addRecurrentConnection(hidden_to_hidden) momentum=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.momentum weightdecay=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.learningRate rnn.sortModules() trainer=BackpropTrainer(rnn,dataset=trdata,momentum=0.1,verbose=True,weightdecay=0.01) trainer.train(); result=(percentError(trainer.testOnClassData(dataset=trdata),trdata['class'])) #result1=percentError(trainer.testOnClassData(dataset=tsdata1),tsdata1['class']) print ('%f \n') % (100-result) #print ('%f \n') % (100-result1) ts=time.time() directory = output_location + sep + str(int(ts)) makedirs(directory) fileObject=open(output_location + sep + str(int(ts)) + sep + 'pybrain_RNN','w') pickle.dump(trainer,fileObject) pickle.dump(rnn,fileObject) fileObject.close()
def exec_algo(xml_file, output_location): rootObj=ml.parse(xml_file) #Getting the root element so that we get the subclasses and its members and member function file=open(rootObj.MachineLearning.classification.datafile) var_inp=rootObj.MachineLearning.classification.input var_out=rootObj.MachineLearning.classification.output classes=rootObj.MachineLearning.classification.classes DS=ClassificationDataSet(var_inp,var_out,nb_classes=classes) for line in file.readlines(): data=[float(x) for x in line.strip().split(',') if x != ''] inp=tuple(data[:var_inp]) output=tuple(data[var_inp:]) DS.addSample(inp,output) split=rootObj.MachineLearning.classification.split tstdata,trndata=DS.splitWithProportion(split) trdata=ClassificationDataSet(trndata.indim,var_out,nb_classes=classes) tsdata=ClassificationDataSet(tstdata.indim,var_out,nb_classes=classes) for i in xrange(trndata.getLength()): trdata.addSample(trndata.getSample(i)[0],trndata.getSample(i)[1]) for i in xrange(tstdata.getLength()): tsdata.addSample(tstdata.getSample(i)[0],tstdata.getSample(i)[1]) trdata._convertToOneOfMany() tsdata._convertToOneOfMany() hiddenNeurons=rootObj.MachineLearning.classification.algorithm.RadialBasisFunctionNetwork.hiddenNeurons fnn=FeedForwardNetwork() inputLayer=LinearLayer(trdata.indim) hiddenLayer=GaussianLayer(hiddenNeurons) outputLayer=LinearLayer(trdata.outdim) fnn.addInputModule(inputLayer) fnn.addModule(hiddenLayer) fnn.addOutputModule(outputLayer) in_to_hidden=FullConnection(inputLayer,hiddenLayer) hidden_to_outputLayer=FullConnection(hiddenLayer,outputLayer) fnn.addConnection(in_to_hidden) fnn.addConnection(hidden_to_outputLayer) fnn.sortModules() learningrate=rootObj.MachineLearning.classification.algorithm.RadialBasisFunctionNetwork.learningRate momentum=rootObj.MachineLearning.classification.algorithm.RadialBasisFunctionNetwork.momentum epochs=rootObj.MachineLearning.classification.algorithm.RadialBasisFunctionNetwork.epochs trainer=BackpropTrainer(fnn,dataset=trdata, verbose=True, learningrate=learningrate, momentum=momentum) trainer.trainEpochs(epochs=epochs) #trainer.train() #trainer.trainUntilConvergence(dataset=trdata, maxEpochs=500, verbose=True, continueEpochs=10, validationProportion=0.25) trresult=percentError(trainer.testOnClassData(),trdata['class']) #testingResult=percentError(trainer.testOnClassData(dataset=tsdata),tsdata['class']) #print "Training accuracy : %f , Testing Accuracy: %f" % (100-trresult,100-testingResult) print "Training accuracy : %f " % (100-trresult) ts=time.time() directory = output_location + sep + str(int(ts)) ; makedirs(directory) fileObject=open(output_location + sep + str(int(ts)) + sep + 'pybrain_RBF','w') pickle.dump(trainer,fileObject) pickle.dump(fnn,fileObject) fileObject.close()
def exec_algo(xml_file, output_location): rootObj = ml.parse(xml_file) #Getting the root element so that we get the subclasses and its members and member function xmlParamDetails = rootObj.MachineLearning.classification #Gather param values from the XML parsed object file = open(xmlParamDetails.datafile) var_inp = xmlParamDetails.input var_out = xmlParamDetails.output classes = xmlParamDetails.classes split = xmlParamDetails.split learningrate = xmlParamDetails.algorithm.MultiLayerPerceptron.learningRate momentum = xmlParamDetails.algorithm.MultiLayerPerceptron.momentum epochs = xmlParamDetails.algorithm.MultiLayerPerceptron.epochs hiddenNeurons = int( xmlParamDetails.algorithm.MultiLayerPerceptron.hiddenLayers) hiddenLayer = xmlParamDetails.algorithm.MultiLayerPerceptron.hiddenLayerActivation outputLayer = xmlParamDetails.algorithm.MultiLayerPerceptron.outputLayerActivation delimiter = xmlParamDetails.delimiter DS = ClassificationDataSet(var_inp, var_out, nb_classes=classes) for line in file.readlines(): data = [float(x) for x in line.strip().split(',') if x != ''] inp = tuple(data[:var_inp]) output = tuple(data[var_inp:]) DS.addSample(inp, output) tstdata, trndata = DS.splitWithProportion(split) trdata = ClassificationDataSet(trndata.indim, var_out, nb_classes=classes) tsdata = ClassificationDataSet(tstdata.indim, var_out, nb_classes=classes) for i in xrange(trndata.getLength()): trdata.addSample(trndata.getSample(i)[0], trndata.getSample(i)[1]) for i in xrange(tstdata.getLength()): tsdata.addSample(tstdata.getSample(i)[0], tstdata.getSample(i)[1]) trdata._convertToOneOfMany() tsdata._convertToOneOfMany() fnn = FeedForwardNetwork() inputLayer = LinearLayer(trdata.indim) if hiddenLayer == 'Sigmoid': hiddenLayer = SigmoidLayer(hiddenNeurons) elif hiddenLayer == 'Softmax': hiddenLayer = SoftmaxLayer(hiddenNeurons) else: hiddenLayer = LinearLayer(hiddenNeurons) if outputLayer == 'Sigmoid': outputLayer = SigmoidLayer(trdata.outdim) elif outputLayer == 'Softmax': outputLayer = SoftmaxLayer(trdata.outdim) else: outputLayer = LinearLayer(trdata.outdim) fnn.addInputModule(inputLayer) fnn.addModule(hiddenLayer) fnn.addOutputModule(outputLayer) in_to_hidden = FullConnection(inputLayer, hiddenLayer) hidden_to_outputLayer = FullConnection(hiddenLayer, outputLayer) fnn.addConnection(in_to_hidden) fnn.addConnection(hidden_to_outputLayer) fnn.sortModules() trainer = BackpropTrainer(fnn, dataset=trdata, verbose=True, learningrate=learningrate, momentum=momentum) trainer.trainEpochs(epochs=epochs) trresult = percentError(trainer.testOnClassData(), trdata['class']) print("Training accuracy : %f " % (100 - trresult)) ts = time.time() directory = output_location + sep + str(int(ts)) makedirs(directory) fileObject = open( output_location + sep + str(int(ts)) + sep + 'pybrain_MLP', 'w') pickle.dump(trainer, fileObject) pickle.dump(fnn, fileObject) fileObject.close()
def exec_algo(xml_file, output_location): rootObj = ml.parse(xml_file) file_name = rootObj.MachineLearning.prediction.datafile file = open(file_name) var_input = rootObj.MachineLearning.prediction.input var_output = rootObj.MachineLearning.prediction.output var_classes = rootObj.MachineLearning.prediction.classes DS = ClassificationDataSet(var_input, var_output, nb_classes=var_classes) #DS1=ClassificationDataSet(13,1,nb_classes=10) for line in file.readlines(): data = [float(x) for x in line.strip().split(',') if x != ''] inp = tuple(data[:var_input]) output = tuple(data[var_input:]) DS.addSample(inp, output) tstdata, trndata = DS.splitWithProportion(0) #trndatatest,tstdatatest=DS1.splitWithProportion(0) trdata = ClassificationDataSet(trndata.indim, 1, nb_classes=10) #tsdata=ClassificationDataSet(DS1.indim,1,nb_classes=10) #tsdata1=ClassificationDataSet(DS1.indim,1,nb_classes=10) for i in xrange(trndata.getLength()): if (trndata.getSample(i)[1][0] != 100): trdata.addSample(trndata.getSample(i)[0], trndata.getSample(i)[1]) trdata._convertToOneOfMany() #tsdata._convertToOneOfMany() #tsdata1._convertToOneOfMany() print "%d" % (trdata.getLength()) rnn = RecurrentNetwork() inputLayer = LinearLayer(trdata.indim) hiddenLayer = rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.hiddenLayerActivation hiddenNeurons = rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.hiddenNeurons if hiddenLayer == 'Sigmoid': hiddenLayer = SigmoidLayer(hiddenNeurons) elif hiddenLayer == 'Softmax': hiddenLayer = SoftmaxLayer(hiddenNeurons) else: hiddenLayer = LinearLayer(hiddenNeurons) outputLayer = rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.outputLayerActivation if outputLayer == 'Sigmoid': outputLayer = SigmoidLayer(trdata.outdim) elif outputLayer == 'Softmax': outputLayer = SoftmaxLayer(trdata.outdim) else: outputLayer = LinearLayer(trdata.outdim) rnn.addInputModule(inputLayer) rnn.addModule(hiddenLayer) rnn.addOutputModule(outputLayer) rnn_type = rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.RNN_Type in_to_hidden = FullConnection(inputLayer, hiddenLayer) hidden_to_outputLayer = FullConnection(hiddenLayer, outputLayer) rnn.addConnection(in_to_hidden) rnn.addConnection(hidden_to_outputLayer) if rnn_type == 'Elman': hidden_to_hidden = FullConnection(hiddenLayer, hiddenLayer, name='c3') rnn.addRecurrentConnection(hidden_to_hidden) #hidden_to_hidden=FullConnection(hiddenLayer,hiddenLayer, name='c3') if rnn_type == 'Jordan': output_to_hidden = FullConnection(outputLayer, hiddenLayer, name='c3') rnn.addRecurrentConnection(output_to_hidden) #rnn.addRecurrentConnection(hidden_to_hidden) momentum = rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.momentum weightdecay = rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.learningRate rnn.sortModules() trainer = BackpropTrainer(rnn, dataset=trdata, momentum=0.1, verbose=True, weightdecay=0.01) trainer.train() result = (percentError(trainer.testOnClassData(dataset=trdata), trdata['class'])) #result1=percentError(trainer.testOnClassData(dataset=tsdata1),tsdata1['class']) print('%f \n') % (100 - result) #print ('%f \n') % (100-result1) ts = time.time() directory = output_location + sep + str(int(ts)) makedirs(directory) fileObject = open( output_location + sep + str(int(ts)) + sep + 'pybrain_RNN', 'w') pickle.dump(trainer, fileObject) pickle.dump(rnn, fileObject) fileObject.close()
def exec_algo(xml_file, output_location): rootObj=ml.parse(xml_file) #Getting the root element so that we get the subclasses and its members and member function xmlParamDetails = rootObj.MachineLearning.classification #Gather param values from the XML parsed object file=open(xmlParamDetails.datafile) var_inp=xmlParamDetails.input var_out=xmlParamDetails.output classes=xmlParamDetails.classes split=xmlParamDetails.split learningrate=xmlParamDetails.algorithm.MultiLayerPerceptron.learningRate momentum=xmlParamDetails.algorithm.MultiLayerPerceptron.momentum epochs=xmlParamDetails.algorithm.MultiLayerPerceptron.epochs hiddenNeurons=int(xmlParamDetails.algorithm.MultiLayerPerceptron.hiddenLayers) hiddenLayer=xmlParamDetails.algorithm.MultiLayerPerceptron.hiddenLayerActivation outputLayer=xmlParamDetails.algorithm.MultiLayerPerceptron.outputLayerActivation delimiter=xmlParamDetails.delimiter DS=ClassificationDataSet(var_inp,var_out,nb_classes=classes) for line in file.readlines(): data=[float(x) for x in line.strip().split(',') if x != ''] inp=tuple(data[:var_inp]) output=tuple(data[var_inp:]) DS.addSample(inp,output) tstdata,trndata=DS.splitWithProportion(split) trdata=ClassificationDataSet(trndata.indim,var_out,nb_classes=classes) tsdata=ClassificationDataSet(tstdata.indim,var_out,nb_classes=classes) for i in xrange(trndata.getLength()): trdata.addSample(trndata.getSample(i)[0],trndata.getSample(i)[1]) for i in xrange(tstdata.getLength()): tsdata.addSample(tstdata.getSample(i)[0],tstdata.getSample(i)[1]) trdata._convertToOneOfMany() tsdata._convertToOneOfMany() fnn=FeedForwardNetwork() inputLayer=LinearLayer(trdata.indim) if hiddenLayer=='Sigmoid': hiddenLayer=SigmoidLayer(hiddenNeurons) elif hiddenLayer=='Softmax': hiddenLayer=SoftmaxLayer(hiddenNeurons) else: hiddenLayer=LinearLayer(hiddenNeurons) if outputLayer=='Sigmoid': outputLayer=SigmoidLayer(trdata.outdim) elif outputLayer=='Softmax': outputLayer=SoftmaxLayer(trdata.outdim) else: outputLayer=LinearLayer(trdata.outdim) fnn.addInputModule(inputLayer) fnn.addModule(hiddenLayer) fnn.addOutputModule(outputLayer) in_to_hidden=FullConnection(inputLayer,hiddenLayer) hidden_to_outputLayer=FullConnection(hiddenLayer,outputLayer) fnn.addConnection(in_to_hidden) fnn.addConnection(hidden_to_outputLayer) fnn.sortModules() trainer=BackpropTrainer(fnn,dataset=trdata, verbose=True, learningrate=learningrate, momentum=momentum) trainer.trainEpochs(epochs=epochs) trresult=percentError(trainer.testOnClassData(),trdata['class']) print("Training accuracy : %f " % (100-trresult)) ts=time.time() directory = output_location + sep + str(int(ts)) makedirs(directory) fileObject=open(output_location + sep + str(int(ts)) + sep + 'pybrain_MLP','w') pickle.dump(trainer,fileObject) pickle.dump(fnn,fileObject) fileObject.close()