loss_.backward() optimizer.step() train_l_sum += loss_.cpu().item() train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item() n += y.shape[0] batch_count += 1 test_acc = evaluate_accuracy(test_iter, net) print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f,\ time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, test_acc, time.time() - start)) lr, num_epochs = 0.01, 5 optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr) loss = nn.CrossEntropyLoss() train(train_iter, test_iter, net, loss, optimizer, device, num_epochs) def predict_sentiment(net, vocab, sentence): """sentence是词语的列表""" device = list(net.parameters())[0].device sentence = torch.tensor([vocab.stoi[word] for word in sentence], device=device) label = torch.argmax(net(sentence.view((1, -1))), dim=1) return 'positive' if label.item() == 1 else 'negative' def main():
print("索引化后的训练数据:", len(train_features)) print("索引化后的测试数据:", len(test_features)) print("训练集标签:", len(labels)) from model import generator, BiRNN, train, evaluate_accuracy from torch import nn, optim import torch from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(np.array(train_features), np.array(labels), test_size=0.2) x_train = torch.tensor(x_train, dtype=torch.long) y_train = torch.tensor(y_train) x_test = torch.tensor(x_test, dtype=torch.long) y_test = torch.tensor(y_test) test_features = torch.tensor(np.array(test_features), dtype=torch.long) embeddings_matrix = torch.tensor(embeddings_matrix) net = BiRNN(word_index, 100, 100, 2) net.embedding.weight.data.copy_(embeddings_matrix) net.embedding.weight.requires_grad = False loss = nn.CrossEntropyLoss() optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=0.0001) num_epochs = 10 train_iter = generator(x_train, y_train, 10, train=True) test_iter = generator(x_test, y_test, 10, train=False) train(train_iter, test_iter, net, loss, optimizer, num_epochs)