示例#1
0
def load_network(trained_on_nao=False):
    if trained_on_nao:
        net = NetworkReader.readFrom(
            config["trained_models_paths"]["nn_nets"] + '/trained_nn_nao.xml')
    else:
        net = NetworkReader.readFrom(
            config["trained_models_paths"]["nn_nets"] + '/trained_nn.xml')
    return net
def classify(data,network_file='network_training_progress.xml'):
    """
    Takes two arguments, 'data' is a 1D array of floats ranging 0-1 representing grayscale values of an image,
    'network_file' is an xml file output from 'pybrain_playground.py', a pre-trained network. 
    Returns two floats, how much it guesses that a given input is a track or other, respectively.
    Again, classify()[0] is chances it is a track, classify()[1] is chances it is other. Ranged 0-1.
    
    Example:
        >>> im = loadImage('path/to/track.png')
        >>> print classify(im)[0]
        0.99
        >>> print classify(im)[1]
        0.01
    Here, 0.99 indicates it believes with 99% certainty that the image is a track, 
    and 0.01% certainty that it is not a track.
    
    !!!IMPORTANT!!!
    THE PROGRAM EXPECTS FILES OUTPUT FROM PREPROCESSING.PY,
    AND PREPREOCESSING.PY EXPECTS FILES THAT HAVE BEEN
    OUTPUT BY PLOTBLOBS.PY
    !!!IMPORTANT!!!
    
    """
    net = NetworkReader.readFrom(network_file)
    return net.activate(data)
示例#3
0
 def __init__(self, name, deck_id):
     self.neural_network = NetworkReader.readFrom('network.xml')
     
     hero = get_hero(deck_id)
     self.deck_id = deck_id
     self.original_deck = get_deck_by_id(deck_id)
     super(Q_player, self).__init__(name, self.original_deck, hero)
示例#4
0
def test(filename, test_data):
    ann = NetworkReader.readFrom(filename)
    #file = open('results.csv', 'w', newline = ' ')
    rank_teams = {1: 'Philadelphia 76ers', 2: 'Los Angeles Lakers', 3: 'Brooklyn Nets', 4: 'Phoenix Suns', 
             5: 'Minnesota Timberwolves', 6: 'New Orleans Pelicans', 7: 'New York Knicks', 
             8: 'Sacramento Kings', 9: 'Milwaukee Bucks', 10: 'Denver Nuggets', 11: 'Orlando Magic', 
             12: 'Utah Jazz', 13: 'Washington Wizards', 14: 'Houston Rockets', 15: 'Chicago Bulls', 
             16: 'Memphis Grizzlies', 17: 'Dallas Mavericks', 18: 'Portland Trail Blazers', 
             19: 'Detroit Pistons', 20: 'Indiana Pacers', 21: 'Miami Heat',
             22: 'Charlotte Hornets', 23: 'Boston Celtics', 24: 'Atlanta Hawks', 
             25: 'Los Angeles Clippers', 26: 'Oklahoma City Thunder', 27: 'Toronto Raptors', 
             28: 'Cleveland Cavaliers', 29: 'San Antonio Spurs', 30: 'Golden State Warriors'}
    list_ = []
    with open('temp_file2.csv', 'w', newline = '') as fp:
        temp = csv.writer(fp)
        for i in range(1,31):
            for j in range(1, 31):
                if(i != j):
                    out = ann.activate([i, j, 0, 0, 0])
                    if (out > 1.00):
                        out = 99
                    else:
                        num = out * 100
                        out = int(num)
                    temp.writerow([rank_teams.get(i), rank_teams.get(j), out])
def runSaveNet(netName):
	net =  NetworkReader.readFrom(netName)

	print '0,0,0->', net.activate([0,0,0])
	print '0,0,1->', net.activate([0,0,1])
	print '0,1,0->', net.activate([0,1,0])
	print '0,1,1->', net.activate([0,1,1])
	print '1,0,0->', net.activate([1,0,0])
	print '1,0,1->', net.activate([1,0,1])
	print '1,1,0->', net.activate([1,1,0])
	print '1,1,1->', net.activate([1,1,1])

	print "-----------------------------------------------------"

	print 'Max position of 0,0,0->', getMaxPosition(net.activate([0,0,0])) + 1
	print 'Max position of 0,0,1->', getMaxPosition(net.activate([0,0,1])) + 1
	print 'Max position of 0,1,0->', getMaxPosition(net.activate([0,1,0])) + 1
	print 'Max position of 0,1,1->', getMaxPosition(net.activate([0,1,1])) + 1
	print 'Max position of 1,0,0->', getMaxPosition(net.activate([1,0,0])) + 1
	print 'Max position of 1,0,1->', getMaxPosition(net.activate([1,0,1])) + 1
	print 'Max position of 1,1,0->', getMaxPosition(net.activate([1,1,0])) + 1
	print 'Max position of 1,1,1->', getMaxPosition(net.activate([1,1,1])) + 1

	print 
	print
示例#6
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 : "
示例#7
0
 def define_category(self, request, categories):
     if type(request) == Request:
         net = NetworkReader.readFrom('net.xml')
         output = list(net.activate(request.token))
         return categories[output.index(max(output))]
     else:
         raise ArgumentException()
示例#8
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 : "
示例#9
0
    def __init__(self,
                 input_layer=784,
                 hidden_layer=30,
                 output_layer=10,
                 load=False):
        """ Initiate the network. """

        self.input_layer = input_layer
        self.hidden_layer = hidden_layer
        self.output_layer = output_layer

        # Loading from a previously saved state.
        if load:
            self.net = NetworkReader.readFrom(self.network_fn)
        else:
            self.net = buildNetwork(input_layer,
                                    hidden_layer,
                                    output_layer,
                                    bias=True,
                                    hiddenclass=SigmoidLayer,
                                    outclass=SoftmaxLayer)

        # Creating training and testing data.
        print ">   Reading data."
        data = minst.MnistData(self.data_path)

        # Reading the data.
        trd = data.get_training_data()
        ttd = data.get_test_data()
        vad = data.get_validation_data()

        # Converting into shape that can be used by the library.
        print ">   Creating training dataset."
        self.ds_training = SupervisedDataSet(input_layer, output_layer)

        for x, y in trd:
            self.ds_training.addSample(x.reshape(input_layer),
                                       y.reshape(output_layer))

        print ">   Creating testing dataset."
        self.ds_test = SupervisedDataSet(input_layer, output_layer)
        for x, y in ttd:
            self.ds_test.addSample(x.reshape(input_layer),
                                   y.reshape(output_layer))

        print ">   Creating validation dataset."
        self.ds_validation = SupervisedDataSet(input_layer, output_layer)
        for x, y in vad:
            self.ds_validation.addSample(x.reshape(input_layer),
                                         y.reshape(output_layer))

        print ">   All data loaded."

        # Default trainer for the network.
        print ">   Creating default trainer. (Use update_learningrate if you want to change)"
        self.trainer = BackpropTrainer(self.net,
                                       self.ds_training,
                                       learningrate=1)
