Self.u(users)

What is self.u(users) from Lecture 5 (1h 05min)?

If self.u is Embedding it should be a matrix of weights, not a function. Trying the following code:

def get_emb(ni,nf):
    e = nn.Embedding(ni, nf)
    e.weight.data.uniform_(-0.01,0.01)
    return e

items = cf.items
users = cf.users
(u, m) = [get_emb(*o) for o in [(len(users), n_factors), (len(items), n_factors)]]
type(u)

returns torch.nn.modules.sparse.Embedding

then testing with

u(users)

throws an error

--------------------------------
AttributeErrorTraceback (most recent call last)
<ipython-input-29-b376461b23c3> in <module>()
----> 1 u(users)

~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    323         for hook in self._forward_pre_hooks.values():
    324             hook(self, input)
--> 325         result = self.forward(*input, **kwargs)
    326         for hook in self._forward_hooks.values():
    327             hook_result = hook(self, input, result)

~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/sparse.py in forward(self, input)
    101             input, self.weight,
    102             padding_idx, self.max_norm, self.norm_type,
--> 103             self.scale_grad_by_freq, self.sparse
    104         )
    105 

~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/_functions/thnn/sparse.py in forward(cls, ctx, indices, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
     37         ctx.sparse = sparse
     38 
---> 39         assert indices.dim() <= 2
     40         assert not ctx.needs_input_grad[0], "Embedding doesn't " \
     41             "compute the gradient w.r.t. the indices"

AttributeError: 'numpy.ndarray' object has no attribute 'dim'

An Embedding isn’t actually a matrix of weights, it’s a layer (a nn.Module). Layers are callable. Doing self.u(users) returns a tensor containing embeddings for each of the users (that is, it has the same shape as users, plus the size of each embedding tacked onto the end).

As for your error, I can’t actually get the notebook to run at the moment, but what is your users argument? That error makes me wonder if it’s a numpy array when it’s supposed to be a pytorch tensor.

2 Likes

Oho, I see. Thanks.

Now I try

users_torch = T(users).cpu()
type(users_torch)

u(users_torch)

And again error

--------------------------------
RuntimeErrorTraceback (most recent call last)
<ipython-input-72-fe56c76151e6> in <module>()
----> 1 u(users_torch)

~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    323         for hook in self._forward_pre_hooks.values():
    324             hook(self, input)
--> 325         result = self.forward(*input, **kwargs)
    326         for hook in self._forward_hooks.values():
    327             hook_result = hook(self, input, result)

~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/sparse.py in forward(self, input)
    101             input, self.weight,
    102             padding_idx, self.max_norm, self.norm_type,
--> 103             self.scale_grad_by_freq, self.sparse
    104         )
    105 

~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/_functions/thnn/sparse.py in forward(cls, ctx, indices, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
     55 
     56         if indices.dim() == 1:
---> 57             output = torch.index_select(weight, 0, indices)
     58         else:
     59             output = torch.index_select(weight, 0, indices.view(-1))

RuntimeError: index out of range at /opt/conda/conda-bld/pytorch_1512387374934/work/torch/lib/TH/generic/THTensorMath.c:277

May be it is too early to go for such details.

You need a CUDA variable to pass to a CUDA module. We do cover it in the course so maybe leave it for now until you get to the relevant lesson.

1 Like

Thank you. I’ll come to this again when I get better understanding of the data structures.

Could you expand on this? Which is the lesson where it is explained?

Is there a any way to do the same with CPU?

Thanks!