Hi,
I create a ‘many to one model’ with LSTM, and I want to transform it into a ‘one to many model’. But I am not sure how to edit the codes.
Below codes are the current ‘many to one model’ with LSTM.
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
# Set initial hidden and cell states
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
# Forward propagate LSTM
out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out
# One to Many
y(t-3) y(t-2) y(t-1) y(t)
| | | |
cell ---> cell ---> cell ---> cell
|
x(t-3)
# Many to One
y(t)
|
cell ---> cell ---> cell ---> cell
| | | |
x(t-3) x(t-2) x(t-1) x(t)