Hi, i am to try studying the libtorch and want to write the programm which will make predict the value of time sequence with using lstp neuro net. I found the example of lstp rnn on python https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html:
lstm = nn.LSTM(3, 3) # Input dim is 3, output dim is 3
inputs = [torch.randn(1, 3) for _ in range(5)] # make a sequence of length 5
# initialize the hidden state.
hidden = (torch.randn(1, 1, 3),
torch.randn(1, 1, 3))
for i in inputs:
# Step through the sequence one element at a time.
# after each step, hidden contains the hidden state.
out, hidden = lstm(i.view(1, 1, -1), hidden)
# alternatively, we can do the entire sequence all at once.
# the first value returned by LSTM is all of the hidden states throughout
# the sequence. the second is just the most recent hidden state
# (compare the last slice of "out" with "hidden" below, they are the same)
# The reason for this is that:
# "out" will give you access to all hidden states in the sequence
# "hidden" will allow you to continue the sequence and backpropagate,
# by passing it as an argument to the lstm at a later time
# Add the extra 2nd dimension
inputs = torch.cat(inputs).view(len(inputs), 1, -1)
hidden = (torch.randn(1, 1, 3), torch.randn(1, 1, 3)) # clean out hidden state
out, hidden = lstm(inputs, hidden)
print(out)
print(hidden)
and trying to rewrite this on c++, but i could not translate the line of code:
out, hidden = lstm(i.view(1, 1, -1), hidden)
Can anybody prompt to me how can i do it?
My c++ code look that:
#include <vector>
#include <iostream>
int main(int argc, char *argv[])
{
// lstm - long short-time memory
torch::nn::LSTM lstm = torch::nn::LSTM(3,3); //dimension of in 3, dimension of out 3
std::cout << lstm << '\n';
// initialize a source data for training of lstm nn
std::vector<torch::Tensor> inputs(5);
for (torch::Tensor &tr : inputs) {
tr = torch::randn({1,3});
}
// matrix of hidden layer
std::vector<torch::Tensor> hidden{torch::randn({1,1,3}),torch::randn({1,1,3})};
// output of nn
torch::Tensor out;
for (torch::Tensor &tr : inputs) {
out = hidden = lstm(tr.view({1,1,-1}),hidden); // there is error, i dont know how do it on c++
}
return 0;
}
Thank you all for your attention!