- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊|接辅导、项目定制# 一、课题背景和开发环境
📌第N4周:使用Word2vec实现文本分类📌
Python 3.8.12
gensim4.3.1
numpy1.21.5 -> 1.24.3
portalocker2.7.0
pytorch1.8.1+cu111
torchtext==0.9.1
📌本周任务:📌文章来源:https://www.toymoban.com/news/detail-533983.html
结合Word2Vec文本内容(第1列)预测文本标签(第2列)
尝试根据第2周的内容独立实现,尽可能的不看本文的代码
进一步了解并学习Word2Vec
任务说明:
本次将加入Word2vec使用PyTorch实现中文文本分类,Word2Vec则是其中的一种词嵌入方法,是一种用于生成词向量的浅层神经网络模型,由Tomas Mikolov及其团队于2013年提出。**Word2Vec通过学习大量文本数据,将每个单词表示为一个连续的向量,这些向量可以捕捉单词之间的语义和句法关系。**更详细的内容可见训练营内的NLP基础知识,数据示例如下:文章来源地址https://www.toymoban.com/news/detail-533983.html
二、数据预处理
1.加载数据
warnings.filterwarnings("ignore") #忽略警告信息
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('device =', device)
print('STEP.1', '-' * 19)
''' 加载自定义中文数据 '''
train_data = pd.read_csv('./data/train.csv', sep='\t', header=None)
print(train_data.head())
# 构造数据集迭代器
def coustom_data_iter(texts, labels):
for x, y in zip(texts, labels):
yield x, y
x = train_data[0].values[:]
# 多类标签的one-hot展开
y = train_data[1].values[:]
2.构建词典
print('STEP.2', '-' * 19)
''' 构建词典 '''
# 训练Word2Vec浅层神经网络模型
w2v = Word2Vec(vector_size=100, #是指特征向量的维度,默认为100。
min_count=3) #可以对字典做截断. 词频少于min_count次数的单词会被丢弃掉, 默认值为5。
w2v.build_vocab(x)
w2v.train(x,
total_examples=w2v.corpus_count,
epochs=20)
# 将文本转化为向量
def average_vec(text):
vec = np.zeros((1,100))
for word in text:
try:
vec += w2v.wv[word].reshape((1,100))
except KeyError:
continue
return vec
# 将词向量保存为ndarray
x_vec = np.concatenate([average_vec(z) for z in x])
print('len(x) =', len(x), 'len(x_vec) =', len(x_vec))
# 保存Word2Vec模型及词向量
w2v.save('output/w2v_model.pkl')
train_iter = coustom_data_iter(x_vec, y)
3.生成数据批次和迭代器
print('STEP.3', '-' * 19)
''' 准备数据处理管道 '''
label_name = list(set(train_data[1].values[:]))
print(label_name)
text_pipeline = lambda x: average_vec(x)
label_pipeline = lambda x: label_name.index(x)
print('你在干嘛', text_pipeline('你在干嘛'))
print('Travel-Query', label_pipeline('Travel-Query'))
''' 生成数据批次和迭代器 '''
def collate_batch(batch):
label_list, text_list = [], []
#offsets = [0]
for (_text, _label) in batch:
# 标签列表
label_list.append(label_pipeline(_label))
# 文本列表
processed_text = torch.tensor(text_pipeline(_text), dtype=torch.float32)
text_list.append(processed_text)
#offsets.append(processed_text.size(0))
label_list = torch.tensor(label_list, dtype=torch.int64) # torch.Size([64])
text_list = torch.cat(text_list) # 若干tensor组成的列表变成一个tensor
#offsets = torch.tensor(offsets[:-1]).cumsum(dim=0) # torch.Size([64])
return text_list.to(device), label_list.to(device) # , offsets.to(device)
# 数据加载器
#dataloader = DataLoader(train_iter, batch_size=8, shuffle=False, collate_fn=collate_batch)
二、模型构建
1.搭建模型
print('STEP.4', '-' * 19)
''' 搭建文本分类模型 '''
class TextClassificationModel(nn.Module):
def __init__(self, num_class):
super(TextClassificationModel, self).__init__()
self.fc = nn.Linear(100, num_class)
def forward(self, text):
output = self.fc(text)
return output
2.初始化模型
print('STEP.5 Initialize', '-' * 19)
''' 初始化实例 '''
num_class = len(label_name)
vocab_size = 100000 # 词典大小
emsize = 12 # 嵌入的维度
model = TextClassificationModel(num_class).to(device)
3.定义训练与评估函数
''' 训练函数 '''
def train(dataloader):
model.train() # 训练模式
total_acc, train_loss, total_count = 0, 0, 0
log_interval = 50
start_time = time.time()
for idx, (text, label) in enumerate(dataloader):
optimizer.zero_grad() # grad属性归零
predited_label = model(text)
loss = criterion(predited_label, label)
loss.backward() # 反向传播
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.1) # 梯度裁剪
optimizer.step() # 每一步自动更新
# 记录acc与loss
total_acc += (predited_label.argmax(1) == label).sum().item()
train_loss += loss.item()
total_count += label.size(0)
if idx % log_interval == 0 and idx > 0:
elapsed = time.time() - start_time
print('| epoch {:3d} | {:5d}/{:5d} batches, train_acc {:8.3f} train_loss {:8.3f}'.format(epoch, idx, len(dataloader), total_acc/total_count, train_loss/total_count))
total_acc, train_loss, total_count = 0, 0, 0
start_time = time.time()
''' 评估函数 '''
def evaluate(dataloader):
model.eval() # 切换为测试模式
total_acc, train_loss, total_count = 0, 0, 0
with torch.no_grad():
for idx, (text, label) in enumerate(dataloader):
predited_label = model(text)
loss = criterion(predited_label, label) # 计算loss值
# 记录测试数据
total_acc += (predited_label.argmax(1) == label).sum().item()
train_loss += loss.item()
total_count += label.size(0)
return total_acc/total_count, train_loss/total_count
三、训练模型
1.拆分数据集并运行模型
''' 开始训练 '''
if __name__ == '__main__':
# 超参数(Hyperparameters)
EPOCHS = 10 # epoch
LR = 5 # learning rate
BATCH_SIZE = 64 # batch size for training
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=LR)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=0.1)
total_accu = None
# 构建数据集
train_iter = coustom_data_iter(train_data[0].values[:], train_data[1].values[:])
train_dataset = list(train_iter)
# 划分数据集
num_train = int(len(train_dataset) * 0.8)
split_train_, split_valid_ = random_split(train_dataset, [num_train, len(train_dataset) - num_train])
# 加载数据集
train_dataloader = DataLoader(split_train_, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch) # shuffle表示随机打乱
valid_dataloader = DataLoader(split_valid_, batch_size=BATCH_SIZE, shuffle=True, collate_fn=collate_batch)
for epoch in range(1, EPOCHS + 1):
epoch_start_time = time.time()
train(train_dataloader)
accu_val, loss_val = evaluate(valid_dataloader)
# 获取当前的学习率
lr = optimizer.state_dict()['param_groups'][0]['lr']
if total_accu is not None and total_accu > accu_val:
scheduler.step()
else:
total_accu = accu_val
print('-' * 59)
print('| end of epoch {:3d} | time: {:5.2f}s | '
'valid_acc {:8.3f} valid_loss {:8.3f} | lr {:8.6f}'.format(epoch, time.time()-epoch_start_time, accu_val, loss_val, lr))
print('-' * 59)
torch.save(model.state_dict(), 'output\\model_TextClassification.pth')
print('Checking the results of test dataset.')
accu_test, loss_test = evaluate(valid_dataloader)
print('test accuracy {:8.3f}, test loss {:8.3f}'.format(accu_test, loss_test))
2.测试指定数据
''' 预测函数 '''
def predict(text, text_pipeline):
with torch.no_grad():
text = torch.tensor(text_pipeline(text), dtype=torch.float32)
print(text.shape)
output = model(text)
return output.argmax(1).item()
''' 以下是预测 '''
if __name__=='__main__':
model.load_state_dict(torch.load('output\\model_TextClassification.pth'))
#label_name = ['Alarm-Update', 'Other', 'Audio-Play', 'Calendar-Query', 'HomeAppliance-Control', 'Radio-Listen', 'Travel-Query', 'Video-Play', 'TVProgram-Play', 'FilmTele-Play', 'Weather-Query', 'Music-Play']
ex_text_str = "随便播放一首专辑阁楼里的佛里的歌"
#ex_text_str = "还有双鸭山到淮阴的汽车票吗13号的"
model = model.to("cpu")
print("该文本的类别是:%s" % label_name[predict(ex_text_str, text_pipeline)])
到了这里,关于第N4周:使用Word2vec实现文本分类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!