Exemplo n.º 1
0
def run(epochs, network_file, file_length, part_length, dominant_frequncies, show_graph, verbose_output):
	start_time = time.time()
	learner = dominant_freqs_learner.DominantFreqsLearner(file_length, part_length ,dominant_frequncies)
	all_files = get_all_split_files()
	if verbose_output:
		print 'started adding files to dataset at ' + time.ctime()

	for f in all_files:
		try:
			learner.add_split_file(f, channel=None, verbose=verbose_output)
		except:
			pass

	dataset_add_time = time.time() - start_time
	if verbose_output:
		print 'finished adding file to dataset at ' + time.ctime()

	errors = []
	learning_start_time = time.time()
	for epoch in range(epochs):
		error = learner.train_single_epoch()
		if verbose_output:
			print '{0}: epoch {1} : {2}'.format(time.ctime(), epoch, error)

		errors.append(error)

	learning_time = time.time() - learning_start_time
	NetworkWriter.writeToFile(learner._net, network_file)
	if show_graph:
		plot_graph(errors)

	return (errors, dataset_add_time, learning_time)
Exemplo n.º 2
0
def main():
    start_time = time.time()
    novice = ArtificialNovice()
    genius = ArtificialGenius()
    game = HangmanGame(genius, novice)

    if __debug__:
        print "------------------- EVALUATION ------------------------"
        network = NetworkReader.readFrom("../IA/network_weight_1000.xml")
        j = 0
        while j < 1:
            game.launch(False, None, network)
            j += 1

        print ("--- %s total seconds ---" % (time.time() - start_time))
    else:
        print "------------------- LEARNING ------------------------"
        network = buildNetwork(3, 4, 1, hiddenclass=SigmoidLayer)
        ds = SupervisedDataSet(3, 1)
        i = 0
        while i < 100:
            game.launch(True, ds)
            i += 1

        print " INITIATE trainer : "
        trainer = BackpropTrainer(network, ds)
        print " START trainer : "
        start_time_trainer = time.time()
        trainer.train()
        print ("---  END trainer in % seconds ---" % (time.time() - start_time_trainer))
        print " START EXPORT network : "
        NetworkWriter.writeToFile(network, "../IA/network_weight_test_learning.xml")
        print " END EXPORT network : "
Exemplo n.º 3
0
def main():
    start_time = time.time()
    novice = ArtificialNovice()
    genius = ArtificialGenius()
    game = HangmanGame(genius, novice)

    if __debug__:
        print "------------------- EVALUATION ------------------------"
        network = NetworkReader.readFrom('../IA/network_weight_1000.xml')
        j = 0
        while j < 1:
            game.launch(False, None, network)
            j += 1

        print("--- %s total seconds ---" % (time.time() - start_time))
    else:
        print "------------------- LEARNING ------------------------"
        network = buildNetwork(3, 4, 1, hiddenclass=SigmoidLayer)
        ds = SupervisedDataSet(3, 1)
        i = 0
        while i < 100:
            game.launch(True, ds)
            i += 1

        print " INITIATE trainer : "
        trainer = BackpropTrainer(network, ds)
        print " START trainer : "
        start_time_trainer = time.time()
        trainer.train()
        print("---  END trainer in % seconds ---" %
              (time.time() - start_time_trainer))
        print " START EXPORT network : "
        NetworkWriter.writeToFile(network,
                                  '../IA/network_weight_test_learning.xml')
        print " END EXPORT network : "
Exemplo n.º 4
0
    def learn(self):
        try:
            self.data_for_training = []
            mem = Memory('http://localhost:9200')
            data = mem.get_data()
            categories = list(data.keys())
            for category in data:
                for req in data[category]:
                    self.data_for_training.append(
                        [req.token, [categories.index(category)]])

            net = buildNetwork(len(self.data_for_training[0][0]),
                               15,
                               len(categories),
                               hiddenclass=SoftmaxLayer,
                               recurrent=False)
            ds = ClassificationDataSet(len(self.data_for_training[0][0]),
                                       nb_classes=len(categories),
                                       class_labels=categories)
            for data in self.data_for_training:
                ds.addSample(data[0], data[1])
            ds._convertToOneOfMany()
            trainer = BackpropTrainer(net,
                                      ds,
                                      momentum=0.1,
                                      learningrate=0.01,
                                      verbose=True)
            trainer.trainUntilConvergence(maxEpochs=500)
            NetworkWriter.writeToFile(net, 'net.xml')
        except Exception as e:
            raise e
        return net
Exemplo n.º 5
0
def run(epochs, network_file, file_length, part_length, dominant_frequncies,
        show_graph, verbose_output):
    start_time = time.time()
    learner = dominant_freqs_learner.DominantFreqsLearner(
        file_length, part_length, dominant_frequncies)
    all_files = get_all_split_files()
    if verbose_output:
        print 'started adding files to dataset at ' + time.ctime()

    for f in all_files:
        try:
            learner.add_split_file(f, channel=None, verbose=verbose_output)
        except:
            pass

    dataset_add_time = time.time() - start_time
    if verbose_output:
        print 'finished adding file to dataset at ' + time.ctime()

    errors = []
    learning_start_time = time.time()
    for epoch in range(epochs):
        error = learner.train_single_epoch()
        if verbose_output:
            print '{0}: epoch {1} : {2}'.format(time.ctime(), epoch, error)

        errors.append(error)

    learning_time = time.time() - learning_start_time
    NetworkWriter.writeToFile(learner._net, network_file)
    if show_graph:
        plot_graph(errors)

    return (errors, dataset_add_time, learning_time)
Exemplo n.º 6
0
def train_nn(trainer,
             structure=(2, ),
             momentum=0.99,
             learningrate=0.001,
             train_time=200):
    load_json = json.load(open(trainer.file_location, "r"))
    training_data = load_json["data"]
    metadata = load_json["metadata"][0]  # 0 because is an array

    struct = (3, ) + structure + (3, )

    print("TRAINING")

    net = buildNetwork(
        *struct, bias=True)  # * converts list tuple into separate parameters

    ds = SupervisedDataSet(3, 3)
    for i in training_data:
        in_pos = trainer.get_bin(i["pos"], metadata["max_pos"],
                                 metadata["num_pos"])
        in_vel = trainer.get_bin(i["vel"], metadata["max_vel"],
                                 metadata["num_vel"])
        in_ang = trainer.get_bin(i["ang"], metadata["max_ang"],
                                 metadata["num_ang"])
        inp = [in_pos, in_vel, in_ang]
        out = [i["out1"], i["out2"], i["out3"]]
        ds.addSample(inp, out)

    train = BackpropTrainer(net,
                            ds,
                            learningrate=learningrate,
                            momentum=momentum)
    train.trainEpochs(train_time)

    NetworkWriter.writeToFile(net, 'nn_trained_networks/trained_nn.xml')
Exemplo n.º 7
0
def main():
    first_layer_size = 11 + 1 + 1 + 150 + 5 + 5 + 5 + 1
    net = buildNetwork(first_layer_size, 200, 1)
    path = os.path.join(os.path.dirname(os.path.dirname(os.getcwd())),
                        'network.xml')
    NetworkWriter.writeToFile(net, path)
    print("Neural Network initialized.")
Exemplo n.º 8
0
    def save_network(self,name_of_the_net):
        print "Saving the trained network to file"

        if self.network is None:
            print "Network has not been trained!!"
        else:
            NetworkWriter.writeToFile(self.network, name_of_the_net)
            print "Saving Finished"
 def train(self):
     print "Training"
     trndata, tstdata = self.ds.splitWithProportion(.1)
     self.trainer.trainUntilConvergence(verbose=True,
                                        trainingData=trndata,
                                        maxEpochs=1000)
     self.trainer.testOnData(tstdata, verbose= True)
     # if raw_input('Save Network?: y/n\n')=='y':
     NetworkWriter.writeToFile(self.net, 'Network1.xml')
     print 'Saving network'
