def evaluate(model_file, data, index, ans_loc, config, save_results=False): ''' evaluate the data by the model file. :param model_file: :param data: :return: ''' word_vec = utils.get_word_vec( '../data/deepwalk_128_unweighted_with_args.txt') model = utils.trans_to_cuda(SGNN(word_vec, config)) model.load_state_dict(torch.load(model_file)) if save_results: filename = model_file[:model_file.rindex('.')] + '_results.pkl' dest = open('../data/' + filename, 'wb') else: dest = sys.stdout model.eval() A, input_data, targets = data.all_data() accuracy = model.evaluate(A, input_data, targets, ans_loc, index, metric=config.metric, dest=dest) return accuracy
def evaluate(model_file, data, ans_loc, save_results=False): ''' evaluate the data by the model file. :param model_file: :param data: :return: ''' word_vec = utils.get_word_vec('../data/deepwalk_128_unweighted_with_args.txt') model = utils.trans_to_cuda(EventChain(embedding_dim=HIDDEN_DIM//4, hidden_dim=HIDDEN_DIM, vocab_size=len(word_vec), word_vec=word_vec, num_layers=1, bidirectional=bidirectional_g)) model.load_state_dict(torch.load(model_file)) if save_results: filename = model_file[:model_file.rindex('.')] + '_result.pkl' dest = open('../data/'+filename, 'wb') else: dest = sys.stdout model.eval() A, input_data, targets = data.all_data() accuracy = model.evaluate(input_data, targets, ans_loc, dest=dest) return accuracy
def forward(self, input, unk_loc): ''' forward :param input: [batch_size, 52, hidden_size] :return: ''' hidden = self.embedding(input) # version 1, put 128*4 into gru # version 2, put 128*3 into gru, and concatenate the first 128 if version == 1: hidden = torch.cat((hidden[:,0:13,:],hidden[:,13:26,:],hidden[:,26:39,:],hidden[:,39:52,:]), 2) input_a = hidden[:, 0:8, :].repeat(1, 5, 1).view(5 * len(hidden), 8, -1) # [5000, 8, 512] input_b = hidden[:, 8:13, :].contiguous().view(-1, 1, 512) hidden = torch.cat((input_a, input_b), 1) # 5000*9*(128*4) hidden_rnn = hidden if unk_loc > 0 and unk_loc < 8: left_output, hl = self.rnn(hidden_rnn[:, 0:unk_loc, :]) right_input = hidden_rnn[:, unk_loc:8, :] idx = [i for i in range(right_input.size(1) - 1, -1, -1)] idx = utils.trans_to_cuda(torch.LongTensor(idx)) inverted_right_input = right_input.index_select(1, idx) inverted_right_output, hr = self.rnn(inverted_right_input) right_output = inverted_right_output.index_select(1, idx) if self.bidirectioinal: hl_init = hl[-2, :, :] # forward hr_init = hr[-2, :, :] h_init = torch.cat([torch.unsqueeze(hl_init, 0), torch.unsqueeze(hr_init, 0)], dim=0) else: hl_init = hl[-1, :, :] hr_init = hr[-1, :, :] h_init = torch.unsqueeze(hl_init, 0) if unk_loc >= 4 else torch.unsqueeze(hr_init, 0) candidate_output, _ = self.rnn(hidden_rnn[:, 8:, :], h_init) # [batch, 1, hidden_dim] rnn_output = torch.cat((left_output, right_output, candidate_output), dim=1) # [batch_size, 9, hidden_dim] elif unk_loc == 0: right_input = hidden_rnn[:, :, :] idx = [i for i in range(right_input.size(1) - 1, -1, -1)] idx = utils.trans_to_cuda(torch.LongTensor(idx)) inverted_right_input = right_input.index_select(1, idx) inverted_right_output, _ = self.rnn(inverted_right_input) right_output = inverted_right_output.index_select(1, idx) rnn_output = right_output elif unk_loc == 8: left_output, _ = self.rnn(hidden_rnn) # init h?, default torch.zeros(shape) rnn_output = left_output elif version == 2: hidden0_13 = hidden[:, 0:13, :] hidden13_26 = hidden[:, 13:26, :] hidden26_39 = hidden[:, 26:39, :] hidden39_52 = hidden[:, 39:, :] hidden_p = torch.cat((hidden0_13[:, 0:8, :].repeat(1, 5, 1).view(5 * len(hidden0_13), 8, -1), hidden0_13[:, 8:13, :].contiguous().view(-1, 1, self.hidden_dim//4)), dim=1) # [5*batch_size, 9, 128] hidden = torch.cat((hidden13_26, hidden26_39, hidden39_52), 2) # [batch_size, 13, 128*3] input_a = hidden[:, 0:8, :].repeat(1, 5, 1).view(5 * len(hidden), 8, -1) # [5000, 8, 128*3] input_b = hidden[:, 8:13, :].contiguous().view(-1, 1, (self.hidden_dim//4)*3) hidden = torch.cat((input_a, input_b), 1) # 5000*9*(128*3) hidden_rnn = hidden if unk_loc > 0 and unk_loc < 8: left_output, hl = self.rnn(hidden_rnn[:, 0:unk_loc, :]) right_input = hidden_rnn[:, unk_loc:8, :] idx = [i for i in range(right_input.size(1) - 1, -1, -1)] idx = utils.trans_to_cuda(torch.LongTensor(idx)) inverted_right_input = right_input.index_select(1, idx) inverted_right_output, hr = self.rnn(inverted_right_input) right_output = inverted_right_output.index_select(1, idx) if self.bidirectioinal: hl_init = hl[-2, :, :] # forward hr_init = hr[-2, :, :] h_init = torch.cat([torch.unsqueeze(hl_init, 0), torch.unsqueeze(hr_init, 0)], dim=0) else: hl_init = hl[-1, :, :] hr_init = hr[-1, :, :] h_init = torch.unsqueeze(hl_init, 0) if unk_loc >= 4 else torch.unsqueeze(hr_init, 0) candidate_output, _ = self.rnn(hidden_rnn[:, 8:, :], h_init) # [batch, 1, hidden_dim] rnn_output = torch.cat((left_output, right_output, candidate_output), dim=1) # [batch_size, 9, hidden_dim] elif unk_loc == 0: right_input = hidden_rnn[:, :, :] idx = [i for i in range(right_input.size(1) - 1, -1, -1)] idx = utils.trans_to_cuda(torch.LongTensor(idx)) inverted_right_input = right_input.index_select(1, idx) inverted_right_output, _ = self.rnn(inverted_right_input) right_output = inverted_right_output.index_select(1, idx) rnn_output = right_output elif unk_loc == 8: left_output, _ = self.rnn(hidden_rnn) # init h?, default torch.zeros(shape) rnn_output = left_output rnn_output = torch.cat((hidden_p, rnn_output), dim=2) scores=self.compute_scores(rnn_output) return scores
parser.add_argument("-m", "--mode", help="train or test", dest="mode", type=str, default='train') args = parser.parse_args() ans_loc = args.ans_loc mode = args.mode print('ans_loc:{}, mode:{}'.format(ans_loc, mode)) if ans_loc >= 8: train_data = DataLoader(pickle.load(open('../data/train_8_data.data', 'rb'))) valid_data = DataLoader(pickle.load(open('../data/valid_8_data.data', 'rb'))) test_data = DataLoader(pickle.load(open('../data/test_8_data.data', 'rb'))) else: train_data = DataLoader(pickle.load(open('../data/train_{}_data.pkl'.format(ans_loc), 'rb'))) valid_data = DataLoader(pickle.load(open('../data/valid_{}_data.pkl'.format(ans_loc), 'rb'))) test_data = DataLoader(pickle.load(open('../data/test_{}_data.pkl'.format(ans_loc), 'rb'))) print('data prepare done.') if mode == 'train': word_vec = utils.get_word_vec('../data/deepwalk_128_unweighted_with_args.txt') print('word vector prepare done.') # define model model = utils.trans_to_cuda(EventChain(embedding_dim=HIDDEN_DIM//4, hidden_dim=HIDDEN_DIM, vocab_size=len(word_vec), word_vec=word_vec, num_layers=1, bidirectional=bidirectional_g)) best_acc = train(model, ans_loc, train_data, valid_data) elif mode == 'test': accuracy = evaluate('../data/event_chain_{}.model'.format(ans_loc), test_data, ans_loc, save_results=True) print('best test dataset acc {:.2f}'.format(accuracy))
'rb'))) test_data = DataLoader( pickle.load(open('../data/test_{}_data.pkl'.format(ans_loc), 'rb'))) print( "ans_loc:{}, data_type:{}, use_lstm:{}, use_attention:{}, unit_type:{}, bidirectional:{}, batch_size:{}" .format(ans_loc, config.data_type, config.use_lstm, config.use_attention, config.unit_type, config.bidirectional, config.batch_size)) print('train data prepare done.') dev_index = pickle.load(open('../data/dev_index.pickle', 'rb')) test_index = pickle.load(open('../data/test_index.pickle', 'rb')) word_vec = utils.get_word_vec( '../data/deepwalk_128_unweighted_with_args.txt') print('word vector prepare done.') # define model model = SGNN(word_vec, config) model = utils.trans_to_cuda(model) # train model best_acc = train(model, ans_loc, train_data, valid_data, dev_index, config) # record the experiment result with open('best_result.txt', 'a') as f: f.write( 'Best Acc: %f, TYPE=%s, L2_penalty=%s, MARGIN=%s, LR=%s, T=%s, BATCH_SIZE=%s, Iteration_times=%s, Use_lstm=%s, Use_attention=%s, Unit_type=%s, PATIENTS=%s, HIDDEN_DIM=%s, METRIC=%s\n' % (best_acc, config.data_type, config.l2_penalty, config.margin, config.lr, config.T, config.batch_size, config.iteration_times, config.use_lstm, config.use_attention, config.unit_type, config.patients, config.hidden_dim, config.metric)) f.close()
def forward(self, A, input, unk_loc=4, metric='euclid'): ''' forward propagation. :return: ''' hidden = self.embedding(input) # [batch_size, 13*4, 128] hidden0_13 = hidden[:, 0:13, :] hidden13_26 = hidden[:, 13:26, :] hidden26_39 = hidden[:, 26:39, :] hidden39_52 = hidden[:, 39:, :] hidden = torch.cat((hidden0_13, hidden13_26, hidden26_39, hidden39_52), dim=2) # [batch_size, 13, 128*4] hidden_rnn = torch.cat((hidden13_26, hidden26_39, hidden39_52), dim=2) # [batch_size, 13, 128*3] # hidden_rnn = hidden # [batch_size, 13, 128*4] # lstm if self.use_lstm: if unk_loc>0 and unk_loc<8: left_output, (hl, cl) = self.rnn(hidden_rnn[:, 0:unk_loc, :]) if self.reverse: right_input = hidden_rnn[:, unk_loc:8, :] idx = [i for i in range(right_input.size(1) - 1, -1, -1)] idx = utils.trans_to_cuda(torch.LongTensor(idx)) inverted_right_input = right_input.index_select(1, idx) if self.left_to_right: inverted_right_output, (hr, cr) = self.rnn(inverted_right_input, (hl, cl)) else: inverted_right_output, (hr, cr) = self.rnn(inverted_right_input) right_output = inverted_right_output.index_select(1, idx) else: if self.left_to_right: right_output, (hr, cr) = self.rnn(hidden_rnn[:, unk_loc:8, :], (hl, cl)) else: right_output, (hr, cr) = self.rnn(hidden_rnn[:, unk_loc:8, :]) if self.bidirectioinal: hl_init = hl[-2, :, :] # forward hr_init = hr[-2, :, :] if self.reverse else hr[-1, :, :] cl_init = cl[-2, :, :] # forward cr_init = cr[-2, :, :] if self.reverse else cr[-1, :, :] h_init = torch.cat([torch.unsqueeze(hl_init, 0), torch.unsqueeze(hr_init, 0)], dim=0) c_init = torch.cat([torch.unsqueeze(cl_init, 0), torch.unsqueeze(cr_init, 0)], dim=0) else: hl_init = hl[-1, :, :] hr_init = hr[-1, :, :] cl_init = cl[-1, :, :] cr_init = cr[-1, :, :] h_init = torch.unsqueeze(hl_init, 0) if unk_loc>=4 else torch.unsqueeze(hr_init, 0) c_init = torch.unsqueeze(cl_init, 0) if unk_loc>=4 else torch.unsqueeze(cr_init, 0) candidate_output, _ = self.rnn(hidden_rnn[:, 8:, :], (h_init, c_init)) # [batch, 5, hidden_dim] lstm_output = torch.cat((left_output, right_output, candidate_output), dim=1) # [batch_size, 13, hidden_dim] elif unk_loc==0: if self.reverse: right_input = hidden_rnn[:, :, :] idx = [i for i in range(right_input.size(1) - 1, -1, -1)] idx = utils.trans_to_cuda(torch.LongTensor(idx)) inverted_right_input = right_input.index_select(1, idx) inverted_right_output, _ = self.rnn(inverted_right_input) right_output = inverted_right_output.index_select(1, idx) else: right_output, _ = self.rnn(hidden_rnn) # reverse lstm_output = right_output elif unk_loc==8: left_output, _ = self.rnn(hidden_rnn) # init h?, default torch.zeros(shape) lstm_output = left_output lstm_output = torch.cat((hidden0_13, lstm_output), dim=2) else: lstm_output = hidden # gnn gnn_output = self.gnn(A, lstm_output) # attention input_a = gnn_output[:, 0:8, :].repeat(1,5,1).view(5*len(gnn_output), 8, -1) # [5*batch_size, 8, 128*4] input_b = gnn_output[:, 8:13, :] # [batch_size, 5, 128*4] if self.use_attention: u_a = F.relu(self.linear_u_one(input_a)) # [5*batch_size, 8, 128*2] u_a2 = F.relu(self.linear_u_one2(u_a)) # [5*batch_size, 8, 1] u_b = F.relu(self.linear_u_two(input_b)) # [batch_size, 5, 128*2] u_b2 = F.relu(self.linear_u_two2(u_b)) # [batch_size, 5, 1] u_c = torch.add(u_a2.view(5 * len(gnn_output), 8), u_b2.view(5 * len(gnn_output), 1)) # [5*batch_size, 8] weight = torch.exp(torch.tanh(u_c)) weight = (weight / torch.sum(weight, 1).view(-1, 1)).view(-1, 8, 1) # [5*batch_size, 8, 1] # print(weight) else: weight = utils.trans_to_cuda(torch.ones((5*len(gnn_output), 8), requires_grad=False)) weight = (weight / torch.sum(weight, 1).view(-1, 1)).view(-1, 8, 1) # [5*batch_size, 8, 1] weighted_input = torch.mul(input_a, weight) # 对位相乘 a = torch.sum(weighted_input, 1) # [5*batch_size, 128*4] b = input_b / 8.0 b = b.view(5 * len(gnn_output), -1) # [5*batch_size, 128*4] # similarity if metric == 'euclid': scores = self.metric_euclid(a, b) return scores