当前位置: 首页>>代码示例>>Python>>正文


Python BackpropTrainer.testOnClassData方法代码示例

本文整理汇总了Python中pybrain.supervised.trainers.BackpropTrainer.testOnClassData方法的典型用法代码示例。如果您正苦于以下问题:Python BackpropTrainer.testOnClassData方法的具体用法?Python BackpropTrainer.testOnClassData怎么用?Python BackpropTrainer.testOnClassData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pybrain.supervised.trainers.BackpropTrainer的用法示例。


在下文中一共展示了BackpropTrainer.testOnClassData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: train

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
    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
开发者ID:kymo,项目名称:ImageRecognition,代码行数:34,代码来源:match.py

示例2: trainNetwork

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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
开发者ID:kdelaney711,项目名称:sudokusolver,代码行数:31,代码来源:network.py

示例3: Classifier

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
class Classifier():
    def __init__(self, testing = False):
        self.training_set, self.test_set = split_samples(0.5 if testing else 1.0)
        self.net = buildNetwork( self.training_set.indim, self.training_set.outdim, outclass=SoftmaxLayer )
        self.trainer = BackpropTrainer( self.net, dataset=self.training_set, momentum=0.1, verbose=True, weightdecay=0.01)
        self.train()

    def train(self):
        self.trainer.trainEpochs( EPOCHS )
        trnresult = percentError( self.trainer.testOnClassData(),
                                  self.training_set['class'] )
        print "  train error: %5.2f%%" % trnresult

    def classify(self, file):
        strengths = self.net.activate(process_sample(*load_sample(file)))
        print strengths
        best_match = None
        strength = 0.0
        for i,s in enumerate(strengths):
            if s > strength:
                best_match = i
                strength = s
        return SOUNDS[best_match]

    def test(self):
        tstresult = percentError( self.trainer.testOnClassData(
               dataset=self.test_set ), self.test_set['class'] )

        print "  test error: %5.2f%%" % tstresult
开发者ID:joesarre,项目名称:web-audio-hack-day,代码行数:31,代码来源:classify_beat.py

示例4: main

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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)
开发者ID:deepxkn,项目名称:facial-expression-recognition-1,代码行数:36,代码来源:rbm_nn.py

示例5: trainNetwork

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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
开发者ID:johnesquivel,项目名称:RaspVoiceRecog,代码行数:30,代码来源:buildModel.py

示例6: createnetwork

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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
开发者ID:junwangcas,项目名称:network_rs,代码行数:34,代码来源:create_neuralnet.py

示例7: __init__

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
class NNetwork:
	def __init__(self):
		self.ds = ClassificationDataSet(7, 1, nb_classes=8)  #8 since we have 8 gestures, 7 since we have 7 features
		
	def add_data(self, training_data):
		for gesture in training_data:
			self.ds.addSample(gesture[1], gesture[0])  #a method to add all the training data we have
			
	def newData(self, training_data):   #a method for replacing the data already existing and adding data from scratch
		self.ds = ClassificationDataSet(7, 1, nb_classes=8)
		for gesture in training_data:
			self.ds.addSample(gesture[1], gesture[0])
	
	def train(self, shouldPrint):
		tstdata, trndata = self.ds.splitWithProportion(0.2)  #splits the data into training and verification data
		trndata._convertToOneOfMany()
		tstdata._convertToOneOfMany()
		self.fnn = buildNetwork(trndata.indim, 64, trndata.outdim, outclass=SoftmaxLayer) #builds a network with 64 hidden neurons
		self.trainer = BackpropTrainer(self.fnn, dataset=trndata, momentum=0.1, learningrate=0.01, verbose=True, weightdecay=0.1)
		#uses the backpropagation algorithm
		self.trainer.trainUntilConvergence(dataset=trndata, maxEpochs=100, verbose=True, continueEpochs=10, validationProportion=0.20) #early stopping with 20% as testing data
		trnresult = percentError( self.trainer.testOnClassData(), trndata['class'] )
		tstresult = percentError( self.trainer.testOnClassData(dataset=tstdata ), tstdata['class'] )
		
		if shouldPrint:
			print "epoch: %4d" % self.trainer.totalepochs, "  train error: %5.2f%%" % trnresult, "  test error: %5.2f%%" % tstresult
	def activate(self, data): #tests a particular data point (feature vector)
	    return self.fnn.activate(data)
开发者ID:sverrirth,项目名称:IIS-master,代码行数:30,代码来源:nnetwork.py

示例8: measuredLearning

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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
开发者ID:DanSGraham,项目名称:School-Projects,代码行数:34,代码来源:learner.py

示例9: train

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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
开发者ID:DanielleSucher,项目名称:mushrooms,代码行数:37,代码来源:mushrooms.py

示例10: main

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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()
开发者ID:mfbx9da4,项目名称:neuron-astrocyte-networks,代码行数:36,代码来源:play_angn.py

示例11: train

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
	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
开发者ID:davidlavy88,项目名称:FaceIdentifier,代码行数:32,代码来源:identify.py

