Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改正了一些bug #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions task2/torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import torch
from torch import nn
import numpy as np
from torch.autograd import Variable
import torch.nn.functional as F


Expand All @@ -18,7 +17,7 @@ def __init__(self):
self.rnn = nn.LSTM(input_size=64, hidden_size=128, bidirectional=True)
# self.rnn = nn.GRU(input_size=64, hidden_size=128, num_layers=2, bidirectional=True)
self.f1 = nn.Sequential(nn.Linear(256, 10),
nn.Softmax())
nn.Softmax(-1)) # 新版pytorch使用softmax时应指定维度,否则会抛出use_warning

def forward(self, x):
x = self.embedding(x) # batch_size x text_len x embedding_size 64*600*64
Expand Down
1 change: 0 additions & 1 deletion task2/torch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from torch import nn
from cnews_loader import read_category, read_vocab
from torch_model import TextCNN,TextRNN
from torch.autograd import Variable
import numpy as np

try:
Expand Down
45 changes: 24 additions & 21 deletions task2/torch_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import torch
from torch import nn
from torch import optim
from torch.autograd import Variable
import os

import numpy as np
Expand All @@ -17,25 +16,28 @@ def evaluate(model, Loss, x_val, y_val):
batch_val = batch_iter(x_val, y_val, 64)
acc = 0
los = 0
for x_batch, y_batch in batch_val:
size = len(x_batch)
x = np.array(x_batch)
y = np.array(y_batch)
x = torch.LongTensor(x)
y = torch.Tensor(y)
# y = torch.LongTensor(y)
# x = Variable(x)
# y = Variable(y)
out = model(x)
loss = Loss(out, y)
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
loss_value = np.mean(loss.detach().numpy())
accracy = np.mean((torch.argmax(out, 1) == torch.argmax(y, 1)).numpy())
acc +=accracy*size
los +=loss_value*size
return los/len(x_val), acc/len(x_val)
model.eval()
with torch.no_grad():
for x_batch, y_batch in batch_val:
size = len(x_batch)
x = np.array(x_batch)
y = np.array(y_batch)
x = torch.LongTensor(x)
y = torch.Tensor(y)
# y = torch.LongTensor(y)
# x = Variable(x)
# y = Variable(y)
out = model(x)
loss = Loss(out, y)
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
loss_value = np.mean(loss.numpy())
accracy = np.mean((torch.argmax(out, 1) == torch.argmax(y, 1)).numpy())
acc +=accracy*size
los +=loss_value*size
model.train()
return los/len(x_val), acc/len(x_val)


base_dir = 'cnews'
Expand All @@ -49,6 +51,7 @@ def train():
x_val, y_val = process_file(val_dir, word_to_id, cat_to_id,600)
#使用LSTM或者CNN
model = TextRNN()
model.train()
# model = TextCNN()
#选择损失函数
Loss = nn.MultiLabelSoftMarginLoss()
Expand Down Expand Up @@ -78,7 +81,7 @@ def train():

# 对模型进行验证
if i % 90 == 0:
los, accracy = evaluate(model, Loss, optimizer, x_val, y_val)
los, accracy = evaluate(model, Loss, x_val, y_val) # 此处不需要优化器参数
print('loss:{},accracy:{}'.format(los, accracy))
if accracy > best_val_acc:
torch.save(model.state_dict(), 'model_params.pkl')
Expand Down