def load_dataset(filename=""):
    if filename == "":
        raise Exception("No dataset loaded because no network name provided")
    ds = SequenceClassificationDataSet.loadFromFile(filename + '.data')
    print("dataset loaded from " + filename + '.data')
    testds = SequenceClassificationDataSet.loadFromFile(filename + '_test.data')
    print("testdataset loaded from " + filename + '_test.data')
    return ds, testds
def load_dataset(filename=""):
    if filename == "":
        raise Exception("No dataset loaded because no network name provided")
    ds = SequenceClassificationDataSet.loadFromFile(filename + '.data')
    print("dataset loaded from " + filename + '.data')
    testds = SequenceClassificationDataSet.loadFromFile(filename +
                                                        '_test.data')
    print("testdataset loaded from " + filename + '_test.data')
    return ds, testds
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 
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 
Example #5
0
def generateUpDown( npoints, nseq , balance = 0.5):
    """ construct a 2-class dataset out of noisy sines """

    lmin = 2
    lmax = 6
    DS = SequenceClassificationDataSet(1,1, nb_classes=2)
    for _ in xrange(nseq):
        slen = randint(lmin,lmax)
        if rand() < balance:
            num = 10
            DS.newSequence()
            for _ in xrange(slen):
                label = 0;
                num = num + rand()
                seq = num
                DS.appendLinked([seq], [label])
        else:
            num = 10
            DS.newSequence()
            for _ in xrange(slen):
                label = 1;
                num = num - rand()
                seq = num
                DS.appendLinked([seq], [label])           
            
    return DS
Example #6
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
  def __init__(self, ds): 

    num_inputs = ds.num_features * sequence_classifier.num_time_steps
    self.alldata = SequenceClassificationDataSet(num_inputs, 
                                                 target = 1,
                                                 nb_classes   = ds.get_num_classes(),
                                                 class_labels = ds.get_classes() )

    for Idx in range(len(ds.all_moves)):
      if not (Idx + sequence_classifier.num_time_steps < len(ds.all_moves)):
        continue
   
      class_first = ds.all_moves[Idx].class_ 
      features  = []
      for i in range(sequence_classifier.num_time_steps):
        features = features + ds.all_moves[Idx + i].get_features()

      class_last = ds.all_moves[Idx + sequence_classifier.num_time_steps].class_
  
      if class_first == class_last:
        self.alldata.appendLinked(features, [ds.get_classes().index(ds.all_moves[Idx].class_)])
        self.alldata.newSequence()
      

    self.tstdata, self.trndata = self.alldata.splitWithProportion(0.25)
    self.trndata._convertToOneOfMany()
    self.tstdata._convertToOneOfMany()

    self.seq_rnn      = None #buildNetwork(num_inputs, 2, self.trndata.outdim, hiddenclass=LSTMLayer,recurrent=True ,outclass=SoftmaxLayer)
    self.create_network(num_inputs)

    self.trainer  = RPropMinusTrainer(module=self.seq_rnn, dataset=self.trndata)
Example #8
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
def generalization_error(net, length, inp_len, med, numb, punct):
    sentence_tuples = get_nice_sentences_as_tuples(MIN=length,
                                                   MAX=length,
                                                   include_numbers=numb,
                                                   include_punctuation=punct)

    exper_data = SequenceClassificationDataSet(inp=inp_len, target=2)

    sentence_matrices = construct_sentence_matrices(sentence_tuples,
                                                    medium=med)
    for s in sentence_matrices:
        insert_grammatical_sequence(exper_data, s)
        insert_randomized_sequence(exper_data, s)

    return 1 - testOnSequenceData(net, exper_data)
 def __createDataset(data):
     ds = SequenceClassificationDataSet(inputs,
                                        1,
                                        nb_classes=nClasses,
                                        class_labels=labels.values())
     for target in classes:
         tupt = np.asarray([target])
         #         print("Target " + str(tupt))
         for x in data[target]:
             ds.newSequence()
             for y in x:
                 tup = tuple(y)
                 ds.appendLinked(tup, tupt)
     print(ds.calculateStatistics())
     #         ds._convertToOneOfMany(bounds=[0, 1])
     #     print ds.getField('target')
     print("DS entries " + str(ds.getNumSequences()))
     return ds
    def __createDataset(data):
        ds = SequenceClassificationDataSet(inputs, 1, nb_classes=nClasses, class_labels=labels.values())
        for target in classes:
            tupt = np.asarray([target])
    #         print("Target " + str(tupt))
            for x in data[target]:
                ds.newSequence()
                for y in x:
                    tup = tuple(y)
                    ds.appendLinked(tup, tupt)
        print(ds.calculateStatistics())
