PyTorch RNN, many to many learning, one to many test

username: Stainley
cooked: <p>Hello! I need to create recurrent NN, during the process of training I have all the <strong>x_i</strong> and <strong>y_i</strong> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/a/6a27c7c9cc37bcbc076a58d6773170b2ba7e28cc.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/6a27c7c9cc37bcbc076a58d6773170b2ba7e28cc" title="IMG_4517"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6a27c7c9cc37bcbc076a58d6773170b2ba7e28cc_2_690x314.jpeg" alt="IMG_4517" data-base62-sha1="f95Q8V9OMdJk15BZj2P9uIkPHFa" width="690" height="314" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6a27c7c9cc37bcbc076a58d6773170b2ba7e28cc_2_690x314.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6a27c7c9cc37bcbc076a58d6773170b2ba7e28cc_2_1035x471.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6a27c7c9cc37bcbc076a58d6773170b2ba7e28cc_2_1380x628.jpeg 2x" data-dominant-color="EBECEC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_4517</span><span class="informations">2238×1021 195 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>but for test I have only <strong>x_1</strong> and want to use <strong>y_1</strong> as <strong>x_2</strong>, <strong>y_2</strong> as <strong>x_3</strong> and etc. How to do it via torch.nn.RNN?</p>

username: ayalaa2
cooked: <p>Assuming your hidden dimension is the same size as your input dimension, you can do it like so:</p>
<pre><code class="lang-auto">rnn = torch.nn.RNN(16, 16)
x1 = torch.randn((1, 1, 16))

y1,h1 = rnn(x1)
y2,h2 = rnn(y1, h1)
...
</code></pre>
<p>The output of the RNN is a sequence of outputs produced and the last hidden state. The RNN layer can take in a sequence of inputs and a hidden state (default to all zeros for the first forward).</p>

username: Stainley
cooked: <p>Can you explain, how should I train and use the net you suggested?</p>

username: ayalaa2
cooked: <p>It’s hard to say without knowing the objective, but I would maybe do something like this:</p>
<pre><code class="lang-auto">rnn = torch.nn.RNN(16, 16)
x1 = torch.randn((1, 1, 16))    # Example input for one-to-many
seq_output_len = 5              # I want a sequence of five produced

yn = x1
hn = None
outputs = []
for _ in range(seq_output_len):
    yn, hn = rnn(yn, hn)        # Use previous output and hidden state
    outputs.append(yn)

output = torch.cat(outputs)     # 5 x 1 x 16
loss = do_something(output)
loss.backward()

</code></pre>
<p>This is a simplistic example which will vary on your use case</p>

username: Stainley
cooked: <p>No, you did not understand, for training I already have all y_i and want to use them for training</p>

username: ayalaa2
cooked: <p>For training, feed your entire sequence into your RNN and you will get a similar size sequence out. Compute a loss between that output and your target  Y.</p>
<p>For testing, refer to the code I provided. <code>output</code> will be what you compare against the ground truth.</p>