Exemplo n.º 1
0
def create_training_data(training_set,timesteps):
  DS = SequenceClassificationDataSet(28*28 , 1 , nb_classes=10)
  length = training_set[0].shape[0] 
  for l in xrange(length):
    DS.newSequence() 
    img = training_set[0][l] 
    targ = training_set[1][l] 
    for j in xrange(timesteps):
      DS.addSample(img , targ) 
  return DS 
Exemplo n.º 2
0
def create_test_data(test_set):
  DS = SequenceClassificationDataSet(28*28,1,nb_classes=10)
  length = test_set[0].shape[0] 
  for i  in xrange(length): 
      DS.newSequence() 
      img = test_set[0][i]
      targ = test_set[1][i] 
      
      DS.addSample(img , targ) 
  return DS 
Exemplo n.º 3
0
def generateNoisySines( npoints, nseq, noise=0.3 ):
    """ construct a 2-class dataset out of noisy sines """
    x = np.arange(npoints)/float(npoints) * 20.
    y1 = np.sin(x+rand(1)*3.)
    y2 = np.sin(x/2.+rand(1)*3.)
    DS = SequenceClassificationDataSet(1,1, nb_classes=2)
    for _ in xrange(nseq):
        DS.newSequence()
        buf = rand(npoints)*noise + y1 + (rand(1)-0.5)*noise
        for i in xrange(npoints):
            DS.addSample([buf[i]],[0])
        DS.newSequence()
        buf = rand(npoints)*noise + y2 + (rand(1)-0.5)*noise
        for i in xrange(npoints):
            DS.addSample([buf[i]],[1])
    return DS
Exemplo n.º 4
0
def generateNoisySines(npoints, nseq, noise=0.3):
    """ construct a 2-class dataset out of noisy sines """
    x = np.arange(npoints) / float(npoints) * 20.
    y1 = np.sin(x + rand(1) * 3.)
    y2 = np.sin(x / 2. + rand(1) * 3.)
    DS = SequenceClassificationDataSet(1, 1, nb_classes=2)
    for _ in xrange(nseq):
        DS.newSequence()
        buf = rand(npoints) * noise + y1 + (rand(1) - 0.5) * noise
        for i in xrange(npoints):
            DS.addSample([buf[i]], [0])
        DS.newSequence()
        buf = rand(npoints) * noise + y2 + (rand(1) - 0.5) * noise
        for i in xrange(npoints):
            DS.addSample([buf[i]], [1])
    return DS
Exemplo n.º 5
0
    def Train(self, dataset, error_observer, logger, dump_file):
        gradientCheck(self.m_net)

        net_dataset = SequenceClassificationDataSet(4, 2)
        for record in dataset:
            net_dataset.newSequence()

            gl_raises = record.GetGlRises()
            gl_min = record.GetNocturnalMinimum()

            if DayFeatureExpert.IsHypoglycemia(record):
                out_class = [1, 0]
            else:
                out_class = [0, 1]

            for gl_raise in gl_raises:
                net_dataset.addSample([gl_raise[0][0].total_seconds() / (24*3600), gl_raise[0][1] / 300, gl_raise[1][0].total_seconds() / (24*3600), gl_raise[1][1] / 300] , out_class)

        train_dataset, test_dataset = net_dataset.splitWithProportion(0.8)

        trainer = RPropMinusTrainer(self.m_net, dataset=train_dataset, momentum=0.8, learningrate=0.3, lrdecay=0.9, weightdecay=0.01, verbose=True)
        validator = ModuleValidator()

        train_error = []
        test_error = []
        for i in range(0, 80):
            trainer.trainEpochs(1)
            train_error.append(validator.MSE(self.m_net, train_dataset)) # here is validate func, think it may be parametrised by custom core function
            test_error.append(validator.MSE(self.m_net, test_dataset))
            print train_error
            print test_error
            error_observer(train_error, test_error)
            gradientCheck(self.m_net)

        dump_file = open(dump_file, 'wb')
        pickle.dump(self.m_net, dump_file)
Exemplo n.º 6
0
    for x in test_index:
        X_test.append(X[x])
        y_test.append(y[x])
    # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3)
    


    # SequenceClassificationDataset(inp,target, nb_classes)
    # inp = input dimension
    # target = number of targets
    # nb_classes = number of classes
    trndata = SequenceClassificationDataSet(100,1, nb_classes=2)
    tstdata = SequenceClassificationDataSet(100,1, nb_classes=2)

    for index in range(len(y_train)):
    	trndata.addSample(X_train[index], y_train[index])

    for index in range(len(y_test)):
    	tstdata.addSample(X_test[index], y_test[index])

    trndata._convertToOneOfMany( bounds=[0.,1.] )
    tstdata._convertToOneOfMany( bounds=[0.,1.] )

    if exists("params.xml"):
        rnn = NetworkReader.readFrom('params.xml')
    else:
        # construct LSTM network - note the missing output bias
        rnn = buildNetwork( trndata.indim, 5, trndata.outdim, hiddenclass=LSTMLayer, outclass=SoftmaxLayer, outputbias=False, recurrent=True)

    # define a training method
    trainer = BackpropTrainer( rnn, dataset=trndata, momentum=0.1, weightdecay=0.01)