#         ds._convertToOneOfMany(bounds=[0, 1])
    #     print ds.getField('target')
        print("DS entries " + str(ds.getNumSequences()))
        return ds
Example #12
0
def sequenceClassificationDataSet(subject='a1', db=None):
	"""Don't know if this is set up right or how to use it"""
	if not db:
		db = gyroWalkingData()

	raw = db.data[subject][:,2:]
	segs = db.segments[subject]
	DS = SequenceClassificationDataSet(21, 1)

	for i in range(0,len(raw),10):
		DS.newSequence()
		isSeg = 0
		for j in range(10):
			if i+j in segs:
				isSeg = 1
			else:
				isSeg = 0
			DS.appendLinked(raw[i+j],[isSeg])
	DS._convertToOneOfMany()
	return DS
Example #13
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)
def train_network(options_file_location,training_data_location,output_location):
    training_file_handle = open(training_data_location,"r")
    training_reader = csv.reader(training_file_handle)
   
    stdout_file = output_location+'training_console_output.txt'
    stderr_file = output_location+'training_console_errput.txt'
    
    sys.stdout = open(stdout_file,"w")
    sys.stderr = open(stderr_file,"w")

    
    options_file_location = options_file_location
    options_file_handle = open(options_file_location,'r')
    options_dictionary = {}
    
    for option in options_file_handle.readlines():
        key,val = option.split('=')
        print key
        print val
        options_dictionary[key] = val;

    num_predictors = int(options_dictionary['num_predictors'])
    num_outputs = int(options_dictionary['num_outputs'])
    num_training_epochs = int(options_dictionary['num_training_epochs'])
    num_hidden_neurons = int(options_dictionary['num_hidden_neurons'])
    num_classes = int((options_dictionary['num_classes']))
    hidden_neuron_type_str = options_dictionary['hidden_neuron_type']
    output_neuron_type_str = options_dictionary['output_neuron_type']
    
    hidden_layer_type,output_layer_type = net_topol.get_layer_types(options_dictionary)
    
    training_dataset = SequenceClassificationDataSet(num_predictors, 1,num_classes)

    previous_sequence_number = 1
    
    #read data into dataset objects
    print 'reading in training data...'
    for row in training_reader:
        #convert list of strings to list of floats
        list = [float(s) for s in row]
        
        #split input line
        predictors = list[0:num_predictors]
        
        #+1 is to skip over the sequence column
        outputs = list[num_predictors+1:num_predictors+1+num_outputs]
        
        #convert from python list to numpy array
        predictors = np.array(predictors)
        outputs = np.array(outputs)
        
        
        sequence_number = math.trunc(list[num_predictors])
        
        if not sequence_number==previous_sequence_number:
            # print sequence_number
            # print previous_sequence_number
            training_dataset.newSequence()
        
        previous_sequence_number = sequence_number
        
        #add to dataset
        training_dataset.appendLinked(predictors, outputs)

    
    
    network = shortcuts.buildNetwork(num_predictors, num_hidden_neurons, num_outputs, hiddenclass=LSTMLayer, outclass=SoftmaxLayer)
    network.sortModules();
    
    training_dataset._convertToOneOfMany();
    
    print str(network)
    print str(training_dataset)
    
    trainer = RPropMinusTrainer(module=network, dataset=training_dataset)

    for i in range(num_training_epochs):
        print 'Starting training epoch: '+str(i)
        trainer.trainEpochs(1)
        sys.stdout.flush()
    
    network_file_location = output_location+'trained_network.xml'
    NetworkWriter.writeToFile(network, network_file_location)
    
    done_file_handle = open(output_location+'training_done.txt',"w")
    done_file_handle.write('%s' % 'done!')
    done_file_handle.close()