Exemplo n.º 10
0
def main():
    print "Calculating mfcc...."
    mfcc_coeff_vectors_dict = {}
    for i in range(1, 201):
        extractor = FeatureExtractor(
            '/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Happiness/HappinessAudios/' + str(i) + '.wav')
        mfcc_coeff_vectors = extractor.calculate_mfcc()
        mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})

    for i in range(201, 401):
        extractor = FeatureExtractor(
            '/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Sadness/SadnessAudios/' + str(i - 200) + '.wav')
        mfcc_coeff_vectors = extractor.calculate_mfcc()
        mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})

    audio_with_min_frames, min_frames = get_min_frames_audio(
        mfcc_coeff_vectors_dict)
    processed_mfcc_coeff = preprocess_input_vectors(
        mfcc_coeff_vectors_dict, min_frames)
    # frames = min_frames
    # print frames
    # print len(processed_mfcc_coeff['1'])
    # for each_vector in processed_mfcc_coeff['1']:
    #     print len(each_vector)
    print "mffcc found..."
    classes = ["happiness", "sadness"]

    training_data = ClassificationDataSet(
        26, target=1, nb_classes=2, class_labels=classes)
    # training_data = SupervisedDataSet(13, 1)
    try:
        network = NetworkReader.readFrom(
            'network_state_frame_level_new2_no_pp1.xml')
    except:
        for i in range(1, 51):
            mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
            for each_vector in mfcc_coeff_vectors:
                training_data.appendLinked(each_vector, [1])

        for i in range(201, 251):
            mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
            for each_vector in mfcc_coeff_vectors:
                training_data.appendLinked(each_vector, [0])

        training_data._convertToOneOfMany()
        print "prepared training data.."
        print training_data.indim, training_data.outdim
        network = buildNetwork(
            training_data.indim, 5, training_data.outdim, fast=True)
        trainer = BackpropTrainer(network, learningrate=0.01, momentum=0.99)
        print "Before training...", trainer.testOnData(training_data)
        trainer.trainOnDataset(training_data, 1000)
        print "After training...", trainer.testOnData(training_data)
        NetworkWriter.writeToFile(
            network, "network_state_frame_level_new2_no_pp.xml")
Exemplo n.º 11
0
 def save_network(self,name_of_the_net):
     print "Saving the trained network to file"
     
     if self.network is None:
         print "Network has not been trained!!"
     else:
         NetworkWriter.writeToFile(self.network, name_of_the_net)
         fileName = name_of_the_net.replace('.xml','')
         fileName = fileName+'_testIndex.txt'
         np.savetxt(fileName,self.tstIndex)
         print "Saving Finished"
Exemplo n.º 12
0
def neuralNet(info, test_data):
    ann = FeedForwardNetwork()
    
    ''' 
        Initiate the input nodes, hidden layer nodes,
        and the output layer nodes.
    '''
    inputLayer = LinearLayer(5)
    hiddenLayer = SigmoidLayer(20) 
    outputLayer = LinearLayer(1)
    
    '''
        Add the nodes to the corresponding layer
    '''
    ann.addInputModule(inputLayer)
    ann.addModule(hiddenLayer)
    ann.addOutputModule(outputLayer)
    
    '''
        Connect the input layer to hidden layer,
        then connect hidden layer to output layer
    '''
    in_to_hidden = FullConnection(inputLayer, hiddenLayer)
    hidden_to_out = FullConnection(hiddenLayer, outputLayer)
    
    ann.addConnection(in_to_hidden)
    ann.addConnection(hidden_to_out)
    
    ann.sortModules ()
    
    data_set = SupervisedDataSet(5, 1)
    for data in info:
        data_set.addSample(data[:-1], data[-1])
    trainer = BackpropTrainer(ann, data_set, verbose=False)
    
    #test_data, train_data = data_set.splitWithProportion(0.2)
    train_data = data_set
    test_data = test_data
    '''
        Using 50 epochs for testing purposes, it will train
        the network until convergence within the first 50 epochs
    
    '''
    train = trainer.trainUntilConvergence(dataset=train_data, maxEpochs=10)
    NetworkWriter.writeToFile(ann, 'filename5.xml')
    
    for d in test_data:
        out = ann.activate(d)
        #print (train)
        print (out) 
        
    '''
Exemplo n.º 13
0
def nn_train(pvar,ovar,code,date1,date2,niter,np,nh):
    
    
    print "Doing Networ "+pvar+" "+ovar+" "+date1+" "+date2+" "+str(iter)
    

    # -----------------------    add samples   ------------------
    # get the training data
    print "adding training data "+pvar+" "
    file="eod_main.db"
    sqldir = os.path.join(datadir,"sql")
    sqlfile = os.path.join(sqldir,file)

    conn,cur=open_sql(sqlfile)
    d = rd_sql(cur,code,'AND date > "'+date1+'" AND date < "'+date2+'"')
    print "Read no of days "+str(len(d))

    if pvar == "basic8":
        print "calling nn_pp_basic8"
        pp = nn_pp_basic8(d,3,3)
    oclose,ohigh,olow,oclose_disc,o3day=nn_po_basic(d,3,3)
    if ovar == "close":
        po=oclose
    if ovar == "high":
        po=ohigh
    if ovar == "3day":
        po=o3day

    
    ds = SupervisedDataSet(np,1)
    for i in range(0,len(po)):
        ds.addSample(pp[i],po[i])

    # -----------------------    Build and Train   ------------------
    print "Training Network"
    net = buildNetwork(np,nh,1,hiddenclass=TanhLayer)
    trainer = BackpropTrainer(net,ds)
    xxx = trainer.trainUntilConvergence(maxEpochs=niter,validationProportion=0.01)
    #for n in range(0,niter):
    #    xxx=trainer.train(validationProportion=0.0)
    #    if n % 100 ==0:
    #        print "{} : {}".format(n,xxx)

        
    # --------------     Save network parameters   ------------------
    print "Saving Network"
    netdir2 = os.path.join(basedir,"inv")
    netdir = os.path.join(netdir2,"analyse")
    netfile = os.path.join(netdir,'net_'+pvar+'_'+ovar+'_'+date1+'_'+date1+'_'+str(niter)+'.xml')
    NetworkWriter.writeToFile(net, netfile)
    
    return
Exemplo n.º 14
0
def main():
    print "Calculating mfcc...."
    mfcc_coeff_vectors_dict = {}
    for i in range(1, 201):
        extractor = FeatureExtractor('/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Happiness/HappinessAudios/' + str(i) + '.wav')
        mfcc_coeff_vectors = extractor.calculate_mfcc()
        mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})

    for i in range(201, 401):
        extractor = FeatureExtractor('/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Sadness/SadnessAudios/' + str(i - 200) + '.wav')
        mfcc_coeff_vectors = extractor.calculate_mfcc()
        mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})

    audio_with_min_frames, min_frames = get_min_frames_audio(mfcc_coeff_vectors_dict)
    processed_mfcc_coeff = preprocess_input_vectors(mfcc_coeff_vectors_dict, min_frames)
    frames = min_frames
    print "mfcc found...."
    classes = ["happiness", "sadness"]
    try:
        network = NetworkReader.readFrom('network_state_new_.xml')
    except:
        # Create new network and start Training
        training_data = ClassificationDataSet(frames * 26, target=1, nb_classes=2, class_labels=classes)
        # training_data = SupervisedDataSet(frames * 39, 1)
        for i in range(1, 151):
            mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
            training_data.appendLinked(mfcc_coeff_vectors.ravel(), [1])
            # training_data.addSample(mfcc_coeff_vectors.ravel(), [1])

        for i in range(201, 351):
            mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
            training_data.appendLinked(mfcc_coeff_vectors.ravel(), [0])
            # training_data.addSample(mfcc_coeff_vectors.ravel(), [0])

        training_data._convertToOneOfMany()
        network = buildNetwork(training_data.indim, 5, training_data.outdim)
        trainer = BackpropTrainer(network, learningrate=0.01, momentum=0.99)
        print "Before training...", trainer.testOnData(training_data)
        trainer.trainOnDataset(training_data, 1000)
        print "After training...", trainer.testOnData(training_data)
        NetworkWriter.writeToFile(network, "network_state_new_.xml")

    print "*" * 30 , "Happiness Detection", "*" * 30
    for i in range(151, 201):
        output = network.activate(processed_mfcc_coeff[str(i)].ravel())
        # print output,
        # if output > 0.7:
        #     print "happiness"
        class_index = max(xrange(len(output)), key=output.__getitem__)
        class_name = classes[class_index]
        print class_name
Exemplo n.º 15
0
    def dump(self, dirPath):
        """
        Save a representation of this classifier and it's network at the given path.
        """
        if os.path.isdir(dirPath) and os.listdir(dirPath):
            raise IOError("The directory exists and is not empty: {}".format(dirPath))
        util.mkdir_p(dirPath)

        #save network
        NetworkWriter.writeToFile(self.net, os.path.join(dirPath, self._NET_NAME))

        #save classifier
        with open(os.path.join(dirPath, self._CLASSIFIER_NAME), 'w') as f:
            f.write(serializer.dump(self))
Exemplo n.º 16
0
 def save(self, filename):
     tmpfile = filename + '~net~'
     NetworkWriter.writeToFile(self.net, tmpfile)
     with open(tmpfile, 'rb') as f:
         network_data = f.read()
     os.unlink(tmpfile)
     with open(filename + '~', 'wb') as f:
         out = pickle.Pickler(f)
         out.dump( (const.PWINDOW, self.window) )
         out.dump( (const.PSIZE, self.size) )
         out.dump( (const.PRATIO, self.ratio) )
         out.dump( (const.PMULTIPLIER, self.multiplier) )
         out.dump( (const.PNETWORK, network_data) )
         f.flush()
     os.rename(filename + '~', filename)
Exemplo n.º 17
0
    def __init__(self, num, traindata, inputdata, hiddenNN, type='Tanh', maxepochs=2000, ifprint=False, toload = False,
                 lRate = 0.0001, moment = 0.005):

        self.dataSet = self.createDataSet(traindata[0], traindata[1]) #trainIn trainOut
        if (toload==True):
            self.net = NetworkReader.readFrom('nets/newnettemplate{}.xml'.format(num))
            print(self.net)
        else:
            self.net = self.createNet(inputdata.shape[1], hiddenNN, type, ifprint=True)
            self.trainer = self.trainTrainer(maxepochs, lRate, moment, ifprint)
            NetworkWriter.writeToFile(self.net, 'nets/newnettemplatetest{}.xml'.format(num))

        self.predictedVals = None
        self.predictedX = None
        self.resultTrainVals = None
Exemplo n.º 18
0
def train_bp():
    global net
    net = buildNetwork(30, 2, 1, bias=True, hiddenclass=TanhLayer)
    ds = SupervisedDataSet(30, 1)
    read_bp_data(ds, "../collect_data/data/data.csv")
    read_bp_data(ds, "../collect_data/data/white_data.csv")
    read_bp_data(ds, "../collect_data/data/black_data.csv")
    print len(ds)
    #for inpt, target in ds:
    #print inpt, target
    trainer = BackpropTrainer(net, ds)
    trainer.trainUntilConvergence(maxEpochs=10000)
    NetworkWriter.writeToFile(
        net, os.path.join(curdir, '../collect_data/data/bpstudy.xml'))
    #test
    '''