示例12: big_training

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
def big_training(np_data, num_nets=1, num_epoch=20, net_builder=net_full, train_size=.1, testing=False):
    sss = cross_validation.StratifiedShuffleSplit(np_data[:,:1].ravel(), n_iter=num_nets , test_size=1-train_size, random_state=3476)
    nets=[None for net_ind in range(num_nets)]
    trainaccu=[[0 for i in range(num_epoch)] for net_ind in range(num_nets)]
    testaccu=[[0 for i in range(num_epoch)] for net_ind in range(num_nets)]
    net_ind=0
    for train_index, test_index in sss:
        print ('%s Building %d. network.' %(time.ctime(), net_ind+1))
        #print("TRAIN:", len(train_index), "TEST:", len(test_index))
        trainset = ClassificationDataSet(np_data.shape[1] - 1, 1)
        trainset.setField('input', np_data[train_index,1:]/100-.6)
        trainset.setField('target', np_data[train_index,:1])
        trainset._convertToOneOfMany( )
        trainlabels = trainset['class'].ravel().tolist()
        if testing:
            testset = ClassificationDataSet(np_data.shape[1] - 1, 1)
            testset.setField('input', np_data[test_index,1:]/100-.6)
            testset.setField('target', np_data[test_index,:1])
            testset._convertToOneOfMany( )
            testlabels = testset['class'].ravel().tolist()
        nets[net_ind] = net_builder()
        trainer = BackpropTrainer(nets[net_ind], trainset)
        for i in range(num_epoch):
            for ii in range(3):
                err = trainer.train()
            print ('%s Epoch %d: Network trained with error %f.' %(time.ctime(), i+1, err))
            trainaccu[net_ind][i]=accuracy_score(trainlabels,trainer.testOnClassData())
            print ('%s Epoch %d: Train accuracy is %f' %(time.ctime(), i+1, trainaccu[net_ind][i]))
            print ([sum([trainaccu[y][i]>tres for y in range(net_ind+1)]) for tres in [0,.1,.2,.3,.4,.5,.6]])
            if testing:
                testaccu[net_ind][i]=accuracy_score(testlabels,trainer.testOnClassData(testset))
                print ('%s Epoch %d: Test accuracy is %f' %(time.ctime(), i+1, testaccu[net_ind][i]))
        NetworkWriter.writeToFile(nets[net_ind], 'nets/'+net_builder.__name__+str(net_ind)+'.xml')
        net_ind +=1
    return [nets, trainaccu, testaccu]
开发者ID:1va,项目名称:pybrain_digits,代码行数:37,代码来源:pybrain_mnist.py

示例13: train

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
  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]
开发者ID:mwebergithub,项目名称:face457b,代码行数:61,代码来源:supervised_facial_classifier.py

示例14: nn_classify

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
def nn_classify():
    # train_X,Y = load_svmlight_file('data/train_metrix')
    # rows = pd.read_csv('data/log_test2.csv',index_col=0).sort_index().index.unique()
    # train_X = pd.read_csv('data/train_tfidf.csv',index_col=0)
    # test_X = pd.read_csv('data/test_tfidf.csv',index_col=0)
    # select = SelectPercentile(f_classif, percentile=50)
    # select.fit(train_X,Y)
    # train_X = select.transform(train_X)
    # test_X = select.transform(test_X)
    # print 'dump train...'
    # dump_svmlight_file(train_X,Y,'data/train_last')
    # test_Y = [0]*(test_X.shape[0])
    # print 'dump test...'
    # dump_svmlight_file(test_X,test_Y,'data/test_last')

    train_X,Y = load_svmlight_file('data/train_last')
    test_X,test_Y = load_svmlight_file('data/test_last')
    train_X = train_X.toarray()
    test_X = test_X.toarray()
    Y = [int(y)-1 for y in Y]
    print 'Y:',len(Y)
    rows = pd.read_csv('data/log_test2.csv',index_col=0).sort_index().index.unique()
    train_n = train_X.shape[0]
    m = train_X.shape[1]
    test_n = test_X.shape[0]
    print train_n,m,#test_n
    train_data = ClassificationDataSet(m,1,nb_classes=12)
    test_data = ClassificationDataSet(m,1,nb_classes=12)
    # test_data = ClassificationDataSet(test_n,m,nb_classes=12)
    for i in range(train_n):
        train_data.addSample(np.ravel(train_X[i]),Y[i])
    for i in range(test_n):
        test_data.addSample(test_X[i],Y[i])
    trndata = train_data
    # tstdata = train_data

    trndata._convertToOneOfMany()
    # tstdata._convertToOneOfMany()
    test_data._convertToOneOfMany()

     # 先用训练集训练出所有的分类器
    print 'train classify...'
    fnn = buildNetwork( trndata.indim, 400 , trndata.outdim, outclass=SoftmaxLayer )
    trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, learningrate=0.01 , verbose=True, weightdecay=0.01)
    trainer.trainEpochs(3)
    # print 'Percent Error on Test dataset: ' , percentError( trainer.testOnClassData (
    #            dataset=tstdata )
    #            , )
    print 'end train classify'
    pre_y = trainer.testOnClassData(dataset=trndata)
    print metrics.classification_report(Y,pre_y)
    pre_y = trainer.testOnClassData(dataset=test_data)
    print 'write result...'
    print 'before:',pre_y[:100]
    pre_y = [int(y)+1 for y in pre_y]
    print 'after:',pre_y[:100]
    DataFrame(pre_y,index=rows).to_csv('data/info_test2.csv', header=False)
    print 'end...'
开发者ID:ganzhiruyi,项目名称:Machine-Learning,代码行数:60,代码来源:test.py

示例15: runNeuralLearningCurveSimulation

# 需要导入模块: from pybrain.supervised.trainers import BackpropTrainer [as 别名]
# 或者: from pybrain.supervised.trainers.BackpropTrainer import testOnClassData [as 别名]
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]))
开发者ID:anantauprety,项目名称:sentiment-analysis,代码行数:58,代码来源:neural_learning_curve.py


注:本文中的pybrain.supervised.trainers.BackpropTrainer.testOnClassData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。