Mysterious nn.Parameter behavior

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 ?

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

print(model(Variable(x)))

For the moment, you can’t mixture Tensors and Variables. 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.

2 Likes

Thanks for your answer. Now its working.