# Implementing a custom RNN using the TorchModelV2

**URL:** <https://discuss.ray.io/t/implementing-a-custom-rnn-using-the-torchmodelv2/8676>\
**Category:** RLlib\
**Created:** [December 16, 2022, 3:08pm UTC](https://discuss.ray.io/t/implementing-a-custom-rnn-using-the-torchmodelv2/8676 "2022-12-16T15:08:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunzro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/kunzro/32/3567_2.png) [@Kunzro](https://discuss.ray.io/u/Kunzro)\
**Post date:** [December 16, 2022, 3:08pm UTC](https://discuss.ray.io/t/implementing-a-custom-rnn-using-the-torchmodelv2/8676/1 "2022-12-16T15:08:43Z")

</div>

What exactly is the expected return (shape and to what do the dimensions correspond to, also what if the batch\_size is only 1) from the forward function of the TorchModelV2 if it is used to implement a RNN. I have the same question regarding the value\_function(). First and foremost: Do I have to keep the “Sequence” dimension for the return values in either of the forward() or value\_function() function?

This is possible according to the documentation:

> Note that the `inputs` arg entering `forward_rnn` is already a time-ranked single tensor (not an `input_dict`!) with shape `(B x T x ...)`. If you further want to customize and need more direct access to the complete (non time-ranked) `input_dict`, you can also override your Model’s `forward` method directly (as you would do with a non-RNN ModelV2). In that case, though, you are responsible for changing your inputs and add the time rank to the incoming data (usually you just have to reshape).

I want to use the “normal” TorchModelV2 since I want to have more control over the inputs, since my state consists of a Dict and I want to handle individual parts differently, while the TorchRNN model just flattens everything and concatenates it.

**How severe does this issue affect your experience of using Ray?**

- High: It blocks me to complete my task.

---

<div class="post-metadata">

**Author:** ![mannyv](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/mannyv/32/606_2.png) [@mannyv](https://discuss.ray.io/u/mannyv)\
**Post date:** [December 16, 2022, 4:17pm UTC](https://discuss.ray.io/t/implementing-a-custom-rnn-using-the-torchmodelv2/8676/2 "2022-12-16T16:17:45Z")

</div>

Hi @Kunzro,

Here is an example they might help you get started.

> <https://github.com/ray-project/ray/blob/master/rllib/examples/models/rnn_model.py>

> <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_rnn_model.py>