Exemplo n.º 19
0
def xmlInvariance(n, forwardpasses=1):
    """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare
    if the result looks the same (compare string representation, and forward processing
    of some random inputs) """
    # We only use this for file creation.
    tmpfile = tempfile.NamedTemporaryFile(dir='.')
    f = tmpfile.name
    tmpfile.close()

    NetworkWriter.writeToFile(n, f)
    tmpnet = NetworkReader.readFrom(f)
    NetworkWriter.writeToFile(tmpnet, f)
    endnet = NetworkReader.readFrom(f)

    # Unlink temporary file.
    os.unlink(f)

    netCompare(tmpnet, endnet, forwardpasses, True)
def neural_train(filename, testfile, output):
    tag, data = readfile(filename)
    testtag, testdata = readfile(testfile)
    net = buildNetwork(len(data[0]), 80, 10)
    ds = SupervisedDataSet(len(data[0]), 10)
    for x in range(0, len(data)):
        ds.addSample(data[x], trans(tag[x]))
    testds = SupervisedDataSet(len(data[0]), 10)
    for x in range(0, len(testdata)):
        testds.addSample(testdata[x], trans(testtag[x]))
    trainer = BackpropTrainer(net, ds, learningrate = 0.001, momentum = 0.99)
    print "training..."
    trainer.trainUntilConvergence(verbose=True,
                              trainingData=ds,
                              validationData=testds,
                              maxEpochs=500)
    print "done"
    NetworkWriter.writeToFile(net, output)
Exemplo n.º 21
0
def xmlInvariance(n, forwardpasses = 1):
    """ try writing a network to an xml file, reading it, rewrite it, reread it, and compare
    if the result looks the same (compare string representation, and forward processing
    of some random inputs) """
    # We only use this for file creation.
    tmpfile = tempfile.NamedTemporaryFile(dir='.')
    f = tmpfile.name
    tmpfile.close()

    NetworkWriter.writeToFile(n, f)
    tmpnet = NetworkReader.readFrom(f)
    NetworkWriter.writeToFile(tmpnet, f)
    endnet = NetworkReader.readFrom(f)

    # Unlink temporary file.
    os.unlink(f)

    netCompare(tmpnet, endnet, forwardpasses, True)
Exemplo n.º 22
0
    def train(self, dataSet):
        """
        Builds a network and trains it.
        """
        if os.stat(self.predictor_path).st_size != 0:
            self.network = NetworkReader.readFrom(self.predictor_path)
        else:
            self.network = buildNetwork(dataSet.indim, 4, dataSet.outdim,recurrent=True)

        t = None


        if len(dataSet) > 0:
            t = BackpropTrainer(self.network, dataSet, learningrate = self.learningrate, momentum = self.momentum, verbose = False)
            for epoch in range(0, self.epochs):
                t.train()

        NetworkWriter.writeToFile(self.network, self.predictor_path)

        return t
Exemplo n.º 23
0
def main():

	start_time = time.time()

	dataModel = [
	    [(0,0,0), (1,0,0,0,0,0,0,0)],
	    [(0,0,1), (0,1,0,0,0,0,0,0)],
	    [(0,1,0), (0,0,1,0,0,0,0,0)],
	    [(0,1,1), (0,0,0,1,0,0,0,0)],
	    [(1,0,0), (0,0,0,0,1,0,0,0)],
	    [(1,0,1), (0,0,0,0,0,1,0,0)],
	    [(1,1,0), (0,0,0,0,0,0,1,0)],
	    [(1,1,1), (0,0,0,0,0,0,0,1)],
	]

	ds = SupervisedDataSet(3, 8)
	 
	for input, target in dataModel:
	    ds.addSample(input, target)

	# create a large random data set
	random.seed()
	trainingSet = SupervisedDataSet(3, 8);
	for ri in range(0,2000):
	    input,target = dataModel[random.getrandbits(3)];
	    trainingSet.addSample(input, target)

	net = buildNetwork(3, 8, 8, bias=True)

	trainer = BackpropTrainer(net, ds, learningrate = 0.001)
	for i in range(10):

		trainer.trainUntilConvergence(verbose=True,
		                              trainingData=trainingSet,
		                              validationData=ds,
		                              maxEpochs=1)

		NetworkWriter.writeToFile(net, 'savedNeuralNets/trainedNet'+str(i)+'.xml')

	print("The Program took %s seconds to run" % (time.time() - start_time))
Exemplo n.º 24
0
def trainNetwork(dirname):
    numFeatures = 5000
    ds = SequentialDataSet(numFeatures, 1)
    
    tracks = glob.glob(os.path.join(dirname, 'train??.wav'))
    for t in tracks:
        track = os.path.splitext(t)[0]
        # load training data
        print "Reading %s..." % track
        data = numpy.genfromtxt(track + '_seg.csv', delimiter=",")
        labels = numpy.genfromtxt(track + 'REF.txt', delimiter='\t')[0::10,1]
        numData = data.shape[0]

        # add the input to the dataset
        print "Adding to dataset..."
        ds.newSequence()
        for i in range(numData):
            ds.addSample(data[i], (labels[i],))
    
    # initialize the neural network
    print "Initializing neural network..."
    net = buildNetwork(numFeatures, 50, 1,
                       hiddenclass=LSTMLayer, outputbias=False, recurrent=True)
    
    # train the network on the dataset
    print "Training neural net"
    trainer = RPropMinusTrainer(net, dataset=ds)
##    trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1)
    error = -1
    for i in range(100):
        new_error = trainer.train()
        print "error: " + str(new_error)
        if abs(error - new_error) < 0.1: break
        error = new_error

    # save the network
    print "Saving neural network..."
    NetworkWriter.writeToFile(net, os.path.basename(dirname) + 'net')
Exemplo n.º 25
0
def main():

	start_time = time.time()

	dataModel = createTheDataModel([2,5,9,15])

	trainingSet = SupervisedDataSet(228, 1)
	for input, target in dataModel:
	    trainingSet.addSample(input, target)


	net = buildNetwork(228, 220, 1, bias=True)

	numberOfEpochsToTrainFor = 2
	for epochNumber in range(1, 3):
		trainer = BackpropTrainer(net, trainingSet)
		trainer.trainEpochs(2)

		NetworkWriter.writeToFile(net, 'savedNeuralNets/trainedNet1-epoch' + str(epochNumber * numberOfEpochsToTrainFor) + '.xml')


	seconds = str(int(time.time() - start_time))
	print("The Program took %s seconds to run" % (seconds))
