NLP transformer网络下Bert模型的完整训练
数据准备
import torch
import pandas as pd
import torch.nn as nn
from transformers import BertModel
from transformers import BertTokenizer
from sklearn.model_selection import train_test_split
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from torch.optim import Adam
data_worse = pd.read_csv('data/1.csv')
data_worse['label'] = 0
data_bad = pd.read_csv('data/2.csv')
data_bad['label'] = 1
data_normal = pd.read_csv('data/3.csv')
data_normal['label'] = 2
data_good = pd.read_csv('data/4.csv')
data_good['label'] = 3
data_better = pd.read_csv('data/happy.csv')
data_better['label'] = 4
data = pd.concat([data_worse[:10000], data_bad[:10000], data_normal[:10000], data_good[:10000], data_better[:10000]],
axis=0).reset_index(drop=True)
X = data.comment.values
y = data.lab