def __init__(self, dataset):
        self.model_name = 'bert'
        # self.train_path = dataset + '/data/train.txt'                                # 训练集
        # self.dev_path = dataset + '/data/dev.txt'                                    # 验证集
        # self.test_path = dataset + '/data/test.txt'                                  # 测试集

        self.train_path = dataset + '/data/train.bin'  # 训练集
        self.dev_path = dataset + '/data/dev.bin'  # 验证集
        self.test_path = dataset + '/data/test.bin'  # 测试集
        self.class_list = [
            x.strip() for x in open(dataset + '/data/class.txt').readlines()
        ]  # 类别名单
        self.save_path = dataset + '/saved_dict/' + self.model_name + '.ckpt'  # 模型训练结果
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')  # 设备

        self.require_improvement = 1000  # 若超过1000batch效果还没提升,则提前结束训练
        self.num_classes = len(self.class_list)  # 类别数
        self.num_epochs = 3  # epoch数
        self.batch_size = 8  # mini-batch大小
        self.pad_size = 100  # 每句话处理成的长度(短填长切)
        self.learning_rate = 1e-5  # 学习率
        self.adam_epsilon = 1e-6  # eps
        self.bert_path = './albert_tiny'
        # self.tokenizer = BertTokenizer.from_pretrained(self.bert_path)
        self.tokenizer = AlbertTokenizer.from_pretrained(
            './albert_tiny/vocab.txt')
        # self.hidden_size = 768
        self.hidden_size = 312
    def __init__(self):

        self.save_path = basedir + '/models/albert.ckpt'  # 模型训练结果
        self.class_list = [
            x.strip() for x in open(basedir + '/models/class.txt').readlines()
        ]  # 类别名单
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')  # 设备

        self.num_classes = len(self.class_list)  # 类别数
        self.bert_path = './albert_tiny'
        self.tokenizer = AlbertTokenizer.from_pretrained(self.bert_path)
        self.hidden_size = 312
        self.pad_size = 100  # 每句话处理成的长度(短填长切)
        # self.learning_rate = 1e-5                                       # 学习率
        # self.adam_epsilon = 1e-6                                       # 学习率
        self.PAD, self.CLS = '[PAD]', '[CLS]'
Example #3
0
from albert_zh import AlbertConfig,AlbertForSequenceClassification,AlbertTokenizer
import torch
if __name__ == "__main__":
    tokenizer = AlbertTokenizer.from_pretrained('albert_tiny/vocab.txt')
    model_config = AlbertConfig.from_json_file('./albert_tiny/config.json')
    model = AlbertForSequenceClassification.from_pretrained('./albert_tiny',config = model_config)

    intput_str = '周杰倫,臺灣著名華語流行歌曲男歌手、音樂家、唱片製片人。同時是演員、導演,也是電競團隊隊長兼老闆、服飾品牌老闆。以其個人風格和聲歌手樂創作能力著稱,影響華語樂壇。 在2000年,周杰倫發行了他的首張專輯《Jay》,從屬於唱片公司阿爾發音樂。'
    input_ids = torch.tensor(tokenizer.encode(intput_str)).unsqueeze(0)  # Batch size 1
    labels = torch.tensor([1]).unsqueeze(0)  # Batch size 1
    outputs = model(input_ids, labels=labels)
    loss, logits = outputs[:2]
    
    print(loss,logits)