Exemplo n.º 26
0
def entrenarO(red):
    #Se inicializa el dataset
    ds = SupervisedDataSet(4096,1)

    """Se crea el dataset, para ello procesamos cada una de las imagenes obteniendo las figuras,
       luego se le asignan los valores deseados del resultado la red neuronal."""

    print "O  - Figura"
    for i,c in enumerate(os.listdir(os.path.dirname('C:\\Users\\LuisD\\Desktop\\Reconocimiento\\prueba/'))):
        try:
            im = cv2.imread('C:\\Users\\LuisD\\Desktop\\Reconocimiento\\prueba/'+c)
            cv2.resize(im,(64,64))
            pim = pi.ProcesarImagen(im)
            ds.appendLinked(pim.flatten(),10)
        except:
            pass

    print len(ds)
    print i,c

    trainer = BackpropTrainer(red, ds)
    print "Entrenando hasta converger"
    trainer.trainUntilConvergence()
    NetworkWriter.writeToFile(red, 'rna_o.xml')
Exemplo n.º 27
0
theNet.addOutputModule(outLayer)

theNet.sortModules()


hidden_to_out = pybrain.structure.FullConnection(hiddenLayer2,outLayer, name = 'hidden to output')

theNet.addConnection(hidden_to_out)

theNet.sortModules()
inp = data['input']
oup = data['target']
print(inp[2,:])
print(oup[2,:])



testData, trainData = data.splitWithProportion(0.2)
trainer = BackpropTrainer(theNet, trainData, verbose=True)
for i in range(2500):
    print ("epoch " + str(i))
    trainer.trainEpochs(1)
    if i%20 == 0:
        NetworkWriter.writeToFile(theNet,"/Users/arcadigonzalez/PycharmProjects/anncomp_pro/ANN_Backups/"+str(i))
        NetworkWriter.writeToFile(theNet,"/Users/arcadigonzalez/Google Drive/NN_BU/NN1/"+str(i))
        print("resting processor, writing file")
        time.sleep(122)



Exemplo n.º 28
0
 def saveNetwork(self, fname):
     """ save the trained network to a file """
     NetworkWriter.writeToFile(self.Trainer.module, fname)
     logging.info("Network saved to: " + fname)
Exemplo n.º 29
0
 def write_to_file(self, file_name):
     if self.net is not None:
         NetworkWriter.writeToFile(self.net, file_name)
Exemplo n.º 30
0
def main():
    global player1
    global player2
    
    global AI_1_ID
    global DECK_1_ID
    global AI_2_ID
    global DECK_2_ID
    global NUM_GAMES
    global CLEAR_RESULTS
    global path_to_result
    global neural_net
    
    ai_1_id = AI_1_ID
    deck_1_id = DECK_1_ID
    ai_2_id = AI_2_ID
    deck_2_id = DECK_2_ID
    numgames = NUM_GAMES
    clear_results = CLEAR_RESULTS
    
    if (len(sys.argv) > 5):
        ''' Number of games to be simulated '''
        ai_1_id = sys.argv[1]
        deck_1_id = sys.argv[2]
        ai_2_id = sys.argv[3]
        deck_2_id = sys.argv[4]
        numgames = sys.argv[5]
        
    if (len(sys.argv) > 6):
        sixth_arg = sys.argv[6]
        if (sixth_arg == "T"):
            clear_results = True
        elif (sixth_arg == "O"):
            clear_results = True
            path_to_result = os.path.join(os.path.dirname(os.getcwd()),'game_results',ai_1_id + '(' + deck_1_id
                                          + ')_' + ai_2_id + '(' + deck_2_id + ').csv')
        else:
            clear_results = True
            path_to_result = os.path.join(os.path.dirname(os.getcwd()),'game_results',sixth_arg)

    if (len(sys.argv) <= 5):
        print("!!! WARNING: Wrong number of arguments. Default values were used. !!!")

    if not numgames.isdigit():
            sys.stderr.write("Usage: %s [NUMGAMES]\n" % (sys.argv[0]))
            exit(1)
    for i in range(int(numgames)):
            if (i == 0):
                test_full_game(ai_1_id,deck_1_id,ai_2_id,deck_2_id, clear_results)
            else:
                test_full_game(ai_1_id,deck_1_id,ai_2_id,deck_2_id, False)
    
    os.path.join(os.getcwd())
    
    if (player1.__class__ is Q_learner):
        net = player1.neural_network
        path = os.path.join(os.path.dirname(os.getcwd()), 'network.xml')
        NetworkWriter.writeToFile(net, path)
        neural_net = net

    if (player2.__class__ is Q_learner):
        net = player2.neural_network
        path = os.path.join(os.path.dirname(os.getcwd()), 'network.xml')
        NetworkWriter.writeToFile(net, path)
        neural_net = net
Exemplo n.º 31
0
def save_arguments(net,filename):
    NetworkWriter.writeToFile(net, filename)
    print 'Arguments save to file net.csv'
Exemplo n.º 32
0
        dataSet.addSample(feature[0], feature[1])

    Network = buildNetwork(dataSet.indim,
                           InputLayerSize * 2,
                           InputLayerSize * 2,
                           1,
                           hiddenclass=SigmoidLayer,
                           outclass=SigmoidLayer,
                           bias=True)
    trainer = BackpropTrainer(Network,
                              dataSet,
                              learningrate=0.001,
                              momentum=0.001)

    trainer.trainUntilConvergence()

    rightAnswers = 0
    for feature in features:
        outputs = Network.activate(feature[0])

        maxValue = -1
        maxValueIndex = -1
        if outputs[0] > 0.5 and feature[1] == 1:
            rightAnswers += 1

        print(outputs[0], feature[1], outputs[0] > 0.5 and feature[1] == 1)

    print('Right answers: ', rightAnswers / len(features) * 100, "%")
    Network._name = 'SupervisedNetwork'
    NetworkWriter.writeToFile(Network, "BestSupervised")
Exemplo n.º 33
0
t = BackpropTrainer(n, learningrate = 0.01 ,
                    momentum = mom)
#train the neural network from the train DataSet

cterrori=1.0
print "trainer momentum:"+str(mom)
for iter in range(25):
  t.trainOnDataset(trndata, 1000)
  ctrndata = mv.calculateModuleOutput(n,trndata)
  cterr = v.MSE(ctrndata,trndata['target'])
  relerr = abs(cterr-cterrori)
  cterrori = cterr
  print 'iteration:',iter+1,'MSE error:',cterr
  myplot(trndata,ctrndata,iter=iter+1)
  if cterr < 1.e-5 or relerr < 1.e-7:
    break
#write the network using xml file     
myneuralnet = os.path.join(os.getcwd(),'myneuralnet.xml')
if os.path.isfile(myneuralnet):
    NetworkWriter.appendToFile(n,myneuralnet)
else:
    NetworkWriter.writeToFile(n,myneuralnet)
    
#calculate the test DataSet based on the trained Neural Network
ctsts = mv.calculateModuleOutput(n,tsts)
tserr = v.MSE(ctsts,tsts['target'])
print 'MSE error on TSTS:',tserr
myplot(trndata,ctrndata,tsts,ctsts)

pylab.show()
Exemplo n.º 34
0
trainer = BackpropTrainer(nn, dataset=alldata, learningrate=0.01, momentum=0.1, verbose=False, weightdecay=0.002,
                          batchlearning=False)

numberOfEpoch = 60

x_errors = np.zeros((1, numberOfEpoch), dtype=int)
errors = np.zeros((2, numberOfEpoch), dtype=float)

for i in range(numberOfEpoch):
    trainer.trainEpochs(1)
    trnresult = percentError(trainer.testOnClassData(), alldata['class'])
    tstresult = percentError(trainer.testOnClassData(dataset=alldatatest), alldatatest['class'])

    x_errors[0, i] = trainer.totalepochs
    errors[0, i] = trnresult
    errors[1, i] = tstresult
    current = time.time() - start
    print("epoch: %4d" % trainer.totalepochs, \
          "  train error: %5.2f%%" % trnresult, \
          "  test error: %5.2f%%" % tstresult,\
          " time : %5.1f" % current)

end = time.time()
print("Time elapsed :" + str(end - start))

plt.plot(x_errors[0,:],errors[0,:],'g',x_errors[0,:],errors[1,:],'b')
plt.legend(["train error","test error"])
plt.show()

NetworkWriter.writeToFile(nn, 'base.xml')
Exemplo n.º 35
0
treinamentoRedeNeural(NETWORK, DATASET,
                      (CAMINHO_RESULTADO + "TREINAMENTO.csv"))

