LSTM-based classifier for ETH/BTC dataset. > Note that we are letting the LSTM have its independent h- and c-dims, and are then taking the outputs and projecting them onto our classification. > We are also taking only the final layer to train our network. From torch do
| 132 | |
| 133 | |
| 134 | class Simple_LSTM_Classifier(nn.Module): |
| 135 | """ |
| 136 | LSTM-based classifier for ETH/BTC dataset. |
| 137 | |
| 138 | > Note that we are letting the LSTM have its independent h- and c-dims, and |
| 139 | are then taking the outputs and projecting them onto our classification. |
| 140 | > We are also taking only the final layer to train our network. |
| 141 | |
| 142 | From torch docs: |
| 143 | rnn = nn.LSTM(input_size=10, hidden_size=20, num_layers=2) |
| 144 | input = torch.randn(5, 3, 10) |
| 145 | h0 = torch.randn(2, 3, 20) |
| 146 | c0 = torch.randn(2, 3, 20) |
| 147 | output, (hn, cn) = rnn(input, (h0, c0)) |
| 148 | """ |
| 149 | def __init__(self, dataset_ref, c_dim=256): |
| 150 | |
| 151 | super(Simple_LSTM_Classifier, self).__init__() |
| 152 | |
| 153 | # --- Save the dims --- |
| 154 | self.in_dim = dataset_ref.get_input_dim() |
| 155 | self.out_dim = dataset_ref.get_output_dim() |
| 156 | self.c_dim = c_dim |
| 157 | |
| 158 | print(self.in_dim, self.out_dim, self.c_dim) |
| 159 | |
| 160 | # --- Layers --- |
| 161 | self.lstm = nn.LSTM(input_size=self.in_dim, hidden_size=self.c_dim) |
| 162 | self.output_projection = nn.Linear(in_features=self.c_dim, out_features=self.out_dim) |
| 163 | |
| 164 | def forward(self, x): |
| 165 | # --- x \in (N, L, H_dim) --- |
| 166 | # --- outputs \in (L, N, H_dim) --- |
| 167 | x = torch.transpose(x, 0, 1) |
| 168 | outputs, (h_n, c_n) = self.lstm(x) |
| 169 | |
| 170 | # --- Only classify the last layer --- |
| 171 | out = self.output_projection(outputs) |
| 172 | |
| 173 | # --- out: (L, N, output_dim) --- |
| 174 | # --- First transpose: (N, L, output_dim) |
| 175 | # --- second transpose (N, output_dim, L) --- |
| 176 | return torch.transpose(torch.transpose(out, 0, 1), 1, 2) |
| 177 | |
| 178 | |
| 179 | # --- For argparse --- |
nothing calls this directly
no outgoing calls
no test coverage detected