I am newbie in LSTM, and I want to clarify some concepts. I know that LSTM has a good effect on time series data, and I want to try to build an LSTM model. I will not use LSTMCell here, because there may be places I don’t understand about cells.
I would like to ask if Run1
and Run2
in the following code are equivalent:
data = torch.ones(4, 100, 5) #batch, time length, feature
lstm = torch.nn.LSTM(
input_size=5, hidden_size=16, num_layers=1,
batch_first=True, bidirectional=False)
def init_variable(_lstm, batchsize):
# batch_size x hidden_size
h_ = torch.zeros(_lstm.num_layers*pow(2,_lstm.bidirectional),
batchsize,
_lstm.hidden_size).requires_grad_()
c_ = torch.zeros(_lstm.num_layers*pow(2,_lstm.bidirectional),
batchsize,
_lstm.hidden_size).requires_grad_()
return h_, c_
def Run1(data=data, lstm=lstm):
(h, c) = init_variable(lstm, data.shape[0])
out, (h, c) = lstm(data, (h, c))
return h
def Run2(data=data, lstm=lstm):
(h, c) = init_variable(lstm, data.shape[0])
for i in range(data.shape[1]):
out, (h, c) = lstm(data[:,i,:].unsqueeze(1), (h, c))
return h
Thanks in advance!