Exemple #1
0
                    t_d = t_d.cuda()

                t_x = t_x.view(-1, 10, 8)
                test_output = rnn(t_x)  # (samples, time_step, input_size)
                if gpu_avaliable:
                    pred_y = torch.max(test_output, 1)[1].cuda().data
                else:
                    pred_y = torch.max(test_output, 1)[1].data.numpy()
                    # t_y = t_y.data.numpy()
                all_pred_y.extend(pred_y)
                all_test_y.extend(list(t_y.data.cpu().numpy()))
                all_test_d.extend(list(t_d.data.cpu().numpy()))
            # accuracy = torch.sum(torch.LongTensor(all_pred_y) == torch.LongTensor(all_test_y)).type(torch.FloatTensor) / len(all_test_y)
            # print_out = 'Epoch: ' + str(epoch) + '| train loss: %.4f' % loss.data.cpu().numpy() + '| test accuracy: %.4f' % accuracy
            print_out = 'Epoch: ' + str(epoch) + '| train loss: %.4f' % loss.data.cpu().numpy() + \
                        '| test accuracy: %.4f' % Evaluate.accuracy(all_pred_y, all_test_y) + \
                        '| test MAE: %.4f' % Evaluate.region_MAE(all_pred_y, all_test_d) + \
                        '| test RMSE: %.4f' % Evaluate.region_RMSE(all_pred_y, all_test_d)
            print(print_out)
            elogger.log(str(print_out))

torch.save(rnn.state_dict(), 'cnn_lstm_prediction_label_is_region.pkl')

# print 10 predictions from test data
# test_output = rnn(test_data[:10].view(-1, 10, 5))
# if gpu_avaliable:
#     pred_y = torch.max(test_output, 1)[1].cuda().data
# else:
#     pred_y = torch.max(test_output, 1)[1].data.numpy()
# print(pred_y, 'prediction number')
# print(test_labels[:10], 'real number')
            all_test_y = []
            for t_step, (t_x, t_y) in enumerate(test_loader):
                if gpu_avaliable:
                    t_x = t_x.cuda()
                    t_y = t_y.cuda()

                t_x = t_x.view(-1, 10, 5)
                test_output = rnn(t_x)  # (samples, time_step, input_size)
                if gpu_avaliable:
                    pred_y = torch.max(test_output, 1)[1].cuda().data
                else:
                    pred_y = torch.max(test_output, 1)[1].data.numpy()
                all_pred_y.extend(pred_y)
                all_test_y.extend(list(t_y.data.cpu().numpy()))
            # accuracy = torch.sum(torch.LongTensor(all_pred_y) == torch.LongTensor(all_test_y)).type(torch.FloatTensor) / len(all_test_y)
            # print_out = 'Epoch: ' + str(epoch) + '| train loss: %.4f' % loss.data.cpu().numpy() + '| test accuracy: %.4f' % accuracy
            print_out = 'Epoch: ' + str(epoch) + '| train loss: %.4f' % loss.data.cpu().numpy() + \
                        '| test accuracy: %.4f' % Evaluate.accuracy(all_pred_y, all_test_y) + \
                        '| test MAE: %.4f' % Evaluate.MAE(all_pred_y, all_test_y) + \
                        '| test RMSE: %.4f' % Evaluate.RMSE(all_pred_y, all_test_y)
            print(print_out)
            elogger.log(str(print_out))

# print 10 predictions from test data
# test_output = rnn(test_data[:10].view(-1, 10, 5))
# if gpu_avaliable:
#     pred_y = torch.max(test_output, 1)[1].cuda().data
# else:
#     pred_y = torch.max(test_output, 1)[1].data.numpy()
# print(pred_y, 'prediction number')
# print(test_labels[:10], 'real number')