700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > hugging face模型微调(1)新闻文本分类

hugging face模型微调(1)新闻文本分类

时间:2022-02-10 19:57:25

相关推荐

hugging face模型微调(1)新闻文本分类

本文借鉴:[1]/sinat_28931055/article/details/119560054

[2]/p/537271887

1、hugging face介绍

hugging face总部位于纽约,是一家专注于自然语言处理、人工智能和分布式系统的创业公司。他们所提供的聊天机器人技术一直颇受欢迎,但更出名的是他们在NLP开源社区上的贡献。Huggingface一直致力于自然语言处理NLP技术的平民化(democratize),希望每个人都能用上最先进(SOTA, state-of-the-art)的NLP技术,而非困窘于训练资源的匮乏。同时Hugging Face专注于NLP技术,拥有大型的开源社区。尤其是在github上开源的自然语言处理,预训练模型库 Transformers,已被下载超过一百万次,github上超过24000个star。Transformers 提供了NLP领域大量state-of-art的 预训练语言模型结构的模型和调用框架。

2、环境安装

transformers

清华源安装transformers

pip install transformers -i https://pypi.tuna./simple/

3、模型下载

由于

model = BertForSequenceClassification.from_pretrained(pretrain_Model_path)

经常会遇到网络中断的问题,所以可以到huggingface官网下载。

config.json:模型的结构文件

pytorch_model.bin:模型的权重

vocab.txt:模型所用的词表

这三个文件下载好之后,把它复制到自己的项目之中(huggingface_test)

4、数据预处理

1、数据集介绍(训练集)

可以看到数据集的类型为句子\t标签

2、数据集处理(训练集)

1、导入包

// 导入的包import refrom sklearn.utils import shuffleimport pandas as pdimport torchfrom torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSamplerfrom transformers import AutoModelForSequenceClassificationfrom transformers import AutoTokenizerimport numpy as np

2、标签定义,总共有10个标签分类

// 标签字典label_dic = {'finance':0,'realty':1,'stocks':2,'education':3,'science':4,'society':5,'politics':6,'sports':7,'game':8,'entertainment':9}

3、数据预处理

(1)使用get_train_data函数读取训练集,制作成两个list,并添加到一个字典中。

(2)只使用前8000个数据进行训练。

def get_train_data(file,label_dic):content = []label = []with open(file, "r", encoding="utf-8") as f:for i in f.readlines():c,l = i.split("\t")content.append(re.sub('[^\u4e00-\u9fa5]',"",c))label.append(int(l.strip()))return content,labelcontent,label = get_train_data('D:\\project_vscode\\Python\\nlp\\huggingface_test\\train.txt',label_dic=label_dic)data = pd.DataFrame({"content":content,"label":label})# data = shuffle(data)train_data = tokenizer(data.content.to_list()[:8000], padding = "max_length", max_length = 40, truncation=True ,return_tensors = "pt")train_label = data.label.to_list()[:8000]

5、模型加载

1、分词器和模型加载

就是导入之前下载的文件

tokenizer = AutoTokenizer.from_pretrained("D:\\project_vscode\\Python\\nlp\\huggingface_test\\")model = AutoModelForSequenceClassification.from_pretrained("D:\\project_vscode\\Python\\nlp\\huggingface_test\\", num_labels=10)

2、定义优化器和学习率

在AutoModelForSequenceClassification.from_pretrained(“./path”, num_labels=10) 这个函数中,transformer 已经帮你定义了损失函数,既10个分类的交叉熵损失,所以下方我们只需要自己定义优化器和学习率即可。

batch_size = 16train = TensorDataset(train_data["input_ids"], train_data["attention_mask"], torch.tensor(train_label))train_sampler = RandomSampler(train)train_dataloader = DataLoader(train, sampler=train_sampler, batch_size=batch_size)# 定义优化器from torch.optim import AdamWoptimizer = AdamW(model.parameters(), lr=1e-4)# 定义学习率和训练轮数num_epochs = 1from transformers import get_schedulernum_training_steps = num_epochs * len(train_dataloader)lr_scheduler = get_scheduler(name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps)

6、模型训练

device = torch.device("cpu") # device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") 因为我的pytorch环境是cpu,所以用的不是cudamodel.to(device)for epoch in range(num_epochs):total_loss = 0model.train()for step, batch in enumerate(train_dataloader):if step % 10 == 0 and not step == 0:print("step: ",step, " loss:",total_loss/(step*batch_size))b_input_ids = batch[0].to(device)b_input_mask = batch[1].to(device)b_labels = batch[2].to(device)model.zero_grad() outputs = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask, labels=b_labels)loss = outputs.loss total_loss += loss.item()loss.backward()torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)optimizer.step()lr_scheduler.step()avg_train_loss = total_loss / len(train_dataloader)print("avg_loss:",avg_train_loss)

7、测试

test = tokenizer("词汇阅读是关键 08年考研暑期英语复习全指南",return_tensors="pt",padding="max_length",max_length=100)model.eval()with torch.no_grad(): outputs = model(test["input_ids"], token_type_ids=None, attention_mask=test["attention_mask"])pred_flat = np.argmax(outputs["logits"],axis=1).numpy().squeeze()print(pred_flat.tolist())

8、实验结果

测试句子,分类是3

下图可以看到实验结果分类就是3

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。