# eachFolder = folder + "\\" + str(e) # os.mkdir(eachFolder) # rnn.save(eachFolder) # """修改于2015/4/3""" # words = numpy.asarray(cwords).astype("int32") # labels = train_y[i] # rnn.train(words, labels, s["clr"]) # print '[learning] epoch %i >> %2.2f%%'%(e,(i+1)*100./nsentences),'completed in %.2f (sec) <<\r'%(time.time()-tic), # sys.stdout.flush() # fileTime = time.strftime("%Y-%m-%d",time.localtime(time.time())) # folder = "..\\paramInfor\\MyRnn(he)\\" + fileTime + os.path.basename(__file__).split('.')[0] # if not os.path.exists(folder): os.mkdir(folder) # else: # folder = folder + "-win5" # os.mkdir(folder) # rnn.save(folder) print " test the model, back into the real world : idx -> words" preFile = open("myRnn-bio.txt","w") for x in test_x: lst = rnn.classify(numpy.asarray(contextwin(x, s['win'])).astype('int32')) testViterbi = NewViterbi(transMatrix, lst[0].tolist(), intialState) path = testViterbi.getFianlPath() predictions_test=map(lambda x: idx2label[x], path) for eachlag in predictions_test: preFile.write(eachlag+" ") preFile.write("\n") preFile.close()
rnn = model(nh=s['nhidden'], nc=nclasses, ne=vocsize, de=s['emb_dimension'], cs=s['win']) # train with early stopping on validation set best_f1 = -numpy.inf s['clr'] = s['lr'] for e in xrange(s['nepochs']): # shuffle shuffle([train_lex, train_ne, train_y], s['seed']) s['ce'] = e tic = time.time() for i in xrange(nsentences): cwords = contextwin(train_lex[i], s['win']) words = map(lambda x: numpy.asarray(x).astype('int32'),\ minibatch(cwords, s['bs'])) labels = train_y[i] for word_batch, label_last_word in zip(words, labels): rnn.train(word_batch, label_last_word, s['clr']) rnn.normalize() if s['verbose']: print '[learning] epoch %i >> %2.2f%%' % ( e, (i + 1) * 100. / nsentences ), 'completed in %.2f (sec) <<\r' % (time.time() - tic), sys.stdout.flush() # evaluation // back into the real world : idx -> words predictions_test = [ map(lambda x: idx2label[x], \ rnn.classify(numpy.asarray(contextwin(x, s['win'])).astype('int32')))\
# for dirpath, dirnames, filenames in os.walk(trainedParams): # print "Traversal the folder" # if len(filenames) != 0: # print "params have been trained, now loading ..." # rnn.load(trainedParams) # else: rnn.load("..\\paramInfor\\Elman(he)\\2015-03-22myElman-forward-3-21") print "train with early stopping nepochs ..." s['clr'] = s['lr'] for e in xrange(s['nepochs']): # shuffle shuffle([train_x, train_y], s['seed']) tic = time.time() for i in xrange(nsentences): cwords = contextwin(train_x[i], s['win']) words = map(lambda x: numpy.asarray(x).astype('int32'),\ minibatch(cwords, s['bs'])) labels = train_y[i] for word_batch , label_last_word in zip(words, labels): rnn.train(word_batch, label_last_word, s['clr']) if s['verbose']: print '[learning] epoch %i >> %2.2f%%'%(e,(i+1)*100./nsentences),'completed in %.2f (sec) <<\r'%(time.time()-tic), sys.stdout.flush() fileTime = time.strftime("%Y-%m-%d",time.localtime(time.time())) folder = "..\\paramInfor\\Elman(he)\\" + fileTime + os.path.basename(__file__).split('.')[0] if not os.path.exists(folder): os.mkdir(folder) else: folder = folder + "-win5" os.mkdir(folder)
rnn = model( nh = s['nhidden'], nc = nclasses, ne = vocsize, de = s['emb_dimension'], cs = s['win'] ) # train with early stopping on validation set best_f1 = -numpy.inf s['clr'] = s['lr'] for e in xrange(s['nepochs']): # shuffle shuffle([train_lex, train_ne, train_y], s['seed']) s['ce'] = e tic = time.time() for i in xrange(nsentences): cwords = contextwin(train_lex[i], s['win']) words = map(lambda x: numpy.asarray(x).astype('int32'),\ minibatch(cwords, s['bs'])) labels = train_y[i] for word_batch , label_last_word in zip(words, labels): rnn.train(word_batch, label_last_word, s['clr']) rnn.normalize() if s['verbose']: print '[learning] epoch %i >> %2.2f%%'%(e,(i+1)*100./nsentences),'completed in %.2f (sec) <<\r'%(time.time()-tic), sys.stdout.flush() # evaluation // back into the real world : idx -> words predictions_test = [ map(lambda x: idx2label[x], \ rnn.classify(numpy.asarray(contextwin(x, s['win'])).astype('int32')))\ for x in test_lex ] groundtruth_test = [ map(lambda x: idx2label[x], y) for y in test_y ]