# tf.keras.layers.TimeDistributed equivalent in PyTorch

**URL:** <https://discuss.pytorch.org/t/tf-keras-layers-timedistributed-equivalent-in-pytorch/89125>\
**Category:** Uncategorized\
**Created:** [July 14, 2020, 9:33am UTC](https://discuss.pytorch.org/t/tf-keras-layers-timedistributed-equivalent-in-pytorch/89125 "2020-07-14T09:33:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VikingAlex](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/71c47a/32.png) [@VikingAlex](https://discuss.pytorch.org/u/VikingAlex)\
**Post date:** [July 14, 2020, 9:33am UTC](https://discuss.pytorch.org/t/tf-keras-layers-timedistributed-equivalent-in-pytorch/89125/1 "2020-07-14T09:33:30Z")

</div>

Hi,

I am changing from TF/Keras to PyTorch. To create a recurrent network with a custom cell, TF provides the handy function ’ tf.keras.layers.TimeDistributed’ that handles the sequence for you and applies your arbitrary cell to each time step.  
Trying to implement something similar in PyTorch, I found code examples (like [https://pytorch.org/tutorials/beginner/former\_torchies/nnft\_tutorial.html](https://pytorch.org/tutorials/beginner/former_torchies/nnft_tutorial.html)) where a simple for-loop is used. While this works, I was wondering if there is a ‘better’ way to do this, or if this is already optimal performance wise. Coming from TF, I am sure that such a simple and elegant solution with the for-loop can be optimal, but PyTorch has surprised me already many times.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 15, 2020, 9:55am UTC](https://discuss.pytorch.org/t/tf-keras-layers-timedistributed-equivalent-in-pytorch/89125/2 "2020-07-15T09:55:25Z")

</div>

[This topic](https://discuss.pytorch.org/t/any-pytorch-function-can-work-as-keras-timedistributed/1346) seems to have a few implementations. 🙂