示例#10
0
    def loadFromDir(cls, dirPath):
        """
        Return a classifier, loaded from the given directory.
        """
        with codecs.open(os.path.join(dirPath, cls._CLASSIFIER_NAME), encoding='utf-8') as f:
            c = serializer.load(f.read())

        c.net = NetworkReader.readFrom(os.path.join(dirPath, cls._NET_NAME))
        return c
示例#11
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)
示例#12
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)
示例#13
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")
示例#14
0
 def __init__(self, name, deck_id, neural_net):
     if (neural_net == None):
         path = os.path.join(os.path.dirname(os.getcwd()), 'network.xml')
         self.neural_network = NetworkReader.readFrom(path)
     else:
         self.neural_network = neural_net
   
     hero = get_hero(deck_id)
     self.deck_id = deck_id
     self.original_deck = get_deck_by_id(deck_id)
     super(Q_learner, self).__init__(name, self.original_deck, hero)
 def __init__(self, inpNeurons, hiddenNeurons, outNeurons):
     self.net = buildNetwork(inpNeurons, hiddenNeurons, outNeurons, hiddenclass=TanhLayer, bias=True)
     if raw_input('Recover Network?: y/n\n')=='y':
         print 'Recovering Network'
         net = NetworkReader.readFrom('Network1.xml')
     else:
         print 'New Network'
         self.net.randomize()
     print self.net
     self.ds = SupervisedDataSet(inpNeurons,outNeurons)
     self.trainer = BackpropTrainer(self.net, self.ds, learningrate = 0.01, momentum=0.99)
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
	def import_network(self, filename):
		train_samples = self.samples
		train_labels  = self.labels
		
		np.random.seed(0)
		np.random.shuffle(train_samples)
		np.random.seed(0)
		np.random.shuffle(train_labels)
		
		self.net_shared = NetworkReader.readFrom(filename)
		self.ds_shared = SupervisedDataSet(300, 1)
		for i in range(len(train_samples)):  
			self.ds_shared.addSample(tuple(np.array(train_samples[i], dtype='float64')), (train_labels[i],))
			
		self.trainer_shared = BackpropTrainer(self.net_shared, self.ds_shared, verbose=True)
示例#18
0
文件: annbp.py 项目: nsh23/ml-bench
    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
def neural_predict(filename, train_file, output):
    testtag, testdata = readfile(filename)
    net = NetworkReader.readFrom(train_file)
    i = 0
    num = 0
    output_file = open(output, 'w')
    output_file.write("test data size: " + str(len(testtag)) + '\n')
    output_type_list = []
    output_type_size = []
    output_type_right = []
    output_typt_error_detail = []
    for k in testdata:
        res = net.activate(k)
        if testtag[i] not in output_type_list:
            output_type_list.append(testtag[i])
            output_type_size.append(0)
            output_type_right.append(0)
            output_typt_error_detail.append([])
        j = output_type_list.index(testtag[i])
        output_type_size[j] += 1
        if labals[max_index(res)] == testtag[i]:
            num += 1
            output_type_right[j] += 1
        else:
            (output_typt_error_detail[j]).append(labals[max_index(res)])
        i += 1
    # for k in testdata:
    #     res = net.activate(k)
    #     if labals[max_index(res)] == testtag[i]:
    #         num += 1
    #     i += 1
    output_file.write("correct number: " + str(num) + '\n')
    output_file.write("correct rate: " + str(num / (float)(len(testtag))) + '\n')
    i = 0
    for x in output_type_list:
        output_file.write(x + "\t")
        output_file.write(str(output_type_right[i]) + '/' + str(output_type_size[i]) + ':'
                          + str(float(output_type_right[i]) / output_type_size[i])[0:5] + '\t')
        c = Counter(output_typt_error_detail[i])
        for y in c:
            output_file.write(y + ":" + str(c[y]) + '\t')
            print(y + ":" + str(c[y]))
        # print c
        i += 1
        output_file.write('\n')
    print num
    output_file.close()
示例#20
0
def runNeuralNets(savedNet):

	net =  NetworkReader.readFrom(savedNet)
	dataModel = createTheDataModel([2,5,9,15])

	totalGamesPredicted = 0
	totalGames = 0
	incorrect = 0
	correct = 0
	for input, target in dataModel:

	    i = list(input)
	    if len(i) != 228:
	    	continue


	    totalGames += 1
	    result = net.activate(i)[0]

	    if result > 0:
	    	result = 1
	    else:
	    	result = 0

	    # elif result < -3.2:
	    # 	result = 0
	    # else:
	    # 	continue

	    totalGamesPredicted += 1

	    if result == target[0]:
	    	correct += 1
	    else:
	    	incorrect += 1

	
	print 'correct: ' + str(correct) + " (" + str(100.0 * correct/totalGamesPredicted)[0:6] + "%)"
	print 'incorrect: ' + str(incorrect) + " (" + str(100.0 * incorrect/totalGamesPredicted)[0:6] + "%)"
	print 'totalGames: ', totalGames
	print 'totalGamesPredicted: ', totalGamesPredicted

	print

	return 
    def testSaveNetwork(self):
        """
        Save a network, make sure it's valid.
        """
        xor = NetworkReader.readFrom(self.storedXor)
        c = classifier.Classifier(imageSize=(2, 2), netSpec=(8, 1))
        c.net = xor

        storedPath = os.path.join(self.workspace, 'testNetDir')
        c.dump(storedPath)

        newC = classifier.Classifier.loadFromDir(storedPath)

        self.assertEqual(c, newC)

        #Make sure the net still works
        for image, expected in self.xorImages:
            self.assertEqual(c.classify(image)[0], expected)
