def train(self, params): """ Train LSTM network on buffered dataset history After training, run LSTM on history[:-1] to get the state correct :param params: :return: """ if params['reset_every_training']: n = params['encoding_num'] self.net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) self.net.reset() # prepare training dataset ds = SequentialDataSet(params['encoding_num'], params['encoding_num']) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i - 1]: ds.addSample(self.encoder.encode(history[i - 1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() if params['num_epochs'] > 1: trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=params['verbosity'] > 0) if len(history) > 1: trainer.trainEpochs(params['num_epochs']) # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset() else: self.trainer.setData(ds) self.trainer.train() # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset()
def train(self, params): n = params['encoding_num'] net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) net.reset() ds = SequentialDataSet(n, n) trainer = RPropMinusTrainer(net, dataset=ds) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i-1]: ds.addSample(self.encoder.encode(history[i-1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() if len(history) > 1: trainer.trainEpochs(params['num_epochs']) net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) predictions = self.encoder.classify(output, num=params['num_predictions']) if resets[i]: net.reset() return net
def visulizeDataSet(network, data, seqno, in_labels, out_labels): seq = data.getSequence(seqno) tmpDs = SequentialDataSet(data.indim, data.outdim) tmpDs.newSequence() for i in xrange(data.getSequenceLength(seqno)): tmpDs.addSample(seq[0][i], seq[1][i]) nplots = len(in_labels) + len(out_labels) for i in range(len(in_labels)): p = PL.subplot(nplots, 1, i + 1) p.clear() p.plot(tmpDs['input'][:, i]) p.set_ylabel(in_labels[i]) for i in range(len(out_labels)): p = PL.subplot(nplots, 1, i + 1 + len(in_labels)) p.clear() output = ModuleValidator.calculateModuleOutput(network, tmpDs) p.plot(tmpDs['target'][:, i], label='train') p.plot(output[:, i], label='sim') p.legend() p.set_ylabel(out_labels[i])
def train(self, params): self.net.reset() ds = SequentialDataSet(self.nDimInput, self.nDimOutput) trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=False) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(params['prediction_nstep'], len(history)): if not resets[i-1]: ds.addSample(self.inputEncoder.encode(history[i-params['prediction_nstep']]), self.outputEncoder.encode(history[i][0])) if resets[i]: ds.newSequence() # print ds.getSample(0) # print ds.getSample(1) # print ds.getSample(1000) # print " training data size", ds.getLength(), " len(history) ", len(history), " self.history ", len(self.history) # print ds if len(history) > 1: trainer.trainEpochs(params['num_epochs']) self.net.reset() for i in xrange(len(history) - params['prediction_nstep']): symbol = history[i] output = self.net.activate(ds.getSample(i)[0]) if resets[i]: self.net.reset()
def visulizeDataSet(network, data, seqno, in_labels, out_labels): seq = data.getSequence(seqno) tmpDs = SequentialDataSet(data.indim, data.outdim) tmpDs.newSequence() for i in xrange(data.getSequenceLength(seqno)): tmpDs.addSample(seq[0][i], seq[1][i]) nplots = len(in_labels) + len(out_labels) for i in range(len(in_labels)): p = PL.subplot(nplots, 1, i + 1) p.clear() p.plot(tmpDs['input'][:, i]) p.set_ylabel(in_labels[i]) for i in range(len(out_labels)): p = PL.subplot(nplots, 1, i + 1 + len(in_labels)) p.clear() output = ModuleValidator.calculateModuleOutput(network, tmpDs) p.plot(tmpDs['target'][:, i], label='train') p.plot(output[:, i], label='sim') p.legend() p.set_ylabel(out_labels[i])
def train(self, params): """ Train LSTM network on buffered dataset history After training, run LSTM on history[:-1] to get the state correct :param params: :return: """ if params['reset_every_training']: n = params['encoding_num'] self.net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) self.net.reset() # prepare training dataset ds = SequentialDataSet(params['encoding_num'], params['encoding_num']) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i - 1]: ds.addSample(self.encoder.encode(history[i - 1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() print "Train LSTM network on buffered dataset of length ", len(history) if params['num_epochs'] > 1: trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=params['verbosity'] > 0) if len(history) > 1: trainer.trainEpochs(params['num_epochs']) # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset() else: self.trainer.setData(ds) self.trainer.train() # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset()
def create_data(self, inputs, targets): data = SequentialDataSet(inputs, targets) for i in xrange(0, len(self.dataframe) - 1): data.newSequence() ins = self.dataframe.ix[i].values target = self.dataframe.ix[i + 1].values[0] data.appendLinked(ins, target) self.data = data
def create_data(self, inputs, targets): data = SequentialDataSet(inputs, targets) for i in xrange(0, len(self.dataframe) - 1): data.newSequence() ins = self.dataframe.ix[i].values target = self.dataframe.ix[i + 1].values[0] data.appendLinked(ins, target) self.data = data
def create_data(dataframe, inputs, targets): data = SequentialDataSet(inputs, targets) for i in range(0, dataframe.shape[0] - 1): row = dataframe.iloc[i] data.newSequence() ins = row.values target = dataframe.iloc[i + 1].values[0] data.appendLinked(ins, target) return data
def buildAppropriateDataset(module): """ build a sequential dataset with 2 sequences of 3 samples, with arndom input and target values, but the appropriate dimensions to be used on the provided module. """ if module.sequential: d = SequentialDataSet(module.indim, module.outdim) for dummy in range(2): d.newSequence() for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) else: d = SupervisedDataSet(module.indim, module.outdim) for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) return d
def buildAppropriateDataset(module): """ build a sequential dataset with 2 sequences of 3 samples, with arndom input and target values, but the appropriate dimensions to be used on the provided module. """ if module.sequential: d = SequentialDataSet(module.indim, module.outdim) for dummy in range(2): d.newSequence() for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) else: d = SupervisedDataSet(module.indim, module.outdim) for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) return d
def trainNetwork(dirname): numFeatures = 5000 ds = SequentialDataSet(numFeatures, 1) tracks = glob.glob(os.path.join(dirname, 'train??.wav')) for t in tracks: track = os.path.splitext(t)[0] # load training data print "Reading %s..." % track data = numpy.genfromtxt(track + '_seg.csv', delimiter=",") labels = numpy.genfromtxt(track + 'REF.txt', delimiter='\t')[0::10,1] numData = data.shape[0] # add the input to the dataset print "Adding to dataset..." ds.newSequence() for i in range(numData): ds.addSample(data[i], (labels[i],)) # initialize the neural network print "Initializing neural network..." net = buildNetwork(numFeatures, 50, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # train the network on the dataset print "Training neural net" trainer = RPropMinusTrainer(net, dataset=ds) ## trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1) error = -1 for i in range(100): new_error = trainer.train() print "error: " + str(new_error) if abs(error - new_error) < 0.1: break error = new_error # save the network print "Saving neural network..." NetworkWriter.writeToFile(net, os.path.basename(dirname) + 'net')
def train(self, params): n = params['encoding_num'] net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) net.reset() ds = SequentialDataSet(n, n) trainer = RPropMinusTrainer(net, dataset=ds) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i - 1]: ds.addSample(self.encoder.encode(history[i - 1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() if len(history) > 1: trainer.trainEpochs(params['num_epochs']) net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = net.activate(self.encoder.encode(symbol)) predictions = self.encoder.classify(output, num=params['num_predictions']) if resets[i]: net.reset() return net
def rnnTrain(data): ds = SequentialDataSet(3,3) s = np.size(input) / 9 ds.newSequence() for idx1 in range(s): ds.appendLinked(data[idx1],(1,0,0)) ds.newSequence() for idx2 in range(s): ds.appendLinked(data[idx2+s],(0,1,0)) ds.newSequence() for idx3 in range(s): ds.appendLinked(data[idx3+s+s],(0,0,1)) net = buildNetwork(3, 8, 3, bias=True, recurrent=True, hiddenclass=LSTMLayer) trainer = BackpropTrainer(net,ds) trainer.trainEpochs(1000) return net
class RWR(DirectSearchLearner): """ Reward-weighted regression. The algorithm is currently limited to discrete-action episodic tasks, subclasses of POMDPTasks. """ # parameters batchSize = 20 # feedback settings verbose = True greedyRuns = 20 supervisedPlotting = False # settings for the supervised training learningRate = 0.005 momentum = 0.9 maxEpochs = 20 validationProportion = 0.33 continueEpochs = 2 # parameters for the variation that uses a value function # TODO: split into 2 classes. valueLearningRate = None valueMomentum = None #valueTrainEpochs = 5 resetAllWeights = False netweights = 0.01 def __init__(self, net, task, valueNetwork=None, **args): self.net = net self.task = task self.setArgs(**args) if self.valueLearningRate == None: self.valueLearningRate = self.learningRate if self.valueMomentum == None: self.valueMomentum = self.momentum if self.supervisedPlotting: from pylab import ion ion() # adaptive temperature: self.tau = 1. # prepare the datasets to be used self.weightedDs = ImportanceDataSet(self.task.outdim, self.task.indim) self.rawDs = ReinforcementDataSet(self.task.outdim, self.task.indim) self.valueDs = SequentialDataSet(self.task.outdim, 1) # prepare the supervised trainers self.bp = BackpropTrainer(self.net, self.weightedDs, self.learningRate, self.momentum, verbose=False, batchlearning=True) # CHECKME: outsource self.vnet = valueNetwork if valueNetwork != None: self.vbp = BackpropTrainer(self.vnet, self.valueDs, self.valueLearningRate, self.valueMomentum, verbose=self.verbose) # keep information: self.totalSteps = 0 self.totalEpisodes = 0 def shapingFunction(self, R): return exp(self.tau * R) def updateTau(self, R, U): self.tau = sum(U) / dot((R - self.task.minReward), U) def reset(self): self.weightedDs.clear() self.valueDs.clear() self.rawDs.clear() self.bp.momentumvector *= 0.0 if self.vnet != None: self.vbp.momentumvector *= 0.0 if self.resetAllWeights: self.vnet.params[:] = randn(len(self.vnet.params)) * self.netweights def greedyEpisode(self): """ run one episode with greedy decisions, return the list of rewards recieved.""" rewards = [] self.task.reset() self.net.reset() while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = argmax(act) self.task.performAction(chosen) reward = self.task.getReward() rewards.append(reward) return rewards def learn(self, batches): self.greedyAvg = [] self.rewardAvg = [] self.lengthAvg = [] self.initr0Avg = [] for b in range(batches): if self.verbose: print print 'Batch', b + 1 self.reset() self.learnOneBatch() self.totalEpisodes += self.batchSize # greedy measure (avg over some greedy runs) rws = 0. for dummy in range(self.greedyRuns): tmp = self.greedyEpisode() rws += (sum(tmp) / float(len(tmp))) self.greedyAvg.append(rws / self.greedyRuns) if self.verbose: print '::', round(rws / self.greedyRuns, 5), '::' def learnOneBatch(self): # collect a batch of runs as experience r0s = [] lens = [] avgReward = 0. for dummy in range(self.batchSize): self.rawDs.newSequence() self.valueDs.newSequence() self.task.reset() self.net.reset() acts, obss, rewards = [], [], [] while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = drawIndex(act) self.task.performAction(chosen) reward = self.task.getReward() obss.append(obs) y = zeros(len(act)) y[chosen] = 1 acts.append(y) rewards.append(reward) avgReward += sum(rewards) / float(len(rewards)) # compute the returns from the list of rewards current = 0 returns = [] for r in reversed(rewards): current *= self.task.discount current += r returns.append(current) returns.reverse() for i in range(len(obss)): self.rawDs.addSample(obss[i], acts[i], returns[i]) self.valueDs.addSample(obss[i], returns[i]) r0s.append(returns[0]) lens.append(len(returns)) r0s = array(r0s) self.totalSteps += sum(lens) avgLen = sum(lens) / float(self.batchSize) avgR0 = mean(r0s) avgReward /= self.batchSize if self.verbose: print '***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')', print 'avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')' print lens # storage: self.rewardAvg.append(avgReward) self.lengthAvg.append(avgLen) self.initr0Avg.append(avgR0) # if self.vnet == None: # # case 1: no value estimator: # prepare the dataset for training the acting network shaped = self.shapingFunction(r0s) self.updateTau(r0s, shaped) shaped /= max(shaped) for i, seq in enumerate(self.rawDs): self.weightedDs.newSequence() for sample in seq: obs, act, dummy = sample self.weightedDs.addSample(obs, act, shaped[i]) # else: # # case 2: value estimator: # # # # train the value estimating network # if self.verbose: print 'Old value error: ', self.vbp.testOnData() # self.vbp.trainEpochs(self.valueTrainEpochs) # if self.verbose: print 'New value error: ', self.vbp.testOnData() # # # produce the values and analyze # rminusvs = [] # sizes = [] # for i, seq in enumerate(self.valueDs): # self.vnet.reset() # seq = list(seq) # for sample in seq: # obs, ret = sample # val = self.vnet.activate(obs) # rminusvs.append(ret-val) # sizes.append(len(seq)) # # rminusvs = array(rminusvs) # shapedRminusv = self.shapingFunction(rminusvs) # # CHECKME: here? # self.updateTau(rminusvs, shapedRminusv) # shapedRminusv /= array(sizes) # shapedRminusv /= max(shapedRminusv) # # # prepare the dataset for training the acting network # rvindex = 0 # for i, seq in enumerate(self.rawDs): # self.weightedDs.newSequence() # self.vnet.reset() # for sample in seq: # obs, act, ret = sample # self.weightedDs.addSample(obs, act, shapedRminusv[rvindex]) # rvindex += 1 # train the acting network tmp1, tmp2 = self.bp.trainUntilConvergence(maxEpochs=self.maxEpochs, validationProportion=self.validationProportion, continueEpochs=self.continueEpochs, verbose=self.verbose) if self.supervisedPlotting: from pylab import plot, legend, figure, clf, draw figure(1) clf() plot(tmp1, label='train') plot(tmp2, label='valid') legend() draw() return avgLen, avgR0
def learn(self,pathdataset=["dstc4_train"], Pathdataroot="data",numberOfHiddenUnit=20, EPOCHS_PER_CYCLE = 10, CYCLES = 40,weightdecayw=0.01): print "Start learning LSTM, and make dictionary file" #Construct dictionary: variable name -> corresponding index of element in i/o vector print "Star make dictionary: variable name -> corresponding index of element in i/o vector" self.dictOut={}#"TOPIC_SLOT_VALUE" -> corresponding index of element self.dictIn={}#"SPEAKER_{val}"or"TOPIC_{val}","WORD_{word}" "BIO_{BIO}", "CLASS_{slot,value}", ""{defined label}-> corresponding index of element #-target vector dictionary index=0 totalNumSlot=0 for topic in self.tagsets.keys(): for slot in self.tagsets[topic].keys(): totalNumSlot+=1 for value in self.tagsets[topic][slot]: self.dictOut[topic+"_"+slot+"_"+value]=index index+=1 print "totalNumSlot:" + str(totalNumSlot) print "outputSize:"+str(len(self.dictOut.keys())) #-input dictionry dataset=[] for pathdat in pathdataset: dataset.append(dataset_walker.dataset_walker(pathdat,dataroot=Pathdataroot,labels=False)) #--(sub input vector 1) Class features i.e., Slot and value ratio (Similar to base line) index=0 for topic in self.tagsets.keys(): for slot in self.tagsets[topic].keys(): if ("CLASS_"+ slot) not in self.dictIn: self.dictIn["CLASS_"+slot]=index index+=1 for value in self.tagsets[topic][slot]: if ("CLASS_"+ value) not in self.dictIn: self.dictIn["CLASS_"+value]=index index+=1 self.TOTALSIZEOFCLASSFeature=index f=open(self.FileNameofNumClassFeature,"wb") pickle.dump(self.TOTALSIZEOFCLASSFeature,f) f.close() #--(sub input vector 2) Sentence features if not self.isUseSentenceRepresentationInsteadofBOW: index=0 for elemDataset in dataset: for call in elemDataset: for (uttr,_) in call: #General info1 (CLASS; this feature must be rejistered at first) if ("SPEAKER_"+uttr["speaker"]) not in self.dictIn: self.dictIn["SPEAKER_"+uttr["speaker"]]=index index+=1 if ("TOPIC_"+uttr["segment_info"]["topic"]) not in self.dictIn: self.dictIn["TOPIC_"+uttr["segment_info"]["topic"]]=index index+=1 #General info2 #-BIO if ("BIO_"+uttr['segment_info']['target_bio']) not in self.dictIn: self.dictIn["BIO_"+uttr['segment_info']['target_bio']]=index index+=1 #BOW if LSTMWithBOWTracker.isIgnoreUtterancesNotRelatedToMainTask: if not (uttr['segment_info']['target_bio'] == "O"): #-BOW splitedtrans=self.__getRegurelisedBOW(uttr["transcript"]) for word in splitedtrans: if ("WORD_"+word) not in self.dictIn: self.dictIn["WORD_"+word]=index index+=1 self.TOTALSIZEOFSENTENCEFeature=index f=open(self.FileNameofNumSentenceFeature,"wb") pickle.dump(self.TOTALSIZEOFSENTENCEFeature,f) f.close() elif self.isUseSentenceRepresentationInsteadofBOW: index=0 for i in range(0,LSTMWithBOWTracker.D2V_VECTORSIZE): self.dictIn[str(index)+"thElemPV"]=index index+=1 index=0 for i in range(0,LSTMWithBOWTracker.D2V_VECTORSIZE): self.dictIn[str(index)+"thAvrWord"]=index index+=1 assert self.D2V_VECTORSIZE == LSTMWithBOWTracker.D2V_VECTORSIZE, "D2V_VECTORSIZE is restrected to be same over the class" else: assert False, "Unexpected block" #--(sub input vector 3) Features M1s defined index=0 if self.isEnableToUseM1sFeature: rejisteredFeatures=self.__rejisterM1sInputFeatureLabel(self.tagsets,dataset) for rFeature in rejisteredFeatures: assert rFeature not in self.dictIn, rFeature +" already registered in input vector. Use different label name. " self.dictIn[rFeature]=index index+=1 self.TOTALSIZEOFM1DEFINEDFeature=index f=open(self.FileNameofNumM1Feature,"wb") pickle.dump(self.TOTALSIZEOFM1DEFINEDFeature,f) f.close() print "inputSize:"+str(len(self.dictIn.keys())) assert self.dictIn["CLASS_INFO"] == 0, "Unexpected index CLASS_INFO should has value 0" assert self.dictIn["CLASS_Fort Siloso"] == 334, "Unexpected index CLASS_Fort Siloso should has value 334" assert self.dictIn["CLASS_Yunnan"] == 1344, "Unexpected index CLASS_Yunnan should has value 1611" #--write fileObject = open('dictInput.pic', 'w') pickle.dump(self.dictIn, fileObject) fileObject.close() fileObject = open('dictOutput.pic', 'w') pickle.dump(self.dictOut, fileObject) fileObject.close() #Build RNN frame work print "Start learning Network" #Capability of network is: (30 hidden units can represents 1048576 relations) wherease (10 hidden units can represents 1024) #Same to Henderson (http://www.aclweb.org/anthology/W13-4073)? net = buildNetwork(len(self.dictIn.keys()), numberOfHiddenUnit, len(self.dictOut.keys()), hiddenclass=LSTMLayer, outclass=SigmoidLayer, outputbias=False, recurrent=True) #Train network #-convert training data into sequence of vector convDataset=[]#[call][uttr][input,targetvec] iuttr=0 convCall=[] for elemDataset in dataset: for call in elemDataset: for (uttr,label) in call: if self.isIgnoreUtterancesNotRelatedToMainTask: if uttr['segment_info']['target_bio'] == "O": continue #-input convInput=self._translateUtteranceIntoInputVector(uttr,call) #-output convOutput=[0.0]*len(self.dictOut.keys())#Occured:+1, Not occured:0 if "frame_label" in label: for slot in label["frame_label"].keys(): for value in label["frame_label"][slot]: convOutput[self.dictOut[uttr["segment_info"]["topic"]+"_"+slot+"_"+value]]=1 #-post proccess if self.isSeparateDialogIntoSubDialog: if uttr['segment_info']['target_bio'] == "B": if len(convCall) > 0: convDataset.append(convCall) convCall=[] convCall.append([convInput,convOutput]) #print "Converted utterance" + str(iuttr) iuttr+=1 if not self.isSeparateDialogIntoSubDialog: if len(convCall) > 0: convDataset.append(convCall) convCall=[] #Online learning trainer = RPropMinusTrainer(net,weightdecay=weightdecayw) EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): #Shuffle order ds = SequentialDataSet(len(self.dictIn.keys()),len(self.dictOut.keys())) datInd=range(0,len(convDataset)) random.shuffle(datInd)#Backpropergation already implemeted data shuffling, however though RpropMinus don't. for ind in datInd: ds.newSequence() for convuttr in convDataset[ind]: ds.addSample(convuttr[0],convuttr[1]) #Evaluation and Train epoch = (i+1) * EPOCHS_PER_CYCLE print "\r epoch {}/{} Error={}".format(epoch, EPOCHS,trainer.testOnData(dataset=ds)) stdout.flush() trainer.trainOnDataset(dataset=ds,epochs=EPOCHS_PER_CYCLE) NetworkWriter.writeToFile(trainer.module, "LSTM_"+"Epoche"+str(i+1)+".rnnw") NetworkWriter.writeToFile(trainer.module, "LSTM.rnnw")
def trainNetwork(dirname): numFeatures = 2000 ds = SequentialDataSet(numFeatures, 1) tracks = glob.glob(os.path.join(dirname, "*.csv")) for t in tracks: track = os.path.splitext(t)[0] # load training data print "Reading %s..." % t data = numpy.genfromtxt(t, delimiter=",") numData = data.shape[0] # add the input to the dataset print "Adding to dataset..." ds.newSequence() for i in range(numData): # ds.addSample(data[i], (labels[i],)) input = data[i] label = input[numFeatures] if label > 0: label = midi_util.frequencyToMidi(label) ds.addSample(input[0:numFeatures], (label,)) # initialize the neural network print "Initializing neural network..." # net = buildNetwork(numFeatures, 50, 1, # hiddenclass=LSTMLayer, bias=True, recurrent=True) # manual network building net = RecurrentNetwork() inlayer = LinearLayer(numFeatures) # h1 = LSTMLayer(70) # h2 = SigmoidLayer(50) octaveLayer = LSTMLayer(5) noteLayer = LSTMLayer(12) combinedLayer = SigmoidLayer(60) outlayer = LinearLayer(1) net.addInputModule(inlayer) net.addOutputModule(outlayer) # net.addModule(h1) # net.addModule(h2) net.addModule(octaveLayer) net.addModule(noteLayer) net.addModule(combinedLayer) # net.addConnection(FullConnection(inlayer, h1)) # net.addConnection(FullConnection(h1, h2)) # net.addConnection(FullConnection(h2, outlayer)) net.addConnection(FullConnection(inlayer, octaveLayer)) net.addConnection(FullConnection(inlayer, noteLayer)) # net.addConnection(FullConnection(octaveLayer,combinedLayer)) for i in range(5): net.addConnection( FullConnection( octaveLayer, combinedLayer, inSliceFrom=i, inSliceTo=i + 1, outSliceFrom=i * 12, outSliceTo=(i + 1) * 12 ) ) net.addConnection(FullConnection(noteLayer, combinedLayer)) net.addConnection(FullConnection(combinedLayer, outlayer)) net.sortModules() # train the network on the dataset print "Training neural net" trainer = RPropMinusTrainer(net, dataset=ds) ## trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1) error = -1 for i in range(150): new_error = trainer.train() print "error: " + str(new_error) if abs(error - new_error) < 0.005: break error = new_error # save the network print "Saving neural network..." NetworkWriter.writeToFile(net, os.path.basename(dirname) + "designnet")
def predict_ball(hidden_nodes, is_elman=True, training_data=16, epoch=-1, parameters={}, predict_count=16): # build rnn n = construct_network(hidden_nodes, is_elman) # make training data ep = 1 if epoch < 0 else epoch initial_p = [9., 7.] initial_v = [1., 1.] # initial_v = ball_data.gen_velocity(BOX_SIZE) data_set = ball_data.bounce_ball((training_data + 1) * ep, BOX_SIZE, initial_p=initial_p, initial_v=initial_v) total_avg = np.average(data_set, axis=0) total_std = np.std(data_set, axis=0) total_std[2] = 1. total_std[3] = 1. # initial_p = data_set[np.random.choice(range(training_data))][:2] training_ds = [] print("data_set = {}".format(data_set)) normalized_d = __normalize(data_set, total_avg, total_std) # print("normalized_d = {}".format(normalized_d)) for e_index in range(ep): t_ds = SequentialDataSet(4, 4) t_ds.newSequence() e_begin = e_index * training_data for j in range(e_begin, e_begin + training_data): # from current, predict next p_in = normalized_d[0].tolist() p_out = normalized_d[j + 1].tolist() t_ds.addSample(p_in, p_out) training_ds.append(t_ds) # training network err1 = 0 if epoch < 0: trainer = BackpropTrainer(n, training_ds[0], learningrate=3e-4, weightdecay=1e-2, verbose=True) err1 = trainer.trainEpochs(20000) else: trainer = BackpropTrainer(n, **parameters) epoch_errs = [] for ds in training_ds: trainer.setData(ds) epoch_errs.append(trainer.train()) err1 = max(epoch_errs) # predict predict = None next_pv = np.hstack((initial_p, initial_v)) n.reset() for i in range(predict_count): predict = next_pv if predict is None else np.vstack((predict, next_pv)) # print("predict = {}".format(predict)) p_normalized = (data_set[0] - total_avg) / total_std next_pv = n.activate(p_normalized.tolist()) restored = np.array(next_pv) * total_std + total_avg next_pv = restored print("restored, answer = {}, {}".format(restored, data_set[i + 1])) real = ball_data.bounce_ball(predict_count, BOX_SIZE, initial_p, initial_v) err_matrix = (predict - real) ** 2 err_distance = np.sqrt(np.sum(err_matrix[:, 0:2], axis=1)).reshape((predict_count, 1)) err_velocity = np.sum(np.sqrt(err_matrix[:, 2:4]), axis=1).reshape((predict_count, 1)) err2 = np.hstack((err_distance, err_velocity)) return predict, real, err1, err2
#sanity check: print("Test wordVecToString: eisenhorn") eiseWord = "eisenhorn" numArray = [wordToNum[x] for x in eiseWord] letterArray = [numToWord[x] for x in numArray] print("Result:",eiseWord,",".join([str(x) for x in numArray]),"".join(letterArray)) #create the training set #takes word vector -> simple enumerated vector of words from vocab print("Creating the DataSet") dataSet = SequentialDataSet(inputLayerSize,outputLayerSize) dataSet.newSequence() for sample,next in zip(blob[0:sampleSize],cycle(blob[1:sampleSize+1])): try: if sample == ' ': dataSet.newSequence() #print("creating Sample of:",sample,next) actual = [0.0 for x in range(inputLayerSize)] actual[wordToNum[sample]] = 1.0 expected = [0.0 for x in range(inputLayerSize)] expected[wordToNum[next]] = 1.0 dataSet.appendLinked(actual,expected) except KeyError as e: print("Missing: ",str(e)) #print("Something went wrong for:",sample,next) print("Data Set Created: ",dataSet.getNumSequences())
# Get queries/hr data with open('dns.json', 'r') as f: samples = map(lambda x: x['ts'][:-2], json.load(f)) # Shuffle to partition test/train random.shuffle(samples) # Set train & test data train_data, test_data = samples[:50], samples[200:] # Initialize ds for rnn for 1 obsv and 1 next ds = SequentialDataSet(1, 1) # Add each timeseries (ts) for ts in train_data: ds.newSequence() # Add obsv and next for t_1, t_2 in zip(ts, ts[1:]): ds.addSample(t_1, t_2) # RNN with 1-5-1 architecture: 1 input, 5 hidden, 1 output layer rnn = buildNetwork(1, 5, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # Initialize trainer trainer = RPropMinusTrainer(rnn, dataset=ds)
y_found = True y_seq.append(1) else: y_seq.append(0) ys.append(1 if y_found else 0) ys_seq.append(y_seq) xs.append(row) maxlen = max(len(row), maxlen) max_features=generator.max_id() + 2 train_ds = SequentialDataSet(max_features, 1) for xrow, yrow in zip(xs, ys_seq): train_ds.newSequence() for x,y in zip(xrow, yrow): one_hot = [0] * max_features one_hot[x] = 1 train_ds.addSample(one_hot, y) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer net = buildNetwork(max_features, 32, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) from pybrain.supervised import RPropMinusTrainer trainer = RPropMinusTrainer(net, dataset=train_ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5
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_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']) compound_prediction = int(options_dictionary['compound_prediction']) #teacher_forced_transient = int(options_dictionary['teacher_forced_transient']) teacher_forced_transient = 0 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 = SequentialDataSet(num_predictors, num_outputs) print 'reach' 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] 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=LinearLayer) network.sortModules(); 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() #brittle network_file_path = output_location+'trained_network.xml' NetworkWriter.writeToFile(network, network_file_path) done_file_handle = open(output_location+'training_done.txt',"w") done_file_handle.write('%s' % 'done!') done_file_handle.close()
def evalSensor(sensorIndex, featureSpaceIndices): #reset data structure allByTime = {} f = open(fileName, 'r') headers = f.readline().split() for line in f: splited = line.split() timeStamp = int(splited[0]) allByTime[timeStamp] = {} f.close() allByJoint = {} for inputIndices in featureSpaceIndices: allByJoint[inputIndices] = {} clfs = {} grades = {} for inputIndices in featureSpaceIndices: allByTime, allByJoint = angleExtraction.prepareAnglesFromInput(fileName, inputIndices, sensorIndex, True, allByTime, allByJoint) #normalizing allByJoint timeSet = Set([]) for inputIndices in featureSpaceIndices: vec = [] for timeStamp in allByTime.keys(): if(timeStamp in allByJoint[inputIndices].keys()): timeSet.add(timeStamp) x = allByJoint[inputIndices][timeStamp] vec.append(x) if(len(vec) > 0): vec = angleExtraction.normelizeVector(vec) i=0 for timeStamp in allByTime.keys(): if(timeStamp in allByJoint[inputIndices].keys()): allByJoint[inputIndices][timeStamp] = vec[i] i = i + 1 #time set to list, output dict to list time = [] for timeStamp in timeSet: time.append(timeStamp) time.sort() allOutput = [] tmpTime = [] #clean zeros, create time ordered output vector for timeStamp in time: out = allByTime[timeStamp]['output'] if(out != 0 and len(allByTime[timeStamp]) == featureNum + 1): tmpTime.append(timeStamp) allOutput.append(out) time = tmpTime #normalize allOutput allOutput = normalByPercentile(allOutput) #create a net hiddenSize = (featureNum + 2)/2 net = buildNetwork(featureNum, hiddenSize, 1, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True, bias=True) #build dataset ds = SequentialDataSet(featureNum, 1) i=0 lastTimeStamp = time[0] for timeStamp in time: if(len(allByTime[timeStamp]) == featureNum+1):#it is a full vector if(timeStamp - lastTimeStamp > 100): ds.newSequence() sample = [] for inputIndices in featureSpaceIndices: sample.append(allByTime[timeStamp][inputIndices]) ds.appendLinked(sample, allOutput[i]) i = i + 1 lastTimeStamp = timeStamp #train net.randomize() tstdata, trndata = ds.splitWithProportion( 0.25 ) trainer = BackpropTrainer(net, trndata) print len(ds) min = 100 trainNum = 100 bestTrainer = None for i in range(trainNum): res = trainer.train() if(res < min): min = res bestTrainer = trainer net.randomize() print min """ res = 100 while(res > min): net.randomize() res = trainer.train() """ trainer = bestTrainer for i in range(trainNum): res = trainer.train() if(i % (trainNum/10) == 0): print res print 'trndata.evaluateModuleMSE ' + str(trndata.evaluateModuleMSE(net)) print 'tstdata.evaluateModuleMSE ' + str(tstdata.evaluateModuleMSE(net)) #print net.activateOnDataset(tstdata) hits = 0.0 total = 0.0 #res = net.activate(tstdata) for i in range(trndata.getNumSequences()): for input, target in trndata.getSequenceIterator(i): res = net.activate(input) total += 1 if(res[0]*target[0] > 0): hits+=1 grade = hits / total print 'grade ' + str(grade) print 'total ' + str(total)
class simple_rnn_classifier(): def __init__(self, ds): self.alldata = SequentialDataSet(ds.num_features, 1) # Now add the samples to the data set. idx = 1 self.alldata.newSequence() for sample in ds.all_moves: self.alldata.addSample(sample.get_features(), [ds.get_classes().index(sample.class_)]) idx += 1 if (idx%6 == 0): self.alldata.newSequence() self.tstdata, self.trndata = self.alldata.splitWithProportion(0.25) #print "Number of training 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] # 5 hidden layers. self.rnn = buildNetwork(self.trndata.indim, 3, self.trndata.outdim, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True) self.rnn.randomize() self.trainer = BackpropTrainer(self.nn, dataset=self.trndata, momentum=0.1, verbose=True, weightdecay=0.01) def start_training(self): f = open("./results/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.fnn.sortModules() for Idx in range (len(self.tstdata)): out = self.fnn.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.fnn, "./results/" + name + "rnn.xml")
class NET(): def __init__(self, inputsize, outputsize, hiden=[1]): self.inputsize = inputsize self.outputsize = outputsize self.hiden = hiden self.err = 1 self.old_err = 1 #print type(self.hiden) if type(self.hiden) == str: #print "type str" self.hiden = self.hiden[1:-1] b = self.hiden.split(", ") c = [] for i in b: c.append(int(i)) self.hiden = c[:] b = [] b.append(self.inputsize) b += self.hiden b.append(self.outputsize) #print b#"%s, %s, %s, hiddenclass=TanhLayer"%(self.inputsize, self.hiden, self.outputsize) self.net = FeedForwardNetwork() self.inputlayer = LinearLayer(self.inputsize, "Input") self.net.addInputModule(self.inputlayer) self.outputlayer = LinearLayer(self.outputsize, "Output") self.net.addOutputModule(self.outputlayer) self.hidenlayers = [] for i in xrange(len(self.hiden)): self.hidenlayers.append(SigmoidLayer(self.hiden[i], "hiden%s" % i)) self.net.addModule(self.hidenlayers[-1]) self.net.addConnection( FullConnection(self.inputlayer, self.outputlayer)) for i in xrange(len(self.hidenlayers)): self.net.addConnection( FullConnection(self.inputlayer, self.hidenlayers[i])) self.net.addConnection( FullConnection(self.hidenlayers[i], self.outputlayer)) for i in xrange(len(self.hidenlayers)): for j in xrange(i + 1, len(self.hidenlayers)): self.net.addConnection( FullConnection(self.hidenlayers[i], self.hidenlayers[j])) #self.print_conections(self.net) self.net.sortModules() self.ds = SupervisedDataSet(self.inputsize, self.outputsize) def Update(self, hiden, h): self.net = FeedForwardNetwork() self.inputlayer = LinearLayer(self.inputsize, "Input") self.net.addInputModule(self.inputlayer) self.outputlayer = LinearLayer(self.outputsize, "Output") self.net.addOutputModule(self.outputlayer) self.hidenlayers = [] for i in xrange(len(hiden)): self.hidenlayers.append(SigmoidLayer(hiden[i], "hiden%s" % i)) self.net.addModule(self.hidenlayers[-1]) self.net.addConnection( FullConnection(self.inputlayer, self.outputlayer)) for i in xrange(len(self.hidenlayers)): self.net.addConnection( FullConnection(self.inputlayer, self.hidenlayers[i])) self.net.addConnection( FullConnection(self.hidenlayers[i], self.outputlayer)) for i in xrange(len(self.hidenlayers)): for j in xrange(i + 1, len(self.hidenlayers)): if i < h: self.net.addConnection( FullConnection(self.hidenlayers[i], self.hidenlayers[j])) elif i == h: self.net.addConnection( FullConnection(self.hidenlayers[i], self.hidenlayers[j], inSliceTo=hiden[i] - 1)) else: self.net.addConnection( FullConnection(self.hidenlayers[i], self.hidenlayers[j])) #self.print_conections(self.net) self.net.sortModules() self.hiden = hiden def print_conections(self, n): print("BEGIN") for mod in n.modules: print(mod) for conn in n.connections[mod]: print(conn) for cc in range(len(conn.params)): print(conn.whichBuffers(cc), conn.params[cc]) print("END") def AddData(self, datainput, dataoutput): if len(dataoutput) != len(datainput): print("Not equals data", len(dataoutput), len(datainput)) return 1 self.ds = SupervisedDataSet(self.inputsize, self.outputsize) for i in xrange(len(dataoutput)): self.ds.appendLinked(datainput[i], dataoutput[i]) self.trainer = RPropMinusTrainer(self.net, dataset=self.ds, learningrate=0.1) return 0 def AddDataSequential(self, data): self.ds = SequentialDataSet(self.inputsize, self.outputsize) for i in xrange(len(data) - 1, 0, -1): t = data[i] k = i - 1 while k > -1: self.ds.appendLinked(data[k], t) k -= 1 self.ds.newSequence() """print self.ds.getNumSequences() for i in range(self.ds.getNumSequences()): for input, target in self.ds.getSequenceIterator(i): print i, TransToIntList_45(input), TransToIntList_45(target)""" self.trainer = RPropMinusTrainer(self.net, dataset=self.ds, learningrate=0.1) return 0 def TrainNet(self, epoch, error): if epoch <= 5: epoch = 5 i = 0 count = 0 while i < epoch: if error == self.err: break self.err = self.trainer.train() if self.err == self.old_err: count += 1 else: count = 0 if count == 3: self.err = self.old_err return (self.err, 1) self.old_err = self.err i += 1 #self.SaveNet('%s %s_%s_%s.work'%(self.err, self.inputsize, self.hiden, self.outputsize)) return [self.err, 0] def TrainNetOnce(self): self.err = self.trainer.train() return self.err def SaveNet(self, filename=None): if filename == None: NetworkWriter.writeToFile( self.net, '%s %s_%s_%s.xml' % (self.err, self.inputsize, self.hiden, self.outputsize)) else: NetworkWriter.writeToFile(self.net, filename) def LoadNet(self, fname): self.net = NetworkReader.readFrom(fname) tree = ET.parse(fname) x = tree.getroot() l = [] for modules in x.findall('Network/Modules/SigmoidLayer/dim'): l.append(int(modules.get("val"))) self.hiden = l[:] self.inputsize = self.net.indim self.outputsize = self.net.outdim def TestNet(self, inp): if len(inp) != self.inputsize: return 0 return self.net.activate(inp[:]) def UpdateWeights(self, f1, f2=None): n = NetworkReader.readFrom(f1) if f2 != None: n2 = NetworkReader.readFrom(f2) def DictParams(n): l1 = [] for mod in n.modules: l = [] for conn in n.connections[mod]: if conn.paramdim > 0: l.append([conn.outmod.name, conn.params]) d = dict(l) l1.append([mod.name, d]) d1 = dict(l1) return d1 d1 = DictParams(n) if f2 != None: d2 = DictParams(n2) d3 = DictParams(self.net) params = np.array([]) if f2 != None: for i in d2: for j in d2[i]: b = d3[i][j][:] b[:d2[i][j].size] = d2[i][j][:] d3[i].update({j: b}) for i in d1: for j in d1[i]: b = d3[i][j][:] b[:d1[i][j].size] = d1[i][j][:] d3[i].update({j: b}) for i in d3["Input"]: params = np.hstack((params, d3["Input"][i])) for i in xrange(len(self.hiden)): for j in d3["hiden%s" % i]: params = np.hstack((params, d3["hiden%s" % i][j])) self.net._setParameters(params)
class LanguageLearner: __OUTPUT = "Sample at {0} epochs (prompt=\"{1}\", length={2}): {3}" def __init__(self, trainingText, hiddenLayers, hiddenNodes): self.__initialized = False with open(trainingText) as f: self.raw = f.read() self.characters = list(self.raw) self.rawData = list(map(ord, self.characters)) print("Creating alphabet mapping...") self.mapping = [] for charCode in self.rawData: if charCode not in self.mapping: self.mapping.append(charCode) print("Mapping of " + str(len(self.mapping)) + " created.") print(str(self.mapping)) print("Converting data to mapping...") self.data = [] for charCode in self.rawData: self.data.append(self.mapping.index(charCode)) print("Done.") self.dataIn = self.data[:-1:] self.dataOut = self.data[1::] self.inputs = 1 self.hiddenLayers = hiddenLayers self.hiddenNodes = hiddenNodes self.outputs = 1 def initialize(self, verbose): print("Initializing language learner...") self.verbose = verbose # Create network and modules self.net = RecurrentNetwork() inp = LinearLayer(self.inputs, name="in") hiddenModules = [] for i in range(0, self.hiddenLayers): hiddenModules.append(LSTMLayer(self.hiddenNodes, name=("hidden-" + str(i + 1)))) outp = LinearLayer(self.outputs, name="out") # Add modules to the network with recurrence self.net.addOutputModule(outp) self.net.addInputModule(inp) for module in hiddenModules: self.net.addModule(module) # Create connections self.net.addConnection(FullConnection(self.net["in"], self.net["hidden-1"])) for i in range(0, len(hiddenModules) - 1): self.net.addConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 2)])) self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 1)])) self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))], self.net["hidden-" + str(len(hiddenModules))])) self.net.addConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))], self.net["out"])) self.net.sortModules() self.trainingSet = SequentialDataSet(self.inputs, self.outputs) for x, y in zip(self.dataIn, self.dataOut): self.trainingSet.newSequence() self.trainingSet.appendLinked([x], [y]) self.net.randomize() print("Neural network initialzed with structure:") print(self.net) self.trainer = BackpropTrainer(self.net, self.trainingSet, verbose=verbose) self.__initialized = True print("Successfully initialized network.") def train(self, epochs, frequency, prompt, length): if not self.__initialized: raise Exception("Attempted to train uninitialized LanguageLearner") print ("Beginning training for " + str(epochs) + " epochs...") if frequency >= 0: print(LanguageLearner.__OUTPUT.format(0, prompt, length, self.sample(prompt, length))) for i in range(1, epochs): print("Error at " + str(i) + " epochs: " + str(self.trainer.train())) if i % frequency == 0: print(LanguageLearner.__OUTPUT.format(i, prompt, length, self.sample(prompt, length))) print("Completed training.") def sample(self, prompt, length): self.net.reset() if prompt == None: prompt = chr(random.choice(self.mapping)) output = prompt charCode = ord(prompt) for i in range(0, length): sampledResult = self.net.activate([charCode]) charCode = int(round(sampledResult[0])) if charCode < 0 or charCode >= len(self.mapping): return output + "#TERMINATED_SAMPLE(reason: learner guessed invalid character)" output += chr(self.mapping[charCode]) return output
class RWR(DirectSearchLearner): """ Reward-weighted regression. The algorithm is currently limited to discrete-action episodic tasks, subclasses of POMDPTasks. """ # parameters batchSize = 20 # feedback settings verbose = True greedyRuns = 20 supervisedPlotting = False # settings for the supervised training learningRate = 0.005 momentum = 0.9 maxEpochs = 20 validationProportion = 0.33 continueEpochs = 2 # parameters for the variation that uses a value function # TODO: split into 2 classes. valueLearningRate = None valueMomentum = None #valueTrainEpochs = 5 resetAllWeights = False netweights = 0.01 def __init__(self, net, task, valueNetwork=None, **args): self.net = net self.task = task self.setArgs(**args) if self.valueLearningRate == None: self.valueLearningRate = self.learningRate if self.valueMomentum == None: self.valueMomentum = self.momentum if self.supervisedPlotting: from pylab import ion ion() # adaptive temperature: self.tau = 1. # prepare the datasets to be used self.weightedDs = ImportanceDataSet(self.task.outdim, self.task.indim) self.rawDs = ReinforcementDataSet(self.task.outdim, self.task.indim) self.valueDs = SequentialDataSet(self.task.outdim, 1) # prepare the supervised trainers self.bp = BackpropTrainer(self.net, self.weightedDs, self.learningRate, self.momentum, verbose=False, batchlearning=True) # CHECKME: outsource self.vnet = valueNetwork if valueNetwork != None: self.vbp = BackpropTrainer(self.vnet, self.valueDs, self.valueLearningRate, self.valueMomentum, verbose=self.verbose) # keep information: self.totalSteps = 0 self.totalEpisodes = 0 def shapingFunction(self, R): return exp(self.tau * R) def updateTau(self, R, U): self.tau = sum(U) / dot((R - self.task.minReward), U) def reset(self): self.weightedDs.clear() self.valueDs.clear() self.rawDs.clear() self.bp.momentumvector *= 0.0 if self.vnet != None: self.vbp.momentumvector *= 0.0 if self.resetAllWeights: self.vnet.params[:] = randn(len( self.vnet.params)) * self.netweights def greedyEpisode(self): """ run one episode with greedy decisions, return the list of rewards recieved.""" rewards = [] self.task.reset() self.net.reset() while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = argmax(act) self.task.performAction(chosen) reward = self.task.getReward() rewards.append(reward) return rewards def learn(self, batches): self.greedyAvg = [] self.rewardAvg = [] self.lengthAvg = [] self.initr0Avg = [] for b in range(batches): if self.verbose: print() print(('Batch', b + 1)) self.reset() self.learnOneBatch() self.totalEpisodes += self.batchSize # greedy measure (avg over some greedy runs) rws = 0. for dummy in range(self.greedyRuns): tmp = self.greedyEpisode() rws += (sum(tmp) / float(len(tmp))) self.greedyAvg.append(rws / self.greedyRuns) if self.verbose: print(('::', round(rws / self.greedyRuns, 5), '::')) def learnOneBatch(self): # collect a batch of runs as experience r0s = [] lens = [] avgReward = 0. for dummy in range(self.batchSize): self.rawDs.newSequence() self.valueDs.newSequence() self.task.reset() self.net.reset() acts, obss, rewards = [], [], [] while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = drawIndex(act) self.task.performAction(chosen) reward = self.task.getReward() obss.append(obs) y = zeros(len(act)) y[chosen] = 1 acts.append(y) rewards.append(reward) avgReward += sum(rewards) / float(len(rewards)) # compute the returns from the list of rewards current = 0 returns = [] for r in reversed(rewards): current *= self.task.discount current += r returns.append(current) returns.reverse() for i in range(len(obss)): self.rawDs.addSample(obss[i], acts[i], returns[i]) self.valueDs.addSample(obss[i], returns[i]) r0s.append(returns[0]) lens.append(len(returns)) r0s = array(r0s) self.totalSteps += sum(lens) avgLen = sum(lens) / float(self.batchSize) avgR0 = mean(r0s) avgReward /= self.batchSize if self.verbose: print(( '***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')', )) print(('avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')')) print(lens) # storage: self.rewardAvg.append(avgReward) self.lengthAvg.append(avgLen) self.initr0Avg.append(avgR0) # if self.vnet == None: # # case 1: no value estimator: # prepare the dataset for training the acting network shaped = self.shapingFunction(r0s) self.updateTau(r0s, shaped) shaped /= max(shaped) for i, seq in enumerate(self.rawDs): self.weightedDs.newSequence() for sample in seq: obs, act, dummy = sample self.weightedDs.addSample(obs, act, shaped[i]) # else: # # case 2: value estimator: # # # # train the value estimating network # if self.verbose: print('Old value error: ', self.vbp.testOnData()) # self.vbp.trainEpochs(self.valueTrainEpochs) # if self.verbose: print('New value error: ', self.vbp.testOnData()) # # # produce the values and analyze # rminusvs = [] # sizes = [] # for i, seq in enumerate(self.valueDs): # self.vnet.reset() # seq = list(seq) # for sample in seq: # obs, ret = sample # val = self.vnet.activate(obs) # rminusvs.append(ret-val) # sizes.append(len(seq)) # # rminusvs = array(rminusvs) # shapedRminusv = self.shapingFunction(rminusvs) # # CHECKME: here? # self.updateTau(rminusvs, shapedRminusv) # shapedRminusv /= array(sizes) # shapedRminusv /= max(shapedRminusv) # # # prepare the dataset for training the acting network # rvindex = 0 # for i, seq in enumerate(self.rawDs): # self.weightedDs.newSequence() # self.vnet.reset() # for sample in seq: # obs, act, ret = sample # self.weightedDs.addSample(obs, act, shapedRminusv[rvindex]) # rvindex += 1 # train the acting network tmp1, tmp2 = self.bp.trainUntilConvergence( maxEpochs=self.maxEpochs, validationProportion=self.validationProportion, continueEpochs=self.continueEpochs, verbose=self.verbose) if self.supervisedPlotting: from pylab import plot, legend, figure, clf, draw figure(1) clf() plot(tmp1, label='train') plot(tmp2, label='valid') legend() draw() return avgLen, avgR0
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']) compound_prediction = int(options_dictionary['compound_prediction']) # teacher_forced_transient = int(options_dictionary['teacher_forced_transient']) teacher_forced_transient = 0 prediction_dataset = SequentialDataSet(num_predictors, num_outputs) previous_sequence_number = 1 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) network = NetworkReader.readFrom(network_location) if compound_prediction==0: results, targets, mse = evalRNN.evalRNNOnSeqDataset(network,prediction_dataset) elif compound_prediction==1: results, targets, mse = evalRNN.compoundEvalRNNOnSeqDataset(network,prediction_dataset, teacher_forced_transient) results_length, results_width = results.shape np.savetxt(prediction_results_file_location,results,delimiter=" ",fmt='%9.9f') done_file_handle = open(output_location+'predicting_done.txt',"w") done_file_handle.write('%s' % 'done!') done_file_handle.close()
def to_bitmask(i): return i files = glob.glob('reel_nopickup/*.csv') #16th notes tunes = [] for filename in files: #tune with open(filename) as f: notes = map(int, map(str.strip, f.readlines()))[::2] #8th ntoes bitmasks = map(to_bitmask, notes) tunes.append(bitmasks) nested_tunes = map(lambda tune: [tune[x:min(len(tune) + 1, x+4)] for x in range(0, len(tune), 4)], tunes) for tune in nested_tunes: for (inp, target) in zip(tune, tune[1:]): chord_ds.newSequence() chord_ds.appendLinked(inp[0], target[0]) for tune in tunes: for beat in tunes: for (inp, target) in zip(beat, beat[1:]): melody_ds.newSequence() melody_ds.appendLinked(inp, target) chord_network.randomize() melody_network.randomize() chord_trainer = BackpropTrainer(chord_network, chord_ds, learningrate=.00001, momentum=.9) melody_trainer = BackpropTrainer(melody_network, melody_ds, learningrate=.00001, momentum=.9) import time print "training chords..."
def chunkifyDataWindowSequentialDS(self, chunkLen, predGap, windowLen, includeDST=True): nData = self.aceVals.shape[0] # List of chunks of the form [([pDyn, Bz, DST], DST_future) .... chunkLen] allChunks = [] currChunk = [] # Build out the allChunks for i in range(nData): if i + predGap >= nData or i - windowLen < 0: continue windowRec = np.ones(5*(windowLen+1)) for j in range(windowLen+1): windowRec[5*j+0] = np.sqrt(self.aceVals[i-j,6]) windowRec[5*j+1] = self.aceVals[i-j,5]*self.aceVals[i-j,9] windowRec[5*j+2] = self.aceVals[i-j,5] windowRec[5*j+3] = self.aceVals[i-j,9] windowRec[5*j+4] = self.dstVals[i-j,1] rec = (windowRec, self.dstVals[i+predGap,1]) currChunk.append(rec) if len(currChunk) == chunkLen: allChunks.append(currChunk) currChunk = [] # Take only certain chunks chunks = [] for chunk in allChunks: chunks.append(chunk) # Uncomment for shuffled records #random.shuffle(chunks) # Normalize norms = np.zeros(len(chunks[0][0][0])) for c in chunks: for rec in c: windowRec = rec[0] for k in range(len(windowRec)): norms[k] = max(norms[k], abs(windowRec[k])) for c in chunks: for rec in c: windowRec = rec[0] for k in range(len(windowRec)): windowRec[k] = windowRec[k] / norms[k] # Build out raw arrays of data from the chunks trainFrac = .8 testFrac = .2 trainInput = [] trainOutput = [] testInput = [] testOutput = [] for iChunk in range(len(chunks)): # Add it to the training set if iChunk / (float(len(chunks))) <= trainFrac: for rec in chunks[iChunk]: trainInput.append(rec[0]) trainOutput.append(rec[1]) # Add it to the testing set else: for rec in chunks[iChunk]: testInput.append(rec[0]) testOutput.append(rec[1]) # Convert the lists to numpy arrays trainInput = np.array(trainInput) trainOutput = np.array(trainOutput).reshape(len(trainOutput),1) testInput = np.array(testInput) testOutput = np.array(testOutput).reshape(len(testOutput),1) # Remove DST if it's not wanted if not includeDST: dstCol = 4 # This index, and the increment below, must be kept # consistent if the record size changes while dstCol < trainInput.shape[1]: trainInput = np.delete(trainInput, dstCol, 1) testInput = np.delete(testInput, dstCol, 1) dstCol += 4 # Put the values in to dataset nInputs = trainInput.shape[1] trainDataset = SequentialDataSet(nInputs, 1) for i in range(trainInput.shape[0]): if i % chunkLen == 0 and i > 0: trainDataset.newSequence() trainDataset.addSample(trainInput[i,:], trainOutput[i]) testDataset = SequentialDataSet(nInputs, 1) for i in range(testInput.shape[0]): if i % chunkLen == 0 and i > 0: testDataset.newSequence() testDataset.addSample(testInput[i,:], testOutput[i]) return trainDataset, testDataset
return "%4.1f" % self df=pd.read_csv(train_file, sep=',',header=None) training_features = [] for row in df.values: # mean temp, mean Dew Point, mean humidity, mean sea level PressurehPa feature_set = [float(row[2]),float(row[5]),float(row[8]),float(row[11])] nans = list( filter((lambda x: math.isnan(x)), feature_set) ) if (len(nans) > 0): continue training_features.append(feature_set) consecutive_days_pressure_dropping_by = 0 ds = SequentialDataSet(5, 1) for sample, next_sample in zip(training_features, cycle(training_features[1:])): ds.newSequence() yesterdays_sample = sample yesterdays_pressure = yesterdays_sample[3] todays_pressure = next_sample[3] raining = 0.0 if (todays_pressure > yesterdays_pressure): consecutive_days_pressure_dropping_by += (todays_pressure - yesterdays_pressure) else: raining = 1.0 consecutive_days_pressure_dropping_by = 0 yesterdays_sample.append(float(consecutive_days_pressure_dropping_by)) ds.appendLinked(yesterdays_sample, raining) net = buildNetwork(5, 10, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True)
class smokeDetect(AbstractVisionModule): """example vision class. Detects faces using the openCV face detector""" def __init__(self, host, port, update_frequency, source="webcam", verbose=False): self.logger = logging.getLogger("Borg.Brain.Vision.smokeDetect") super(smokeDetect, self).__init__(host, port) self.update_frequency = update_frequency self.__ticker = Ticker(frequency=update_frequency) self.verbose = verbose self.source = [source] self.vid_mem_reader = util.vidmemreader.VidMemReader(self.source) # set the type of objects that someone can detect self.set_known_objects(['smoke']) self.windowLocations = [] self.labels = [] self.data = [] self.frameAverage = 5 self.svm = None self.load_svm() def __del__(self): cv2.destroyAllWindows() def init_nn(self, datain, dataout): INPUTS = 5 OUTPUTS = 1 HIDDEN = 20 self.net = buildNetwork(INPUTS, HIDDEN, OUTPUTS, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True, bias=True) self.ds = SequentialDataSet(INPUTS, OUTPUTS) for x, y in itertools.izip(datain, dataout): self.ds.newSequence() self.ds.appendLinked(tuple(x), tuple(y)) self.net.randomize() trainer = BackpropTrainer(self.net, self.ds) for _ in range(1000): print trainer.train() def train(self): pass def stop(self): cv2.destroyAllWindows() def get_vid(self): if not hasattr(self, 'vid_idx'): self.vid_idx = 0 else: self.vid_idx += 1 if self.vid_idx >= len(vid_files): self.vid_idx = 0 print "NOW LOADING ", vid_files[self.vid_idx] return cv2.VideoCapture(vid_files[self.vid_idx]) def get_image_from_video(self, vid, size=(320, 240)): (result, image) = vid.read() if not result: #vid.release() vid = self.get_vid() (_, image) = vid.read() image = cv2.resize(image, size) return image def run(self): """start loop which gets a new image, then processes it""" #cv2.namedWindow("color_hist", cv2.cv.CV_WINDOW_NORMAL) im = None while im == None: im = self.vid_mem_reader.get_latest_image() if im == None: print "not receiving images yet..." time.sleep(0.2) vid = self.get_vid() idx = -1 while True: histogramList = [] imageList = [] colorList = [] imageOrigList = [] croppedList = [] timeBlock = time.time() heartBeat = time.time() idx = -1 while time.time() - timeBlock < 3: idx += 1 if idx > 300: break #Getting pictures for 5 seconds if time.time() - heartBeat > 2: self._AbstractVisionModule__send_heartbeat() heartBeat = time.time() #temp = self.vid_mem_reader.get_latest_image()[0] #image = np.asarray(temp[:,:]) #image = self.get_image_from_video(vid) (result, image) = vid.read() if not result: #vid.release() vid = self.get_vid() (_, image) = vid.read() image = cv2.resize(image, (320, 240)) if self.vid_idx == 0: w, h, _ = image.shape image = image[100:240, :, :] image = cv2.resize(image, (320, 240)) image_gs = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) colorList.append(image) imageOrigList.append(image_gs) image_gs = cv2.blur(image_gs, (15, 15)) imageList.append(image_gs) #Calculating Histogram of Gray scale Image and Normalizing it hist = cv2.calcHist(images=[image_gs], channels=[0], mask=None, histSize=[32], ranges=[0, 256]) cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX) histogramList.append(hist) #Calculating Histogram of the Color Image curve = hist_curve(image, 128) if self.verbose: image_gs_rs = cv2.resize(image_gs, (613, 408)) cv2.imshow("Smoothed_Gray_Scale", image_gs_rs) cv2.moveWindow("Smoothed_Gray_Scale", 55, 0) cv2.waitKey(1) self._AbstractVisionModule__send_heartbeat() contourList = [] for i in range(len(imageList) - 1): if time.time() - heartBeat > 4: self._AbstractVisionModule__send_heartbeat() heartBeat = time.time() motion, equalized_motion, denoised_motion, denoised_mask = self.extractMotion( imageList[i], imageList[i + 1]) #Calculating Normalized Histogram of motion image motion_hist = hist_curve(equalized_motion, bin=64, mask=denoised_motion) y = cv2.resize(motion_hist, (320, 240)) #Cropped Grayscale image based on motion final = np.zeros(image_gs.shape, dtype=np.int8) final = cv2.add(final, imageOrigList[i], mask=denoised_mask, dtype=cv2.CV_8UC1) #Getting cropped color image finalColor = self.cropColor(colorList[i], denoised_mask) croppedList.append(finalColor) finalOrigColor = colorList[i] #Calculating Contours contours, hierarchy = cv2.findContours(denoised_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) sanityList = [] #Accept frames having more than one contour if len(contours) > 1: for array in contours: temp_data = [] #Calculate contour structures area = cv2.contourArea(array) if area < 100: continue center, radius = cv2.minEnclosingCircle(array) circleArea = math.pi * math.pow(radius, 2) isConvex = cv2.isContourConvex(array) boundingRect = cv2.boundingRect(array) rectArea = boundingRect[2] * boundingRect[2] ellipseRect = cv2.fitEllipse(array) rectArea = ellipseRect[2] * ellipseRect[2] temp_data.extend([area, circleArea, rectArea]) temp_data.extend([radius, float(isConvex)]) xpos, ypos, w, h = boundingRect contour_hist = cv2.calcHist( images=[ equalized_motion[xpos:xpos + w, ypos:ypos + h] ], channels=[0], mask=denoised_motion[xpos:xpos + w, ypos:ypos + h], histSize=[32], ranges=[0, 256]) cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX) self.logger.debug("contour hist shape : %s", str(contour_hist.shape)) temp_data.extend( np.array(contour_hist).reshape(-1, ).tolist()) #Add deficiency information if not isConvex: temp_data.extend(self.convexDeficiency(array)) else: #Put zero for all the expected values temp_data.extend([0., 0., 0., 0., 0.]) #Ignore big areas and areas which look rectangular if area > 100 and rectArea / area > 2.5: sanityList.append(center) self.data.append(temp_data) #Automatic Labeling based on video if self.vid_idx == 0: self.labels.append(1.0) else: self.labels.append(0.0) test = np.array(temp_data) test = test.astype(np.float32) if self.svm.predict(test) == 1: tempol = np.copy(finalOrigColor) cv2.drawContours(tempol, array, -1, (255, 0, 0)) tempol_rs = cv2.resize(tempol, (614, 408)) cv2.imshow("Finall", tempol_rs) cv2.moveWindow("Finall", 1800 + 45, 460) cv2.waitKey(1) mean = np.mean(np.asarray(sanityList)) std = np.std(np.asarray(sanityList)) contourList.append((mean, std, contours)) if self.verbose: motion_rs = cv2.resize(motion, (613, 408)) cv2.normalize(motion_rs, motion_rs, 0, 255, cv2.NORM_MINMAX) cv2.imshow("motion", motion_rs) cv2.moveWindow("motion", 640 + 45, 0) temp_rs = cv2.resize(denoised_motion, (613, 408)) cv2.imshow("equap", temp_rs) cv2.moveWindow("equap", 1280 + 45, 0) finalColor_rs = cv2.resize(finalColor, (613, 408)) cv2.imshow("cropped-motion", finalColor_rs) cv2.moveWindow("cropped-motion", 0 + 45, 460) y_rs = cv2.resize(y, (613, 408)) cv2.imshow("cropped-motion-hist", y_rs) cv2.moveWindow("cropped-motion-hist", 640 + 45, 460) cv2.waitKey(1) #time.sleep(0.1) ''' DISCRETE FOURIER TRANSFORM dft_im = dft(denoised_motion) pyrdown = dft_im for i in range(4): pyrdown = cv2.pyrDown(pyrdown) w,h = dft_im.shape pyrdown = cv2.resize(pyrdown, (h,w)) cv2.imshow("pyrdown", pyrdown) ''' #Scoring detections in frame sequences Score = 0 minStd = 100 for mean, std, contours in contourList: #print 'Standard deviation of contours', std if std > 0 and std < 50: if std < minStd: minStd = std bestContour = contours minMean = mean Score += 1 elif std > 50: Score -= 30 elif std > 39: Score -= 1 else: #Score -= 1 pass if Score > 0: self.add_property('name', 'smoke') self.add_property('center', minMean) self.add_property('image_path', '/dev/shm/smoke.png') self.store_observation() tempo = np.copy(finalOrigColor) cv2.drawContours(tempo, contours, -1, (255, 0, 0)) cv2.imwrite('/dev/shm/smoke.png', tempo) tempo_rs = cv2.resize(tempo, (614, 408)) cv2.imshow("Final", tempo_rs) cv2.moveWindow("Final", 1280 + 45, 460) cv2.waitKey(1) self.update() self.save(self.data, self.labels) self.update() def save(self, data, labels): data = np.array(data) labels = np.array(labels) self.logger.info("Data size is %s", str(data.shape)) self.logger.info("label size is %s", str(labels.shape)) np.save("/dev/shm/data", data) np.save("/dev/shm/label", labels) np.savetxt("/dev/shm/data.txt", data) np.savetxt("/dev/shm/label.txt", labels) def convexDeficiency(self, contour): ''' Calculates the convex deficiency and returns a list of: 1- mean and standard deviation of deficiency depth, and length of the starting/ending point of the deficiency 2- Number of deficiencies 3- perimiter of each defeciency @contour The contour of the image ''' convex_hull = cv2.convexHull(contour, returnPoints=False) convex_defects = cv2.convexityDefects(contour, convex_hull) self.logger.debug("convex_defects is: %s", str(convex_defects)) defect_number = len(convex_defects) distance_list = [] depth_list = [] premeter_list = [] if defect_number: for defect in convex_defects: self.logger.debug("current defect is: %s", str(defect)) start_idx, end_idx, farthest_pt, pt_depth, = defect[0] self.logger.debug("start/end contour points are %s and %s ", str(contour[start_idx][0]), str(contour[end_idx][0])) distance = math.pow( (contour[start_idx][0][0] - contour[end_idx][0][0]), 2) + math.pow( (contour[start_idx][0][1] - contour[end_idx][0][1]), 2) distance_list.append(distance) depth_list.append(pt_depth) mean_distance = np.mean(np.asarray(distance_list)) std_distance = np.std(np.asarray(distance_list)) mean_depth = np.mean(np.asarray(depth_list)) std_depth = np.std(np.asarray(depth_list)) return [ mean_distance, std_distance, mean_depth, std_depth, defect_number ] def cropColor(self, image, mask): #Crop a color image based on a mask ch1 = np.zeros(mask.shape, dtype=np.int8) ch2 = np.zeros(mask.shape, dtype=np.int8) ch3 = np.zeros(mask.shape, dtype=np.int8) c1 = np.copy(image[:, :, 0]) c2 = np.copy(image[:, :, 1]) c3 = np.copy(image[:, :, 2]) ch1 = cv2.add(ch1, c1, mask=mask, dtype=cv2.CV_8UC1) ch2 = cv2.add(ch2, c2, mask=mask, dtype=cv2.CV_8UC1) ch3 = cv2.add(ch3, c3, mask=mask, dtype=cv2.CV_8UC1) finalColor = cv2.merge((ch1, ch2, ch3)) return finalColor def extractMotion(self, image1, image2): #Background Subtraction motion = cv2.absdiff(image1, image2) #Histogram Equalization of the motion image #TODO: Test without equalization and only normalization #eqaulized_motion = cv2.equalizeHist(motion) #eqaulized_motion = np.zeros(motion.shape) eqaulized_motion = cv2.normalize(src=motion, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) #Removing Noise by opening and closing the image se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 8)) temp = cv2.morphologyEx(eqaulized_motion, cv2.MORPH_OPEN, se) se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) temp = cv2.morphologyEx(temp, cv2.MORPH_CLOSE, se) denoised_motion = temp #Making a mask from the closed/opened image denoised_mask = np.zeros(image1.shape, dtype=np.uint8) denoised_mask[denoised_motion > 70] = 1 return motion, eqaulized_motion, denoised_motion, denoised_mask def deltaHistogram(self, histogramList, image_gs, hist): ''' This function doesn't work, it should calculate delta histograms ''' for i in range(len(histogramList) - 1): deltaHist = np.absolute( np.subtract(histogramList[i], histogramList[i + 1])) deltaHist /= np.sum(deltaHist) nans = np.isnan(deltaHist) deltaHist[nans] = 0 deltaImage = np.zeros(image_gs.shape) binStep = image_gs.shape[1] / deltaHist.shape[0] print deltaHist for i in range(len(hist) - 1): pt1 = (binStep * i, image_gs.shape[0] - (deltaHist[i] * image_gs.shape[0])) pt2 = (binStep * i + 1, image_gs.shape[0] - (deltaHist[i + 1] * image_gs.shape[0])) color = 255 cv2.line(deltaImage, pt1, pt2, color, 2, 8, 0) if self.verbose: cv2.imshow("delta_hist", deltaImage) cv2.moveWindow("delta_hist", 1600, 0) cv2.waitKey(10) time.sleep(0.1) def archive(self, image): #Adds all types of image conversion to the specified list pass def load_svm(self): self.svm = cv2.SVM() try: self.svm.load("/dev/shm/svm.model") except: self.svm = None
def learn(self, pathdataset=["dstc4_train"], Pathdataroot="data", numberOfHiddenUnit=20, EPOCHS_PER_CYCLE=10, CYCLES=40, weightdecayw=0.01): print "Start learning LSTM, and make dictionary file" #Construct dictionary: variable name -> corresponding index of element in i/o vector print "Star make dictionary: variable name -> corresponding index of element in i/o vector" self.dictOut = { } #"TOPIC_SLOT_VALUE" -> corresponding index of element self.dictIn = { } #"SPEAKER_{val}"or"TOPIC_{val}","WORD_{word}" "BIO_{BIO}", "CLASS_{slot,value}", ""{defined label}-> corresponding index of element #-target vector dictionary index = 0 totalNumSlot = 0 for topic in self.tagsets.keys(): for slot in self.tagsets[topic].keys(): totalNumSlot += 1 for value in self.tagsets[topic][slot]: self.dictOut[topic + "_" + slot + "_" + value] = index index += 1 print "totalNumSlot:" + str(totalNumSlot) print "outputSize:" + str(len(self.dictOut.keys())) #-input dictionry dataset = [] for pathdat in pathdataset: dataset.append( dataset_walker.dataset_walker(pathdat, dataroot=Pathdataroot, labels=False)) #--(sub input vector 1) Class features i.e., Slot and value ratio (Similar to base line) index = 0 for topic in self.tagsets.keys(): for slot in self.tagsets[topic].keys(): if ("CLASS_" + slot) not in self.dictIn: self.dictIn["CLASS_" + slot] = index index += 1 for value in self.tagsets[topic][slot]: if ("CLASS_" + value) not in self.dictIn: self.dictIn["CLASS_" + value] = index index += 1 self.TOTALSIZEOFCLASSFeature = index f = open(self.FileNameofNumClassFeature, "wb") pickle.dump(self.TOTALSIZEOFCLASSFeature, f) f.close() #--(sub input vector 2) Sentence features if not self.isUseSentenceRepresentationInsteadofBOW: index = 0 for elemDataset in dataset: for call in elemDataset: for (uttr, _) in call: #General info1 (CLASS; this feature must be rejistered at first) if ("SPEAKER_" + uttr["speaker"]) not in self.dictIn: self.dictIn["SPEAKER_" + uttr["speaker"]] = index index += 1 if ("TOPIC_" + uttr["segment_info"]["topic"] ) not in self.dictIn: self.dictIn["TOPIC_" + uttr["segment_info"]["topic"]] = index index += 1 #General info2 #-BIO if ("BIO_" + uttr['segment_info']['target_bio'] ) not in self.dictIn: self.dictIn[ "BIO_" + uttr['segment_info']['target_bio']] = index index += 1 #BOW if LSTMWithBOWTracker.isIgnoreUtterancesNotRelatedToMainTask: if not (uttr['segment_info']['target_bio'] == "O"): #-BOW splitedtrans = self.__getRegurelisedBOW( uttr["transcript"]) for word in splitedtrans: if ("WORD_" + word) not in self.dictIn: self.dictIn["WORD_" + word] = index index += 1 self.TOTALSIZEOFSENTENCEFeature = index f = open(self.FileNameofNumSentenceFeature, "wb") pickle.dump(self.TOTALSIZEOFSENTENCEFeature, f) f.close() elif self.isUseSentenceRepresentationInsteadofBOW: index = 0 for i in range(0, LSTMWithBOWTracker.D2V_VECTORSIZE): self.dictIn[str(index) + "thElemPV"] = index index += 1 index = 0 for i in range(0, LSTMWithBOWTracker.D2V_VECTORSIZE): self.dictIn[str(index) + "thAvrWord"] = index index += 1 assert self.D2V_VECTORSIZE == LSTMWithBOWTracker.D2V_VECTORSIZE, "D2V_VECTORSIZE is restrected to be same over the class" else: assert False, "Unexpected block" #--(sub input vector 3) Features M1s defined index = 0 if self.isEnableToUseM1sFeature: rejisteredFeatures = self.__rejisterM1sInputFeatureLabel( self.tagsets, dataset) for rFeature in rejisteredFeatures: assert rFeature not in self.dictIn, rFeature + " already registered in input vector. Use different label name. " self.dictIn[rFeature] = index index += 1 self.TOTALSIZEOFM1DEFINEDFeature = index f = open(self.FileNameofNumM1Feature, "wb") pickle.dump(self.TOTALSIZEOFM1DEFINEDFeature, f) f.close() print "inputSize:" + str(len(self.dictIn.keys())) assert self.dictIn[ "CLASS_INFO"] == 0, "Unexpected index CLASS_INFO should has value 0" assert self.dictIn[ "CLASS_Fort Siloso"] == 334, "Unexpected index CLASS_Fort Siloso should has value 334" assert self.dictIn[ "CLASS_Yunnan"] == 1344, "Unexpected index CLASS_Yunnan should has value 1611" #--write fileObject = open('dictInput.pic', 'w') pickle.dump(self.dictIn, fileObject) fileObject.close() fileObject = open('dictOutput.pic', 'w') pickle.dump(self.dictOut, fileObject) fileObject.close() #Build RNN frame work print "Start learning Network" #Capability of network is: (30 hidden units can represents 1048576 relations) wherease (10 hidden units can represents 1024) #Same to Henderson (http://www.aclweb.org/anthology/W13-4073)? net = buildNetwork(len(self.dictIn.keys()), numberOfHiddenUnit, len(self.dictOut.keys()), hiddenclass=LSTMLayer, outclass=SigmoidLayer, outputbias=False, recurrent=True) #Train network #-convert training data into sequence of vector convDataset = [] #[call][uttr][input,targetvec] iuttr = 0 convCall = [] for elemDataset in dataset: for call in elemDataset: for (uttr, label) in call: if self.isIgnoreUtterancesNotRelatedToMainTask: if uttr['segment_info']['target_bio'] == "O": continue #-input convInput = self._translateUtteranceIntoInputVector( uttr, call) #-output convOutput = [0.0] * len( self.dictOut.keys()) #Occured:+1, Not occured:0 if "frame_label" in label: for slot in label["frame_label"].keys(): for value in label["frame_label"][slot]: convOutput[self.dictOut[ uttr["segment_info"]["topic"] + "_" + slot + "_" + value]] = 1 #-post proccess if self.isSeparateDialogIntoSubDialog: if uttr['segment_info']['target_bio'] == "B": if len(convCall) > 0: convDataset.append(convCall) convCall = [] convCall.append([convInput, convOutput]) #print "Converted utterance" + str(iuttr) iuttr += 1 if not self.isSeparateDialogIntoSubDialog: if len(convCall) > 0: convDataset.append(convCall) convCall = [] #Online learning trainer = RPropMinusTrainer(net, weightdecay=weightdecayw) EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): #Shuffle order ds = SequentialDataSet(len(self.dictIn.keys()), len(self.dictOut.keys())) datInd = range(0, len(convDataset)) random.shuffle( datInd ) #Backpropergation already implemeted data shuffling, however though RpropMinus don't. for ind in datInd: ds.newSequence() for convuttr in convDataset[ind]: ds.addSample(convuttr[0], convuttr[1]) #Evaluation and Train epoch = (i + 1) * EPOCHS_PER_CYCLE print "\r epoch {}/{} Error={}".format( epoch, EPOCHS, trainer.testOnData(dataset=ds)) stdout.flush() trainer.trainOnDataset(dataset=ds, epochs=EPOCHS_PER_CYCLE) NetworkWriter.writeToFile( trainer.module, "LSTM_" + "Epoche" + str(i + 1) + ".rnnw") NetworkWriter.writeToFile(trainer.module, "LSTM.rnnw")
y_found = True y_seq.append(1) else: y_seq.append(0) ys.append(1 if y_found else 0) ys_seq.append(y_seq) xs.append(row) maxlen = max(len(row), maxlen) max_features = generator.max_id() + 2 train_ds = SequentialDataSet(max_features, 1) for xrow, yrow in zip(xs, ys_seq): train_ds.newSequence() for x, y in zip(xrow, yrow): one_hot = [0] * max_features one_hot[x] = 1 train_ds.addSample(one_hot, y) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer net = buildNetwork(max_features, 32, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True)