![](https://img.haomeiwen.com/i3968643/4c879cd2489cc75f.png)
image.png
![](https://img.haomeiwen.com/i3968643/ea26b3c8629014e0.png)
image.png
![](https://img.haomeiwen.com/i3968643/ea1ba4cb48a0c301.png)
image.png
import torch
import torchtext
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torchtext.vocab import GloVe
TEXT = torchtext.data.Field(lower=True, fix_length=200, batch_first=False)
LABEL = torchtext.data.Field(sequential=False)
# make splits for data
train, test = torchtext.datasets.IMDB.splits(TEXT, LABEL)
# 构建词表 vocab
#TEXT.build_vocab(train, vectors=GloVe(name='6B', dim=300),
# max_size=20000, min_freq=10)
TEXT.build_vocab(train, max_size=10000, min_freq=10, vectors=None)
LABEL.build_vocab(train)
[(k, v) for k, v in TEXT.vocab.stoi.items() if v>9999]
TEXT.vocab.vectors
BATCHSIZE = 512
# make iterator for splits
train_iter, test_iter = torchtext.data.BucketIterator.splits(
(train, test), batch_size=BATCHSIZE)
b = next(iter(train_iter))
hidden_size = 300
embeding_dim = 100
class RNN_Encoder(nn.Module):
def __init__(self, input_dim, hidden_size):
super(RNN_Encoder, self).__init__()
self.rnn = nn.LSTMCell(input_dim, hidden_size)
def forward(self, inputs):
bz = inputs.shape[1]
ht = torch.zeros((bz, hidden_size)).cuda()
ct = torch.zeros((bz, hidden_size)).cuda()
for word in inputs:
ht, ct = self.rnn(word, (ht, ct))
return ht, ct
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.em = nn.Embedding(20002, embeding_dim) # 200*batch*100
self.rnn = RNN_Encoder(embeding_dim, hidden_size) # batch*300
self.fc1 = nn.Linear(hidden_size, 256)
self.fc2 = nn.Linear(256, 3)
def forward(self, x):
x = self.em(x)
_, x = self.rnn(x)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
model = Net()
if torch.cuda.is_available():
model.to('cuda')
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
def fit(epoch, model, trainloader, testloader):
correct = 0
total = 0
running_loss = 0
model.train()
for b in trainloader:
x, y = b.text, b.label
if torch.cuda.is_available():
x, y = b.text.to('cuda'), b.label.to('cuda')
y_pred = model(x)
loss = loss_fn(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
y_pred = torch.argmax(y_pred, dim=1)
correct += (y_pred == y).sum().item()
total += y.size(0)
running_loss += loss.item()
# exp_lr_scheduler.step()
epoch_loss = running_loss / len(trainloader.dataset)
epoch_acc = correct / total
test_correct = 0
test_total = 0
test_running_loss = 0
model.eval()
with torch.no_grad():
for b in testloader:
x, y = b.text, b.label
if torch.cuda.is_available():
x, y = x.to('cuda'), y.to('cuda')
y_pred = model(x)
loss = loss_fn(y_pred, y)
y_pred = torch.argmax(y_pred, dim=1)
test_correct += (y_pred == y).sum().item()
test_total += y.size(0)
test_running_loss += loss.item()
epoch_test_loss = test_running_loss / len(testloader.dataset)
epoch_test_acc = test_correct / test_total
print('epoch: ', epoch,
'loss: ', round(epoch_loss, 3),
'accuracy:', round(epoch_acc, 3),
'test_loss: ', round(epoch_test_loss, 3),
'test_accuracy:', round(epoch_test_acc, 3)
)
return epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc
epochs = 30
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs):
epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc = fit(epoch,
model,
train_iter,
test_iter)
train_loss.append(epoch_loss)
train_acc.append(epoch_acc)
test_loss.append(epoch_test_loss)
test_acc.append(epoch_test_acc)
epoch: 0 loss: 0.001 accuracy: 0.497 test_loss: 0.001 test_accuracy: 0.507
epoch: 1 loss: 0.001 accuracy: 0.515 test_loss: 0.001 test_accuracy: 0.509
epoch: 2 loss: 0.001 accuracy: 0.526 test_loss: 0.001 test_accuracy: 0.526
epoch: 3 loss: 0.001 accuracy: 0.546 test_loss: 0.001 test_accuracy: 0.523
epoch: 4 loss: 0.001 accuracy: 0.567 test_loss: 0.001 test_accuracy: 0.571
epoch: 5 loss: 0.001 accuracy: 0.597 test_loss: 0.001 test_accuracy: 0.588
epoch: 6 loss: 0.001 accuracy: 0.706 test_loss: 0.001 test_accuracy: 0.765
epoch: 7 loss: 0.001 accuracy: 0.822 test_loss: 0.001 test_accuracy: 0.802
epoch: 8 loss: 0.001 accuracy: 0.861 test_loss: 0.001 test_accuracy: 0.805
epoch: 9 loss: 0.001 accuracy: 0.887 test_loss: 0.001 test_accuracy: 0.823
epoch: 10 loss: 0.0 accuracy: 0.909 test_loss: 0.001 test_accuracy: 0.827
epoch: 11 loss: 0.0 accuracy: 0.926 test_loss: 0.001 test_accuracy: 0.83
epoch: 12 loss: 0.0 accuracy: 0.942 test_loss: 0.001 test_accuracy: 0.83
epoch: 13 loss: 0.0 accuracy: 0.949 test_loss: 0.001 test_accuracy: 0.818
epoch: 14 loss: 0.0 accuracy: 0.968 test_loss: 0.001 test_accuracy: 0.827
epoch: 15 loss: 0.0 accuracy: 0.979 test_loss: 0.001 test_accuracy: 0.825
epoch: 16 loss: 0.0 accuracy: 0.982 test_loss: 0.001 test_accuracy: 0.825
epoch: 17 loss: 0.0 accuracy: 0.988 test_loss: 0.002 test_accuracy: 0.823
epoch: 18 loss: 0.0 accuracy: 0.995 test_loss: 0.002 test_accuracy: 0.822
epoch: 19 loss: 0.0 accuracy: 0.996 test_loss: 0.002 test_accuracy: 0.827
epoch: 20 loss: 0.0 accuracy: 0.997 test_loss: 0.002 test_accuracy: 0.825
epoch: 21 loss: 0.0 accuracy: 0.999 test_loss: 0.002 test_accuracy: 0.829
epoch: 22 loss: 0.0 accuracy: 0.999 test_loss: 0.002 test_accuracy: 0.827
epoch: 23 loss: 0.0 accuracy: 0.999 test_loss: 0.003 test_accuracy: 0.828
epoch: 24 loss: 0.0 accuracy: 0.999 test_loss: 0.003 test_accuracy: 0.824
epoch: 25 loss: 0.0 accuracy: 0.999 test_loss: 0.003 test_accuracy: 0.826
epoch: 26 loss: 0.0 accuracy: 0.998 test_loss: 0.003 test_accuracy: 0.825
epoch: 27 loss: 0.0 accuracy: 0.995 test_loss: 0.002 test_accuracy: 0.821
epoch: 28 loss: 0.0 accuracy: 0.993 test_loss: 0.002 test_accuracy: 0.825
epoch: 29 loss: 0.0 accuracy: 0.997 test_loss: 0.002 test_accuracy: 0.821
class RNN_Encoder(nn.Module):
def __init__(self, input_dim, hidden_size):
super(RNN_Encoder, self).__init__()
self.rnn = nn.GRUCell(input_dim, hidden_size)
def forward(self, inputs):
bz = inputs.shape[1]
ht = torch.zeros((bz, hidden_size)).cuda()
for word in inputs:
ht = self.rnn(word, ht)
return ht # 注意, 这里我们只取了最后一个状态输出
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.em = nn.Embedding(20002, embeding_dim) # 200*batch*100
self.rnn = RNN_Encoder(embeding_dim, hidden_size) # batch*300
self.fc1 = nn.Linear(hidden_size, 256)
self.fc2 = nn.Linear(256, 3)
def forward(self, x):
x = self.em(x)
x = self.rnn(x)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
model = Net()
if torch.cuda.is_available():
model.to('cuda')
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs):
epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc = fit(epoch,
model,
train_iter,
test_iter)
train_loss.append(epoch_loss)
train_acc.append(epoch_acc)
test_loss.append(epoch_test_loss)
test_acc.append(epoch_test_acc)
epoch: 0 loss: 0.002 accuracy: 0.492 test_loss: 0.002 test_accuracy: 0.505
epoch: 1 loss: 0.001 accuracy: 0.503 test_loss: 0.001 test_accuracy: 0.5
epoch: 2 loss: 0.001 accuracy: 0.504 test_loss: 0.001 test_accuracy: 0.503
epoch: 3 loss: 0.001 accuracy: 0.507 test_loss: 0.001 test_accuracy: 0.505
epoch: 4 loss: 0.001 accuracy: 0.516 test_loss: 0.001 test_accuracy: 0.503
epoch: 5 loss: 0.001 accuracy: 0.511 test_loss: 0.001 test_accuracy: 0.504
epoch: 6 loss: 0.001 accuracy: 0.524 test_loss: 0.001 test_accuracy: 0.504
epoch: 7 loss: 0.001 accuracy: 0.526 test_loss: 0.001 test_accuracy: 0.506
epoch: 8 loss: 0.001 accuracy: 0.517 test_loss: 0.001 test_accuracy: 0.511
epoch: 9 loss: 0.001 accuracy: 0.534 test_loss: 0.001 test_accuracy: 0.506
epoch: 10 loss: 0.001 accuracy: 0.525 test_loss: 0.001 test_accuracy: 0.529
epoch: 11 loss: 0.001 accuracy: 0.549 test_loss: 0.001 test_accuracy: 0.538
epoch: 12 loss: 0.001 accuracy: 0.558 test_loss: 0.001 test_accuracy: 0.55
epoch: 13 loss: 0.001 accuracy: 0.571 test_loss: 0.001 test_accuracy: 0.585
epoch: 14 loss: 0.001 accuracy: 0.604 test_loss: 0.001 test_accuracy: 0.611
epoch: 15 loss: 0.001 accuracy: 0.666 test_loss: 0.001 test_accuracy: 0.69
epoch: 16 loss: 0.001 accuracy: 0.72 test_loss: 0.001 test_accuracy: 0.73
epoch: 17 loss: 0.001 accuracy: 0.755 test_loss: 0.001 test_accuracy: 0.749
epoch: 18 loss: 0.001 accuracy: 0.771 test_loss: 0.001 test_accuracy: 0.756
epoch: 19 loss: 0.001 accuracy: 0.787 test_loss: 0.001 test_accuracy: 0.75
epoch: 20 loss: 0.001 accuracy: 0.797 test_loss: 0.001 test_accuracy: 0.771
epoch: 21 loss: 0.001 accuracy: 0.811 test_loss: 0.001 test_accuracy: 0.784
epoch: 22 loss: 0.001 accuracy: 0.82 test_loss: 0.001 test_accuracy: 0.792
epoch: 23 loss: 0.001 accuracy: 0.818 test_loss: 0.001 test_accuracy: 0.787
epoch: 24 loss: 0.001 accuracy: 0.83 test_loss: 0.001 test_accuracy: 0.795
epoch: 25 loss: 0.001 accuracy: 0.837 test_loss: 0.001 test_accuracy: 0.796
epoch: 26 loss: 0.001 accuracy: 0.842 test_loss: 0.001 test_accuracy: 0.804
epoch: 27 loss: 0.001 accuracy: 0.846 test_loss: 0.001 test_accuracy: 0.806
epoch: 28 loss: 0.001 accuracy: 0.854 test_loss: 0.001 test_accuracy: 0.806
epoch: 29 loss: 0.001 accuracy: 0.854 test_loss: 0.001 test_accuracy: 0.811
网友评论