# VALIDACAO
audio = carregarAudios(
    "caminho da pasta com os audios de validacao do falante 1/", 1)

audioAposFFT = aplicarFFTnasAmostras(audio)

arrayComOsSegundosDoAudio = separacaoEmSegundosDoAudio(audioAposFFT)

validacaoRedeNeural(NETWORK, arrayComOsSegundosDoAudio, "1.ogg",
                    [10, 10, 10, 10, 10],
                    (CAMINHO_RESULTADO + "VALIDACAO.csv"))

NetworkWriter.writeToFile(NETWORK, (CAMINHO_RESULTADO + "network.xml"))

# SESSAO 2 => TESTE

# # CRIANDO OS DIRETORIOS
# os.mkdir("resultado/" + VERSAO)
#
# # VALIDACAO
# audio = carregarAudios("caminho da pasta com os audios de validacao do falante x/", 1)
#
# audioAposFFT = aplicarFFTnasAmostras(audio)
#
# arrayComOsSegundosDoAudio = separacaoEmSegundosDoAudio(audioAposFFT)
#
# validacaoRedeNeural(NETWORK, arrayComOsSegundosDoAudio, "1.ogg", [10, 10, 10, 10, 10],
#                     (CAMINHO_RESULTADO + "VALIDACAO.csv"))
Exemplo n.º 36
0
t = BackpropTrainer(n, learningrate=0.01, momentum=mom)
#train the neural network from the train DataSet

cterrori = 1.0
print("trainer momentum:" + str(mom))
for iter in range(25):
    t.trainOnDataset(trndata, 1000)
    ctrndata = mv.calculateModuleOutput(n, trndata)
    cterr = v.MSE(ctrndata, trndata['target'])
    relerr = abs(cterr - cterrori)
    cterrori = cterr
    print('iteration:', iter + 1, 'MSE error:', cterr)
    myplot(trndata, ctrndata, iter=iter + 1)
    if cterr < 1.e-5 or relerr < 1.e-7:
        break
#write the network using xml file
myneuralnet = os.path.join(os.getcwd(), 'myneuralnet.xml')
if os.path.isfile(myneuralnet):
    NetworkWriter.appendToFile(n, myneuralnet)
else:
    NetworkWriter.writeToFile(n, myneuralnet)

#calculate the test DataSet based on the trained Neural Network
ctsts = mv.calculateModuleOutput(n, tsts)
tserr = v.MSE(ctsts, tsts['target'])
print('MSE error on TSTS:', tserr)
myplot(trndata, ctrndata, tsts, ctsts)

pylab.show()
Exemplo n.º 37
0
# split up training data for cross validation
print "Split data into training and test sets..."

net = buildNetwork(200, 134, 2, bias=True, outclass=SoftmaxLayer)
trainer = BackpropTrainer(net, dataset=trndata)
print "training for {} epochs..."

trainer.trainUntilConvergence( verbose = True, validationProportion = 0.15, maxEpochs = 1000, continueEpochs = 10 )



trnresult = percentError( trainer.testOnClassData(),trndata['class'] )
tstresult = percentError( trainer.testOnClassData(dataset=tstdata ), tstdata['class'] )

print "epoch: %4d" % trainer.totalepochs,"  train error: %5.2f%%" % trnresult,           "  test error: %5.2f%%" % tstresult
NetworkWriter.writeToFile(net, 'oliv-x2-80.xml')


# predict using test data
# print "Making predictions..."
# ypreds = []
# ytrues = []
# for i in range(Xtest.getLength()]):
    # pred = fnn.activate(getSample(i)[0])
    # ypreds.append(pred.argmax())
    # ytrues.append(ytest[i])
# print "Accuracy on test set: %7.4f" % accuracy_score(ytrues, ypreds, 
                                                     # normalize=True)


	
    training_input,input_len = build_training_input(pca_dict, synergy_dict)
    # input_len = training_input[list(training_input.keys())[0]]['INPUT']
    target_len = 1
    ds = SupervisedDataSet(input_len, target_len)
    for t1 in training_input:
        for t2 in training_input[t1]:
            print("Input Vector", training_input[t1][t2]['INPUT'], training_input[t1][t2]['OUTPUT'])
            ds.addSample(training_input[t1][t2]['INPUT'], training_input[t1][t2]['OUTPUT'])


    n = buildNetwork(ds.indim, 3, ds.outdim, bias=True)
    t = BackpropTrainer(n, learningrate=0.001, momentum=0.05, verbose=True)
    print("Training")
    t.trainUntilConvergence(ds,
                            verbose=True)
    NetworkWriter.writeToFile(n, 'trainedNetwork.xml')

    # n = NetworkReader.readFrom('trainedNetwork_2.xml')

    predictions = {}
    for d1 in pca_dict:
        if not predictions.get(d1, None):
            predictions[d1]={}
        for d2 in pca_dict:
            predictions[d1][d2] = n.activate(pca_dict[d1] + pca_dict[d2])[0]

    with open('predictions_4.json', 'w') as outfile:
        json.dump(predictions, outfile)


Exemplo n.º 39
0
 def saveNetwork(self, filename):
     NetworkWriter.writeToFile(self.network, filename)
Exemplo n.º 40
0
#!/usr/bin/env python3.7
import dict
import json
import numpy as np
import random
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.customxml import NetworkWriter
from pybrain.structure import TanhLayer, SoftmaxLayer, LinearLayer

net = buildNetwork(len(dict.train_x[0]),
                   5,
                   len(dict.train_y[0]),
                   hiddenclass=LinearLayer,
                   recurrent=False)
ds = SupervisedDataSet(len(dict.train_x[0]), len(dict.train_y[0]))
i = 0
for inp in dict.train_x:
    ds.addSample(inp, dict.train_y[i])
    i += 1

trainer = BackpropTrainer(net, ds, learningrate=0.05)
trainer.trainEpochs(500)

NetworkWriter.writeToFile(net, 'net.xml')
Exemplo n.º 41
0
 def export_network(self, filename):
     NetworkWriter.writeToFile(self.net_shared, filename)
Exemplo n.º 42
0
            vals.append(float(n.activate(x)))

        error = 0.0
        num = 0.0;
        for o, t in zip(vals, prediction_outputs):
            if abs(t - o) < 10:
                error += abs(t - o)
                num = num + 1

        error = error / num

        if error < local_min_error:
            local_min_error = error

        if error < min_error and num >= 16:
            NetworkWriter.writeToFile(n, "20 prediction games with num = 16.xml")
            min_error = error
            num_n = num
            min_vals = []
            for x in vals:
                x = float(x)
                min_vals.append(x)

        print("\n")
        for x in vals:
            print x
        print("\n")
        print(min_error)
        print(num_n)
        print("\n")
        for x in min_vals:
Exemplo n.º 43
0
#build a network
net = buildNetwork(inputdim, noofhiddenunits, outputdim, bias=True, outclass=SoftmaxLayer)
#defing a backpropagator
trainer = BackpropTrainer(net, dataset=traindata)
print "training for {} epochs..."

trainerror,valerror=trainer.trainUntilConvergence( verbose = True, validationProportion = 0.15, maxEpochs = 1000, continueEpochs = 10 )

# plt.plot(trainerror,'b',valerror,'r')
# plt.show()

# trainer.trainOnDataset(traindata,500)



trnresult = percentError( trainer.testOnClassData(),traindata['class'] )
tstresult = percentError( trainer.testOnClassData(dataset=testdata ), testdata['class'] )
# valiresult = percentError( trainer.testOnClassData(dataset=validata ), validata['class'] )
 
print "epoch: %4d" % trainer.totalepochs,"  train error: %5.2f%%" % trnresult,           "  test error: %5.2f%%" % tstresult
# ,    "valid error:%5.2ff%" %valiresult


# if  os.path.isfile('clean-oliv.xml'): 
 # fnn = NetworkReader.readFrom('clean-oliv.xml') 
# else:
 # fnn = buildNetwork( traindata.indim, 64 , traindata.outdim, outclass=SoftmaxLayer )
 # NetworkWriter.writeToFile(net, 'clean-oliv.xml')
NetworkWriter.writeToFile(net, 'clean-oliv-136.xml')
Exemplo n.º 44
0
    cut_score = 0.0001
else:
    cut_score = 0.001

if (args.c == "True"):
    while (i < 1000 and score > cut_score):
        score = trainer.train()
        i = i + 1
        #print "Score: ", score, " loop: ", i
        print >> FH, "Score: ", score, " loop: ", i
        FH.flush()
    mse = score  # Store last MSE value as network MSE
    mse = round(mse, 4)
    netfilename = netname + ".mse_" + str(mse) + ".local.net"

    NetworkWriter.writeToFile(trainer.module,
                              netfilename)  # Save network as XML file