class sequence_classifier():
  num_time_steps = 5
  def __init__(self, ds): 

    num_inputs = ds.num_features * sequence_classifier.num_time_steps
    self.alldata = SequenceClassificationDataSet(num_inputs, 
                                                 target = 1,
                                                 nb_classes   = ds.get_num_classes(),
                                                 class_labels = ds.get_classes() )

    for Idx in range(len(ds.all_moves)):
      if not (Idx + sequence_classifier.num_time_steps < len(ds.all_moves)):
        continue
   
      class_first = ds.all_moves[Idx].class_ 
      features  = []
      for i in range(sequence_classifier.num_time_steps):
        features = features + ds.all_moves[Idx + i].get_features()

      class_last = ds.all_moves[Idx + sequence_classifier.num_time_steps].class_
  
      if class_first == class_last:
        self.alldata.appendLinked(features, [ds.get_classes().index(ds.all_moves[Idx].class_)])
        self.alldata.newSequence()
      

    self.tstdata, self.trndata = self.alldata.splitWithProportion(0.25)
    self.trndata._convertToOneOfMany()
    self.tstdata._convertToOneOfMany()

    self.seq_rnn      = None #buildNetwork(num_inputs, 2, self.trndata.outdim, hiddenclass=LSTMLayer,recurrent=True ,outclass=SoftmaxLayer)
    self.create_network(num_inputs)

    self.trainer  = RPropMinusTrainer(module=self.seq_rnn, dataset=self.trndata)

  def create_network(self, num_inputs):

    self.seq_rnn = RecurrentNetwork()

    in_layer        = LinearLayer(num_inputs)
    hidden_LSTM_   = LSTMLayer(24)
    hidden_layer_0  = LinearLayer(12)
    hidden_layer_1  = SigmoidLayer(12)
    output_layer    = LinearLayer(self.trndata.outdim)

    self.seq_rnn.addInputModule(in_layer)
    self.seq_rnn.addModule(hidden_layer_0)
    self.seq_rnn.addModule(hidden_layer_1)
    self.seq_rnn.addOutputModule(output_layer)
  
    #Now add the connections:
    in_to_LTSM = FullConnection(in_layer      , hidden_LSTM)
    LTSM_to_h0 = FullConnection(hidden_LSTM, hidden_layer_0)

    in_to_h0   = FullConnection(in_layer      , hidden_layer_0)
    h0_to_h1   = FullConnection(hidden_layer_0, hidden_layer_1)
    h1_to_out  = FullConnection(hidden_layer_1, output_layer)

    self.seq_rnn.addConnection(in_to_LSTM)
    self.seq_rnn.addConnection(LSTM_to_h0)

    self.seq_rnn.addConnection(in_to_h0)
    self.seq_rnn.addConnection(h0_to_h1)
    self.seq_rnn.addConnection(h1_to_out)

    self.seq_rnn.sortModules()

  def start_training(self):
    f = open("./results/seq_rnn_perf.txt", "w");
    for i in range(200):
      print "training step: " , i
      self.trainer.trainEpochs(1)
      err = self.evaluate()
      f.write(str(err) + ",")
      f.flush()
    f.close()

  def evaluate(self):
    print "epoch:" , self.trainer.totalepochs
    correct = 0
    wrong = 0
    self.seq_rnn.sortModules()
    for Idx in range (len(self.tstdata)):
      out = self.seq_rnn.activate(self.tstdata['input'][Idx])
      if argmax(out) == argmax(self.tstdata['target'][Idx]) : 
        correct += 1
      else:
        wrong += 1 

    correct_ratio = correct*1.0/(wrong + correct)    
    self.correct_perc.append(correct_ratio)

    print "Wrong Predictions: "  , wrong ,   "Ratio = ", wrong*100.0/(wrong+correct) , "%"
    print "Correct Predictions: ", correct,  "Ratio = ", correct*100.0/(wrong+correct) , "%"
    if (self.max_ratio < correct_ratio): 
      print "Found new max, saving network"
      self.write_out("best_perfrming_")
      self.max_ratio = correct_ratio

    return 1 - correct_ratio
 
  def write_out(self, name=""):
    NetworkWriter.writeToFile(self.seq_rnn,  "./results/" + name + "req_rnn.xml")
