Example #1
0
    def get_output_vector(self, qlist=None, batch_size=64):

        with torch.no_grad():
            for i in range(0, len(qlist), batch_size):
                st = i
                if i + batch_size < len(qlist):
                    et = i + batch_size
                else:
                    et = len(qlist)

                # features
                t = get_specific_comp_list("title", qlist[st:et])
                dt = get_specific_comp_list("desc_text", qlist[st:et])
                dc = get_specific_comp_list("desc_code", qlist[st:et])

                t = torch.tensor(t).long()
                dt = torch.tensor(dt).long()
                dc = torch.tensor(dc).long()

                if self.args.cuda:
                    t, dt, dc = t.cuda(), dt.cuda(), dc.cuda()

                t = self.title_embed(t.cuda() if torch.cuda.is_available() else t)
                dt = self.desc_text_embed(dt.cuda() if torch.cuda.is_available() else dt)
                dc = self.desc_code_embed(dc.cuda() if torch.cuda.is_available() else dc)

                if self.args.static:
                    t = Variable(t)
                    dt = Variable(dt)
                    dc = Variable(dc)

                t = t.unsqueeze(1)  # (N, Ci, W, D)
                dt = dt.unsqueeze(1)  # (N, Ci, W, D)
                dc = dc.unsqueeze(1)  # (N, Ci, W, D)

                t = [F.relu(conv(t)).squeeze(3) for conv in self.convs_t]  # [(N, Co, W), ...]*len(Ks)
                dt = [F.relu(conv(dt)).squeeze(3) for conv in self.convs_dt]  # [(N, Co, W), ...]*len(Ks)
                dc = [F.relu(conv(dc)).squeeze(3) for conv in self.convs_dc]  # [(N, Co, W), ...]*len(Ks)

                t = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in t]  # [(N, Co), ...]*len(Ks)
                dt = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in dt]  # [(N, Co), ...]*len(Ks)
                dc = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in dc]  # [(N, Co), ...]*len(Ks)

                x_t, x_dt, x_dc = torch.cat(t, 1), torch.cat(dt, 1), torch.cat(dc, 1)

                for j in range(et - st):
                    qlist[st + j].title = (Variable(x_t).data).cpu().numpy()[j]
                    qlist[st + j].desc_text = (Variable(x_dt).data).cpu().numpy()[j]
                    qlist[st + j].desc_code = (Variable(x_dc).data).cpu().numpy()[j]

        return qlist
Example #2
0
def train(train_iter, dev_iter, model, args, global_train_step):
    if args.cuda:
        model.cuda()

    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)

    steps = 0
    model.train()
    for epoch in range(1, args.epochs + 1):
        print("\n#epoch %s" % epoch, get_current_time())
        for batch in train_iter:
            # features
            t = np.array(get_specific_comp_list("title", batch))
            dt = np.array(get_specific_comp_list("desc_text", batch))
            dc = np.array(get_specific_comp_list("desc_code", batch))
            # label
            target = get_specific_comp_list("tags", batch)

            t = torch.tensor(t).long()
            dt = torch.tensor(dt).long()
            dc = torch.tensor(dc).long()
            target = torch.tensor(target).float()

            if args.cuda:
                t, dt, dc, target = t.cuda(), dt.cuda(), dc.cuda(), target.cuda()

            optimizer.zero_grad()
            logit = model(t, dt, dc)
            # debug
            # print("logit.reshape(-1) shape %s"%logit.reshape(-1).shape)
            # print("logit.reshape(-1).[:10] %s"%logit.reshape(-1)[:10])
            # print("target.reshape(-1) shape %s"%target.reshape(-1).shape)
            # print("target.reshape(-1)[:10] %s"%target.reshape(-1)[:10])
            loss = nn.BCELoss()
            loss = loss(logit.reshape(-1), target.reshape(-1))

            loss.backward()
            optimizer.step()

            steps += 1
            global_train_step += 1
            if steps % args.log_interval == 0:
                sys.stdout.write('\rBatch[{}] - loss: {:.10f}'.format(steps, loss))
            if global_train_step % args.save_interval == 0:
                print("\nglobal_train_step {} - step {} - loss {:.10f}".format(global_train_step, steps, loss),
                      get_current_time())
                save(model, args.save_dir, 'snapshot', global_train_step)
    return model, global_train_step
Example #3
0
def eval_compute_f1_5(data_iter, model, args):
    with torch.no_grad():
        model.eval()
        f1_5_dict = dict()
        print("type of f1_5 %s" % type(f1_5_dict))
        cnt = 0
        for batch in data_iter:

            id_list = [x.qid for x in batch]

            # features
            t = get_specific_comp_list("title", batch)
            dt = get_specific_comp_list("desc_text", batch)
            dc = get_specific_comp_list("desc_code", batch)

            # label
            target = np.array(get_specific_comp_list("tags", batch))

            t = torch.tensor(t).long()
            dt = torch.tensor(dt).long()
            dc = torch.tensor(dc).long()
            target = torch.tensor(target).float()

            if args.cuda:
                t, dt, dc, target = t.cuda(), dt.cuda(), dc.cuda(), target.cuda()

            logit = model(t, dt, dc)
            if torch.cuda.is_available():
                f1_batch, cnt_batch = evaluate_batch_f1_5(pred=logit.cpu().detach().numpy(),
                                                          label=target.cpu().detach().numpy())
            else:
                f1_batch, cnt_batch = evaluate_batch_f1_5(pred=logit.detach().numpy(),
                                                          label=target.detach().numpy())

            for i in range(len(id_list)):
                f1_5_dict[id_list[i]] = f1_batch[i]
            cnt += cnt_batch

    print("type of f1_5 %s" % type(f1_5_dict))
    return f1_5_dict, cnt
Example #4
0
def eval(data_iter, model, args, topk_list):
    with torch.no_grad():
        model.eval()
        pre = [0.0] * len(topk_list)
        rc = [0.0] * len(topk_list)
        f1 = [0.0] * len(topk_list)
        cnt = 0
        for batch in data_iter:
            # features
            t = get_specific_comp_list("title", batch)
            dt = get_specific_comp_list("desc_text", batch)
            dc = get_specific_comp_list("desc_code", batch)
            # label
            target = np.array(get_specific_comp_list("tags", batch))

            t = torch.tensor(t).long()
            dt = torch.tensor(dt).long()
            dc = torch.tensor(dc).long()
            target = torch.tensor(target).float()

            if args.cuda:
                t, dt, dc, target = t.cuda(), dt.cuda(), dc.cuda(), target.cuda()

            logit = model(t, dt, dc)
            if torch.cuda.is_available():
                pre_batch, rc_batch, f1_batch, cnt_batch = evaluate_batch(pred=logit.cpu().detach().numpy(),
                                                                          label=target.cpu().detach().numpy(),
                                                                          topk_list=topk_list)
            else:
                pre_batch, rc_batch, f1_batch, cnt_batch = evaluate_batch(pred=logit.detach().numpy(),
                                                                          label=target.detach().numpy(),
                                                                          topk_list=topk_list)

            for idx, topk in enumerate(topk_list):
                pre[idx] += pre_batch[idx]
                rc[idx] += rc_batch[idx]
                f1[idx] += f1_batch[idx]
            cnt += cnt_batch

    return pre, rc, f1, cnt