示例#22
0
文件: network.py 项目: majek/transfer
    def load(self, filename):
        with open(filename, 'rb') as f:
            inp = pickle.Unpickler(f)
            while True:
                try:
                    k, v = inp.load()
                except EOFError:
                    break
                if k == const.PNETWORK:
                    network_data = v
                elif k == const.PWINDOW:
                    if self.window:
                        if self.window != v:
                            print "[!] window differs"
                    else:
                        self.window = v
                elif k == const.PSIZE:
                    if self.size:
                        if self.size != v:
                            print "[!] size differs"
                    else:
                        self.size = v
                elif k == const.PRATIO:
                    if self.ratio:
                        if self.ratio != v:
                            print "[!] ratio differs"
                    else:
                        self.ratio = v
                elif k == const.PMULTIPLIER:
                    if self.multiplier:
                        if self.multiplier != v:
                            print "[!] multiplier differs"
                    else:
                        self.multiplier = v
                else:
                    FATAL("%r" % (k,))

        tmpfile = filename + '~net~'
        with open(tmpfile, 'wb') as f:
            f.write(network_data)
        self.net = NetworkReader.readFrom(tmpfile)
        os.unlink(tmpfile)
        self.net.sortModules()
示例#23
0
    def import_network(self, filename):
        train_samples = self.samples
        train_labels = self.labels

        np.random.seed(0)
        np.random.shuffle(train_samples)
        np.random.seed(0)
        np.random.shuffle(train_labels)

        self.net_shared = NetworkReader.readFrom(filename)
        self.ds_shared = SupervisedDataSet(300, 1)
        for i in range(len(train_samples)):
            self.ds_shared.addSample(
                tuple(np.array(train_samples[i], dtype='float64')),
                (train_labels[i], ))

        self.trainer_shared = BackpropTrainer(self.net_shared,
                                              self.ds_shared,
                                              verbose=True)
示例#24
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
示例#25
0
文件: util.py 项目: nvaller/pug-ann
def weight_matrices(nn):
    """ Extract list of weight matrices from a Network, Layer (module), Trainer, Connection or other pybrain object"""

    if isinstance(nn, ndarray):
        return nn

    try:
        return weight_matrices(nn.connections)
    except:
        pass

    try:
        return weight_matrices(nn.module)
    except:
        pass

    # Network objects are ParameterContainer's too, but won't reshape into a single matrix,
    # so this must come after try nn.connections
    if isinstance(nn, (ParameterContainer, Connection)):
        return reshape(nn.params, (nn.outdim, nn.indim))

    if isinstance(nn, basestring):
        try:
            fn = nn
            nn = NetworkReader(fn, newfile=False)
            return weight_matrices(nn.readFrom(fn))
        except:
            pass
    # FIXME: what does NetworkReader output? (Module? Layer?) need to handle it's type here

    try:
        return [weight_matrices(v) for (k, v) in nn.iteritems()]
    except:
        try:
            connections = nn.module.connections.values()
            nn = []
            for conlist in connections:
                nn += conlist
            return weight_matrices(nn)
        except:
            return [weight_matrices(v) for v in nn]
示例#26
0
def weight_matrices(nn):
    """ Extract list of weight matrices from a Network, Layer (module), Trainer, Connection or other pybrain object"""

    if isinstance(nn, ndarray):
        return nn

    try:
        return weight_matrices(nn.connections)
    except:
        pass

    try:
        return weight_matrices(nn.module)
    except:
        pass

    # Network objects are ParameterContainer's too, but won't reshape into a single matrix,
    # so this must come after try nn.connections
    if isinstance(nn, (ParameterContainer, Connection)):
        return reshape(nn.params, (nn.outdim, nn.indim))

    if isinstance(nn, basestring):
        try:
            fn = nn
            nn = NetworkReader(fn, newfile=False)
            return weight_matrices(nn.readFrom(fn))
        except:
            pass
    # FIXME: what does NetworkReader output? (Module? Layer?) need to handle it's type here

    try:
        return [weight_matrices(v) for (k, v) in nn.iteritems()]
    except:
        try:
            connections = nn.module.connections.values()
            nn = []
            for conlist in connections:
                nn += conlist
            return weight_matrices(nn)
        except:
            return [weight_matrices(v) for v in nn]
示例#27
0
def getEmotion(filename):

    os.path.abspath(__file__)  #文件绝对路径

    dir = os.path.dirname(os.path.abspath(__file__))  #文件目录绝对路径

    # dir.split(os.sep)[-1] #获取目录名字  os.sep 路径分隔符

    #   读取特征参数

    feature = Feature(filename=filename)

    feature_avg = list(feature.getFeature())

    #   加载神经网络

    net = NetworkReader.readFrom(dir + '/net.xml')

    o = net.activate(feature_avg).tolist()

    result = o.index(max(o))

    return result
