Пример #1
0
def measuredLearning(ds):

    trndata,tstdata = splitData(ds,.025)

    #build network


    ###
    # This network has no hidden layters, you might need to add some
    ###
    fnn = buildNetwork( trndata.indim, 22, trndata.outdim, outclass=SoftmaxLayer )
    trainer = BackpropTrainer( fnn, verbose=True,dataset=trndata)
                               
    ####
    #   Alter this to figure out how many runs you want.  Best to start small and be sure that you see learning.
    #   Before you ramp it up.
    ###
    for i in range(150):
        trainer.trainEpochs(5)
   
        
        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
        if(trnresult<.5): 
            return
Пример #2
0
def calculateANNaccuracy(model, trndata, tstdata, trainer,iterations=20,maxEpochs=10):
    
    trn_sum=0
    tst_sum=0
    trnfinal=[]
    tstfinal=[]
           
    for i in range(iterations):

            trainer.trainUntilConvergence(maxEpochs=maxEpochs, continueEpochs=1)
            trnresult = percentError( trainer.testOnClassData(),
                              trndata['class'] )
            tstresult = percentError( trainer.testOnClassData(
           dataset=tstdata ), tstdata['class'] )
            trnres=100-trnresult
            tstres=100-tstresult
            trn_sum=trn_sum+trnres
            tst_sum=tst_sum+tstres
            trnfinal.append(trnres)
            tstfinal.append(tstres)
            
    trn_avg=trn_sum/(i+1)
    tst_avg=tst_sum/(i+1)
    print "Train average accuracy: %5.2f%%" % trn_avg
    print "Test average accuracy: %5.2f%%" % tst_avg

    print "Train SD = ", np.std(trnfinal)
    print "Test SD = ", np.std(tstfinal)
    print "\n"
    return tst_avg
Пример #3
0
def train(args):
  inputs, ys, gc = args
  row_length = len(inputs[0])
  d = ds.ClassificationDataSet(
      row_length, nb_classes=2, class_labels=['Poisonous',
                                              'Edible'])
  d.setField('input', inputs)
  d.setField('target', ys)
  test, train = d.splitWithProportion(.25)
  test._convertToOneOfMany()
  train._convertToOneOfMany()

  hidden = row_length // 2
  print "indim:", train.indim
  net = buildNetwork(train.indim,
                     hidden,
                     train.outdim,
                     outclass=SoftmaxLayer)
  trainer = BackpropTrainer(net,
                            dataset=train,
                            momentum=0.0,
                            learningrate=0.1,
                            verbose=True,
                            weightdecay=0.0)
  for i in xrange(20):
      trainer.trainEpochs(1)
      trnresult = percentError(trainer.testOnClassData(),
                                train['class'])
      tstresult = percentError(
              trainer.testOnClassData(dataset=test),
              test['class'])
      print "epoch: %4d" % trainer.totalepochs, \
            "  train error: %5.2f%%" % trnresult, \
            "  test error: %5.2f%%" % tstresult
  return net, gc
Пример #4
0
    def classicNeuralNetwork(self, features, labels, autoencoder=False):
        dataSet = SupervisedDataSet(features.shape[1], 1)
        dataSet.setField('input', features)
        if autoencoder: labels = features
        dataSet.setField('target', labels)
        tstdata, trndata = dataSet.splitWithProportion(0.25)
        print features.shape
        simpleNeuralNetwork = _buildNetwork(\
                                    (LinearLayer(features.shape[1],'in'),),\
                                    (SigmoidLayer(20,'hidden0'),),\
                                    (LinearLayer(labels.shape[1],'out'),),\
                                    bias=True)
        trainer = BackpropTrainer(simpleNeuralNetwork,
                                  dataset=trndata,
                                  verbose=True)  #, momentum=0.1)
        trainer.trainUntilConvergence(maxEpochs=15)

        trnresult = percentError(trainer.testOnData(dataset=trndata),
                                 trndata['target'])
        tstresult = percentError(trainer.testOnData(dataset=tstdata),
                                 tstdata['target'])

        print "epoch: %4d" % trainer.totalepochs, \
          "  train error: %5.2f%%" % trnresult, \
          "  test error: %5.2f%%" % tstresult

        self.neuralNetwork = simpleNeuralNetwork
Пример #5
0
    def train(self):

        #self.init_iri()
        self.init_image()
        self.ds = ClassificationDataSet(self.IN, 1, nb_classes=128)
        #classifier.init_image()
        self.load_data()
        print "Number of trianing patterns: ", len(self.trndata)
        print "Input and output dimensions: ", self.trndata.indim, self.trndata.outdim
        print "First sample (input, target, class):"
        print self.trndata['input'][0], self.trndata['target'][0], self.trndata['class'][0]
        print self.trndata.indim, self.trndata.outdim
        self.net = buildNetwork(self.trndata.indim, 7, self.trndata.outdim)


        trainer = BackpropTrainer(self.net, dataset=self.trndata, momentum=0.1, verbose=True, weightdecay=0.01)

        """
        for i in range(200):
            trainer.trainEpochs(1)
            trnresult = percentError(trainer.testOnClassData(), self.trndata['class'])
            tstresult = percentError(trainer.testOnClassData(dataset = self.tstdata), self.tstdata["class"])
            print "epch: %4d" %  trainer.totalepochs, \
                " train error: %5.2f%%" % trnresult, \
                " test error: %5.2f%%" % tstresult
        """
        trainer.trainUntilConvergence()
        trnresult = percentError(trainer.testOnClassData(), self.trndata['class'])
        tstresult = percentError(trainer.testOnClassData(dataset = self.tstdata), self.tstdata["class"])
        print "epch: %4d" %  trainer.totalepochs, \
            " train error: %5.2f%%" % trnresult, \
            " test error: %5.2f%%" % tstresult
Пример #6
0
 def trainNetwork(self,proportion = 0):        
     if proportion != 0:
         tstdata, trndata = self.alldata.splitWithProportion( 0.01*proportion )
     else:
         trndata = self.alldata
     trndata._convertToOneOfMany( )
     if proportion != 0:
         tstdata._convertToOneOfMany( )
     print "Number of training patterns: ", len(trndata)
     print "Input and output dimensions: ", trndata.indim, trndata.outdim
     self.fnn = buildNetwork( trndata.indim, self.hidden_layer_size, trndata.outdim, 
                              hiddenclass=SigmoidLayer,outclass=SoftmaxLayer )
     self.trainer = BackpropTrainer( self.fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
     for i in range(self.iterations_number):
         self.trainer.trainEpochs( 1 )
         trnresult = percentError( self.trainer.testOnClassData(),
                                   trndata['class'] )
         if proportion != 0:
             tstresult = percentError( self.trainer.testOnClassData(
                dataset=tstdata ), tstdata['class'] )
     
         if proportion != 0:
             print "epoch: %4d" % self.trainer.totalepochs, \
               "  train error: %5.2f%%" % trnresult, \
               "  test error: %5.2f%%" % tstresult
         else:
             print "epoch: %4d" % self.trainer.totalepochs, \
               "  train error: %5.2f%%" % trnresult
	def test(self,filename,classes,trainer,net):
		testLabels = []

		#load test data
		tstdata = ClassificationDataSet(103, 1, nb_classes=classes)
		tstdata = self.loaddata(filename, classes)

		testLabels = tstdata['target'];

		# some sort of mandatory conversion
		tstdata._convertToOneOfMany()
		
		# using numpy array
		output = np.array([net.activate(x) for x, _ in tstdata])
		output = output.argmax(axis=1)
		print(output)
		print("on test data",percentError( output, tstdata['class'] ))

		for i, l in enumerate(output):
			print l, '->', testLabels[i][0]

		# alternate version - using activateOnDataset function
		out = net.activateOnDataset(tstdata).argmax(axis=1)
		print out
		return percentError( out, tstdata['class'])
def main():
    images, labels = load_labeled_training(flatten=True)
    images = standardize(images)
    # images, labels = load_pca_proj(K=100)
    shuffle_in_unison(images, labels)
    ds = ClassificationDataSet(images.shape[1], 1, nb_classes=7)
    for i, l in zip(images, labels):
        ds.addSample(i, [l - 1])
    # ds._convertToOneOfMany()
    test, train = ds.splitWithProportion(0.2)
    test._convertToOneOfMany()
    train._convertToOneOfMany()
    net = shortcuts.buildNetwork(train.indim, 1000, train.outdim, outclass=SoftmaxLayer)

    trainer = BackpropTrainer(net, dataset=train, momentum=0.1, learningrate=0.01, weightdecay=0.05)
    # trainer = RPropMinusTrainer(net, dataset=train)
    # cv = validation.CrossValidator(trainer, ds)
    # print cv.validate()
    net.randomize()
    tr_labels_2 = net.activateOnDataset(train).argmax(axis=1)
    trnres = percentError(tr_labels_2, train["class"])
    # trnres = percentError(trainer.testOnClassData(dataset=train), train['class'])
    testres = percentError(trainer.testOnClassData(dataset=test), test["class"])
    print "Training error: %.10f, Test error: %.10f" % (trnres, testres)
    print "Iters: %d" % trainer.totalepochs

    for i in range(100):
        trainer.trainEpochs(10)
        trnres = percentError(trainer.testOnClassData(dataset=train), train["class"])
        testres = percentError(trainer.testOnClassData(dataset=test), test["class"])
        trnmse = trainer.testOnData(dataset=train)
        testmse = trainer.testOnData(dataset=test)
        print "Iteration: %d, Training error: %.5f, Test error: %.5f" % (trainer.totalepochs, trnres, testres)
        print "Training MSE: %.5f, Test MSE: %.5f" % (trnmse, testmse)
    def run_nn(trndata, tstdata, outpath, name, fold):
        fnn = buildNetwork(trndata.indim, 20, trndata.outdim, hiddenclass=TanhLayer, bias=True)

        trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, weightdecay=0.01)

        acc = 0.0

        real_obj = []
        predicted_obj = []

        # for i in range(5):
        for i in range(50):
            trainer.trainEpochs( 1 )
            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
            predicted = np.array( trainer.testOnClassData(dataset=tstdata) )
            real = np.array( tstdata['class'][:,0] )
            # print real
            if (accuracy_score(real, predicted) > acc) & (0 != len(np.where(predicted==1)[0])):
                real_obj = real
                predicted_obj = predicted
                acc = accuracy_score(real, predicted) 

        Utility.save_obj(real_obj, '{}/{}_fold_{}_real.npy'.format(outpath, name, fold))
        Utility.save_obj(predicted_obj, '{}/{}_fold_{}_predicted.npy'.format(outpath, name, fold))
        print 'Accuracy : {}'.format(acc)
        return acc
