コード例 #1
0
 def __init__(self,
              model: BertForSequenceClassification,
              tokenizer: BertTokenizer,
              seed: int = 100):
     """
     :param model: `BertForSequenceClassification` model to train, num_labels should be set to 3
     :param tokenizer: tokenizer for the model
     :param seed: seed for reproducible results
     """
     random.seed(seed)
     np.random.seed(seed)
     torch.manual_seed(seed)
     self._set_up_device()
     self.model = model
     model.cuda()
     model.to(self.device)
     self.tokenizer = tokenizer
コード例 #2
0
        eval_list.pop(i)

# 主要用于统计类别std_id数量 #
myDataset = SupremeClsDataset(
    tokenizer, './dataset/computed/test_with_label'.format(LABEL_ID),
    './dataset/supreme/l{}/std_dict'.format(LABEL_ID), 100)

# %%
config = BertConfig.from_json_file('./dataset/bert_config.json')
config.num_labels = len(myDataset.cls_label_2_id)
judger_config = BertConfig.from_json_file('./dataset/bert_config.json')
judger_config.num_labels = 2
model = BertForSequenceClassification(config=config)
model_1 = BertForSequenceClassification(config=judger_config)

model.cuda()
model_1.cuda()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3]).cuda()
model_1 = torch.nn.DataParallel(model_1, device_ids=[0, 1, 2, 3]).cuda()
model.to(device)
model_1.to(device)

model_dict = torch.load("./model/supreme/l{}/bert_supreme_{}.pth".format(
    LABEL_ID, EVAL_EPOCH)).module.state_dict()
model.module.load_state_dict(model_dict)

# %%
with torch.no_grad():
    model.eval()
    model_1.eval()
コード例 #3
0
# 主要用于统计类别std_id数量 #
myDataset = SupremeClsDataset(
    tokenizer, './dataset/computed/test_with_label'.format(LABEL_ID),
    './dataset/supreme/l{}/std_dict'.format(LABEL_ID), 100)

# %%
EVAL_EPOCH = [12, 22]

config = BertConfig.from_json_file('./dataset/bert_config.json')
config.num_labels = len(myDataset.cls_label_2_id)
_config = BertConfig.from_json_file('./dataset/bert_config.json')
_config.num_labels = len(myDataset.cls_label_2_id)
model = BertForSequenceClassification(config=config)
_model = BertForSequenceClassification(config=_config)

model.cuda()
_model.cuda()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3]).cuda()
_model = torch.nn.DataParallel(_model, device_ids=[0, 1, 2, 3]).cuda()
model.to(device)
_model.to(device)

model_dict = torch.load("./model/supreme/l{}/bert_supreme_{}.pth".format(
    LABEL_ID, EVAL_EPOCH[0])).module.state_dict()
model.module.load_state_dict(model_dict)

_model_dict = torch.load("./model/supreme/l{}/bert_supreme_{}.pth".format(
    LABEL_ID, EVAL_EPOCH[1])).module.state_dict()
_model.module.load_state_dict(_model_dict)
コード例 #4
0
config = BertConfig.from_json_file('./dataset/bert_config.json')
config.output_hidden_states = True
model = BertModel.from_pretrained(
    './model/bert_pre58_4/pytorch_model.bin', config=config)

model.cuda()
model = torch.nn.DataParallel(model, device_ids=[0, 1, 2, 3]).cuda()
model.to(device)

save_offset = 12

supreme_config = BertConfig.from_json_file('./dataset/bert_config.json')
supreme_config.num_labels = len(myDataset.cls_label_2_id)
model_ = BertForSequenceClassification(config=supreme_config)

model_.cuda()
model_ = torch.nn.DataParallel(model_, device_ids=[0, 1, 2, 3]).cuda()
model_.to(device)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam([{'params': model.parameters(), 'lr': 5e-5},
                        {'params': textCNN.parameters(), 'lr': 1e-3}], lr=1e-3, weight_decay=0.)

# %%
losses = []

num_epochs = 30
for epoch in range(num_epochs):
    train_count = 0
    train_loss = 0
    train_acc = []
コード例 #5
0
    model = BertForSequenceClassification(config=config)
    model = BertForSequenceClassification.from_pretrained('bert-base-chinese',
                                                          config=config)

    optimizer = AdamW(model.parameters(), lr=LR, correct_bias=False)
    scheduler = WarmupLinearSchedule(optimizer,
                                     warmup_steps=WARMUP_STEPS,
                                     t_total=T_TOTAL)

    # optimizer = optim.Adam(model.parameters(), lr=LR)

    print('开始加载训练完成的model...')
    model.load_state_dict(torch.load('90.9847368421052632_bert_cla.ckpt'))
    print('开始测试...')
    model.eval()
    model = model.cuda()
    test_result = []
    num = 0
    for item in test_dataset:
        text_list = list(json.loads(item[1]))
        text_tensor = torch.tensor(text_list).unsqueeze(0).cuda()

        with torch.no_grad():
            # print('list', text_list)
            # print('tensor', text_tensor)
            # print('tensor.shape', text_tensor.shape)
            outputs = model(text_tensor, labels=None)
            num += 1
            print(outputs[0].softmax(dim=1)[0][0], num)
            pre = outputs[0].argmax(dim=1)
            perdl = outputs[0].softmax(dim=1)[0][0]