#--------------------- TEST NETWORK on testset ---------------------------
net2 = NetworkReader.readFrom(netfilename)

rp, rn, tp09, fp09, fn09, tn09 = 0, 0, 0, 0, 0, 0
rp, rn, tp09, fp09, fn09, tn09, target, predicted = testNetwork(testdata)

FHper = open(netfilename + ".performance", "a")

print >> FHper, "# Test data:%s" % t_test

for x in xrange(len(target)):
    print >> FHper, target[x], "\t", predicted[x]

# Measures
#add sample data set
for i in range(len(traininglist)):
    DS.addSample(traininglist[i], traininglabels[i])

X = DS['input']
Y = DS['target']
dataTrain, dataTest = DS.splitWithProportion(0.8)
xTrain, yTrain = dataTrain['input'], dataTrain['target']
xTest, yTest = dataTest['input'], dataTest['target']

#step3
# trainner use BP algorithm
verbose = True
trainer = BackpropTrainer(fnn,
                          dataTrain,
                          verbose=True,
                          learningrate=0.5,
                          lrdecay=0.5,
                          momentum=0)
trainer.trainUntilConvergence(DS, maxEpochs=10)

NetworkWriter.writeToFile(fnn, 'networkClassifier.txt')

print("#############")
out = fnn.activateOnDataset(DS)
print(out)
# u can give an input, and use activate function to see the result.
#fnn.activate(input)

# Reference: http://pybrain.org/docs/tutorial/netmodcon.html#examining-a-network; https://www.zengmingxia.com/use-pybrain-to-fit-neural-networks/
Exemplo n.º 46
0
ds = SupervisedDataSet( 28 ** 2, 10)

for row in data_training:
    ds.addSample(row[1], row[2])

net = buildNetwork(28 ** 2, 35, 10, bias=True, hiddenclass=TanhLayer)

trainer = BackpropTrainer(net, ds)

def outputsToNumber(outputs):
    max = outputs[0]
    value = 0
    for x in range(0, 10):
        if outputs[x] > max:
            max = outputs[x]
            value = x
    return value

print "Beginning a training pass..."
for _ in range(0, 5):
    print trainer.train()

for thing in data_training[0:20]:
    print "expected label: ", thing[0], "nnet label: ", outputsToNumber(net.activate(thing[1]))

fileObject = open('trainedNetwork', 'w')
cPickle.dump(net, fileObject)
fileObject.close()

NetworkWriter.writeToFile(net, 'nnet.xml')
Exemplo n.º 47
0
    exif_data
    return data


dataTraining = getDataImage('image/retangulo.png')
size = largura*altura*3

network = buildNetwork(size, 120, 120, 1)
dataSet = SupervisedDataSet(size, 1)

dataSet.addSample(getDataImage('image/retangulo.png'), (1))
dataSet.addSample(getDataImage('image/retangulo2.png'), (1))
dataSet.addSample(getDataImage('image/retangulo3.png'), (1))
dataSet.addSample(getDataImage('image/retangulo4.png'), (1))
dataSet.addSample(getDataImage('image/retangulo5.png'), (1))
dataSet.addSample(getDataImage('image/retangulo6.png'), (1))
dataSet.addSample(getDataImage('image/retangulo7.png'), (1))
dataSet.addSample(getDataImage('image/retangulo8.png'), (1))

trainer = BackpropTrainer(network, dataSet)
error = 1
iteration = 0
output = []
while error > 0.001:
    error = trainer.train()
    output.append(error)
    iteration += 1
    print(iteration, error)

NetworkWriter.writeToFile(network, "dataBase-Ai.xml")
Exemplo n.º 48
0
 def saveresult(self, destname=None):
     if destname == None:
         destname = self.destname
     NetworkWriter.writeToFile(self.__fnn, destname + '.xml')
     joblib.dump(self.__sy, destname + '_sy.pkl', compress=3)
     joblib.dump(self.__sx, destname + '_sx.pkl', compress=3)
Exemplo n.º 49
0
ds.addSample((21, -16, 444, 0, -1, -21, 16, -444, 0, 1), (-1, 1))
ds.addSample((-146, 29, -126, 0, -1, 146, -29, 126, 0, 1), (-1, 1))
ds.addSample((68, 34, 192, -2, -1, -68, -34, -192, 2, 1), (1, -1))
ds.addSample((-44, 45, 132, -1, -1, 44, -45, -132, 1, 1), (1, -1))
ds.addSample((38, 5, -554, 1, -1, -38, -5, 554, -1, 1), (1, -1))
ds.addSample((238, 175, 962, -2, -1, -238, -175, -962, 2, 1), (1, -1))
ds.addSample((56, 74, -86, -2, -1, -56, -74, 86, 2, 1), (1, -1))

ds.addSample((151, 45, 684, -2, -1, -151, -45, -684, 2, 1), (1, -1))
ds.addSample((-35, -16, -288, -1, -1, 35, 16, 288, 1, 1), (1, -1))
ds.addSample((-116, -45, -164, 2, -1, 116, 45, 164, -2, 1), (-1, 1))
ds.addSample((99, 59, 174, -3, -1, -99, -59, -174, 3, 1), (1, -1))
ds.addSample((-125, -162, -345, -3, -1, 125, 162, 345, 3, 1), (1, -1))
ds.addSample((51, -59, 24, -4, -1, -51, 59, -24, 4, 1), (1, -1))
ds.addSample((-53, 57, 104, 0, -1, 53, -57, -104, 0, 1), (1, -1))
ds.addSample((-86, -88, 48, 0, -1, 86, 88, -48, 0, 1), (1, -1))
ds.addSample((36, 81, 84, -2, -1, -36, -81, -84, 2, 1), (1, -1))
ds.addSample((-48, -123, -704, 3, -1, 48, 123, 704, -3, 1), (-1, 1))
ds.addSample((-123, -25, 52, 2, -1, 123, 25, -52, -2, 1), (-1, 1))
ds.addSample((-221, -32, 244, 1, -1, 221, 32, -244, -1, 1), (-1, 1))
ds.addSample((128, 86, 650, -2, -1, -128, -86, -650, 2, 1), (1, -1))
ds.addSample((33, 26, -96, 1, -1, -33, -26, 96, -1, 1), (-1, 1))
ds.addSample((-8, 10, -30, -1, -1, 8, -10, 30, 1, 1), (1, -1))
ds.addSample((-40, -77, -498, 1, -1, 40, 77, 498, -1, 1), (-1, 1))

net = buildNetwork(10, 17, 2, bias=True, hiddenclass=TanhLayer)
trainer = BackpropTrainer(net, ds)
result = trainer.trainUntilConvergence(maxEpochs=10000)
print(result)
NetworkWriter.writeToFile(net, 'LoserBowlANN.xml')
Exemplo n.º 50
0
trainer = BackpropTrainer(net, trainingDataSet, learningrate = 0.001, momentum = 0.99)
trainer.trainUntilConvergence(trainingData=trainingDataSet, maxEpochs=20)


#measure success

total = 0
correct = 0

for input, output in training:
	guess = net.activate(input)
	if output[0] == round(guess):
		correct += 1
	total += 1

	#print output[0], ',', guess[0]

accuracy = correct/float(total)

#print "features: ", features
#print "total: ", total
#print "correct: ", correct
print "accuracy: ", accuracy


#save network for future use

filename = 'networks/newest/' + str(round(accuracy,3)) + '--' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + '.xml'

NetworkWriter.writeToFile(net, filename)
Exemplo n.º 51
0
def save_network(net):
    NetworkWriter.writeToFile(
        net, config["trained_models_paths"]["nn_nets"] +
        '/trained_nn.xml')  # '../nn_trained_networks/trained_nn.xml')
    print("Saving network to:",
          config["trained_models_paths"]["nn_nets"] + '/trained_nn.xml')