Пример #10
0
def trainNetwork(inData, numOfSamples, numOfPoints, epochs):
    # Build the dataset
    alldata = createRGBdataSet(inData, numOfSamples, numOfPoints)
    # Split into test and training data
    trndata, tstdata = splitData(alldata)

    # Report  stats
    print "Number of training patterns: ", len(trndata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim
    print "First sample (input, target, class):"
    print trndata['input'][0], trndata['target'][0], trndata['class'][0]

    # Build and train the network
    fnn = buildNetwork( trndata.indim, 256, trndata.outdim, outclass=SoftmaxLayer )
    trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.001, verbose=True, weightdecay=0.001)
    #trainer.trainEpochs( epochs )
    trainer.trainUntilConvergence(maxEpochs=epochs)

    # Report results
    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

    # Report results of final network
    checkNeuralNet(trainer, alldata, numOfSamples)
    return fnn
Пример #11
0
def trainStep(fnn, trainer, trndata, tstdata):
    trainer.trainEpochs(1)
    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

    out = fnn.activateOnDataset(griddata)
    out = out.argmax(axis=1)  # the highest output activation gives the class
    out = out.reshape(X.shape)

    figure(1)
    ioff()  # interactive graphics off
    clf()  # clear the plot
    hold(True)  # overplot on
    for c in [0, 1, 2]:
        here, _ = where(trndata["class"] == c)
        plot(trndata["input"][here, 0], trndata["input"][here, 1], "o")
    if out.max() != out.min():  # safety check against flat field
        contourf(X, Y, out)  # plot the contour
    ion()  # interactive graphics on
    draw()  # update the plot

    figure(2)
    ioff()  # interactive graphics off
    clf()  # clear the plot
    hold(True)  # overplot on
    for c in [0, 1, 2]:
        here, _ = where(tstdata["class"] == c)
        plot(tstdata["input"][here, 0], tstdata["input"][here, 1], "o")
    if out.max() != out.min():  # safety check against flat field
        contourf(X, Y, out)  # plot the contour
    ion()  # interactive graphics on
    draw()  # update the plot
Пример #12
0
def drawPic():
    try:
        sampleCount = int(inputEntry.get())
    except:
        sampleCount = 50
        print 'Enter an integer.'
        inputEntry.delete(0, END)
        inputEntry.insert(0, '50')
    # Need column vectors in dataset, not arrays
    for i in range(sampleCount):
        trainer.trainEpochs(1)
        trnresult = percentError(trainer.testOnClassData(), trndata['class'])
        tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
        if i % 20 == 0:
            t.delete(1.0, END)
        t.insert(END, "epoch:" + str(trainer.totalepochs) + "  train error:" + str(round(trnresult, 2)) \
                  + "%  test error:" + str(round(tstresult, 2)) + "%\n")
        # Clear the Figure
        drawPic.f.clf()
        drawPic.a = drawPic.f.add_subplot(111, projection='3d')
        drawPic.a.set_title('Training...')
        for a, c, m in [(0, 'r', 'o'), (1, 'b', '^'), (2, 'y', 's')]:
            out = fnn.activateOnDataset(alldata)
            out = out.argmax(axis=1)
            here = (out == a)
            drawPic.a.scatter(alldata['input'][here, 0], alldata['input'][here, 1], alldata['input'][here, 2], c=c, marker=m)
        drawPic.canvas.show()
Пример #13
0
def calculateANNaccuracy(model, trndata, tstdata, trainer):
    trn_sum=0
    tst_sum=0
    trnfinal=[]
    tstfinal=[]
    for i in range(20):
        trainer.trainUntilConvergence(maxEpochs=10, continueEpochs=3, validationProportion=0.30)
        trnresult = percentError( trainer.testOnClassData(),
                          trndata['class'] )
        tstresult = percentError( trainer.testOnClassData(
       dataset=tstdata ), tstdata['class'] )
        trnres=100-trnresult
        tstres=100-tstresult
        trn_sum=trn_sum+trnres
        tst_sum=tst_sum+tstres
        trnfinal.append(trnres)
        tstfinal.append(tstres)

    trn_avg=trn_sum/(i+1)
    tst_avg=tst_sum/(i+1)
    '''
    print "Test average accuracy: %5.2f%%" % tst_avg

    print "Test SD = ", np.std(tstfinal)
    '''
    return tst_avg
Пример #14
0
def createnetwork(n_hoglist,n_classlist,n_classnum,n_hiddensize=100):
    n_inputdim=len(n_hoglist[0])
    n_alldata = ClassificationDataSet(n_inputdim,1, nb_classes=n_classnum)
    for i in range(len(n_hoglist)):
        n_input = n_hoglist[i]
        n_class = n_classlist[i]
        n_alldata.addSample(n_input, [n_class])
    n_tstdata, n_trndata = n_alldata.splitWithProportion( 0.25 )
    n_trndata._convertToOneOfMany( )
    n_tstdata._convertToOneOfMany( )

    print "Number of training patterns: ", len(n_trndata)
    print "Input and output dimensions: ", n_trndata.indim, n_trndata.outdim
    print "First sample (input, target, class):"
    print n_trndata['input'][0], n_trndata['target'][0], n_trndata['class'][0]

    n_fnn = buildNetwork(n_trndata.indim,n_hiddensize, n_trndata.outdim, outclass=SoftmaxLayer)
    n_trainer = BackpropTrainer(n_fnn, dataset=n_trndata, momentum=0.1, verbose=True, weightdecay=0.01)

    n_result = 1
    while n_result > 0.1:
        print n_result
        n_trainer.trainEpochs(1)
        n_trnresult = percentError(n_trainer.testOnClassData(),
                                 n_trndata['class'])
        n_tstresult = percentError(n_trainer.testOnClassData(
            dataset=n_tstdata), n_tstdata['class'])

        print "epoch: %4d" % n_trainer.totalepochs, \
            "  train error: %5.2f%%" % n_trnresult, \
            "  test error: %5.2f%%" % n_tstresult
        n_result = n_tstresult
Пример #15
0
def runTest(hidden_layer=3,
            learning_rate=0.1,
            momentum=0.5,
            epochs=5000,
            filename='RCNetwork2.xml'):
    ds = buildDataSet()
    tstdata, trndata = ds.splitWithProportion(0.25)
    trndata._convertToOneOfMany()
    tstdata._convertToOneOfMany()
    net = buildNetwork(hidden_layer)
    #define the connections
    trainer = BackpropTrainer(net,
                              dataset=trndata,
                              momentum=momentum,
                              verbose=False,
                              weightdecay=learning_rate)
    #trainer = BackpropTrainer(net, learningrate = 0.01, dataset = ds, momentum = 0.99, verbose = True)
    trainer.trainEpochs(epochs)
    trnresult = percentError(trainer.testOnClassData(), trndata['class'])
    tstresult = percentError(trainer.testOnClassData(dataset=tstdata),
                             tstdata['class'])
    print filename
    print "epoch: %4d" % trainer.totalepochs, \
          "  train error: %5.2f%%" % trnresult, \
          "  test error: %5.2f%%" % tstresult
    #trainer.train()
    print 'Final weights:', net.params

    NetworkWriter.writeToFile(net, filename)
