# When should I use nn.ModuleList and when should I use nn.Sequential?

**URL:** <https://discuss.pytorch.org/t/when-should-i-use-nn-modulelist-and-when-should-i-use-nn-sequential/5463>\
**Category:** vision\
**Created:** [July 27, 2017, 12:13pm UTC](https://discuss.pytorch.org/t/when-should-i-use-nn-modulelist-and-when-should-i-use-nn-sequential/5463 "2017-07-27T12:13:45Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![dpernes](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dpernes/32/16250_2.png) [@dpernes](https://discuss.pytorch.org/u/dpernes)\
**Post date:** [July 28, 2017, 11:56am UTC](https://discuss.pytorch.org/t/when-should-i-use-nn-modulelist-and-when-should-i-use-nn-sequential/5463/4 "2017-07-28T11:56:32Z")

</div>

Not really. Maybe there are some situations where you could use both, but the main idea is the following:

In `nn.Sequential`, the `nn.Module`'s stored inside are connected in a cascaded way. For instance, in the example that I gave, I define a neural network that receives as input an image with 3 channels and outputs 10 neurons. That network is composed by the following blocks, in the following order: Conv2D -\> ReLU -\> Linear layer. Moreover, an object of type `nn.Sequential` has a `forward()` method, so if I have an input image `x` I can directly call `y = simple_cnn(x)` to obtain the scores for `x`. When you define an `nn.Sequential` you must be careful to make sure that the output size of a block matches the input size of the following block. Basically, it behaves just like a `nn.Module`

On the other hand, `nn.ModuleList` does not have a `forward()` method, because it does not define any neural network, that is, there is no connection between each of the `nn.Module`'s that it stores. You may use it to store `nn.Module`'s, just like you use Python lists to store other types of objects (integers, strings, etc). The advantage of using `nn.ModuleList`'s instead of using conventional Python lists to store `nn.Module`'s is that Pytorch is “aware” of the existence of the `nn.Module`'s inside an `nn.ModuleList`, which is not the case for Python lists. If you want to understand exactly what I mean, just try to redefine my `class LinearNet` using a Python list instead of a `nn.ModuleList` and train it. When defining the `optimizer()` for that net, you’ll get an error saying that your model has no parameters, because PyTorch does not see the parameters of the layers stored in a Python list. If you use a `nn.ModuleList` instead, you’ll get no error.

---

_[View the full topic](https://discuss.pytorch.org/t/when-should-i-use-nn-modulelist-and-when-should-i-use-nn-sequential/5463)._
