# Mysterious nn.Parameter behavior

**URL:** <https://discuss.pytorch.org/t/mysterious-nn-parameter-behavior/1525>\
**Category:** Uncategorized\
**Created:** [March 31, 2017, 9:02pm UTC](https://discuss.pytorch.org/t/mysterious-nn-parameter-behavior/1525 "2017-03-31T21:02:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![username1](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/u/ee59a6/32.png) [@username1](https://discuss.pytorch.org/u/username1)\
**Post date:** [March 31, 2017, 9:02pm UTC](https://discuss.pytorch.org/t/mysterious-nn-parameter-behavior/1525/1 "2017-03-31T21:02:32Z")

</div>

I am new to machine learning, and I am trying to implement regression with polynomials.  
I am aware of the regression to polynomials example, but I try to do it differently - by creating  
a submodule. Here is the code:

```
import torch
from torch import nn

class Model(nn.Module):
    def __init__ (self, deg):
	super(Model, self). __init__ ()
	self.deg = deg + 1

	self.theta = nn.Parameter(torch.ones(self.deg), 1)

     def forward(self, xs):
	phi = torch.cat([xs**i for i in range(self.deg)], 1)
	res = phi @ self.theta 
            # phi.mv(self.theta) # if not python 3.5 or later
	print(res)
	return res

model = Model(2)
#print(list(model.parameters()))
x = FloatTensor([1, 2,3, 4]).unfold(0,1,1)

print(model(x))

```

Here is the error that I get:

```
Traceback (most recent call last):
  File "01-polynomial-fitting.py", line 40, in <module>
    print(model(x))
  File "/home/arseni/.anaconda/envs/mlt/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "01-polynomial-fitting.py", line 29, in forward
    res = phi @ self.theta
  File "/home/arseni/.anaconda/envs/mlt/lib/python3.6/site-packages/torch/tensor.py", line 357, in __matmul__
    return self.mv(other)
TypeError: mv received an invalid combination of arguments - got (Parameter), but expected (torch.FloatTensor vec)

```

If I change `res = phi @ self.theta` to `res = phi @ self.theta.data` I get the following error

```
Traceback (most recent call last):
  File "01-polynomial-fitting.py", line 40, in <module>
    print(model(x))
  File "/home/arseni/.anaconda/envs/mlt/lib/python3.6/site-packages/torch/nn/modules/module.py", line 215, in __call__
    var = var[0]
TypeError: 'float' object is not subscriptable

```

If I change it to `res = self.theta @ phi.t()`. I get the following error:

```
Traceback (most recent call last):
  File "01-polynomial-fitting.py", line 41, in <module>
    print(model(x))
  File "/home/arseni/.anaconda/envs/mlt/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "01-polynomial-fitting.py", line 29, in forward
    res = (self.theta @ phi.t())
  File "/home/arseni/.anaconda/envs/mlt/lib/python3.6/site-packages/torch/autograd/variable.py", line 770, in __matmul__
    return self.unsqueeze(0).mm(other).squeeze(0)
  File "/home/arseni/.anaconda/envs/mlt/lib/python3.6/site-packages/torch/autograd/variable.py", line 523, in mm
    output = Variable(self.data.new(self.data.size(0), matrix.data.size(1)))
AttributeError: 'FloatTensor' object has no attribute 'data'

```

What am I doing wrong ?

---

<div class="post-metadata">

**Author:** ![fmassa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/32/58_2.png) [@fmassa](https://discuss.pytorch.org/u/fmassa)\
**Post date:** [March 31, 2017, 9:54pm UTC](https://discuss.pytorch.org/t/mysterious-nn-parameter-behavior/1525/2 "2017-03-31T21:54:39Z")

</div>

Try passing a `Variable` instead of a `Tensor` to your model.

```python
print(model(Variable(x)))

```

For the moment, you can’t mixture `Tensor`s and `Variable`s. Also, autograd expect that the first argument that is returned is a `Variable`, and that’s not the case when you get the `.data` from `self.theta` for computing res.

---

<div class="post-metadata">

**Author:** ![username1](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/u/ee59a6/32.png) [@username1](https://discuss.pytorch.org/u/username1)\
**Post date:** [April 2, 2017, 8:48am UTC](https://discuss.pytorch.org/t/mysterious-nn-parameter-behavior/1525/3 "2017-04-02T08:48:23Z")

</div>

Thanks for your answer. Now its working.
