在前面使用word embedding去实现了文本情感分类。那么现在在这个模型中添加上LSTM层,为了达到更好的效果,做一下修改
1.MAX_LEN=200
2.构建dataset的过程,将数据转化为2分类,前面是十分类。pos类为1,neg为0,因为25000个要本做十分类数据量太小了
3.在实例化LSTM的时候,使用dropout=0.5,在模型评估过程中,dropout自动会为0
一.修改模型
python
"""构建模型model.py文件"""
import torch.nn as nn
import torch
import config
import torch.nn.functional as F
class IMDBLstmmodel(nn.Module):
def __init__(self):
super(IMDBLstmmodel,self).__init__()
self.hidden_size = 64 # 设置隐藏个数
self.embedding_dim = 200 # 设置一个词语用多大的向量表示
self.num_layer = 2 # 设置隐藏层数量
self.bidriectional = True # 是否使用双向LSTM
self.bi_num = 2 if self.bidriectional else 1
self.dropout = 0.5
#以上部分为超参数,可以自行修改
self.embedding = nn.Embedding(len(config.ws),self.embedding_dim,padding_idx=config.ws.PAD) #实例化embedding[N,300]
self.lstm = nn.LSTM(self.embedding_dim,self.hidden_size,self.num_layer,bidirectional=True,dropout=self.dropout) # 实例化lstm
#使用两个全连接层,中间使用relu激活函数
self.fc = nn.Linear(self.hidden_size*self.bi_num,20)
self.fc2 = nn.Linear(20,2)
def forward(self, x):
x = self.embedding(x)
x = x.permute(1,0,2) #进行轴交换
h_0,c_0 = self.init_hidden_state(x.size(1))
_,(h_n,c_n) = self.lstm(x,(h_0,c_0))
#只要最后一个lstm单元处理的结果,这里多去的hidden state
out = torch.cat([h_n[-2, :, :], h_n[-1, :, :]], dim=-1)
out = self.fc(out)
out = F.relu(out)
out = self.fc2(out)
return F.log_softmax(out,dim=-1)
def init_hidden_state(self,batch_size):
h_0 = torch.rand(self.num_layer * self.bi_num, batch_size, self.hidden_size).to(device)
c_0 = torch.rand(self.num_layer * self.bi_num, batch_size, self.hidden_size).to(device)
return h_0,c_0
二.完成训练和测试代码
python
train_batch_size = 64
test_batch_size = 5000
# imdb_model = IMDBModel(MAX_LEN) #基础model
imdb_model = IMDBLstmmodel().to(device) #在gpu上运行,提高运行速度
# imdb_model.load_state_dict(torch.load("model/mnist_net.pkl"))
optimizer = optim.Adam(imdb_model.parameters())
criterion = nn.CrossEntropyLoss()
def train(epoch):
mode = True
imdb_model.train(mode)
train_dataloader =get_dataloader(mode,train_batch_size)
for idx,(target,input,input_lenght) in enumerate(train_dataloader):
target = target.to(device)
input = input.to(device)
optimizer.zero_grad()
output = imdb_model(input)
loss = F.nll_loss(output,target) #traget需要是[0,9],不能是[1-10]
loss.backward()
optimizer.step()
if idx %10 == 0:
pred = torch.max(output, dim=-1, keepdim=False)[-1]
acc = pred.eq(target.data).cpu().numpy().mean()*100.
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\t ACC: {:.6f}'.format(epoch, idx * len(input), len(train_dataloader.dataset),
100. * idx / len(train_dataloader), loss.item(),acc))
torch.save(imdb_model.state_dict(), "model/mnist_net.pkl")
torch.save(optimizer.state_dict(), 'model/mnist_optimizer.pkl')
def test():
mode = False
imdb_model.eval()
test_dataloader = get_dataloader(mode, test_batch_size)
with torch.no_grad():
for idx,(target, input, input_lenght) in enumerate(test_dataloader):
target = target.to(device)
input = input.to(device)
output = imdb_model(input)
test_loss = F.nll_loss(output, target,reduction="mean")
pred = torch.max(output,dim=-1,keepdim=False)[-1]
correct = pred.eq(target.data).sum()
acc = 100. * pred.eq(target.data).cpu().numpy().mean()
print('idx: {} Test set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(idx,test_loss, correct, target.size(0),acc))
if __name__ == "__main__":
test()
for i in range(10):
train(i)
test()