示例#28
0
def evaluate_nn(trainer,
                number_of_trials=10,
                iteration_limit=200,
                action_threshold=0.2,
                pos_range=(-max_pos, max_pos),
                vel_range=(-max_vel, max_vel),
                learn_rate=0.4,
                discount_factor=0.99,
                explore_rate=0.5,
                explore_rate_reduction=2,
                explore_rate_freq=50,
                draw_output=True,
                draw_speed=60,
                train=True,
                prnt=False,
                net=None):

    if net is None:
        net = NetworkReader.readFrom('nn_trained_networks/trained_nn.xml')

    number_completed = 0  # The number of trials in which the ball stays on for the max number of iterations

    print("EVALUATING")

    i = 0
    running = True
    while i < number_of_trials and trainer.continue_running() and running:
        if i % explore_rate_freq == 0:
            print(i)
            explore_rate /= explore_rate_reduction
        new_ang = (random.randint(0, trainer.max_ang * 100) / 100) * (
            (-1)**(random.randint(
                1, 2)))  # Random andgle the tray will be for this test
        new_pos = int(
            random.uniform(*pos_range)
        )  # random.randint(0, max_pos) * ((-1)**(random.randint(1,2)))  # Random position of the ball on the tray for this test
        new_vel = int(
            random.uniform(*vel_range)
        )  # random.randint(0, max_vel) * ((-1)**(random.randint(1,2)))  # Random velocity of the ball for this test

        if random.random(
        ) < 1:  # Get it into a smaller range more often, as these values are seen much more
            new_vel = new_vel // 5.0

        trainer.update_tray_angle(
            new_ang)  # Move the tray to the chosen position
        trainer.add_ball(new_pos, new_vel)  # Add the new ball
        trainer.a = trainer.get_bin(new_ang, trainer.max_ang,
                                    trainer.num_bins_ang)
        # print("\n\n\n")
        continue_this_ball = 0
        limit = iteration_limit
        while trainer.is_ball_on_tray(
        ) and continue_this_ball < limit and running:  # See what happens after action has taken place
            # Get initial state s
            p, v, a = trainer.get_ball_info()
            p, v, a = trainer.get_bin_info(
                p, trainer.max_pos, trainer.num_bins_pos, v, trainer.max_vel,
                trainer.num_bins_vel, a, trainer.max_ang, trainer.num_bins_ang)

            # Use NN to pick and execute best action
            nn_out = net.activate([p, v, a])
            action = np.argmax(nn_out)

            if train:
                if random.random() < explore_rate:
                    action = random.randint(0, 2)

            # print(p, v, a, nn_out, action)
            if abs(nn_out[action]) < action_threshold:
                trainer.step_simulation(False, True, draw_output, draw_speed)
            elif action == 0:
                trainer.do_action(1,
                                  1,
                                  slow=False,
                                  draw=draw_output,
                                  speed=draw_speed)
            elif action == 2:
                trainer.do_action(1,
                                  -1,
                                  slow=False,
                                  draw=draw_output,
                                  speed=draw_speed)
            elif action == 1:
                trainer.step_simulation(False, True, draw_output, draw_speed)
            #    trainer.do_action(1, 0, slow=True, draw=draw_output, speed=draw_speed)

            if not train and prnt:
                print(p, v, a, nn_out, action)

            if train:
                # Get new state s'
                p2, v2, a2 = trainer.get_ball_info()
                p2, v2, a2 = trainer.get_bin_info(p2, trainer.max_pos,
                                                  trainer.num_bins_pos, v2,
                                                  trainer.max_vel,
                                                  trainer.num_bins_vel, a2,
                                                  trainer.max_ang,
                                                  trainer.num_bins_ang)

                # Calculate reward for being in s'
                reward = trainer.calculate_reward(p2, trainer.num_bins_pos, v2,
                                                  trainer.num_bins_vel)

                # Get the value of the best action from the new state
                action2_val = max(net.activate([p2, v2, a2]))

                if prnt:
                    print(p, v, a, "/", action, "/", p2, v2, a2, "/", reward)
                    print(nn_out)

                # Update the output vector values
                nn_out[action] = ((1 - learn_rate) * nn_out[action]) + (
                    learn_rate * (reward + discount_factor * action2_val))

                if prnt:
                    print(nn_out, "\n")

                # update the network
                #print("updating")
                net = update_nn(net, [p, v, a], nn_out)
                #print("finished")

            continue_this_ball += 1

            for event in pygame.event.get():
                if event.type == QUIT:
                    running = False
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    running = False

            if continue_this_ball == limit:
                number_completed += 1

        trainer.remove_ball()

        i += 1

    print(number_completed / number_of_trials)
    if train:
        NetworkWriter.writeToFile(net, 'nn_trained_networks/q_learnt_nn.xml')
示例#29
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')
示例#30
0
    if os.path.isfile(out_file_name):
        out_file_read = open(out_file_name).read()
        if seqname in out_file_read:
            # print "Sequence already compared, %s, continue to next"%seqname
            line = testvector.readline()
            i = i + 1
            continue

    seqfeats = map(float, elements[3:-1])

    max_score = 0
    max_net = "none"
    for netfile in netfiles:

        net = NetworkReader.readFrom(args.n + netfile)
        score = net.activate(seqfeats)

        if score > float(args.m):
            print >> outall_file, "Testseq:\t", seqname, "\tNetwork:\t", netfile, "\tScore:\t", score

        if float(score) > max_score:
            max_score = float(score)
            max_net = netfile

    if max_score > float(args.m):
        print >> out_file, "Testseq:\t", seqname, "\tNetwork:\t", max_net, "\tScore:\t", max_score
    else:
        print >> out_file, "Nomatch Testseq:\t", seqname, "\tNetwork:\t", max_net, "\tScore:\t", max_score

    if i % 100 == 0:
示例#31
0
from pybrain.tools.customxml import NetworkReader

