# Are torch.nn.Functional layers learnable?

**URL:** <https://discuss.pytorch.org/t/are-torch-nn-functional-layers-learnable/13687>\
**Category:** Uncategorized\
**Created:** [February 15, 2018, 2:27pm UTC](https://discuss.pytorch.org/t/are-torch-nn-functional-layers-learnable/13687 "2018-02-15T14:27:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [February 15, 2018, 3:15pm UTC](https://discuss.pytorch.org/t/are-torch-nn-functional-layers-learnable/13687/2 "2018-02-15T15:15:16Z")

</div>

When using `torch.nn.Linear` for example, the `nn.Linear` class looks after initialising and using the parameters that it requires.  
When using the `torch.nn.Functional.linear` variant, it is up to you to provide the parameters on each forward pass.

Basically instead of

```auto
class Model(nn.Module):
    def __init__ (self):
        super(Model, self). __init__ ()
        self.linear = nn.Linear(in_features, out_features)

    def forward(self, input):
        return self.linear(input)

```

You would do this

```auto
class Model(nn.Module):
    def __init__ (self):
        super(Model, self). __init__ ()
        self.weight = nn.Parameter(torch.randn(out_features, in_features))
        self.bias = nn.Parameter(torch.randn(out_features))

    def forward(self, input):
        return F.linear(input, weight, bias)

```

In the first case Model knows that it has an `nn.Linear` submodule, in the second case Model knows that it has two parameter tensors.

So in the first case `Model.parameters()` will list the weight and bias parameters of the `nn.Linear` submodule, in the second case `Model.parameters()` will list the weight and bias parameters defined in **init**.

Training, saving and loading can all be done in exactly the same way in both cases.

---

_[View the full topic](https://discuss.pytorch.org/t/are-torch-nn-functional-layers-learnable/13687)._
