1 year ago
#75583
patrick823
Understanding Pytorch LSTM Model Architecture
I put my LSTM model code below and I am not quite sure what my model actually looks like or how I would change it.
For example. What would my model look like with these params:
n_features = 5
n_layers = 3
n_hidden=64
n_classes=3
I would have said:
- Layer is Input layer with 5 Neurons
- LSTM with 64 Neurons
- LSTM with 64 Neurons
- LSTM with 64 Neurons
- Linear Output with 3 Neurons
Now if this is correct, could I write the same model with the 3 LSTM Layers seperately?
class Model_LSTM(nn.Module):
def __init__(self, n_features, n_classes, n_hidden, n_layers):
super().__init__()
self.lstm = nn.LSTM(
input_size=n_features,
hidden_size=n_hidden,
num_layers=n_layers,
batch_first=True,
dropout=0.75
)
# HE-Initialisierung
weight = torch.zeros(n_layers,n_hidden)
nn.init.kaiming_uniform_(weight)
self.weight = nn.Parameter(weight)
self.classifier = nn.Linear(n_hidden, n_classes)
def init_hidden(self):
hidden_state = torch.zeros(self.lstm.num_layers,batch_size,self.lstm.hidden_size)
cell_state = torch.zeros(self.lstm.num_layers,batch_size,self.lstm.hidden_size)
return (hidden_state, cell_state)
def forward(self, x):
self.hidden = self.init_hidden()
_, (hidden, _) = self.lstm(x)
out=hidden[-1]
return self.classifier(out)
python
pytorch
lstm
0 Answers
Your Answer