net = NetworkReader.readFrom('LoserBowlANN.xml')
test = [[('KC', 'NE'), (-126, 35, -33, 0.65, -1, 126, -35, 33, -0.65, 1),
         (1, -1)],
        [('NYJ', 'BUF'),
         (-26.1875, -42.3125, 26.6875, -0.171875, -1, 26.1875, 42.3125,
          -26.6875, 0.171875, 1), (-1, 1)],
        [('ATL', 'CHI'),
         (27.875, -19.75, 130.8125, 0.64453125, -1, -27.875, 19.75, -130.8125,
          -0.64453125, 1), (1, -1)],
        [('BAL', 'CIN'),
         (-23.3125, -17.5, 54.375, 0.21875, -1, 23.3125, 17.5, -54.375,
          -0.21875, 1), (1, -1)],
        [('PIT', 'CLE'),
         (-218.875, -40.75, 130.9375, 0.5859375, -1, 218.875, 40.75, -130.9375,
          -0.5859375, 1), (1, -1)],
        [('ARI', 'DET'),
         (-49.5625, -12.625, 18.125, 1.0078125, -1, 49.5625, 12.625, -18.125,
          -1.0078125, 1), (-1, 1)],
        [('JAX', 'HOU'),
         (23.5625, 10.4375, -135.3125, -0.484375, -1, -23.5625, -10.4375,
          135.3125, 0.484375, 1), (1, -1)],
        [('OAK', 'TEN'),
         (17.4375, 28.0625, 79.875, 0.703125, -1, -17.4375, -28.0625, -79.875,
          -0.703125, 1), (1, -1)],
        [('PHI', 'WAS'),
         (-27.0625, -16.3125, 168.875, 0.16796875, -1, 27.0625, 16.3125,
          -168.875, -0.16796875, 1), (1, -1)],
        [('IND', 'LA'),
         (32.9375, 15.75, 52.1875, 0.06640625, -1, -32.9375, -15.75, -52.1875,
示例#32
0
 def __init__(self, networkName):
     self.network = NetworkReader.readFrom(networkName)
     self.indim = self.network.indim
     self.outdim = self.network.outdim
示例#33
0
import gensim
from pybrain.tools.customxml  import NetworkReader
import numpy as np
#load the gensim model
model = gensim.models.Word2Vec.load('news.en.model')

fnn = NetworkReader.readFrom('oliv.xml') 
word =np.array([model['us-led_JJ'],model['american-led_JJ']] )
pred= fnn.activate(word.flatten())
print pred

word =np.array([model['white_JJ'],model['black_JJ']] )
pred= fnn.activate(word.flatten())
print pred

word =np.array([model['local_JJ'],model['international_JJ']] )
pred= fnn.activate(word.flatten())
print pred

word =np.array([model['bike_NN'],model['motorcycle_NN']] )
pred= fnn.activate(word.flatten())
print pred
# and 
#bike and motorcycle 
#bike and banana_NN
word =np.array([model['polite_JJ'],model['impolite_JJ']] )
pred= fnn.activate(word.flatten())
print pred


示例#34
0
# lets train the network
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import TanhLayer

import random

# lets train the network
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import TanhLayer

from pybrain.tools.customxml import NetworkWriter
from pybrain.tools.customxml import NetworkReader

net = NetworkReader.readFrom('nnet.xml')

print "Loaded network from file."

print "testing..."

for datum in dataset[0:100]:
    print "expected: ", datum[0], "result: ", nnetResultToChar(net.activate(datum[1]))

print "Accuracy rateing: ", scoreNetAccuracy(net, dataset)

from pybrain.tools.customxml import NetworkWriter
NetworkWriter.writeToFile(net, 'nnet.xml')
        
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)+".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
mcc09 = float(tp09*tn09 - fp09*fn09) / float(sqrt( (tp09+fp09) * (tp09+fn09) * (tn09+fp09) * (tn09+fn09) ))
PCCr_row, PCCp_value = pearsonr(target,predicted) 
示例#36
0
import gensim
from pybrain.tools.customxml  import NetworkReader
import numpy as np
#load the gensim model
model = gensim.models.Word2Vec.load('news.en.model')

fnn = NetworkReader.readFrom('clean-oliv.xml') 
# word =np.array([model['us-led_JJ'],model['american-led_JJ']] )
# pred= fnn.activate(word.flatten())
# print pred

# word =np.array([model['white_JJ'],model['black_JJ']] )
# pred= fnn.activate(word.flatten())
# print pred

#local and international
#bike and motorcycle 
#bike and banana_NN
# word =np.array([model['polite_JJ'],model['impolite_JJ']] )
# pred= fnn.activate(word.flatten())
# print pred 100-1 010-2 100-4

word =np.array([model['fresh_JJ'],model['preserved_JJ']] )
pred= fnn.activate(word.flatten())
print pred

word =np.array([model['cheap_JJ'],model['expensive_JJ']] )
pred= fnn.activate(word.flatten())
print pred

word =np.array([model['bad_JJ'],model['terrible_JJ']] )
示例#37
0
def load_network():
    net = NetworkReader.readFrom('nn_trained_networks/trained_nn.xml')
    return net
示例#38
0
# net.addInputModule(inLayer)
# net.addModule(hiddenLayer1)
# net.addModule(hiddenLayer2)
# net.addModule(hiddenLayer3)
# net.addOutputModule(outputLayer)

# net.addConnection( in_to_hidden1)
# net.addConnection(hidden1_to_hidden2)
# net.addConnection(hidden2_to_hidden3)
# net.addConnection(hidden3_to_out)

# net.sortModules()

# net = buildNetwork(10,100,100,100,100,100,1)
net = NetworkReader.readFrom('net-15029.xml')
# print net

print net.activate(
    [0.45, 0.46, 0.45, 0.45, 0.44, 0.40, 0.30, 0.24, 0.24, 0.22])

# data = loadtxt('station-15029.dat')
# data = [x/100 for x in data]
# ds = SupervisedDataSet(10,1)
# index = 0
# for sample in data:
#     if index > len(data) - 11 :
#         break
#     inputVector = [data[index], data[index+1], data[index+2],data[index+3],data[index+4], data[index+5],data[index+6],data[index+7],data[index+8],data[index+9],]
#     targetVector = [ data[index+10], ]
#     ds.addSample(inputVector, targetVector)
示例#39
0
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
servo = GPIO.PWM(18,50)

servo.start(0.0)

#find XML file
if os.path.exists(XML) == False:
    print('XML file does not exist!')
    raw_input(">")
    sys.exit()

# create network
network = NetworkReader.readFrom(XML)
    
time.sleep(1.0)
print "ready"
time.sleep(1.0)

GPIO.output(23,True)
GPIO.output(24,False)

while True:
    try:
        while True:

            #capture from camera
            stream = picamera.array.PiRGBArray(cam0)
            cam0.capture(stream, format = 'bgr', use_video_port = True)
示例#40
0
import pandas as pd #Usado para ler os dados da planilha Excel

from pybrain.tools.customxml import NetworkReader #usado para ler a RNA treinada

#Para Impressao dos dados
import matplotlib.pyplot as plt

#Carregando dados da planilha excel
df = pd.read_excel("assets/dadoLeitura/dadosTrabalhoRNA.xlsx",  engine='openpyxl', dtype={'Entrada':int, 'Saída':int})

RNA = NetworkReader.readFrom('name.xml') #lendo RNA Treinada

#Esperando entrada de 0-1 para visualizar no grafico
while(True):
    entrada = float(input("Digite o valor de entrada entre 0-1: "))    
    saida = RNA.activate([entrada])*max(df['Saída'])
    entrada = entrada*max(df['Entrada'])
    print(entrada, saida)
    #Plotando Graficos
    fig, ax = plt.subplots()
    ax.plot(df['Entrada'], df['Saída'])
    ax.plot(entrada, saida, 'o')    
    ax.grid()
    plt.show()
    def __init__(self,
                 tagsets=None,
                 nameOfODictPickle=None,
                 nameOfIDictPickle=None,
                 nameOfLSTMFile=None,
                 NameOfLearnedD2VFile="LearnedDoc2Vec.d2v"):
        #Print out Experimenal setup
        print "In both of learning and tracking,"
        if self.isStoreFrameOutputedByLSTMAtEachTurnInSubDialog:
            print "the tracker store output of LSTM at each turn in Sub.dial,"
        else:
            print "the tracker doesn't store output of LSTM at each turn in Sub.dial,"
        if self.isIgnoreUtterancesNotRelatedToMainTask:
            print "and tracker ignore the utterance which is not related to main task."
        else:
            print "and tracker doesn't ignore the utterance which is not related to main task."
        if self.isSeparateDialogIntoSubDialog:
            print "and tracker consider one subdialog at one sequence."
        else:
            print "and tracker consider one dialog at one sequence."
        if self.isCombineResultWithBaseline:
            print "and the output of LSTM is combined with that of baseline."
        else:
            print "and the output of LSTM is not combined with that of baseline."
        if self.isEnableToUseM1sFeature:
            print "and features made by M1s are used in input."
        else:
            print "and features made by M1s are not used in input."
        if self.isUseSentenceRepresentationInsteadofBOW:
            print "and distributed sentence reprentation is used instead of BOW and meta info."
        else:
            print "and BOW and meta indo is used for sentense feature, and Distributed sentence reprentation is not used."

        #Variables for tracking state
        self.LSTM = None
        self.dictOut = None
        self.dictIn = None
        if nameOfLSTMFile is not None:
            print "Load LSTM network file from " + nameOfLSTMFile
            self.LSTM = NetworkReader.readFrom(nameOfLSTMFile)
            assert self.LSTM is not None, "Failed to read LSTM"
        if nameOfIDictPickle is not None:
            print "Load input dictionary file from " + nameOfIDictPickle
            f = open(nameOfIDictPickle, "r")
            self.dictIn = pickle.load(f)
            f.close()
            assert self.dictIn is not None, "Failed to read Input dictionary"

        if nameOfODictPickle is not None:
            print "Load output dictionary file from " + nameOfODictPickle
            f = open(nameOfODictPickle, "r")
            self.dictOut = pickle.load(f)
            f.close()
            assert self.dictOut is not None, "Failed to read Output dictionary"
        if tagsets == None:
            self.tagsets = ontology_reader.OntologyReader(
                "scripts/config/ontology_dstc4.json").get_tagsets()
        else:
            self.tagsets = tagsets
        #Variables for fast processing
        #-1
        if LSTMWithBOWTracker.dictFuzzyMatchingResult == None:
            try:
                f = gzip.open(self.FileNameofdictFuzzyMatchingResult, "rb")
            except Exception:
                print "FuzzyMatchingResult.pic was not found. Dictionary are newly created. "
                LSTMWithBOWTracker.dictFuzzyMatchingResult = {}
            else:
                print "FuzzyMatchingResult.pic Dictionary are loaded."
                LSTMWithBOWTracker.dictFuzzyMatchingResult = pickle.load(f)
                f.close()
        #-2
        if self.isEnableToUseM1sFeature and self.isUtilizeM1VectorDictionary:
            try:
                f = gzip.open(self.FileNameofM1Vector, "rb")
            except Exception:
                print self.FileNameofM1Vector + "was not found. Dictionary are newly created. "
                self.dictM1Vector = {}
            else:
                print self.FileNameofM1Vector + " Dictionary are loaded."
                self.dictM1Vector = pickle.load(f)
                f.close()

        #
        try:
            f = open(self.FileNameofNumClassFeature, "rb")
            self.TOTALSIZEOFCLASSFeature = pickle.load(f)
            f.close()
            print "TSizeClasssFeature=" + str(self.TOTALSIZEOFCLASSFeature)
        except Exception:
            print self.FileNameofNumClassFeature + " was not found. learn() is required before tracking. "
        try:
            f = open(self.FileNameofNumSentenceFeature, "rb")
            self.TOTALSIZEOFSENTENCEFeature = pickle.load(f)
            f.close()
            print "TSizeSentenceFeature=" + str(
                self.TOTALSIZEOFSENTENCEFeature)
        except Exception:
            print self.FileNameofNumSentenceFeature + " was not found. learn() is required before tracking. "
        try:
            f = open(self.FileNameofNumM1Feature, "rb")
            self.TOTALSIZEOFM1DEFINEDFeature = pickle.load(f)
            f.close()
            print "TSizeM1Feature=" + str(self.TOTALSIZEOFM1DEFINEDFeature)
        except Exception:
            print self.FileNameofNumM1Feature + " was not found. learn() is required before tracking. "

        #
        if self.isUseSentenceRepresentationInsteadofBOW:
            self.d2v = LSTMWithBOWTracker.loadDoc2VecAndCheckAppropriateness(
                NameOfLearnedD2VFile)
        #
        self.frame = {}
        self.reset()
示例#42
0
# lets train the network
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import TanhLayer

import random

# lets train the network
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure.modules import TanhLayer

from pybrain.tools.customxml import NetworkWriter
from pybrain.tools.customxml import NetworkReader

net = NetworkReader.readFrom('nnet.xml')

print "Loaded network from file."

print "testing..."

for datum in dataset[0:100]:
    print "expected: ", datum[0], "result: ", nnetResultToChar(
        net.activate(datum[1]))

print "Accuracy rateing: ", scoreNetAccuracy(net, dataset)

from pybrain.tools.customxml import NetworkWriter
NetworkWriter.writeToFile(net, 'nnet.xml')
示例#43
0
import pybrain
from pybrain.tools.customxml import NetworkReader
from compressionFN import compressSingle
bitsToAnalise = 128*8

def bits(f):
    bytes = (ord(b) for b in f.read())
    for b in bytes:
        for i in xrange(8):
            yield (b >> i) & 1

ratiosNetwork = pybrain.FeedForwardNetwork()
ratiosNetwork = NetworkReader.readFrom('/Users/arcadigonzalez/PycharmProjects/anncomp_pro/ANN_Backups/2480')
bestNetwork = pybrain.FeedForwardNetwork()
bestNetwork = NetworkReader.readFrom('/Users/arcadigonzalez/PycharmProjects/anncomp_pro/ANN2_Backups/2480')

algorithms = ['ZIP, no compression', 'ZIP and DEFLATE','BZip', 'LZMA2', 'LZMA1','delta and LZMA', 'LZMA2 with high literal context bits', 'LZMA with high literal position bits'\
              ,'LZMA with high position bits', 'LZMA with ARM architechture BCJ','LZMA with ARM Thumb architechture BCJ','LZMA with IA64 architechture BCJ', \
              'LZMA with IBM PowerPC architechture BCJ','LZMA with Oracle SPARC architechture BCJ','LZMA with Intel x64-86 architechture BCJ','GZip','Zlib']

exitval = 0
while exitval < 1:
    file = raw_input("enter full path to file: ")
    bitsArr = []

    i = 0
    #generate array of bits to feed the network
    for b in bits(open(file)):
        if i < bitsToAnalise:
             bitsArr.append(b)
             i+=1
示例#44
0
def load_network(trainer):
    net = NetworkReader.readFrom(trainer.file_location_network)
    return net
示例#45
0
def build_bp():
    global net
    net = NetworkReader.readFrom(
        os.path.join(curdir, '../collect_data/data/bpstudy.xml'))
示例#46
0
def evaluate_nn(trainer,
                number_of_trials=10,
                iteration_limit=200,
                pos_range=(-max_pos, max_pos),
                vel_range=(-max_vel, max_vel),
                draw_output=True,
                draw_speed=60,
                record_data=False,
                two_acts=False):
    print("Loading network from:", trainer.file_location_network)
    net = NetworkReader.readFrom(trainer.file_location_network)

    number_completed = 0  # The number of trials in which the ball stays on for the max number of iterations

    print("Evaluating...")

    i = 0
    running = True
    while i < number_of_trials and trainer.continue_running() and running:
        if i % 100 == 0:
            print(i)
        new_ang = (random.randint(0, trainer.max_ang * 100) / 100) * (
            (-1)**(random.randint(
                1, 2)))  # Random andgle the tray will be for this test
        new_pos = int(
            random.uniform(*pos_range)
        )  # random.randint(0, max_pos) * ((-1)**(random.randint(1,2)))  # Random position of the ball on the tray for this test
        new_vel = int(
            random.uniform(*vel_range)
        )  # random.randint(0, max_vel) * ((-1)**(random.randint(1,2)))  # Random velocity of the ball for this test

        if random.random(
        ) < 1:  # Get it into a smaller range more often, as these values are seen much more
            new_vel = new_vel // 5.0

        trainer.update_tray_angle(
            new_ang)  # Move the tray to the chosen position
        trainer.add_ball(new_pos, new_vel)  # Add the new ball
        trainer.a = trainer.get_bin(new_ang, trainer.max_ang,
                                    trainer.num_bins_ang)

        continue_this_ball = 0
        limit = iteration_limit
        while trainer.is_ball_on_tray(
                iterate=True
        ) and continue_this_ball < limit and running:  # See what happens after action has taken place
            p_val, v_val, a_val = trainer.get_ball_info()
            p, v, a = trainer.get_bin_info(p_val, trainer.max_pos,
                                           trainer.num_bins_pos, v_val,
                                           trainer.max_vel,
                                           trainer.num_bins_vel, a_val,
                                           trainer.max_ang,
                                           trainer.num_bins_ang)

            nn_out = net.activate([p, v, a])
            action = np.argmax(nn_out)
            if two_acts:  # Have only left and right tilt
                if action == 1:
                    if nn_out[0] > nn_out[2]:
                        action = 0
                    else:
                        action = 2
            if abs(nn_out[action]) < 0.4:
                trainer.step_simulation(True, True, draw_output, draw_speed)
            elif action == 0:
                trainer.do_action(1,
                                  1,
                                  slow=True,
                                  draw=draw_output,
                                  speed=draw_speed)
            elif action == 2:
                trainer.do_action(1,
                                  -1,
                                  slow=True,
                                  draw=draw_output,
                                  speed=draw_speed)
            elif action == 1:
                trainer.do_action(1,
                                  0,
                                  slow=True,
                                  draw=draw_output,
                                  speed=draw_speed)

            continue_this_ball += 1

            for event in pygame.event.get():
                if event.type == QUIT:
                    running = False
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    running = False

            if continue_this_ball == limit:
                number_completed += 1
            print(p, v, a, action, nn_out)
            trainer.record_current_state(
                float(p_val), float(v_val), float(a_val),
                int(action))  # Record values before changed to bin values

        trainer.remove_ball()

        i += 1
    if record_data:
        trainer.save_state_data(trainer.num_bins_pos, trainer.num_bins_vel,
                                trainer.num_bins_ang, trainer.max_pos,
                                trainer.max_vel, trainer.max_ang, trainer.desc)
    print(number_completed / number_of_trials)
示例#47
0
    if os.path.isfile(out_file_name):
        out_file_read=open(out_file_name).read()
        if seqname in out_file_read:
           # print "Sequence already compared, %s, continue to next"%seqname
            line = testvector.readline()
            i=i+1
            continue

    seqfeats = map(float,elements[3:-1])

    max_score=0
    max_net = "none"
    for netfile in netfiles:

        net = NetworkReader.readFrom(args.n + netfile)
        score = net.activate(seqfeats)

        if (score > float(args.m)):
            print >> outall_file, "Testseq:\t", seqname, "\tNetwork:\t" , netfile ,"\tScore:\t", score        

        if (float(score) > max_score):
            max_score = float(score)
            max_net = netfile


    if (max_score > float(args.m)):
        print >> out_file, "Testseq:\t", seqname, "\tNetwork:\t" , max_net ,"\tScore:\t", max_score        
    else:
        print >> out_file, "Nomatch Testseq:\t", seqname, "\tNetwork:\t" , max_net ,"\tScore:\t", max_score        
       
示例#48
0
def evaluate_nn(trainer,
                number_of_trials=10,
                iteration_limit=200,
                action_threshold=0.2,
                pos_range=(-max_pos, max_pos),
                vel_range=(-max_vel, max_vel),
                draw_output=True,
                draw_speed=60):
    net = NetworkReader.readFrom('nn_trained_networks/trained_nn.xml')

    number_completed = 0  # The number of trials in which the ball stays on for the max number of iterations

    print("EVALUATING")

    i = 0
    running = True
    while i < number_of_trials and trainer.continue_running() and running:
        if i % 100 == 0:
            print(i)
        new_ang = (random.randint(0, trainer.max_ang * 100) / 100) * (
            (-1)**(random.randint(
                1, 2)))  # Random andgle the tray will be for this test
        new_pos = int(
            random.uniform(*pos_range)
        )  # random.randint(0, max_pos) * ((-1)**(random.randint(1,2)))  # Random position of the ball on the tray for this test
        new_vel = int(
            random.uniform(*vel_range)
        )  # random.randint(0, max_vel) * ((-1)**(random.randint(1,2)))  # Random velocity of the ball for this test

        if random.random(
        ) < 1:  # Get it into a smaller range more often, as these values are seen much more
            new_vel = new_vel // 5.0

        trainer.update_tray_angle(
            new_ang)  # Move the tray to the chosen position
        trainer.add_ball(new_pos, new_vel)  # Add the new ball
        trainer.a = trainer.get_bin(new_ang, trainer.max_ang,
                                    trainer.num_bins_ang)
        # print("\n\n\n")
        continue_this_ball = 0
        limit = iteration_limit
        while trainer.is_ball_on_tray(
        ) and continue_this_ball < limit and running:  # See what happens after action has taken place
            p, v, a = trainer.get_ball_info()
            p, v, a = trainer.get_bin_info(
                p, trainer.max_pos, trainer.num_bins_pos, v, trainer.max_vel,
                trainer.num_bins_vel, a, trainer.max_ang, trainer.num_bins_ang)

            nn_out = net.activate([p, v, a])
            action = np.argmax(nn_out)
            # print(p, v, a, nn_out, action)
            if abs(nn_out[action]) < 0.4:
                trainer.step_simulation(True, True, draw_output, draw_speed)
            elif action == 0:
                trainer.do_action(1,
                                  1,
                                  slow=True,
                                  draw=draw_output,
                                  speed=draw_speed)
            elif action == 2:
                trainer.do_action(1,
                                  -1,
                                  slow=True,
                                  draw=draw_output,
                                  speed=draw_speed)
            elif action == 1:
                trainer.do_action(1,
                                  0,
                                  slow=True,
                                  draw=draw_output,
                                  speed=draw_speed)

            continue_this_ball += 1

            for event in pygame.event.get():
                if event.type == QUIT:
                    running = False
                elif event.type == KEYDOWN and event.key == K_ESCAPE:
                    running = False

            if continue_this_ball == limit:
                number_completed += 1
        trainer.remove_ball()

        i += 1

    print(number_completed / number_of_trials)
示例#49
0
    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')

if __name__ == '__main__':
    dirname = os.path.normpath(sys.argv[1])
    # wave_reader.extractFeatures(track)
    trainNetwork(dirname)
    net = NetworkReader.readFrom(os.path.basename(dirname) + 'net')
    
    # predict on some of the training examples
    print "Predicting on training set"
    data = numpy.genfromtxt(os.path.join(dirname, 'train09_seg.csv'), delimiter=",")
    labels = numpy.genfromtxt(os.path.join(dirname, 'train09REF.txt'), delimiter='\t')[0::10,1]
##    for i in range(200):
##        print net.activate(data[i]), labels[i]
    cdata = numpy.array([])
    for feature in data:
        freq = max(0, net.activate(feature))
        sample = wave_gen.saw(freq, 0.1, 44100)
        cdata = numpy.concatenate([cdata, sample])
    wave_gen.saveAudioBuffer('test.wav', cdata)
##    for freq in labels:
##        sample = wave_gen.saw(freq, 0.1, 44100)
示例#50
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")
示例#51
0
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
mcc09 = float(tp09 * tn09 - fp09 * fn09) / float(
    sqrt((tp09 + fp09) * (tp09 + fn09) * (tn09 + fp09) * (tn09 + fn09)))
PCCr_row, PCCp_value = pearsonr(target, predicted)
示例#52
0
文件: nn_fc.py 项目: kizombakid/inv
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
        
# --------------     Read network parameters   ------------------
print "Saving Network"
from pybrain.tools.customxml import NetworkReader
netdir2 = os.path.join(basedir,"inv")
netdir = os.path.join(netdir2,"analyse")
#netfile = os.path.join(netdir,'net_basic8_close_10000_rio_pre2018.xml')
netfile = os.path.join(netdir,'net_'+pvar+'_'+ovar+'_'+date1+'_'+date1+'_'+str(niter)+'.xml')
net=NetworkReader.readFrom(netfile)
        
        
        
# --------------      Apply to new data/test   ------------------
print "Doing Prediction"
from pylab import *
predict=[]
for i in range(0,len(pp)):  
    xx=net.activate(pp[i]) 
    predict.append(xx)


scatter(po,predict)
show()
示例#53
0
def load_arguments(filename):
    return NetworkReader.readFrom(filename)
示例#54
0
    plotname = os.path.join(plotdir, ('jpq2layers_plot' + str(iter)))
    pylab.savefig(plotname)


# set-up the neural network
nneuron = 5
mom = 0.98
netname = "LSL-" + str(nneuron) + "-" + str(mom)
mv = ModuleValidator()
v = Validator()

#create the test DataSet
x = numpy.arange(0.0, 1.0 + 0.01, 0.01)
s = 0.5 + 0.4 * numpy.sin(2 * numpy.pi * x)
tsts = SupervisedDataSet(1, 1)
tsts.setField('input', x.reshape(len(x), 1))
tsts.setField('target', s.reshape(len(s), 1))
#read the train DataSet from file
trndata = SupervisedDataSet.loadFromFile(os.path.join(os.getcwd(), 'trndata'))

myneuralnet = os.path.join(os.getcwd(), 'myneuralnet.xml')
if os.path.isfile(myneuralnet):
    n = NetworkReader.readFrom(myneuralnet, name=netname)
    #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, tsts=tsts, ctsts=ctsts)

    pylab.show()
示例#55
0
from random import randint
import random
from datetime import datetime
import pyraptools
from pybrain.tools.customxml import NetworkReader
print("Enter the name of the rapper xml file")
rappername = input()
net = NetworkReader.readFrom(rappername+".xml")
word_list = list()
rapper_list = list()
random.seed(datetime.now())
#fetching list of all possible words
with open('progfiles/wordlist.txt') as f:
	content = f.readlines()
	for line in content:
		for subline in line.split(':'):
			for word in subline.split():
				word_list.append(word.strip())

#fetching list of rappers
with open('progfiles/rapperlist.txt') as f:
	content = f.readlines()
	for line in content:
		for word in line.split(","):
			if not word =="\n":
				if not word =="":
					rapper_list.append(word.strip())

n1 = randint(0,20)
n2 = randint(0,20)
print("Enter file to save the rap to.")
示例#56
0
 def load_network(self,name_of_the_net):
     print "load existing trained network"
     network=NetworkReader.readFrom(name_of_the_net)
     print "Succeed!"
     return network

#create training set
trainingDataSet = SupervisedDataSet(features, 1)
for input, target in training:
    trainingDataSet.addSample(input, target)


#create testing set
testingDataSet = SupervisedDataSet(features, 1)
for input, target in testing:
	testingDataSet.addSample(input, target)



net = NetworkReader.readFrom(filename)

#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)
示例#58
0
 def loadNetwork(self, filename):
     self.network = NetworkReader.readFrom(filename)