str(parent_num_train[0])) #print index_train[0] #print word_train[0] #print tree_train[0] #exit(0) return tree_train, word_train, index_train, parent_num_train, y_train, tree_test, word_test, index_test, parent_num_test, y_test ##################################### MAIN #################################### ## 1. load tree & word & index & label tree_train, word_train, index_train, parent_num_train, y_train, tree_test, word_test, index_test, parent_num_test, y_test = loadData( ) ## 2. ini RNN model t0 = time.time() model = TD_RvNN.RvNN(vocabulary_size, hidden_dim, Nclass) t1 = time.time() print('Recursive model established,' + str((t1 - t0) / 60)) #if os.path.isfile(modelPath): # load_model_Recursive_gruEmb(modelPath, model) # lr = 0.0001 ######debug here###### #print len(tree_test[121]), len(index_test[121]), len(word_test[121]) #print tree_test[121] #exit(0) #loss, pred_y = model.train_step_up(word_test[121], index_test[121], tree_test[121], y_test[121], lr) #print loss, pred_y #exit(0) '''i=568
def run(_vocabulary_size, _hidden_dim, _n_class, _n_epoch, _learning_rate, _label_path, _tree_path, _train_path, _test_path, _eid_pool): # 1. load tree & word & index & label tree_train, word_train, index_train, parent_num_train, y_train, \ tree_test, word_test, index_test, parent_num_test, y_test = load_data( _label_path, _tree_path, _train_path, _test_path, _eid_pool ) # 2. ini RNN model t0 = time.time() model = TD_RvNN.RvNN(_vocabulary_size, _hidden_dim, _n_class) t1 = time.time() print('Recursive model established,', (t1 - t0) / 60) # if os.path.isfile(modelPath): # load_model_Recursive_gruEmb(modelPath, model) # lr = 0.0001 # debug here # print len(tree_test[121]), len(index_test[121]), len(word_test[121]) # print tree_test[121] # exit(0) # loss, pred_y = model.train_step_up(word_test[121], index_test[121], tree_test[121], y_test[121], lr) # print loss, pred_y # exit(0) '''i=568 loss, pred_y = model.train_step_up(word_train[i], index_train[i], parent_num_train[i], tree_train[i], y_train[i], lr) print loss, pred_y print len(tree_train[i]), len(word_train[i]), parent_num_train[i] print tree_train[i] print word_train[i] print 'final_state:',model._evaluate(word_train[i], index_train[i], parent_num_train[i], tree_train[i]) tree_states=model._evaluate2(word_train[i], index_train[i], parent_num_train[i], tree_train[i]) print 'tree_states:', tree_states print tree_states[-1:].mean(axis=0) tree_states_test=model._evaluate3(word_train[i], index_train[i], tree_train[i]) print 'l:',len(tree_states_test) print 'lo:',tree_states_test[parent_num_train[i]:]''' # # 3. looping SGD losses_5, losses = [], [] num_examples_seen = 0 for epoch in range(_n_epoch): # one SGD indexes = [i for i in range(len(y_train))] for i in indexes: '''print i,":", len(tree_train[i]) print tree_train[i] tree_state = model._state(word_train[i], index_train[i], child_num_train[i], tree_train[i]) print len(tree_state) print tree_state evl = model._evaluate(word_train[i], index_train[i], child_num_train[i], tree_train[i]) print len(evl) print evl''' loss, pred_y = model.train_step_up(word_train[i], index_train[i], parent_num_train[i], tree_train[i], y_train[i], _learning_rate) # print loss, pred_y losses.append(round(float(loss), 2)) '''if math.isnan(loss): # continue print loss, pred_y print i print len(tree_train[i]), len(word_train[i]), parent_num_train[i] print tree_train[i] print word_train[i] print 'final_state:',model._evaluate(word_train[i], index_train[i], parent_num_train[i], tree_train[i])''' num_examples_seen += 1 print("epoch=%d: loss=%f" % (epoch, np.mean(losses))) # floss.write(str(time)+": epoch="+str(epoch)+" loss="+str(loss) +'\n') sys.stdout.flush() # print losses # exit(0) # cal loss & evaluate if epoch % 5 == 0: losses_5.append((num_examples_seen, np.mean(losses))) time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print("%s: Loss after num_examples_seen=%d epoch=%d: %f" % (time_now, num_examples_seen, epoch, np.mean(losses))) # floss.write(str(time)+": epoch="+str(epoch)+" loss="+str(loss) +'\n') # floss.flush() sys.stdout.flush() prediction = [] for j in range(len(y_test)): # print j prediction.append( model.predict_up(word_test[j], index_test[j], parent_num_test[j], tree_test[j])) res = evaluation_4class(prediction, y_test) print('results:', res) # floss.write(str(res)+'\n') # floss.flush() sys.stdout.flush() # Adjust the learning rate if loss increases if len(losses_5) > 1 and losses_5[-1][1] > losses_5[-2][1]: _learning_rate = _learning_rate * 0.5 print("Setting learning rate to %f" % _learning_rate) # floss.write("Setting learning rate to:"+str(lr)+'\n') # floss.flush() sys.stdout.flush() # save_model_Recursive_gruEmb(modelPath, model) # floss.flush() losses = []