# Initialize module only upon first input? (coding best practices)

**URL:** <https://discuss.pytorch.org/t/initialize-module-only-upon-first-input-coding-best-practices/20545>\
**Category:** Uncategorized\
**Created:** [July 1, 2018, 6:29pm UTC](https://discuss.pytorch.org/t/initialize-module-only-upon-first-input-coding-best-practices/20545 "2018-07-01T18:29:31Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![lack](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/4af34b/32.png) [@lack](https://discuss.pytorch.org/u/lack)\
**Post date:** [July 2, 2018, 10:09pm UTC](https://discuss.pytorch.org/t/initialize-module-only-upon-first-input-coding-best-practices/20545/5 "2018-07-02T22:09:11Z")

</div>

> calculate the shape of in\_features using the shape formula for conv layers etc.

Yes, I suppose I am talking about a way to do that, not specific to `nn.Linear`.

The motivation for my question is I have seen several implementations of experimental network architectures with frustrating (anti)patterns such as

- magic numbers (e.g. a layer initialized with 1152 neurons as a result of unexplained calculations)
- user-settable parameters with unnecessary coupling (e.g. a command line parameter `--unit-size=1152` which cannot be freely changed in reality, because it is determined by the input image size and other hyperparameters)

As a new Pytorch user, I am wondering if there is a “best practices” way to avoid such patterns in my own code, especially when writing custom subclasses of `nn.Module`.

BTW, I see my “helper method” idea is really the same as [Carson McNeil’s idea here](https://discuss.pytorch.org/t/utility-function-for-calculating-the-shape-of-a-conv-output/11173/4).

---

_[View the full topic](https://discuss.pytorch.org/t/initialize-module-only-upon-first-input-coding-best-practices/20545)._
