class experiment: '''experiment variables''' # None at the moment def __init__(self, name="Uninitialized Experiment"): self.name = name self.initialized = False self.trained = False self.num_trains = 0 self.trained = False self.exp = {} # this is our experiment meta-data. Dictionary with lots of parameters self.filepath = None self.W = None def read(self, filepath, loadW=False): self.filepath = filepath if self.filepath == None: raise Exception("No filepath initialized for this experiment") expfile = open(filepath, 'rb') ############################################# # Read in the experiment file ############################################# # swallow everything in memory block = expfile.read() lines = block.split("\n") expfile.close() # free our in-core file copy asap del block # First, set a line index for reading in the input i = 0 print "Reading in experiment: {}".format(filepath) # Now read in the experiment line by line and fill in data while i < len(lines): if not lines[i].startswith("#") and lines[i] != '\n' and lines[i] != '': line = lines[i].split(None, 1) # used to be line = lines[i].split('\t') try: self.exp[line[0]] = int(line[1]) except: self.exp[line[0]] = line[1] i += 1 try: if self.exp['train_pct'] > 100 or self.exp['train_pct'] <= 0: raise Exception("train_pct must be > 0 and <= 100") except: self.exp['train_pct'] = 100 # get the dictionary of phase times self.exp['phase_times'] = json.loads(self.exp['phase_times']) # get the dictionary of delay times self.exp['phase_var'] = json.loads(self.exp['phase_var']) if "gauss_inputs" in self.exp: self.exp['gauss_inputs'] = util.isTrue(self.exp['gauss_inputs']) else: self.exp['gauss_inputs'] = False for phase in self.exp['phase_times']: self.exp['phase_times'][phase] = int(self.exp['phase_times'][phase]) for phase in self.exp['phase_var']: self.exp['phase_var'][phase] = util.isTrue(self.exp['phase_var'][phase]) if not 'name' in self.exp: if not 'directory' in self.exp: self.exp['name'] = "default_name" else: self.exp['name'] = self.exp['directory'].split('/')[-1] if not 'directory' in self.exp: self.exp['directory'] = None elif not self.exp['directory'].endswith('/'): self.exp['directory'] += '/' if not 'train_file' in self.exp: self.exp['train_file'] = "{}.train".format(self.exp['name']) if "version" in self.exp: self.exp['input_layer'] = self.exp["num_locs"] + 4 else: self.exp["version"] = 1 self.exp['input_layer'] = self.exp['input_side']*self.exp['input_side'] + 4 if loadW == True: self.exp["W"] = "W" else: self.exp["W"] = None self.rnn = HessianRNN(layers=[self.exp['input_layer'], self.exp['hidden_layer'], self.exp['out_layer']], struc_damping=0.5, use_GPU=False, debug=False, loadW=loadW, dataPath=self.exp['directory']) np.set_printoptions(edgeitems = 10) self.exp['epath'] = self.exp['directory'] + self.exp['name'] + '.exp' self.initialized = True def createTrainSet(self): trainFilepath = "{}{}".format(self.exp['directory'], self.exp['train_file']) if self.exp['version'] == 1: if self.exp['type'] == "simple": pass # ig.simpleGen() elif self.exp['type'] == "attention": ig.attenGen(self.exp['input_side'], self.exp['num_locs'], self.exp['phase_times'], self.exp['phase_var'], self.exp['train_pct'], filepath=self.exp['directory'], ifile=self.exp['train_file']) elif self.exp['type'] == "selection": ig.selGen(self.exp['input_side'], self.exp['num_locs'], self.exp['phase_times'], self.exp['phase_var'], self.exp['train_pct'], filepath=self.exp['directory'], ifile=self.exp['train_file']) elif self.exp['type'] == "combined": ig.combGen(self.exp['input_side'], self.exp['num_locs'], self.exp['phase_times'], self.exp['phase_var'], self.exp['train_pct'], filepath=self.exp['directory'], ifile=self.exp['train_file']) else: raise Exception("Experiment type not in list: {}".format(self.exp['type'])) elif self.exp['version'] == 2: igV2.inputGen(self) elif self.exp['version'] == 3: igV2.inputGen(self) elif self.exp['version'] == 4: igV2.inputGen(self) elif self.exp['version'] == 5: igV2.inputGen(self) else: raise Exception("Unkown Exp Version") def getTrainInputs(self): return util.readTrials(self.exp['directory'] + self.exp['train_file']) def getTestInputs(self): return util.readTrials(self.exp['directory'] + "Unused_Locs.train") def train(self, loadW=False): if loadW is True: if self.exp['W'] is None: loadW = "{}W".format(self.exp['directory']) else: loadW = "{}{}".format(self.exp['directory'], self.exp['W']) if self.exp['directory'] == None or self.exp['directory'] == '': trainFilepath = self.exp['train_file'] else: trainFilepath = "{}{}".format(self.exp['directory'], self.exp['train_file']) ret = util.readTrials(trainFilepath) inputs = ret['inputs'] targets = ret['targets'] inputNames = ret["inputNames"] targetNames = ret["targetNames"] if not loadW: isSuccess = train_saccade(self.num_trains, inputNames, targetNames, inputs, targets, self.rnn) self.num_trains += 1 if isSuccess != True: print "Error: Overfitting in batch run" return False print "Training Successful \(0_0)/" # Indicate that we finished at least one training cycle and may now load old weights self.trained = True else: print "Loading weights from previous training" print "\n\n\n(0_0)\n\n\n" isSuccess = train_saccade(self.num_trains, inputNames, targetNames, inputs, targets, self.rnn, load_weights=loadW) self.num_trains += 1 if isSuccess != True: print "Error: Overfitting in batch run" return False print "Training Successful \(0_0)/" return True def test(self, W=None): raise Exception("Function not implemented yet") def testError(self): """Compute RMS error of the testing set""" testFilepath = "{}{}".format(self.exp['directory'], "Unused_Locs.train") ret = util.readTrials(testFilepath) return self.rnn.error(inputs=ret['inputs'], targets=ret['targets']) def trainError(self): """Compute RMS error of the training set""" trainFilepath = "{}{}".format(self.exp['directory'], self.exp['train_file']) ret = util.readTrials(trainFilepath) return self.rnn.error(inputs=ret['inputs'], targets=ret['targets']) def __repr__(self): return "Experiment Object, Name: {}".format(self.name) def __str__(self): return "\nExperiment Data: \n\n{}\n".format(self.exp)
def read(self, filepath, loadW=False): self.filepath = filepath if self.filepath == None: raise Exception("No filepath initialized for this experiment") expfile = open(filepath, 'rb') ############################################# # Read in the experiment file ############################################# # swallow everything in memory block = expfile.read() lines = block.split("\n") expfile.close() # free our in-core file copy asap del block # First, set a line index for reading in the input i = 0 print "Reading in experiment: {}".format(filepath) # Now read in the experiment line by line and fill in data while i < len(lines): if not lines[i].startswith("#") and lines[i] != '\n' and lines[i] != '': line = lines[i].split(None, 1) # used to be line = lines[i].split('\t') try: self.exp[line[0]] = int(line[1]) except: self.exp[line[0]] = line[1] i += 1 try: if self.exp['train_pct'] > 100 or self.exp['train_pct'] <= 0: raise Exception("train_pct must be > 0 and <= 100") except: self.exp['train_pct'] = 100 # get the dictionary of phase times self.exp['phase_times'] = json.loads(self.exp['phase_times']) # get the dictionary of delay times self.exp['phase_var'] = json.loads(self.exp['phase_var']) if "gauss_inputs" in self.exp: self.exp['gauss_inputs'] = util.isTrue(self.exp['gauss_inputs']) else: self.exp['gauss_inputs'] = False for phase in self.exp['phase_times']: self.exp['phase_times'][phase] = int(self.exp['phase_times'][phase]) for phase in self.exp['phase_var']: self.exp['phase_var'][phase] = util.isTrue(self.exp['phase_var'][phase]) if not 'name' in self.exp: if not 'directory' in self.exp: self.exp['name'] = "default_name" else: self.exp['name'] = self.exp['directory'].split('/')[-1] if not 'directory' in self.exp: self.exp['directory'] = None elif not self.exp['directory'].endswith('/'): self.exp['directory'] += '/' if not 'train_file' in self.exp: self.exp['train_file'] = "{}.train".format(self.exp['name']) if "version" in self.exp: self.exp['input_layer'] = self.exp["num_locs"] + 4 else: self.exp["version"] = 1 self.exp['input_layer'] = self.exp['input_side']*self.exp['input_side'] + 4 if loadW == True: self.exp["W"] = "W" else: self.exp["W"] = None self.rnn = HessianRNN(layers=[self.exp['input_layer'], self.exp['hidden_layer'], self.exp['out_layer']], struc_damping=0.5, use_GPU=False, debug=False, loadW=loadW, dataPath=self.exp['directory']) np.set_printoptions(edgeitems = 10) self.exp['epath'] = self.exp['directory'] + self.exp['name'] + '.exp' self.initialized = True
for temp in range(trial_time): row = np.array(lines[index].split(' ')) try: data = row[0:num_targets].astype(np.float32) except: print row exit() index += 1 targets[trial][temp] = data assert(lines[index] == 'end') # verify where we are index += 1 ######################################################## # create a recurrent NN rnn = HessianRNN(layers=[lay[0], lay[1], lay[3]], struc_damping=0.5, use_GPU=False, debug=False, loadW = wFileString) #create an output file for the activations. I don't actually use this yet. # out2 = open("activations", "wb+") # shows activations for each time step for each input index = 1 for i, t in zip(inputs, targets): j = np.asarray(i) # print >> out2, "\nTrail Num: {}".format(index) index += 1 hidden = rnn.forward(j, rnn.W)[-2] out = rnn.forward(j, rnn.W)[-1] # for x in range(len(output)): # for y in range(len(output[x][0])):