# Using nn.Module apart from the model for training

**URL:** <https://discuss.pytorch.org/t/using-nn-module-apart-from-the-model-for-training/76311>\
**Category:** Uncategorized\
**Created:** [April 11, 2020, 12:00pm UTC](https://discuss.pytorch.org/t/using-nn-module-apart-from-the-model-for-training/76311 "2020-04-11T12:00:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![braindotai](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/braindotai/32/61373_2.png) [@braindotai](https://discuss.pytorch.org/u/braindotai)\
**Post date:** [April 11, 2020, 12:00pm UTC](https://discuss.pytorch.org/t/using-nn-module-apart-from-the-model-for-training/76311/1 "2020-04-11T12:00:57Z")

</div>

Let’s say I wanna build a custom metric, so in that case, I could just simply just write a function and use accordingly in my training loop.  
Can I rather define metric as a class inherited from `nn.Module` and there define the forward method for the metric definition?  
I know I can, but is it beneficial in any way (if it is) or just a function definition is fine.

---

<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:** [April 12, 2020, 6:58am UTC](https://discuss.pytorch.org/t/using-nn-module-apart-from-the-model-for-training/76311/2 "2020-04-12T06:58:11Z")

</div>

If your metric is stateless (i.e. you don’t want to store arguments or other parameters), then you could just define a method. Otherwise, a custom class derived from `nn.Module` could store some arguments e.g. `reductions` as an internal attribute.  
Also, have a look at [this post](https://discuss.pytorch.org/t/beginner-should-relu-sigmoid-be-called-in-the-init-method/18689/6) I’ve written some time ago where I discuss the advantages of both approaches.