Пример #16
0
def trainNetwork(epochs, rate, trndata, tstdata, network=None):
    '''
    epochs: number of iterations to run on dataset
    trndata: pybrain ClassificationDataSet
    tstdat: pybrain ClassificationDataSet
    network: filename of saved pybrain network, or None
    '''
    if network is None:
        net = buildNetwork(400, 25, 25, 9, bias=True, hiddenclass=SigmoidLayer, outclass=SigmoidLayer)
    else:
        net = NetworkReader.readFrom(network)

    print "Number of training patterns: ", len(trndata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim
    print "First sample input:"
    print trndata['input'][0]
    print ""
    print "First sample target:", trndata['target'][0]
    print "First sample class:", trndata.getClass(int(trndata['class'][0]))
    print ""

    trainer = BackpropTrainer(net, dataset=trndata, learningrate=rate)
    for i in range(epochs):
        trainer.trainEpochs(1)
        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

    return net
Пример #17
0
	def testBPHasLearned(self):
		trnresult = percentError(self.trainer.testOnClassData(),
			self.trn_d['class'])
		tstresult = percentError(self.trainer.testOnClassData(dataset=self.tst_d),
			self.tst_d['class'])
		print 'trn perc error', trnresult
		print 'tst perc error', tstresult
def main():
  trndata, tstdata = createDS()
  for repeat in xrange(repeats):
    iter_trn_results = []
    iter_tst_results = []
    nn = createNNLong(trndata)
    hiddenAstrocyteLayer, outputAstrocyteLayer = associateAstrocyteLayers(nn)
    trainer = BackpropTrainer(nn, dataset=trndata, learningrate=0.01,
                              momentum=0.1, verbose=False, weightdecay=0.0)
    for grand_iter in xrange(iterations):
      trainer.trainEpochs(1)
      trnresult = percentError(trainer.testOnClassData(), trndata['class'])
      iter_trn_results.append(trnresult)
      tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
      iter_tst_results.append(tstresult)
      
      if not grand_iter%20:
        print 'epoch %4d' %trainer.totalepochs, 'train error %5.2f%%' %trnresult, \
            'test error %5.2f%%' %tstresult
            
      inputs  = list(trndata['input'])
      random.shuffle(inputs)
      for inpt in trndata['input']:
        nn.activate(inpt)
        for minor_iter in range(hiddenAstrocyteLayer.astrocyte_processing_iters):
          hiddenAstrocyteLayer.update()
          outputAstrocyteLayer.update()
        hiddenAstrocyteLayer.reset()
        outputAstrocyteLayer.reset()
    all_trn_results.append(iter_trn_results)
    all_tst_results.append(iter_tst_results)
  plotResults(all_trn_results)
  plotResults(all_tst_results)
  plt.show()
Пример #19
0
	def train(self):
		print "Enter the number of times to train, -1 means train until convergence:"
		t = int(raw_input())
		print "Training the Neural Net"
		print "self.net.indim = "+str(self.net.indim)
		print "self.train_data.indim = "+str(self.train_data.indim)

		trainer = BackpropTrainer(self.net, dataset=self.train_data, momentum=0.1, verbose=True, weightdecay=0.01)
		
		if t == -1:
			trainer.trainUntilConvergence()
		else:
			for i in range(t):
				trainer.trainEpochs(1)
				trnresult = percentError( trainer.testOnClassData(), self.train_data['class'])
				# print self.test_data

				tstresult = percentError( trainer.testOnClassData(dataset=self.test_data), self.test_data['class'] )

				print "epoch: %4d" % trainer.totalepochs, \
					"  train error: %5.2f%%" % trnresult, \
					"  test error: %5.2f%%" % tstresult

				if i % 10 == 0 and i > 1:
					print "Saving Progress... Writing to a file"
					NetworkWriter.writeToFile(self.net, self.path)

		print "Done training... Writing to a file"
		NetworkWriter.writeToFile(self.net, self.path)
		return trainer
Пример #20
0
  def train(self, inputData, verbose=True):

    # Set of data to classify:
    # - IMG_SIZE input dimensions per data point
    # - 1 dimensional output
    # - 4 clusters of classification
    all_faces = ClassificationDataSet(IMG_SIZE, 1, nb_classes=4)

    for entry in inputData:
      (emotion, data) = entry
      all_faces.addSample(data, [emotion])
     
    # Generate a test and a train set from our data
    test_faces, train_faces = all_faces.splitWithProportion(0.25)

    # Hack to convert a 1-dimensional output into 4 output neurons
    test_faces._convertToOneOfMany()   
    train_faces._convertToOneOfMany()
    
    # Set up the actual network. These are the tunable params
    self.fnn = buildNetwork( 
      train_faces.indim, 
      20, 
      train_faces.outdim, 
      outclass=SoftmaxLayer
    )
    
    # Set up the network trainer. Also nice tunable params
    trainer = BackpropTrainer(
      self.fnn, 
      dataset=train_faces, 
      momentum=0.1, 
      verbose=False,
      weightdecay=0.01
    )
    
    tabledata = []     

    # Train this bitch. 
    if verbose:
      # Report after every epoch if verbose
      for i in range(EPOCHS):
        trainer.trainEpochs(1)

        trnresult = percentError( trainer.testOnClassData(),
                                  train_faces['class'] )
        tstresult = percentError( trainer.testOnClassData(
               dataset=test_faces ), test_faces['class'] )

        tabledata.append((trainer.totalepochs,trnresult,tstresult))
    else:
      trainer.trainEpochs(EPOCHS)

    if verbose:
      print "Epoch\tTrain Error\tTest Error"
      for line in tabledata:
         print "%4d\t" % line[0], \
               "%5.2f%%\t\t" % line[1], \
               "%5.2f%%" % line[2]
Пример #21
0
def train_model(net, train_ds, test_ds):

    # train model
    tstdata, trndata = test_ds, train_ds
    tstdata._convertToOneOfMany()
    trndata._convertToOneOfMany()

    print "Number of training patterns: ", len(trndata)
    print "Number of test patterns: ", len(tstdata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim

    trainer = ExtendedBackpropTrainer(net,
                                      learningrate=0.01,
                                      dataset=trndata,
                                      verbose=True)

    for i in range(20):
        trainer.trainEpochs(5)
        trnresult = percentError(trainer.testOnClassData(), trndata['class'])
        tstresult = percentError(trainer.testOnClassData(dataset=tstdata),
                                 tstdata['class'])

        # Compute ROC curve and area the curve
        probas_ = net.activateOnDataset(tstdata)
        fpr, tpr, thresholds = roc_curve(tstdata['class'], probas_[:, 1])
        roc_auc = auc(fpr, tpr)
        print "Area under the ROC curve : %f" % roc_auc

        print "epoch: %4d" % trainer.totalepochs, \
              "  train error: %5.2f%%" % trnresult, \
              "  test error: %5.2f%%" % tstresult

        guess = []
        correct = []
        count = 0
        for i in range(len(tstdata)):
            output = net.activate(tstdata['input'][i])[0]
            output = int(round(output))
            real = int(tstdata['target'][i][0])
            guess.append(output)
            correct.append(real)
            if output == real:
                count += 1
        conf_arr = np.zeros((2, 2))
        for j in range(len(guess)):
            conf_arr[guess[j]][correct[j]] += 1
        print conf_arr

    # Plot ROC curve
    pl.clf()
    pl.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
    pl.plot([0, 1], [0, 1], 'k--')
    pl.xlim([0.0, 1.0])
    pl.ylim([0.0, 1.0])
    pl.xlabel('False Positive Rate')
    pl.ylabel('True Positive Rate')
    pl.title('Receiver operating characteristic example')
    pl.legend(loc="lower right")
    pl.show()
Пример #22
0
 def estimate_error(self):
     trnerror = percentError(
         self.trainer.testOnClassData(dataset=self.trndata),
         self.trndata['class'])
     tsterror = percentError(
         self.trainer.testOnClassData(dataset=self.tstdata),
         self.tstdata['class'])
     return self.trainer.totalepochs, trnerror, tsterror
Пример #23
0
 def testBPHasLearned(self):
     trnresult = percentError(self.trainer.testOnClassData(),
                              self.trn_d['class'])
     tstresult = percentError(
         self.trainer.testOnClassData(dataset=self.tst_d),
         self.tst_d['class'])
     print 'trn perc error', trnresult
     print 'tst perc error', tstresult
Пример #24
0
 def score_train_test(self, trainer=None):
     self.test_result = percentError(
         trainer.testOnClassData(dataset=self.test), self.test['class'])
     logger.info(
         'test error result: {result}'.format(result=self.test_result))
     self.train_result = percentError(
         trainer.testOnClassData(dataset=self.train), self.train['class'])
     logger.info(
         'train error result: {result}'.format(result=self.train_result))
	def livetest(self,data):
		trainer, net = self.unpickleModel()
		testData = ClassificationDataSet(103, 1, nb_classes=9)
		testData.addSample(data[0],1);
		testData._convertToOneOfMany()
		out = net.activateOnDataset(testData).argmax(axis=1)
		percentError(out, testData['class'])
		print self.labelToLetter[str(out[0])]
		return self.labelToLetter[str(out[0])]
Пример #26
0
def train_model(net, train_ds, test_ds):

    # train model
    tstdata, trndata = test_ds, train_ds
    tstdata._convertToOneOfMany()
    trndata._convertToOneOfMany()

    print "Number of training patterns: ", len(trndata)
    print "Number of test patterns: ", len(tstdata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim

    trainer = ExtendedBackpropTrainer(net, learningrate=0.01, dataset=trndata, verbose=True)

    for i in range(20):
        trainer.trainEpochs(5)
        trnresult = percentError( trainer.testOnClassData(),
                              trndata['class'] )
        tstresult = percentError( trainer.testOnClassData(
               dataset=tstdata ), tstdata['class'] )

        # Compute ROC curve and area the curve
        probas_ = net.activateOnDataset(tstdata)
        fpr, tpr, thresholds = roc_curve(tstdata['class'], probas_[:, 1])
        roc_auc = auc(fpr, tpr)
        print "Area under the ROC curve : %f" % roc_auc

        print "epoch: %4d" % trainer.totalepochs, \
              "  train error: %5.2f%%" % trnresult, \
              "  test error: %5.2f%%" % tstresult

        guess = []
        correct = []
        count = 0
        for i in range(len(tstdata)):
            output = net.activate(tstdata['input'][i])[0]
            output = int(round(output))
            real = int(tstdata['target'][i][0])
            guess.append(output)
            correct.append(real)
            if output == real:
                count += 1
        conf_arr = np.zeros((2, 2))
        for j in range(len(guess)):
            conf_arr[guess[j]][correct[j]] += 1
        print conf_arr
    
    # Plot ROC curve
    pl.clf()
    pl.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
    pl.plot([0, 1], [0, 1], 'k--')
    pl.xlim([0.0, 1.0])
    pl.ylim([0.0, 1.0])
    pl.xlabel('False Positive Rate')
    pl.ylabel('True Positive Rate')
    pl.title('Receiver operating characteristic example')
    pl.legend(loc="lower right")
    pl.show()
Пример #27
0
def run_epoch(trainer, trndata, tstdata):
    trainer.trainEpochs( 1 )
    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
    print
def runNeuralLearningCurveSimulation(dataTrain, dataTest, train_tfidf, test_tfidf, outFile):
    print 'running neural learning curve'
    outFile.write('-------------------------------------\n')
    outFile.write('train==> %d, %d \n'%(train_tfidf.shape[0],train_tfidf.shape[1]))
    outFile.write('test==>  %d, %d \n'%(test_tfidf.shape[0],test_tfidf.shape[1]))
    
    trainDS = getDataSetFromTfidf(train_tfidf, dataTrain.target)
    testDS = getDataSetFromTfidf(test_tfidf, dataTest.target)
    
    print "Number of training patterns: ", len(trainDS)
    print "Input and output dimensions: ", trainDS.indim, trainDS.outdim
    print "First sample (input, target, class):"
    print len(trainDS['input'][0]), trainDS['target'][0], trainDS['class'][0]
    '''
    with SimpleTimer('time to train', outFile):
        net = buildNetwork(trainDS.indim, trainDS.indim/2, trainDS.indim/4, trainDS.indim/8, trainDS.indim/16, 2, hiddenclass=TanhLayer, outclass=SoftmaxLayer)
        trainer = BackpropTrainer( net, dataset=trainDS, momentum=0.1, verbose=True, weightdecay=0.01, batchlearning=True)
    '''
    net = RecurrentNetwork()
    net.addInputModule(LinearLayer(trainDS.indim, name='in'))
    net.addModule(SigmoidLayer(trainDS.indim/2, name='hidden'))
    net.addModule(SigmoidLayer(trainDS.indim/4, name='hidden2'))
    net.addOutputModule(SoftmaxLayer(2, name='out'))
    net.addConnection(FullConnection(net['in'], net['hidden'], name='c1'))
    net.addConnection(FullConnection(net['hidden'], net['out'], name='c2'))
    net.addRecurrentConnection(FullConnection(net['hidden'], net['hidden'], name='c3'))
    net.addRecurrentConnection(FullConnection(net['hidden2'], net['hidden'], name='c4'))
    net.sortModules()
    trainer = BackpropTrainer( net, dataset=trainDS, momentum=0.01, verbose=True, weightdecay=0.01)
    
    outFile.write('%s \n' % (net.__str__()))
    epochs = 200
    with SimpleTimer('time to train %d epochs' % epochs, outFile):
        for i in range(epochs):
            trainer.trainEpochs(1)
            trnresult = percentError( trainer.testOnClassData(),
                                  trainDS['class'] )
            tstresult = percentError( trainer.testOnClassData(
               dataset=testDS ), testDS['class'] )
    
            print "epoch: %4d" % trainer.totalepochs, \
                  "  train error: %5.2f%%" % trnresult, \
                  "  test error: %5.2f%%" % tstresult
                  
    outFile.write('%5.2f , %5.2f \n' % (100.0-trnresult, 100.0-tstresult))
                  
    predicted = trainer.testOnClassData(dataset=testDS)
    results = predicted == testDS['class'].flatten()
    wrong = []
    for i in range(len(results)):
        if not results[i]:
            wrong.append(i)
    print 'classifier got these wrong:'
    for i in wrong[:10]:
        print dataTest.data[i], dataTest.target[i]
        outFile.write('%s %d \n' % (dataTest.data[i], dataTest.target[i]))
Пример #29
0
 def __trainNetwork(self,trndata,tstdata):
     
     self.fnn = buildNetwork( trndata.indim, 2, trndata.outdim, outclass=SigmoidLayer )
     trainer = BackpropTrainer( self.fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01,learningrate=0.01)
     
     for i in range(self.iterations):
         trainer.trainEpochs( 1 )
         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
Пример #30
0
def main():
    # Get Data
    dataSets = genfromtxt('normalizedData.csv', delimiter=',')
    alldata = ClassificationDataSet(13, 1, nb_classes=3)
    for dataSet in dataSets:
        alldata.addSample(dataSet[1:14], int(dataSet[0]) - 1)

    # Split the data
    tstdata_temp, trndata_temp = alldata.splitWithProportion(0.25)
    tstdata = ClassificationDataSet(13, 1, nb_classes=3)
    for n in range(0, tstdata_temp.getLength()):
        tstdata.addSample(
            tstdata_temp.getSample(n)[0],
            tstdata_temp.getSample(n)[1])
    trndata = ClassificationDataSet(13, 1, nb_classes=3)
    for n in range(0, trndata_temp.getLength()):
        trndata.addSample(
            trndata_temp.getSample(n)[0],
            trndata_temp.getSample(n)[1])
    trndata._convertToOneOfMany()
    tstdata._convertToOneOfMany()

    # Build Network
    fnn = buildNetwork(trndata.indim, 4, 4, 4, trndata.outdim)

    # Construct Trainer
    trainer = BackpropTrainer(fnn, trndata, learningrate=0.1)

    # Train
    while True:
        trainer.trainEpochs(1)
        trnresult = percentError(trainer.testOnClassData(), trndata['class'])
        print("Training Test Error: %5.2f%%" % trnresult)
        if trnresult < 1:
            break

    tstresult = percentError(trainer.testOnClassData(dataset=tstdata),
                             tstdata['class'])
    print("test error: %5.2f%%" % tstresult)
    out1 = fnn.activate([
        0.70789474, 0.13636364, 0.60962567, 0.31443299, 0.41304348, 0.83448276,
        0.70253165, 0.11320755, 0.51419558, 0.47098976, 0.33333333, 0.58608059,
        0.71825963
    ])
    out2 = fnn.activate([
        0.26578947, 0.70355731, 0.54545455, 0.58762887, 0.10869565, 0.3862069,
        0.29746835, 0.54716981, 0.29652997, 0.11262799, 0.25203252, 0.47619048,
        0.21540656
    ])
    out3 = fnn.activate([
        0.81578947, 0.66403162, 0.73796791, 0.71649485, 0.2826087, 0.36896552,
        0.08860759, 0.81132075, 0.29652997, 0.67576792, 0.10569106, 0.12087912,
        0.20114123
    ])
    print(out1, out2, out3)
Пример #31
0
def trainNN(data: list, targets: list, seed):
    """
    Trains a neural network
    """
    X_tweet_counts = count_vect.fit_transform(data)

    # Compute term frequencies and store in X_train_tf
    # Compute tfidf feature values and store in X_train_tfidf
    X_train_tfidf = tfidf_transformer.fit_transform(X_tweet_counts)
    arr = X_train_tfidf.toarray()

    trainingdata = arr[:int(.75 * len(arr))]
    testdata = arr[int(.75 * len(arr)):]
    trainingtargets = targets[:int(.75 * len(targets))]
    testtargets = targets[int(.75 * len(targets)):]

    trainingds = ClassificationDataSet(len(arr[0]), 1, nb_classes=2)
    testds = ClassificationDataSet(len(arr[0]), 1, nb_classes=2)

    for index, data in enumerate(trainingdata):
        trainingds.addSample(data, trainingtargets[index])
    for index, data in enumerate(testdata):
        testds.addSample(data, testtargets[index])

    trainingds._convertToOneOfMany()
    testds._convertToOneOfMany()

    net = buildNetwork(trainingds.indim, 10, 10, 10, trainingds.outdim, outclass=SoftmaxLayer)
    trainer = BackpropTrainer(net, dataset=trainingds, learningrate=.65, momentum=.1)

    besttrain = 99.9
    besttest = 99.9
    bestresults = []
    bestclass = []

    for i in range(20):
        trainer.trainEpochs(1)
        trainresult = percentError(trainer.testOnClassData(), trainingds['class'])
        teststuff = trainer.testOnClassData(dataset=testds)
        testresult = percentError(teststuff, testds['class'])
        if testresult < besttest:
            besttest = testresult
            besttrain = trainresult
            bestresults = teststuff
            bestclass = testds['class']

        print("epoch: %2d" % trainer.totalepochs)
        print("train error: %2.2f%%" % trainresult)
        print("test error: %2.2f%%" % testresult)
    print("Best test error accuracy: {:.2f}%".format(besttest))
    print("Best test error f1 score: {:.4f}%".format(f1_score(bestclass, bestresults, average='macro')))
    print("Confusion Matrix:")
    print(confusion_matrix(bestclass, bestresults))

    return besttest
def main():
    means = [(-1,0),(2,4),(3,1)]
    cov = [diag([1,1]), diag([0.5,1.2]), diag([1.5,0.7])]
    alldata = ClassificationDataSet(2, 1, nb_classes=3)
    for n in xrange(400):
        for klass in range(3):
            input = multivariate_normal(means[klass],cov[klass])
            alldata.addSample(input, [klass])
    tstdata, trndata = alldata.splitWithProportion( 0.25 )
    trndata._convertToOneOfMany( )
    tstdata._convertToOneOfMany( )
    print "Number of training patterns: ", len(trndata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim
    print "First sample (input, target, class):"
    print trndata['input'][0], trndata['target'][0], trndata['class'][0]
    
    fnn = buildNetwork( trndata.indim, 5, trndata.outdim, outclass=SoftmaxLayer )
    trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
    ticks = arange(-3.,6.,0.2)
    X, Y = meshgrid(ticks, ticks)
    # need column vectors in dataset, not arrays
    griddata = ClassificationDataSet(2,1, nb_classes=3)
    for i in xrange(X.size):
        griddata.addSample([X.ravel()[i],Y.ravel()[i]], [0])
    griddata._convertToOneOfMany()  # this is still needed to make the fnn feel comfy
    
    for i in range(20):
        trainer.trainEpochs(1)
    
        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
        
        out = fnn.activateOnDataset(griddata)
        out = out.argmax(axis=1)  # the highest output activation gives the class
        out = out.reshape(X.shape)
        figure(1)
        ioff()  # interactive graphics off
        clf()   # clear the plot
        hold(True) # overplot on
        for c in [0,1,2]:
            here, _ = where(tstdata['class']==c)
            plot(tstdata['input'][here,0],tstdata['input'][here,1],'o')
        if out.max()!=out.min():  # safety check against flat field
            contourf(X, Y, out)   # plot the contour
        ion()   # interactive graphics on
        draw()  # update the plot
        
    ioff()
    show()
Пример #33
0
def basicneuralnetwork(number_of_hidden_nodes, weightdecay, layers, alldata):
    '''
    This is a dataset 
    first argument is the dimension of the input
     second argument is dimension of the output
    '''  

    nr_of_iterations = 10
    # Construct neural network
    print "Constructing network"
    

    print "  (number_of_hidden_nodes : %s, weight decay : %s, layers: %s)" % (number_of_hidden_nodes, weightdecay, layers)

    train_results   = []
    test_results    = []
    neural_networks = []
    for i in  xrange(1,nr_of_iterations+1):
        print "Iteration %d" % i

        # Prepare the data
        tstdata, trndata = alldata.splitWithProportion( 0.15 )
        trndata._convertToOneOfMany()
        tstdata._convertToOneOfMany()
        # construct a neural network
        fnn = construct_neural_network(number_of_hidden_nodes, layers, trndata.indim, trndata.outdim)
        trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, verbose=False, weightdecay=weightdecay)
        #early stopping validation set = 0.25
        trainer.trainUntilConvergence(continueEpochs=5)   
        train_results.append(percentError( trainer.testOnClassData(), trndata['class']))
        test_results.append(percentError( trainer.testOnClassData(dataset=tstdata ), tstdata['class']))
        neural_networks.append(fnn)
        global activation_samples
        for sample in activation_samples:
            print "The activation sample: %s"%sample
            print "The output :%s"%fnn.activate(sample)
            print "Weight:%s , %s"%(fnn['in'].outputbuffer[fnn['in'].offset],fnn['hidden0'].outputbuffer[fnn['hidden0'].offset])
        log_debug(trndata.indim,number_of_hidden_nodes, weightdecay,train_results[-1],test_results[-1])         
            
    # Compute means
    mean_train_error    = sum(train_results)/len(train_results)
    mean_test_error     = sum(test_results)/len(test_results)

    # Compute optimal network configuration
    optimal_test_error  = min(test_results)
    optimal_index       = test_results.index(optimal_test_error)

    # Save the optimal configuration to the file system
    import os
    neuralnetwork = os.path.join('networks', 'neuralHiddenNode%sdecay%s'%(number_of_hidden_nodes, weightdecay))
    fileObject = open(neuralnetwork, 'w')
    pickle.dump(neural_networks[optimal_index], fileObject)
    fileObject.close()

    return (mean_train_error, mean_test_error)
Пример #34
0
 def train_the_network(self,no_hidden1,no_hidden2):
     input_neurons = len(self.feature_matrix[self.classes[0]][0])
     self.fnet = buildNetwork(input_neurons,no_hidden1,no_hidden2,2)
     self.trainer = BackpropTrainer(self.fnet,self.traindata)
     for i in range(125):
         error = self.trainer.train()
         #print('%d iteratrions of training completed' % i)
         #error = trainer.train()
         trnresult = percentError(self.trainer.testOnClassData(), self.traindata['class'])
         tstresult = percentError(self.trainer.testOnClassData(dataset=self.testdata),self.testdata['class'])
         print "epoch: %4d" % self.trainer.totalepochs," train error: %5.2f%%" % trnresult, " test error: %5.2f%%" % tstresult
def runNeuralSimulation(dataTrain, dataTest, train_tfidf, test_tfidf):
    outFile = open('neuralLog.txt','a')
    outFile.write('-------------------------------------\n')
    outFile.write('train==> %d, %d \n'%(train_tfidf.shape[0],train_tfidf.shape[1]))
    outFile.write('test==>  %d, %d \n'%(test_tfidf.shape[0],test_tfidf.shape[1]))
    
    trainDS = getDataSetFromTfidf(train_tfidf, dataTrain.target)
    testDS = getDataSetFromTfidf(test_tfidf, dataTest.target)
    
    print "Number of training patterns: ", len(trainDS)
    print "Input and output dimensions: ", trainDS.indim, trainDS.outdim
    print "First sample (input, target, class):"
    print len(trainDS['input'][0]), trainDS['target'][0], trainDS['class'][0]
    
#     with SimpleTimer('time to train', outFile):
#         net = buildNetwork(trainDS.indim, trainDS.indim/2, trainDS.indim/4, trainDS.indim/8, trainDS.indim/16, 2, hiddenclass=TanhLayer, outclass=SoftmaxLayer)
#         trainer = BackpropTrainer( net, dataset=trainDS, momentum=0.1, verbose=True, weightdecay=0.01, batchlearning=True)
    net = RecurrentNetwork()
    net.addInputModule(LinearLayer(trainDS.indim, name='in'))
    net.addModule(SigmoidLayer(trainDS.indim/2, name='hidden'))
    net.addModule(SigmoidLayer(trainDS.indim/4, name='hidden2'))
    net.addOutputModule(SoftmaxLayer(2, name='out'))
    net.addConnection(FullConnection(net['in'], net['hidden'], name='c1'))
    net.addConnection(FullConnection(net['hidden'], net['out'], name='c2'))
    net.addRecurrentConnection(FullConnection(net['hidden'], net['hidden'], name='c3'))
    net.addRecurrentConnection(FullConnection(net['hidden2'], net['hidden'], name='c4'))
    net.sortModules()
    trainer = BackpropTrainer( net, dataset=trainDS, momentum=0.01, verbose=True, weightdecay=0.01)
    
    outFile.write('%s \n' % (net.__str__()))
    epochs = 2000
    with SimpleTimer('time to train %d epochs' % epochs, outFile):
        for i in range(epochs):
            trainer.trainEpochs(1)
            trnresult = percentError( trainer.testOnClassData(),
                                  trainDS['class'] )
            tstresult = percentError( trainer.testOnClassData(
               dataset=testDS ), testDS['class'] )
    
            print "epoch: %4d" % trainer.totalepochs, \
                  "  train error: %5.2f%%" % trnresult, \
                  "  test error: %5.2f%%" % tstresult
            outFile.write('%5.2f , %5.2f \n' % (100.0-trnresult, 100.0-tstresult))
                  
    predicted = trainer.testOnClassData(dataset=testDS)
    results = predicted == testDS['class'].flatten()
    wrong = []
    for i in range(len(results)):
        if not results[i]:
            wrong.append(i)
    print 'classifier got these wrong:'
    for i in wrong[:10]:
        print dataTest.data[i], dataTest.target[i]
        outFile.write('%s %d \n' % (dataTest.data[i], dataTest.target[i]))
Пример #36
0
def perceptron(hidden_neurons=5, weightdecay=0.01, momentum=0.1):
    INPUT_FEATURES = 41
    CLASSES = 10
    HIDDEN_NEURONS = hidden_neurons
    WEIGHTDECAY = weightdecay
    MOMENTUM = momentum

    # Generate the labeled set
    g = load_data()

    #g = generate_data2()
    alldata = g['d']
    minX, maxX, minY, maxY = g['minX'], g['maxX'], g['minY'], g['maxY']

    # Split data into test and training dataset
    tstdata, trndata = alldata.splitWithProportion(0.25)

    trndata._convertToOneOfMany()  # This is necessary, but I don't know why
    tstdata._convertToOneOfMany()  # http://stackoverflow.com/q/8154674/562769

    print("Number of training patterns: %i" % len(trndata))
    print("Input and output dimensions: %i, %i" % (trndata.indim,
                                                   trndata.outdim))
    print("Hidden neurons: %i" % HIDDEN_NEURONS)
    print("First sample (input, target, class):")

    #print(trndata['input'][0], trndata['target'][0], trndata['class'])

    fnn = buildNetwork(trndata.indim, HIDDEN_NEURONS, trndata.outdim,bias=True)
                       #outclass=SoftmaxLayer)

    trainer = BackpropTrainer(fnn, dataset=trndata, momentum=MOMENTUM,
                              verbose=True, weightdecay=WEIGHTDECAY)

     # Visualization

    for i in range(20):
        trainer.trainEpochs(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)


        test = [0]*41
        test[0],test[3],test[40],test[39],test[38],test[37],test[36] = 1,1,1,1,1,1,1
        print fnn.activate(test)

    return fnn
    """
def main():
    trndata, tstdata = createDS()
    for repeat in xrange(repeats):
        iter_trn_results = []
        iter_tst_results = []
        nn = createNN(4, 6, 3)
        nn.randomize()
        hiddenAstrocyteLayer, outputAstrocyteLayer = associateAstrocyteLayers(nn)
        trainer = BackpropTrainer(nn, dataset=trndata, learningrate=0.01,
                                  momentum=0.1, verbose=False, weightdecay=0.0)
        for grand_iter in xrange(iterations):
            if grand_iter ==0:
                trainer.train()
            
            if grand_iter > 1000 and grand_iter < 2000:
                inputs  = list(trndata['input'])
                random.shuffle(inputs)
                for inpt in trndata['input']:
                    nn.activate(inpt)
                    for minor_iter in range(hiddenAstrocyteLayer.astrocyte_processing_iters):
                        hiddenAstrocyteLayer.update()
                        outputAstrocyteLayer.update()
                    hiddenAstrocyteLayer.reset()
                    outputAstrocyteLayer.reset()
    
    
            trainer.train()
          
            trnresult = percentError(trainer.testOnClassData(), trndata['class'])
            iter_trn_results.append(trnresult)
            tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
            iter_tst_results.append(tstresult)
    
            if not grand_iter%100:
                print 'epoch %4d' %trainer.totalepochs, 'train error %5.2f%%' %trnresult, \
                    'test error %5.2f%%' %tstresult
          
          
            all_trn_results.append(iter_trn_results)
            all_tst_results.append(iter_tst_results)

    path = '/home/david/Dropbox/programming/python/ann/mypybrain/'
    f = plt.figure(figsize=(10,5))
    plotErrorBar(all_trn_results)
    plotErrorBar(all_tst_results)
    plt.legend(('Training', 'Test'))
    f.savefig(path+'angn_bpThenAstro_error_bar.svg', format='svg')
    f = plt.figure(figsize=(10,5))
    side_text = 'Trials='+str(repeats)+'\n\nFinal\ntrain\nerror\n'
    plotNoErrorBar(all_trn_results, label=side_text, xytxt=(1.02, 0.7))
    plotNoErrorBar(all_tst_results, label='Final\ntest\nerror\n', xytxt=(1.02, 0.4))
    plt.legend(('Training', 'Test'))
    f.savefig(path+'angn_bpThenAstro_no_error_bar.svg', format='svg')
    plt.close()
def main():
    trndata, tstdata = createDS()
    for repeat in xrange(repeats):
        iter_trn_results = []
        iter_tst_results = []
        nn = createNN(4, 6, 3)
        nn.randomize()
        print nn.params
        hiddenAstrocyteLayer, outputAstrocyteLayer = associateAstrocyteLayers(nn)
        trainer = BackpropTrainer(nn, dataset=trndata, learningrate=0.01,
                                  momentum=0.1, verbose=False, weightdecay=0.0)
        for grand_iter in xrange(iterations):
            if grand_iter == 0:
                trainer.train()
            
            if grand_iter > iterations/3 and grand_iter < iterations/3:
                inputs  = trndata['input'][:]
                random.shuffle(inputs)
                for inpt in trndata['input']:
                    nn.activate(inpt)
                    for minor_iter in range(hiddenAstrocyteLayer.astrocyte_processing_iters):
                        hiddenAstrocyteLayer.update()
                        outputAstrocyteLayer.update()
                    hiddenAstrocyteLayer.reset()
                    outputAstrocyteLayer.reset()
    
            trainer.train()
          
            trnresult = percentError(trainer.testOnClassData(), trndata['class'])
            iter_trn_results.append(trnresult)
            tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class'])
            iter_tst_results.append(tstresult)
    
            if not grand_iter%100:
                print 'epoch %4d' %trainer.totalepochs, 'train error %5.2f%%' %trnresult, \
                    'test error %5.2f%%' %tstresult
          
          
            all_trn_results.append(iter_trn_results)
            all_tst_results.append(iter_tst_results)

    path = '/home/david/Dropbox/programming/python/ANN (Case Conflict 1)/pybrain/'
    f = plt.figure(figsize=(10,5))
    plotErrorBar(all_trn_results)
    plotErrorBar(all_tst_results)
    plt.legend(('Training', 'Test'))
    f.savefig(path+'angn_bpThenAstro_error_bar.svg', format='svg')
    f = plt.figure(figsize=(10,5))
    side_text = 'Trials='+str(repeats)+'\n\nFinal\ntrain\nerror\n'
    plotNoErrorBar(all_trn_results, label=side_text, xytxt=(1.02, 0.7))
    plotNoErrorBar(all_tst_results, label='Final\ntest\nerror\n', xytxt=(1.02, 0.4))
    plt.legend(('Training', 'Test'))
    f.savefig(path+'angn_bpThenAstro_no_error_bar.svg', format='svg')
    plt.close()
def mlpClassifier(X,y,train_indices, test_indices, mom=0.1,weightd=0.01, epo=5):
    X_train, y_train, X_test, y_test = X[train_indices],y[train_indices], X[test_indices], y[test_indices]

    #Converting the data into a dataset which is easily understood by PyBrain. 
    tstdata = ClassificationDataSet(X.shape[1],target=1,nb_classes=8)
    trndata = ClassificationDataSet(X.shape[1],target=1,nb_classes=8)
 #   print "shape of X_train & y_train: " + str(X_train.shape) + str(y_train.shape)
    for i in range(y_train.shape[0]):
        trndata.addSample(X_train[i,:], y_train[i])
    for i in range(y_test.shape[0]):
        tstdata.addSample(X_test[i,:], y_test[i])
    trndata._convertToOneOfMany()
    tstdata._convertToOneOfMany()

    #printing the specs of data
#    print "Number of training patterns: ", len(trndata)
#    print "Input and output dimensions: ", trndata.indim, trndata.outdim
#    print "First sample (input, target, class):"
#    print trndata['input'][0], trndata['target'][0], trndata['class'][0]

    #The neural-network used
 #   print "Building Network..."
    #input layer, hidden layer of size 10(very small), output layer
    ANNc = FeedForwardNetwork()
    inLayer = LinearLayer(trndata.indim, name="ip")
    hLayer1 = TanhLayer(100, name = "h1")
    hLayer2 = SigmoidLayer(100, name = "h2")
    outLayer = SoftmaxLayer(trndata.outdim, name = "op")

    ANNc.addInputModule(inLayer)
    ANNc.addModule(hLayer1)
    ANNc.addModule(hLayer2)
    ANNc.addOutputModule(outLayer)

    ip_to_h1 = FullConnection(inLayer, hLayer1, name = "ip->h1")
    h1_to_h2 = FullConnection(hLayer1, hLayer2, name = "h1->h2")
    h2_to_op = FullConnection(hLayer2, outLayer, name = "h2->op")

    ANNc.addConnection(ip_to_h1)
    ANNc.addConnection(h1_to_h2)
    ANNc.addConnection(h2_to_op)
    ANNc.sortModules()

#    print "Done. Training the network."

    #The trainer used, in our case Back-propagation trainer
    trainer = BackpropTrainer( ANNc, dataset=trndata, momentum=mom, verbose=True, weightdecay=weightd)
    trainer.trainEpochs( epo )

    #The error
    trnresult = percentError( trainer.testOnClassData(dataset=trndata), trndata['class'] )
    tstresult = percentError( trainer.testOnClassData(dataset=tstdata ), tstdata['class'] )
 #   print "Done."
    return ANNc, trainer.totalepochs, (100 - trnresult), (100 - tstresult) 
Пример #40
0
def EvaluateArtificialNeuralNetwork(training_data, Input_features, Output_feature, NUMBER_CLASSES, HIDDEN_NEURONS, NUMBER_LAYERS, dataset_name, ParameterVal):

	X = training_data[Input_features]
	Y = training_data[Output_feature]

	ds = ClassificationDataSet(X.shape[1], nb_classes=NUMBER_CLASSES)

	for k in xrange(len(X)): 
		ds.addSample((X.ix[k,:]), Y.ix[k,:])

	tstdata_temp, trndata_temp = ds.splitWithProportion(.25)

	tstdata = ClassificationDataSet(X.shape[1], nb_classes=NUMBER_CLASSES)
	for n in xrange(0, tstdata_temp.getLength()):
		tstdata.addSample( tstdata_temp.getSample(n)[0], tstdata_temp.getSample(n)[1] )

	trndata = ClassificationDataSet(X.shape[1], nb_classes=NUMBER_CLASSES)
	for n in xrange(0, trndata_temp.getLength()):
		trndata.addSample( trndata_temp.getSample(n)[0], trndata_temp.getSample(n)[1] )

	if NUMBER_CLASSES > 1:
		trndata._convertToOneOfMany( )
		tstdata._convertToOneOfMany( )

	'''*****Actual computation with one layer and HIDDEN_NEURONS number of neurons********'''

	fnn = buildNetwork( trndata.indim, HIDDEN_NEURONS , trndata.outdim, outclass=SoftmaxLayer )

	trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, verbose=False, weightdecay=0.01)

	trainer.trainUntilConvergence(maxEpochs=3)

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

	print ("Accuracy with Artificial Neural Network: epoch: " + str(trainer.totalepochs) + "  TrainingSet:" + str(1-trnresult/100) + "  TestSet:" + str(1-tstresult/100))

	'''****** Graphical Representation*****'''

	'''tot_hidden_tests, X_train, X_test, Y_train, Y_test, training_error, test_error = InitiateErrorCalcData(ParameterVal, training_data[Input_features], training_data[Output_feature])

	for  hidden_unit in tot_hidden_tests:
		print ("Computing hidden unit :" + str(hidden_unit))
		model = buildNetwork( trndata.indim, hidden_unit , trndata.outdim, outclass=SoftmaxLayer )
		temp_trainer = BackpropTrainer( model, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01)
		temp_trainer.trainUntilConvergence(maxEpochs=3)
		training_error.append(MSE( temp_trainer.testOnClassData(), trndata['class'] ))
		test_error.append(MSE( temp_trainer.testOnClassData(dataset=tstdata ), tstdata['class'] ))

	PlotErrors(tot_hidden_tests, training_error, test_error, dataset_name, "Number of Hidden Units for single layer ANN", "MSE")'''

	'''*****Graphical representation with multiple layers and HIDDEN_NEURONS number of neurons********'''

	'''ffn = FeedForwardNetwork()
Пример #41
0
def run_neural_network(xn, xt, yl, epochs, hidden_neurons):

    f = open('total_list_probe_mac_all.txt', 'rb')
    role_list = pickle.load(f)

    teacher_list = [a for a in role_list if a[0] == 1]
    student_list = [a for a in role_list if a[0] == 0]
    print 'len st: ', len(student_list), 'len te: ', len(teacher_list)

    student_list = random.sample(student_list, len(teacher_list))
    total_list = student_list + teacher_list
    shuffle(total_list)

    alldata = ClassificationDataSet(72, 1, nb_classes=2)

    for (o, i) in total_list:
        alldata.addSample(i, o)

    tstdata, trndata = alldata.splitWithProportion(0.25)

    trndata._convertToOneOfMany()
    tstdata._convertToOneOfMany()

    print "Number of training patterns: ", len(trndata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim
    print "First sample (input, target, class):"
    print trndata['input'][0], trndata['target'][0], trndata['class'][0]

    fnn = buildNetwork(trndata.indim,
                       hidden_neurons,
                       trndata.outdim,
                       outclass=SoftmaxLayer)
    trainer = BackpropTrainer(fnn,
                              trndata,
                              momentum=0.1,
                              verbose=True,
                              weightdecay=0.01)

    for i in range(epochs):
        trnresult = percentError(trainer.testOnClassData(), trndata['class'])
        tstresult = percentError(trainer.testOnClassData(dataset=tstdata),
                                 tstdata['class'])

        trainer.trainEpochs(1)

        xn.append(trnresult)
        xt.append(tstresult)
        yl.append(i)

        print "epoch: %4d" % trainer.totalepochs, \
              "  train error: %5.2f%%" % trnresult, \
              "  test error: %5.2f%%" % tstresult
Пример #42
0
    def validateModel(self):
        trnresult = percentError(self.trainer.testOnClassData(dataset=self.trainDataSet), self.trainDataSet['class'])
        tstresult = percentError(self.trainer.testOnClassData(dataset=self.testDataSet), self.testDataSet['class'])
        print "epoch: %4d" % self.trainer.totalepochs, \
              "  train error: %5.2f%%" % trnresult, \
              "  test error: %5.2f%%" % tstresult

        attributeSet = np.c_[self.flattenNumericalData, self.flattenCategoryData]
        for row in range(len(attributeSet)):
            rowData = attributeSet[row]
            tar = self.net.activate(rowData)
            score = self.getAlignedScore(tar)
            print tar, ': ', np.argmax(tar), ': ', self.flattenTargetDataConverted[row], ': ', score, ': ', self.flattenTargetData[row], ': ', np.abs(score-self.flattenTargetData[row])*1.0 / self.flattenTargetData[row]
Пример #43
0
def classif():
    means = [(-1, 0), (2, 4), (3, 1)]
    cov = [diag([1, 1]), diag([0.5, 1.2]), diag([1.5, 0.7])]
    alldata = ClassificationDataSet(2, 1, nb_classes=3)
    for n in xrange(400):
        for klass in range(3):
            input = multivariate_normal(means[klass], cov[klass])
            alldata.addSample(input, [klass])

    tstdata, trndata = alldata.splitWithProportion(0.25)

    trndata._convertToOneOfMany()
    tstdata._convertToOneOfMany()

    print "Number of training patterns: ", len(trndata)
    print "Input and output dimensions: ", trndata.indim, trndata.outdim
    print "First sample (input, target, class):"
    print trndata['input'][0], trndata['target'][0], trndata['class'][0]
    fnn = buildNetwork(trndata.indim, 5, trndata.outdim, outclass=SoftmaxLayer)
    trainer = BackpropTrainer(fnn,
                              dataset=trndata,
                              momentum=0.1,
                              verbose=True,
                              weightdecay=0.01)
    ticks = arange(-3., 6., 0.2)
    X, Y = meshgrid(ticks, ticks)
    # need column vectors in dataset, not arrays
    griddata = ClassificationDataSet(2, 1, nb_classes=3)
    for i in xrange(X.size):
        griddata.addSample([X.ravel()[i], Y.ravel()[i]], [0])
    griddata._convertToOneOfMany(
    )  # this is still needed to make the fnn feel comfy

    for i in range(20):
        trainer.trainEpochs(5)

        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

        out = fnn.activateOnDataset(griddata)
        out = out.argmax(
            axis=1)  # the highest output activation gives the class
        out = out.reshape(X.shape)
        print out
Пример #44
0
 def testPercentErrorIsSame(self):
     NN.pat = zip(self.trn_d['input'], self.trn_d['target'])
     pyb_ws = self.net.params.copy()
     nn = NN()
     nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
     nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
     correct = 0
     wrong = 0
     argmax_cor = 0
     argmax_wng = 0
     all_aos = []
     for i, x in enumerate(self.trn_d['input']):
         nn.activate(x)
         out = self.net.activate(x)
         # print 'ga bp trg', nn.ao, out, self.trn_d['target'][i], '++++' if not (out - self.trn_d['target'][i]).any() else '-'
         all_aos.append(nn.ao.copy())
         if not (out - self.trn_d['target'][i]).any():
             correct += 1
         else:
             wrong += 1
         if argmax(out) == argmax(self.trn_d['target'][i]):
             argmax_cor += 1
         else:
             argmax_wng += 1
     print 'actual', wrong, 'wrong', correct, 'correct', float(wrong) / (
         wrong + correct) * 100
     print 'using argmax', argmax_wng, 'wrong', argmax_cor, 'correct', float(
         argmax_wng) / (argmax_wng + argmax_cor) * 100
     argmax_perc_err = float(argmax_wng) / (argmax_wng + argmax_cor) * 100
     res = nn.sumErrors()
     nn_perc_err = 100 - res[1]
     pb_nn_perc_err = percentError(self.trainer.testOnClassData(),
                                   self.trn_d['class'])
     self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err)
     self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err, argmax_perc_err)
Пример #45
0
def one_iteration():

    print "Training"
    trainer.trainEpochs(1)

    print "Testing on testing dataset"

    tstresult = percentError(trainer.testOnClassData(dataset = test_dataset),test_dataset['class'])
    print "Error %5.2f" % tstresult

    rsamples = [get_random_sample_with_label() for i in range(10)]
        
    rsample,  rlabel = zip(*rsamples)

    with open("nn" + str(time.time()), 'w') as nndump:

        pickle.dump(network, nndump)

    out = network.activateOnDataset(create_dataset(rsample, rlabel))

    print out,  rlabel
    for sample, label, prediction in zip(rsample, rlabel, out.argmax(axis =1)):
        
    
        print_sample(sample)
        
        print "Sample labeled as ",  label

        print "Neural network says it's a ", prediction
Пример #46
0
def fnn3(frame,string):
    df=frame.dropna(axis=0,how='any')
    name=string
    nm1=name+"tst1.png"
    nm2=name+"cov1.png"
    x=df.drop(columns=['gvkey','date','price'])
    x=np.array(x)
    x=normalize(x, axis=0, norm='max')
    y=np.array(df['ret'])
    x=np.delete(x,1,axis=1)
    xdim=x.shape[1]
    ydim=1
    DS=SupervisedDataSet(xdim,ydim)
    for i in range(len(x)):
        DS.addSample(x[i],y[i])
    dataTrain, dataTest = DS.splitWithProportion(0.8)
    dataPlot, datadrop =DS.splitWithProportion(0.002)
    xTrain, yTrain = dataTrain['input'],dataTrain['target']
    xTest, yTest = dataTest['input'], dataTest['target']
    xPlot, yPlot= dataPlot['input'], dataPlot['target']
    fnn=buildNetwork(xdim,xdim+1,xdim+2,int(0.5*(xdim+1)),ydim,hiddenclass=TanhLayer,outclass=LinearLayer)
    trainer=BackpropTrainer(fnn,dataTrain,learningrate=0.000000001,verbose=True)
    err_train, err_valid =trainer.trainUntilConvergence(maxEpochs=100)
    
    tstresult = percentError( trainer.testOnClassData(), dataTest['target'] )
    print("epoch: %4d" % trainer.totalepochs, " test error: %5.2f%%" % tstresult)
    
    predict_resutl=[]
    for i in np.arange(len(xPlot)):
        predict_resutl.append(fnn.activate(xPlot[i])[0])
    print(predict_resutl)
    
    #yTest2=yTest([0:len(yTest):12])
    #pred2=predict_resutl([0:len(predict_resutl):12])
    
    plt.figure(figsize=(30,6), dpi=600)
    plt.xlabel("Test Timeline")
    plt.ylabel("Result")
    plt.plot(np.arange(0,len(xPlot)), yPlot,'ko-', label='true number')
    plt.plot(np.arange(0,len(xPlot)), predict_resutl,'ro--', label='predict number')
    lgnd1=plt.legend()
    plt.savefig(nm1,dpi=600, bbox_extra_artists=(lgnd1))
    
    plt.figure(figsize=(9,9), dpi=600)
    plt.plot(err_train,'b',label='train_err')
    plt.plot(err_valid,'r',label='valid_err')
    plt.xlabel("Training Times")
    plt.ylabel("Total Error")
    lgnd2=plt.legend()
    plt.savefig(nm2,dpi=600, bbox_extra_artists=(lgnd2))
    plt.show()
    
    return
    
    fnn3(Apro,"Apro")
    fnn3(Aval,"Aval")
    fnn3(Amom,"Amom")
    fnn3(Atra,"Atra")
    fnn3(Afd,"Afd")
    fnn3(Atec,"Atec")
    def construct_net(self):
        data = self.consturt_train_data()
        trndata = data[0]
        tstdata = data[1]
        test_data = data[2]
        ds = data[3]
        print self.vct_len
        net = buildNetwork(self.vct_len, 30, 2, outclass=SoftmaxLayer)
        trainer = BackpropTrainer(net,
                                  trndata,
                                  momentum=0.01,
                                  verbose=True,
                                  weightdecay=0.001)
        err_train = trainer.trainUntilConvergence(maxEpochs=20000)
        tstresult = percentError(trainer.testOnClassData(), tstdata['target'])
        print tstresult
        out = net.activateOnDataset(test_data)
        out = out.argmax(
            axis=1)  # the highest output activation gives the class
        print out
        path = "../../Results/output.txt"
        mse_file = open(path, 'w')
        for i in out:
            mse_file.write(str(i))
            mse_file.write("\n")
        mse_file.close()

        return
Пример #48
0
def make_prediction(tstdata):
    global trainer
    if trainer is not None:
        error = percentError(trainer.testOnClassData(
                        dataset=tstdata)
                        , tstdata['class'])
        print ('Percent Error on dataset: ', error)
Пример #49
0
def test_trained_model(filename, training_filename):
    fileObject = open(filename, 'r')
    fann = pickle.load(fileObject)
    testing_dataset = np.genfromtxt(training_filename,
                                    skip_header=0,
                                    dtype="int",
                                    delimiter='\t')

    data = ClassificationDataSet(len(testing_dataset[0]) - 1, 2, nb_classes=2)
    for aSample in testing_dataset:
        data.addSample(aSample[0:len(aSample) - 1],
                       [aSample[len(aSample) - 1]])

    #
    data._convertToOneOfMany()
    test = BackpropTrainer(fann,
                           dataset=data,
                           momentum=0.1,
                           verbose=False,
                           weightdecay=0.01)

    trnresult = percentError(test.testOnClassData(), data['class'])
    results = "Train error on testing data : %5.2f%%" % trnresult
    log_file.write(results + "  , The length of data " + str(len(data)))
    print results
    def run_nn_train_tone_classification(trndata, tstdata, outpath, name, fold):

        print 'run_nn_train_tone_classification'

        fnn = buildNetwork(trndata.indim, 20, trndata.outdim, hiddenclass=TanhLayer, bias=True)

        trainer = BackpropTrainer( fnn, dataset=trndata, weightdecay=0.006)
        # trainer = BackpropTrainer( fnn, dataset=trndata, verbose=True)

        acc = 0.0
        tag = 'No acc'
        real_obj = []
        predicted_obj = []

        class_recog = 0

        for i in range(100):
            trainer.trainEpochs( 1 )
            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
            predicted = np.array( trainer.testOnClassData(dataset=tstdata) )
            real = np.array( tstdata['class'][:,0] )
            # print real
            # if (accuracy_score(real, predicted) > acc) & (0 != len(np.where(predicted==1)[0])):
            if ( len(set(predicted)) >= class_recog):
                class_recog = len(set(predicted))
                print 'Tone num : {}'.format(set(predicted))
                tag = "Add acc when epoch: %4d" % trainer.totalepochs
                real_obj = real
                predicted_obj = predicted
                acc = accuracy_score(real, predicted) 
            # else:
            #     print predicted

        Utility.save_obj(real_obj, '{}/{}_fold_{}_real.npy'.format(outpath, name, fold))
        Utility.save_obj(predicted_obj, '{}/{}_fold_{}_predicted.npy'.format(outpath, name, fold))
        print tag
        print 'Accuracy : {}'.format( acc )
        print 'Precision : {}'.format( precision_score(real_obj, predicted_obj, average=None) )
        print 'Recall : {}'.format( recall_score(real_obj, predicted_obj, average=None) )
        print 'F-1 : {}'.format( f1_score(real_obj, predicted_obj, average=None) )
        return acc
Пример #51
0
def testOnTrainData(nn):
    outs = []
    for i, t in trn_data:
        outs.append(nn.activate(i))
    outs, trgs = OutToClass(outs, trn_data['target'])
    print outs
    print trn_data['class']
    return percentError(outs, trn_data['class'])
def main():
    images, labels = load_labeled_training(flatten=True)
    images = standardize(images)
    #images, labels = load_pca_proj(K=100)
    shuffle_in_unison(images, labels)
    ds = ClassificationDataSet(images.shape[1], 1, nb_classes=7)
    for i, l in zip(images, labels):
        ds.addSample(i, [l - 1])
    #ds._convertToOneOfMany()
    test, train = ds.splitWithProportion(0.2)
    test._convertToOneOfMany()
    train._convertToOneOfMany()
    net = shortcuts.buildNetwork(train.indim,
                                 1000,
                                 train.outdim,
                                 outclass=SoftmaxLayer)

    trainer = BackpropTrainer(net,
                              dataset=train,
                              momentum=0.1,
                              learningrate=0.01,
                              weightdecay=0.05)
    #trainer = RPropMinusTrainer(net, dataset=train)
    #cv = validation.CrossValidator(trainer, ds)
    #print cv.validate()
    net.randomize()
    tr_labels_2 = net.activateOnDataset(train).argmax(axis=1)
    trnres = percentError(tr_labels_2, train['class'])
    #trnres = percentError(trainer.testOnClassData(dataset=train), train['class'])
    testres = percentError(trainer.testOnClassData(dataset=test),
                           test['class'])
    print "Training error: %.10f, Test error: %.10f" % (trnres, testres)
    print "Iters: %d" % trainer.totalepochs

    for i in range(100):
        trainer.trainEpochs(10)
        trnres = percentError(trainer.testOnClassData(dataset=train),
                              train['class'])
        testres = percentError(trainer.testOnClassData(dataset=test),
                               test['class'])
        trnmse = trainer.testOnData(dataset=train)
        testmse = trainer.testOnData(dataset=test)
        print "Iteration: %d, Training error: %.5f, Test error: %.5f" % (
            trainer.totalepochs, trnres, testres)
        print "Training MSE: %.5f, Test MSE: %.5f" % (trnmse, testmse)