Exemplo n.º 52
0
def train(data_file, vis_matrix, vis_graph, save_file=''):
	load_params()
	#import dataset
	ds = ClassificationDataSet(micro_dim, 1, nb_classes=num_classes)
	extract_data(data_file, ds)
	
	tr, val = ds.splitWithProportion(2/3.)
	#softmax output layer
	tr._convertToOneOfMany()
	val._convertToOneOfMany()
	
	#build network
	layer_sizes = [tr.indim]
	for layer_size in num_hidden:
		layer_sizes.append(layer_size)
	layer_sizes.append(tr.outdim)
	if save_file == '':
		ann = buildNetwork(layer_sizes, hiddenclass=SigmoidLayer, recurrent=False, outclass=SoftmaxLayer, bias=inc_bias)
		iteration = 0
	else:
		ann = NetworkReader.readFrom(save_file)
		match = re.search('([0-9]+)_(?:[0-9]{1,3}).xml', save_file)
		if match == None:
			print 'Net save files should be named I_E.xml, where I is the iteration and E is the rounded error from 0-100'
			exit(1)
		else:
			iteration = int(match.group(1)) + 1
	
	#training 
	trainer = BackpropTrainer(ann,	dataset=tr, momentum=momentum, weightdecay=weight_decay)
	done = False
	errors, variations = [], []
	testing_errors, testing_variations = [], []
	
	while(not done):
		trainer.trainEpochs(num_epochs)
		
		# visualize iteration
		if vis_matrix or vis_graph:
			vertices, edges = vertsEdges(ann)
			if vis_matrix:
				matrixVisualizer(edges)
			if vis_graph:
				graphVisualizer(vertices, edges, iteration)
		
		# calculate and print error info
		training_error, testing_error, training_variation, testing_variation = trainer.testOnData(), trainer.testOnData(dataset=val), calcVariation(trainer), calcVariation(trainer, dataset=val)
		errors.append(training_error)
		variations.append(training_variation)
		testing_errors.append(testing_error)
		testing_variations.append(testing_variation)
		fig, ax1 = plt.subplots()
		iterations = range(iteration+1)
		ax1.plot(iterations, map(log10, errors), 'r-')
		ax1.plot(iterations, map(log10, testing_errors), 'b-')
		ax1.set_xlabel('iteration')
		ax1.set_ylabel('log mean squared error (red=train, blue=test)')
		for tick in ax1.get_yticklabels():
			tick.set_color('b')
		ax2 = ax1.twinx()
		ax2.plot(iterations, map(log10, variations), 'r--')
		ax2.plot(iterations, map(log10, testing_variations), 'b--')
		ax2.set_ylabel('log variation (L1 error) (red=train, blue=test)')
		for tick in ax2.get_yticklabels():
			tick.set_color('r')
		plt.savefig('error-3layer-48.pdf')
		plt.close()
		print 'iter %d, training error %f, testing error %f, training variation %f, testing variation %f' % (iteration, training_error, testing_error, training_variation, testing_variation)
		
		#save every <snapshot> iterations
		if iteration % snapshot == -1:
			file_data = (iteration, int(errors[-1]*100))
			print 'Saving model %d_%d.xml...' % file_data
			NetworkWriter.writeToFile(ann, '%d_%d.xml' % file_data)
		
		# go to the next iteration if not done
		iteration = iteration + 1
		if iteration >= max_iterations:
			done = True
	
	#testing
	val_errors, val_variations = [], []
	for i in range(5):
		val_error, val_variation = trainer.testOnData(dataset=val), calcVariation(trainer, dataset=val)
		print 'error %f, variation %f' % (val_error, val_variation)
		val_errors.append(val_error)
		val_variations.append(val_variation)
		tr, val = ds.splitWithProportion(0.9)
		val._convertToOneOfMany()
	print 'average error %f, average variation %f' % (np.average(val_errors), np.average(val_variations))
	
	#plotting
	iterations = range(max_iterations)
	fig, ax1 = plt.subplots()
	ax1.plot(iterations, map(log10, errors), 'b-')
	ax1.set_xlabel('iteration')
	ax1.set_ylabel('log mean squared error')
	for tick in ax1.get_yticklabels():
		tick.set_color('b')
	ax1.set_title('error for validation dataset: %f, variation for validation dataset: %f' % (val_error, val_variation))
	ax2 = ax1.twinx()
	ax2.plot(iterations, map(log10, variations), 'r-')
	ax2.set_ylabel('log variation (L1 error)')
	for tick in ax2.get_yticklabels():
		tick.set_color('r')
	plt.savefig('error-4layer-48-96.pdf')
Exemplo n.º 53
0
def reset_network():
    net = NetworkReader.readFrom(config["trained_models_paths"]["nn_nets"] +
                                 '/trained_nn_original.xml')
    NetworkWriter.writeToFile(
        net, config["trained_models_paths"]["nn_nets"] + '/trained_nn.xml')
    print("Network reset")
Exemplo n.º 54
0
def save_arguments(net):
    NetworkWriter.writeToFile(net, 'huge_data.csv')
    print 'Arguments save to file net.csv'
Exemplo n.º 55
0
def trainNetwork(dirname):

    numFeatures = 2000

    ds = SequentialDataSet(numFeatures, 1)

    tracks = glob.glob(os.path.join(dirname, "*.csv"))
    for t in tracks:
        track = os.path.splitext(t)[0]
        # load training data
        print "Reading %s..." % t
        data = numpy.genfromtxt(t, delimiter=",")
        numData = data.shape[0]

        # add the input to the dataset
        print "Adding to dataset..."
        ds.newSequence()
        for i in range(numData):
            # ds.addSample(data[i], (labels[i],))
            input = data[i]
            label = input[numFeatures]
            if label > 0:
                label = midi_util.frequencyToMidi(label)
            ds.addSample(input[0:numFeatures], (label,))

    # initialize the neural network
    print "Initializing neural network..."
    # net = buildNetwork(numFeatures, 50, 1,
    #                   hiddenclass=LSTMLayer, bias=True, recurrent=True)

    # manual network building
    net = RecurrentNetwork()
    inlayer = LinearLayer(numFeatures)
    # h1 = LSTMLayer(70)
    # h2 = SigmoidLayer(50)
    octaveLayer = LSTMLayer(5)
    noteLayer = LSTMLayer(12)
    combinedLayer = SigmoidLayer(60)
    outlayer = LinearLayer(1)

    net.addInputModule(inlayer)
    net.addOutputModule(outlayer)
    # net.addModule(h1)
    # net.addModule(h2)
    net.addModule(octaveLayer)
    net.addModule(noteLayer)
    net.addModule(combinedLayer)

    # net.addConnection(FullConnection(inlayer, h1))
    # net.addConnection(FullConnection(h1, h2))
    # net.addConnection(FullConnection(h2, outlayer))

    net.addConnection(FullConnection(inlayer, octaveLayer))
    net.addConnection(FullConnection(inlayer, noteLayer))
    # net.addConnection(FullConnection(octaveLayer,combinedLayer))
    for i in range(5):
        net.addConnection(
            FullConnection(
                octaveLayer, combinedLayer, inSliceFrom=i, inSliceTo=i + 1, outSliceFrom=i * 12, outSliceTo=(i + 1) * 12
            )
        )
    net.addConnection(FullConnection(noteLayer, combinedLayer))
    net.addConnection(FullConnection(combinedLayer, outlayer))

    net.sortModules()

    # train the network on the dataset
    print "Training neural net"
    trainer = RPropMinusTrainer(net, dataset=ds)
    ##    trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1)
    error = -1
    for i in range(150):
        new_error = trainer.train()
        print "error: " + str(new_error)
        if abs(error - new_error) < 0.005:
            break
        error = new_error

    # save the network
    print "Saving neural network..."
    NetworkWriter.writeToFile(net, os.path.basename(dirname) + "designnet")
Exemplo n.º 56
0
while remainingEpoch > 0:
    trainer.trainEpochs(1)
    trnresult = percentError(trainer.testOnClassData(), alldata["class"])
    tstresult = percentError(trainer.testOnClassData(dataset=alldatatest), alldatatest["class"])

    if tstresult < 4.12:
        low = True
    if low:
        remainingEpoch = remainingEpoch - 1

    x_errors[0, i] = trainer.totalepochs
    errors[0, i] = trnresult
    errors[1, i] = tstresult
    current = time.time() - start
    print(
        "epoch: %4d" % trainer.totalepochs,
        "  train error: %5.2f%%" % trnresult,
        "  test error: %5.2f%%" % tstresult,
        "  time : %5.1f s" % current,
    )

end = time.time()
print("Time elapsed :" + str(end - start))

plt.plot(x_errors[0, :], errors[0, :], "g", x_errors[0, :], errors[1, :], "b")
plt.legend(["train error", "test error"])
plt.show()

