# RNN: for many to many classification task

**URL:** <https://discuss.pytorch.org/t/rnn-for-many-to-many-classification-task/15457>\
**Category:** Uncategorized\
**Created:** [March 25, 2018, 5:14pm UTC](https://discuss.pytorch.org/t/rnn-for-many-to-many-classification-task/15457 "2018-03-25T17:14:46Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 26, 2018, 9:49am UTC](https://discuss.pytorch.org/t/rnn-for-many-to-many-classification-task/15457/2 "2018-03-26T09:49:02Z")

</div>

You are using a separate Linear layer with different parameters for each timestep.

Here is what I would do

```auto
class RNNS2S(nn.Module):
    def __init__ (self, ...):
        ...
        self.fc = nn.Linear(in_features=self.hidden_dimensions, out_features=self.num_classes)

    def forward(self, X):
        ...
        output_gru, h_n = self.gru(X, h_0)
        # output_gru has shape (batch_size, seq_len, hidden_dimensions)
        
        # nn.Linear operates on the last dimension of its input
        # i.e. for each slice [i, j, :] of gru_output it produces a vector of size num_classes
        fc_output = self.fc(output_gru)
        # fc_output will be batch_size*seq_len*num_classes
        return fc_output 

```

Now, I can produce `input_data` of shape `(batches, timesteps, 4)` along with `targets` of shape `(batches, timesteps, 1)` and if I feed `input_data` into the model then I will get output of shape `(batches, timesteps, 1)`, i.e. one prediction for each timestep of each sample in the batch.

Then I can use any of the standard loss functions in PyTorch such as CrossEntropyLoss to compare `fc_output` to my `targets`.

---

_[View the full topic](https://discuss.pytorch.org/t/rnn-for-many-to-many-classification-task/15457)._