Example #16
0
        correct += 1.0
train_accuracy2 = correct / float(len(Y_train))
print "training accuracy is ", train_accuracy2
test_out = net.activateOnDataset(test_ds)
Y_pred = convert_output(test_out)
correct = 0.0
for i in range(len(Y_test)):
    if compare_list(Y_test[i], Y_pred[i]):
        correct += 1.0
test_accuracy2 = correct / float(len(Y_test))
print "test accuracy is ", test_accuracy2
"""

x_dimension = len(X_train[0])
y_dimension = len(Y_train[0])
DS = SequenceClassificationDataSet(x_dimension, y_dimension, nb_classes=3)

ds = SupervisedDataSet(x_dimension, y_dimension)
for i in range(len(X_train)):
    ds.addSample(X_train[i], Y_train[i])
# construct LSTM network - note the missing output bias
rnn = buildNetwork(x_dimension,
                   x_dimension,
                   y_dimension,
                   hiddenclass=LSTMLayer,
                   outclass=SoftmaxLayer,
                   outputbias=False,
                   recurrent=True)

# define a training method
trainer = RPropMinusTrainer(rnn, dataset=ds, verbose=True)
Example #17
0
    plt.xticks(tick_marks, behavior_names)
    plt.yticks(tick_marks, behavior_names)
    plt.xlabel("Predicted Class")
    plt.ylabel("True Class")
    plt.tight_layout()
    

# Original Joint data
BallLiftJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallLift/JointData.txt').astype(np.float32)
BallRollJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallRoll/JointData.txt').astype(np.float32)
BellRingLJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BellRingL/JointData.txt').astype(np.float32)
BellRingRJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BellRingR/JointData.txt').astype(np.float32)
BallRollPlateJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallRollPlate/JointData.txt').astype(np.float32)
RopewayJoint = np.loadtxt('../../20fpsFullBehaviorSampling/Ropeway/JointData.txt').astype(np.float32)

trndata = SequenceClassificationDataSet(10,1, nb_classes=6, class_labels=["BL", "BR", "BRL", "BRR", "BRP", "RW"])
tstdata = SequenceClassificationDataSet(10,1, nb_classes=6, class_labels=["BL", "BR", "BRL", "BRR", "BRP", "RW"])
    
for i in range(6000):
    if i%100==0:
        trndata.newSequence()
    trndata.appendLinked(BallLiftJoint[i,:], [0])
for i in range(6000):
    if i%100==0:
        trndata.newSequence()
    trndata.appendLinked(BallRollJoint[i,:], [1])
for i in range(6000):
    if i%100==0:
        trndata.newSequence()
    trndata.appendLinked(BellRingLJoint[i,:], [2])
for i in range(6000):
Example #18
0
BallLiftJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallLift/JointData.txt').astype(np.float32)
BallRollJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallRoll/JointData.txt').astype(np.float32)
BellRingLJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BellRingL/JointData.txt').astype(np.float32)
BellRingRJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BellRingR/JointData.txt').astype(np.float32)
BallRollPlateJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallRollPlate/JointData.txt').astype(np.float32)
RopewayJoint = np.loadtxt('../../20fpsFullBehaviorSampling/Ropeway/JointData.txt').astype(np.float32)

jointRemap = interp1d([-2.2,2.2],[-1,1])
BallLiftJoint = jointRemap(BallLiftJoint)
BallRollJoint = jointRemap(BallRollJoint)
BellRingLJoint = jointRemap(BellRingLJoint)
BellRingRJoint = jointRemap(BellRingRJoint)
BallRollPlateJoint = jointRemap(BallRollPlateJoint)
RopewayJoint = jointRemap(RopewayJoint)

trndata = SequenceClassificationDataSet(10,1, nb_classes=6)
tstdata = SequenceClassificationDataSet(10,1, nb_classes=6)

for i in range(6000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(BallLiftJoint[i,:], [4])
for i in range(6000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(BallRollJoint[i,:], [5])

for i in range(6000,8000):
    if i%200==0:
        tstdata.newSequence()
    tstdata.appendLinked(BallLiftJoint[i,:], [4])
Example #19
0
BallLiftJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallLift/JointData.txt').astype(np.float32)
BallRollJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallRoll/JointData.txt').astype(np.float32)
BellRingLJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BellRingL/JointData.txt').astype(np.float32)
BellRingRJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BellRingR/JointData.txt').astype(np.float32)
BallRollPlateJoint = np.loadtxt('../../20fpsFullBehaviorSampling/BallRollPlate/JointData.txt').astype(np.float32)
RopewayJoint = np.loadtxt('../../20fpsFullBehaviorSampling/Ropeway/JointData.txt').astype(np.float32)

jointRemap = interp1d([-2.2,2.2],[-1,1])
BallLiftJoint = jointRemap(BallLiftJoint)
BallRollJoint = jointRemap(BallRollJoint)
BellRingLJoint = jointRemap(BellRingLJoint)
BellRingRJoint = jointRemap(BellRingRJoint)
BallRollPlateJoint = jointRemap(BallRollPlateJoint)
RopewayJoint = jointRemap(RopewayJoint)

trndata = SequenceClassificationDataSet(10,1, nb_classes=6, class_labels=["BL", "BR", "BRL", "BRR", "BRP", "RW"])
tstdata = SequenceClassificationDataSet(10,1, nb_classes=6, class_labels=["BL", "BR", "BRL", "BRR", "BRP", "RW"])
   
for i in range(6000):
    if i%100==0:
        trndata.newSequence()
    trndata.appendLinked(BallLiftJoint[i,:], [0])
for i in range(6000):
    if i%100==0:
        trndata.newSequence()
    trndata.appendLinked(BallRollJoint[i,:], [1])
for i in range(6000):
    if i%100==0:
        trndata.newSequence()
    trndata.appendLinked(BellRingLJoint[i,:], [2])
for i in range(6000):
Example #20
0
    for _ in range(nseq):
        DS.newSequence()
        buf = rand(npoints) * noise + y1 + (rand(1) - 0.5) * noise
        for i in range(npoints):
            DS.addSample([buf[i]], [0])
        DS.newSequence()
        buf = rand(npoints) * noise + y2 + (rand(1) - 0.5) * noise
        for i in range(npoints):
            DS.addSample([buf[i]], [1])
    return DS


DS = SequenceClassificationDataSet

# create training and test data
trndata = SequenceClassificationDataSet(X_train, Y_train)
tstdata = SequenceClassificationDataSet(X_test, Y_test)

# construct LSTM network - note the missing output bias

rnn = buildNetwork(trndata.indim, (),
                   trndata.outdim,
                   hiddenclass=LSTMLayer,
                   outclass=SoftmaxLayer,
                   outclass=SoftmaxLayer)

#buildNetwork( MultiDimensionalLSTM
#rnn.addInputModule(LinearLayer(3, name='in'))
#rnn.addModule(MDLSTMLayer(5,2, name='hidden'))
#rnn.addOutputModule(SoftmaxLayer(1, name='out'))
#
Example #21
0
high_prices = np.asarray([n['high'] for n in data])
close_prices = np.asarray([n['adj_close'] for n in data])
volumes = np.asarray([n['vol'] for n in data])
features = Features(open_prices, low_prices, high_prices, close_prices, volumes)
f_input = features.getInput()
f_output = features.getOutput()
training_input = f_input[200:-test_n]
training_output = f_output[200:-test_n]
testing_input = f_input[-test_n:-20]
testing_output = f_output[-test_n:-20]
testing_label = date[-test_n:-20]
n_input = len(f_input[0])
n_output = 1

# build sequential dataset
training_dataset = SequenceClassificationDataSet(n_input, n_output, nb_classes=3, class_labels=["UP", "DOWN", "NOWHERE"])
for x, y in zip(training_input, training_output):
  training_dataset.appendLinked(x, [y])
  training_dataset.newSequence()

testing_dataset = SequenceClassificationDataSet(n_input, n_output, nb_classes=3, class_labels=["UP", "DOWN", "NOWHERE"])
for x, y in zip(testing_input, testing_output):
  testing_dataset.appendLinked(x, [y])
  testing_dataset.newSequence()

training_dataset._convertToOneOfMany()
testing_dataset._convertToOneOfMany()

# build network
net = RecurrentNetwork()
net.addInputModule(LinearLayer(training_dataset.indim, name="input"))
Example #22
0
    for x in train_index:
        X_train.append(X[x])
        y_train.append(y[x])

    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
def network_predict(options_file_location,prediction_data_location,output_location,network_location):
    
    prediction_data_file_handle = open(prediction_data_location,"r")
    prediction_data_reader = csv.reader(prediction_data_file_handle)
    
    stdout_file = output_location+'prediction_console_output.txt'
    stderr_file = output_location+'prediction_console_errput.txt'
    
    sys.stdout = open(stdout_file,"w")
    sys.stderr = open(stderr_file,"w")
    
    prediction_results_file_location = output_location+'prediction_results.csv'
    prediction_results_file_handle = open(prediction_results_file_location,"w")
    
    options_file_handle = open(options_file_location,'r')
    options_dictionary = {}

    for option in options_file_handle.readlines():
        key,val = option.split('=')
        print key
        print val
        options_dictionary[key] = val;

    num_predictors = int(options_dictionary['num_predictors'])
    num_outputs = int(options_dictionary['num_outputs'])
    num_training_epochs = int(options_dictionary['num_training_epochs'])
    num_hidden_neurons = int(options_dictionary['num_hidden_neurons'])
    num_classes = int((options_dictionary['num_classes']))
    
    prediction_dataset = SequenceClassificationDataSet(num_predictors, 1,num_classes)
    
    
    previous_sequence_number = 1
    # frame_number_debug = 0
    print 'reading in prediction data...'
    for row in prediction_data_reader:
        #convert list of strings to list of floats
        list = [float(s) for s in row]
        
        #split input line
        predictors = list[0:num_predictors]
        
        #+1 is to skip over the sequence column
        outputs = list[num_predictors+1:num_predictors+1+num_outputs]
        
        #convert from python list to numpy array
        predictors = np.array(predictors)
        outputs = np.array(outputs)
        
        sequence_number = math.trunc(list[num_predictors])
        
        if not sequence_number==previous_sequence_number:
            # print 'sequence_number '+str(sequence_number)
            # print 'previous_sequence_number '+str(previous_sequence_number)
            # frame_number_debug = 0;
            prediction_dataset.newSequence()
        
        previous_sequence_number = sequence_number
        
        #add to dataset
        prediction_dataset.appendLinked(predictors, outputs)
        # frame_number_debug += 1
        # print 'frame_number_debug '+str(frame_number_debug)
    
    prediction_dataset._convertToOneOfMany();
    
    network = NetworkReader.readFrom(network_location)
        
    results, targets, accuracy = evalRNN.evalRNNOnSeqClassificationDataset(network,prediction_dataset)
    print 'Accuracy: '+str(accuracy)
        
    results_length = results.shape
    
    np.savetxt(prediction_results_file_location,results,delimiter=" ",fmt='%5.5f')
    
    done_file_handle = open(output_location+'predicting_done.txt',"w")
    done_file_handle.write('%s' % 'done!')
    done_file_handle.close()
Example #24
0
for i in range(100,10000,100):
    LRopeway = np.vstack((LRopeway, BellRingLJoint[i:i+100], RopewayJoint[i:i+100]))
for i in range(100,10000,100):
    RRopeway = np.vstack((RRopeway, BellRingRJoint[i:i+100], RopewayJoint[i:i+100]))

print LBallLift.shape
print RBallLift.shape
print LBallRoll.shape
print RBallRoll.shape
print LBallRollPlate.shape
print RBallRollPlate.shape
print LRopeway.shape
print RRopeway.shape

    
trndata = SequenceClassificationDataSet(10,1, nb_classes=8)
tstdata = SequenceClassificationDataSet(10,1, nb_classes=8)

for i in range(12000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(LBallLift[i,:], [0])
for i in range(12000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(RBallLift[i,:], [1])
for i in range(12000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(LBallRoll[i,:], [2])
for i in range(12000):
Example #25
0
for i in range(100,10000,100):
    LRopeway = np.vstack((LRopeway, BellRingLJoint[i:i+100], RopewayJoint[i:i+100]))
for i in range(100,10000,100):
    RRopeway = np.vstack((RRopeway, BellRingRJoint[i:i+100], RopewayJoint[i:i+100]))

print LBallLift.shape
print RBallLift.shape
print LBallRoll.shape
print RBallRoll.shape
print LBallRollPlate.shape
print RBallRollPlate.shape
print LRopeway.shape
print RRopeway.shape

    
trndata = SequenceClassificationDataSet(10,1, nb_classes=8)
tstdata = SequenceClassificationDataSet(10,1, nb_classes=8)

for i in range(12000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(LBallLift[i,:], [0])
for i in range(12000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(RBallLift[i,:], [1])
for i in range(12000):
    if i%200==0:
        trndata.newSequence()
    trndata.appendLinked(LBallRoll[i,:], [2])
for i in range(12000):
Example #26
0
	   secList.append(sectors[i]['day_price_change'])
    data.append(secList)
#print data
#dataset = SequentialDataSet(inputs, outputs)

#(data1, data2) = np.array_split(data,2)
#print data2
#for x in itertools.izip(data):
#    dataset.newSequence()
#    dataset.addSample(x)
# create training and test data
#datatrn = data1 + data2
#trndata = generateNoisySines(50, 40)

#the data is now 2 d array - need to determine the dimensions of the data sets
trndata = SequenceClassificationDataSet(numSectors,1)
for i in xrange( 1, len(data[0]) ):
    trndata.newSequence()
    trndata.appendLinked(data[i - 1], (data[i] ))

tstdata = SequenceClassificationDataSet(numSectors,1)
for i in xrange( 1, len(data[0]) ):
    tstdata.newSequence()
    tstdata.appendLinked(data[i - 1], (data[i] ))

#trndata._convertToOneOfMany( bounds=[0.,1.] )
#tstdata = SequenceClassificationDataSet(1,1)
#for i in xrange(len(data2) -1):
#    tstdata.newSequence()
#    tstdata.addSample(data2[i], data2[i+1])
#tstdata._convertToOneOfMany( bounds=[0.,1.] )