Input preparation

Hello all,

I’ve never worked with Torch, only tf/th…, but the workflow seemed very pleasant to me so I decided try to learn. I know that pytorch is a very new project but it would be neat if one writes a “pytorch for dummies”.

For instance, the fallowing code results in error:

(class model above)
    self.affine1(10, 100)
(...)

x = np.random.random(10)
ipt = torch.from_numpy(x)
probs = model(Variable(ipt))

Then

TypeError: addmm_ received an invalid combination of arguments - got (int, int, torch.DoubleTensor, torch.FloatTensor), but expected one of:
 * (torch.DoubleTensor mat1, torch.DoubleTensor mat2)
(...)

How is the proper data preparation in pytorch?

Thanks in advance and looking forward to use pytorch (the performance of the cart pole in openai gym was better than with other frameworks).

Obs: As I didn’t saw any other mechanic-ish question topic, nor a question flag, hope that is not off topic for the forum.

1 Like

hi there.

In this particular case, thanks for your feedback. We will improve our onboarding process, by making more newbie friendly tutorials.

In this case, x = np.random.random(10) returns a numpy ndarray of dtype=float64.
When you call ipt = torch.from_numpy(x), ipt is now a torch.DoubleTensor.

However, your model is expecting a torch.FloatTensor, so you simply do:

ipt = torch.from_numpy(x)
ipt = ipt.float()

That’s all :slight_smile:

9 Likes

Hi @gabrieldlm,

I’m planning to release a series of video tutorials on PyTorch, something along the line of my torch-Video-Tutorials.
Would this meet your request?

Please, let me know of anything you think it should be included, so that I can better plan the structure of my lessons.

3 Likes

Hi @Atcold,
Great material! I was thinking on something like the video 2.2, a tutorial about how pytorch works.

@smth,

Thanks for the reply, but after changing the tensor type to float I get the fallowing error:

RuntimeError: matrices expected, got 1D, 2D tensors at /Users/soumith/anaconda/conda-bld/pytorch0.1.6_1484755992574/work/torch/lib/TH/generic/THTensorMath.c:857

and the output for printing the ipt variable is the fallowing:

print ipt
(...)
[torch.FloatTensor of size 10]

@gabrieldlm pytorch does not support broadcasting yet. You are trying to send ipt wich is a 1D vector of size 10 into a matrix-multiply, which expects a 2D Tensor.

You can do: ipt = ipt.view(1, 10) if it fits your usecase.

1 Like

Hey @gabrieldlm, I created a new repo: pytorch-Video-Tutorials.
Feel free to edit the wiki with your suggestions.

I’ll be reading the docs through this week, if I have any ideas I’ll add :+1:

Haha, I’m reading the docs and the source code too! It’s been a nice journey so far :grin:

This is the first place I ended up in when searching the error: matrices expected, got 1D, 2D tensors at /py/conda-bld/pytorch_1490983232023/work/torch/lib/TH/generic/THTensorMath.c:1224

And even from smth’s comment and the documentation I’d say it’s not immediately obvious that the solution, at least in my case, is that the dimensionality of the input needed to be 3D rather than 2D. It does explain why .view() makes frequent appearances in some of the tutorials.