NetworkWriter.writeToFile(nn, "base.xml")
    def learn(self,
              pathdataset=["dstc4_train"],
              Pathdataroot="data",
              numberOfHiddenUnit=20,
              EPOCHS_PER_CYCLE=10,
              CYCLES=40,
              weightdecayw=0.01):
        print "Start learning LSTM, and make dictionary file"
        #Construct dictionary: variable name -> corresponding index of element in i/o vector
        print "Star make dictionary: variable name -> corresponding index of element in i/o vector"
        self.dictOut = {
        }  #"TOPIC_SLOT_VALUE" -> corresponding index of element
        self.dictIn = {
        }  #"SPEAKER_{val}"or"TOPIC_{val}","WORD_{word}" "BIO_{BIO}", "CLASS_{slot,value}", ""{defined label}-> corresponding  index of element
        #-target vector dictionary
        index = 0
        totalNumSlot = 0
        for topic in self.tagsets.keys():
            for slot in self.tagsets[topic].keys():
                totalNumSlot += 1
                for value in self.tagsets[topic][slot]:
                    self.dictOut[topic + "_" + slot + "_" + value] = index
                    index += 1
        print "totalNumSlot:" + str(totalNumSlot)
        print "outputSize:" + str(len(self.dictOut.keys()))
        #-input dictionry
        dataset = []
        for pathdat in pathdataset:
            dataset.append(
                dataset_walker.dataset_walker(pathdat,
                                              dataroot=Pathdataroot,
                                              labels=False))
        #--(sub input vector 1) Class features i.e., Slot and value ratio (Similar to base line)
        index = 0
        for topic in self.tagsets.keys():
            for slot in self.tagsets[topic].keys():
                if ("CLASS_" + slot) not in self.dictIn:
                    self.dictIn["CLASS_" + slot] = index
                    index += 1
                for value in self.tagsets[topic][slot]:
                    if ("CLASS_" + value) not in self.dictIn:
                        self.dictIn["CLASS_" + value] = index
                        index += 1
        self.TOTALSIZEOFCLASSFeature = index
        f = open(self.FileNameofNumClassFeature, "wb")
        pickle.dump(self.TOTALSIZEOFCLASSFeature, f)
        f.close()
        #--(sub input vector 2) Sentence features
        if not self.isUseSentenceRepresentationInsteadofBOW:
            index = 0
            for elemDataset in dataset:
                for call in elemDataset:
                    for (uttr, _) in call:
                        #General info1 (CLASS; this feature must be rejistered at first)
                        if ("SPEAKER_" + uttr["speaker"]) not in self.dictIn:
                            self.dictIn["SPEAKER_" + uttr["speaker"]] = index
                            index += 1
                        if ("TOPIC_" + uttr["segment_info"]["topic"]
                            ) not in self.dictIn:
                            self.dictIn["TOPIC_" +
                                        uttr["segment_info"]["topic"]] = index
                            index += 1
                        #General info2
                        #-BIO
                        if ("BIO_" + uttr['segment_info']['target_bio']
                            ) not in self.dictIn:
                            self.dictIn[
                                "BIO_" +
                                uttr['segment_info']['target_bio']] = index
                            index += 1

                        #BOW
                        if LSTMWithBOWTracker.isIgnoreUtterancesNotRelatedToMainTask:
                            if not (uttr['segment_info']['target_bio'] == "O"):
                                #-BOW
                                splitedtrans = self.__getRegurelisedBOW(
                                    uttr["transcript"])
                                for word in splitedtrans:
                                    if ("WORD_" + word) not in self.dictIn:
                                        self.dictIn["WORD_" + word] = index
                                        index += 1
            self.TOTALSIZEOFSENTENCEFeature = index
            f = open(self.FileNameofNumSentenceFeature, "wb")
            pickle.dump(self.TOTALSIZEOFSENTENCEFeature, f)
            f.close()
        elif self.isUseSentenceRepresentationInsteadofBOW:
            index = 0
            for i in range(0, LSTMWithBOWTracker.D2V_VECTORSIZE):
                self.dictIn[str(index) + "thElemPV"] = index
                index += 1
            index = 0
            for i in range(0, LSTMWithBOWTracker.D2V_VECTORSIZE):
                self.dictIn[str(index) + "thAvrWord"] = index
                index += 1
            assert self.D2V_VECTORSIZE == LSTMWithBOWTracker.D2V_VECTORSIZE, "D2V_VECTORSIZE is restrected to be same over the class"
        else:
            assert False, "Unexpected block"
        #--(sub input vector 3) Features M1s defined
        index = 0
        if self.isEnableToUseM1sFeature:
            rejisteredFeatures = self.__rejisterM1sInputFeatureLabel(
                self.tagsets, dataset)
            for rFeature in rejisteredFeatures:
                assert rFeature not in self.dictIn, rFeature + " already registered in input vector. Use different label name. "
                self.dictIn[rFeature] = index
                index += 1
            self.TOTALSIZEOFM1DEFINEDFeature = index
            f = open(self.FileNameofNumM1Feature, "wb")
            pickle.dump(self.TOTALSIZEOFM1DEFINEDFeature, f)
            f.close()

        print "inputSize:" + str(len(self.dictIn.keys()))
        assert self.dictIn[
            "CLASS_INFO"] == 0, "Unexpected index CLASS_INFO should has value 0"
        assert self.dictIn[
            "CLASS_Fort Siloso"] == 334, "Unexpected index CLASS_Fort Siloso should has value 334"
        assert self.dictIn[
            "CLASS_Yunnan"] == 1344, "Unexpected index CLASS_Yunnan should has value 1611"
        #--write
        fileObject = open('dictInput.pic', 'w')
        pickle.dump(self.dictIn, fileObject)
        fileObject.close()
        fileObject = open('dictOutput.pic', 'w')
        pickle.dump(self.dictOut, fileObject)
        fileObject.close()

        #Build RNN frame work
        print "Start learning Network"
        #Capability of network is: (30 hidden units can represents 1048576 relations) wherease (10 hidden units can represents 1024)
        #Same to Henderson (http://www.aclweb.org/anthology/W13-4073)?
        net = buildNetwork(len(self.dictIn.keys()),
                           numberOfHiddenUnit,
                           len(self.dictOut.keys()),
                           hiddenclass=LSTMLayer,
                           outclass=SigmoidLayer,
                           outputbias=False,
                           recurrent=True)

        #Train network
        #-convert training data into sequence of vector
        convDataset = []  #[call][uttr][input,targetvec]
        iuttr = 0
        convCall = []
        for elemDataset in dataset:
            for call in elemDataset:
                for (uttr, label) in call:
                    if self.isIgnoreUtterancesNotRelatedToMainTask:
                        if uttr['segment_info']['target_bio'] == "O":
                            continue
                    #-input
                    convInput = self._translateUtteranceIntoInputVector(
                        uttr, call)
                    #-output
                    convOutput = [0.0] * len(
                        self.dictOut.keys())  #Occured:+1, Not occured:0
                    if "frame_label" in label:
                        for slot in label["frame_label"].keys():
                            for value in label["frame_label"][slot]:
                                convOutput[self.dictOut[
                                    uttr["segment_info"]["topic"] + "_" +
                                    slot + "_" + value]] = 1
                    #-post proccess
                    if self.isSeparateDialogIntoSubDialog:
                        if uttr['segment_info']['target_bio'] == "B":
                            if len(convCall) > 0:
                                convDataset.append(convCall)
                            convCall = []
                    convCall.append([convInput, convOutput])
                    #print "Converted utterance" + str(iuttr)
                    iuttr += 1
                if not self.isSeparateDialogIntoSubDialog:
                    if len(convCall) > 0:
                        convDataset.append(convCall)
                    convCall = []
        #Online learning
        trainer = RPropMinusTrainer(net, weightdecay=weightdecayw)
        EPOCHS = EPOCHS_PER_CYCLE * CYCLES
        for i in xrange(CYCLES):
            #Shuffle order
            ds = SequentialDataSet(len(self.dictIn.keys()),
                                   len(self.dictOut.keys()))
            datInd = range(0, len(convDataset))
            random.shuffle(
                datInd
            )  #Backpropergation already implemeted data shuffling, however though RpropMinus don't.
            for ind in datInd:
                ds.newSequence()
                for convuttr in convDataset[ind]:
                    ds.addSample(convuttr[0], convuttr[1])
            #Evaluation and Train
            epoch = (i + 1) * EPOCHS_PER_CYCLE
            print "\r epoch {}/{} Error={}".format(
                epoch, EPOCHS, trainer.testOnData(dataset=ds))
            stdout.flush()
            trainer.trainOnDataset(dataset=ds, epochs=EPOCHS_PER_CYCLE)
            NetworkWriter.writeToFile(
                trainer.module, "LSTM_" + "Epoche" + str(i + 1) + ".rnnw")
            NetworkWriter.writeToFile(trainer.module, "LSTM.rnnw")