Exemplo n.º 1
0
def train(trainset, model, optimizer, loss_function, testset):
    index = 0
    losses = []
    acc = []
    for epoch in range(6):
        total_loss = torch.Tensor([0])
        for instance in trainset:
            print(index)
            index += 1
            for question in instance['questions']:
                text = stemming(instance['text'])
                ques = stemming(question['question'])
                for answer in question['answers']:
                    model.zero_grad()
                    ans = stemming(answer['answer'])
                    output = model(text, ques, ans)
                    if answer['correct'] == 'True':
                        y = autograd.Variable(torch.FloatTensor([1]))
                    else:
                        y = autograd.Variable(torch.FloatTensor([0]))
                    print("output", output.data[0][0])
                    # avoid 0 gradient
                    if output.data[0][0] == 0:
                        output = output + autograd.Variable(
                            torch.FloatTensor([0.0001]))
                    if output.data[0][0] == 1:
                        output = output - autograd.Variable(
                            torch.FloatTensor([0.0001]))
                    loss = loss_function(output, y)
                    # print('output', output.data[0])
                    # print('loss', loss.data[0])
                    loss.backward()
                    optimizer.step()
                    # for param in model.parameters():
                    #     print('param', param.grad.data.sum())
                    total_loss += loss.data[0]
        losses.append(total_loss)
        y, predicty = test(testset, model)
        # print(len(y))
        eval = Evaluation()
        acc.append(eval.accuracy(y, predicty, data))
    return losses, acc
Exemplo n.º 2
0
# train
rnn = RNN(100, 128, len(vocab))
optimizer = optim.SGD(rnn.parameters(), lr=0.1)
loss_function = nn.BCELoss()
losses, acc = rnn_train(data.trainset, rnn, optimizer, loss_function,
                        data.testset)
# plt.xlabel("Train epoch")
# plt.ylabel("loss")
# plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], losses)
# plt.show()
plt.xlabel("Train epoch")
plt.ylabel("accuracy")
plt.plot([1, 2, 3, 4, 5, 6], acc)
plt.show()
# test
y, predicty = rnn_test(data.testset, rnn)
eval = Evaluation()
eval.accuracy(y, predicty, data)
with open('result_rnn.txt', 'w') as f:
    for index, maxd in enumerate(eval.wrong):
        f.write("Case #{}: {} ".format(index + 1, maxd) + '\n')
# predicty=[0.1, 0.2, 0.55, 0.51, 0.53, 0.7]
# use common sense
predicty = use_csk(data.testset, predicty, rnn)
# Evaluation
eval = Evaluation()
eval.accuracy(y, predicty, data)

final = time.time()
print("time:", final - begin)