# Timedistributed CNN

**URL:** <https://discuss.pytorch.org/t/timedistributed-cnn/51707>\
**Category:** Uncategorized\
**Created:** [July 26, 2019, 10:24am UTC](https://discuss.pytorch.org/t/timedistributed-cnn/51707 "2019-07-26T10:24:04Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ilyes](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/f0a364/32.png) [@ilyes](https://discuss.pytorch.org/u/ilyes)\
**Post date:** [July 26, 2019, 1:18pm UTC](https://discuss.pytorch.org/t/timedistributed-cnn/51707/2 "2019-07-26T13:18:27Z")

</div>

Great news! I think I finally got the result that I needed using the nn.ModuleList() !

```auto
class TimeDistributed(nn.Module):
    def __init__ (self, layer, time_steps, *args):        
        super(TimeDistributed, self). __init__ ()
        
        self.layers = nn.ModuleList([layer(*args) for i in range(time_steps)])

    def forward(self, x):

        batch_size, time_steps, C, H, W = x.size()
        output = torch.tensor([])
        for i in range(time_steps):
          output_t = self.layers[i](x[:, i, :, :, :])
          output_t = y.unsqueeze(1)
          output = torch.cat((output, output_t ), 1)
        return output

```

And checked this by counting the number of parameters after using Conv2D on 100 time\_steps:

```auto
x = torch.rand(20, 100, 1, 5, 9)

model = TimeDistributed(nn.Conv2d, time_steps = 100, 1, 8, (3, 3) , 2, 1 ,True)
output = model(x)

print(output.size()) ## (20, 100, 8, 3, 5)

print("number of parameters : ", sum([p.numel() for p in model.parameters()]))

## number of parameters : 8000 instead of 80

```

or with Batchnormalization:

````auto

x = torch.rand(20, 100, 8, 3, 5)
model = TimeDistributed(nn.BatchNorm2d, time_steps = 100, 8)```
````

---

_[View the full topic](https://discuss.pytorch.org/t/timedistributed-cnn/51707)._
