Diff of /model/model.py [000000] .. [225570]

Switch to unified view

a b/model/model.py
1
# import pandas as pd
2
# from copy import deepcopy
3
import pandas as pd
4
import torch
5
import torch.nn as nn
6
# import torch.nn.functional as F
7
# import time
8
9
10
class LSTMModel(nn.Module):
11
    def __init__(self, input_dim=5, h_RNN_layers=2, h_RNN=256, drop_p=0.2, num_classes=1):
12
        super(LSTMModel, self).__init__()
13
        self.input_dim = input_dim
14
        self.h_RNN_layers = h_RNN_layers   # RNN hidden layers
15
        self.h_RNN = h_RNN                 # RNN hidden nodes
16
        self.drop_p = drop_p
17
        if h_RNN_layers < 2:
18
            drop_p = 0
19
        self.num_classes = num_classes
20
        self.LSTM = nn.LSTM(
21
            input_size=self.input_dim,
22
            hidden_size=self.h_RNN,
23
            num_layers=h_RNN_layers,
24
            dropout=drop_p,
25
            batch_first=True,       # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
26
        )
27
        self.fc1 = nn.Linear(self.h_RNN, self.num_classes)
28
29
    def forward(self, x, h_s=None):
30
        # print('forward started')
31
        self.LSTM.flatten_parameters()
32
        RNN_out, h_s = self.LSTM(x, h_s)
33
        """ h_n shape (n_layers, batch, hidden_size), h_c shape (n_layers, batch, hidden_size) """
34
        """ None represents zero initial hidden state. RNN_out has shape=(batch, time_step, output_size) """
35
36
        # FC layers
37
        out = self.fc1(RNN_out[:, -1, :])   # choose RNN_out at the last time step
38
        return out, h_s
39
40
# model = LSTMModel(h_RNN=16, h_RNN_layers=2, drop_p=0.2, num_classes=7)
41
# model.load_state_dict(torch.load('lstm.sav'))
42
# model.eval()
43
# df = pd.read_csv('dataset/2sec_multi_train_data.csv', header=None)
44
# sum = 0
45
# h_s = None
46
# for j in range(0, 80):
47
#     xdata = df.iloc[j, :180].values.reshape((36, 5), order='F')
48
#     # print(xdata)
49
# #
50
#     for i in range(1):
51
#         xcurr = torch.Tensor(xdata.reshape(-1, 36, 5))
52
#         outputs, h_s = model(xcurr, h_s)
53
#         _, predicted = torch.max(outputs.data, 1)
54
#         sum += (predicted.cpu().numpy()[0] == 0)
55
#
56
# print(sum)