Can't create tensor in sgd notebook

Recently after I update the course materials, there is one notebook called “lesson2-sgd”. However, when I tried the codes inside the notebook. I got the error after I run the code:
a = tensor(3.,2); a
with the error:
TypeError Traceback (most recent call last)
<ipython-input-13-440e075cecad> in <module>
----> 1 a = tensor(3.,2); a

TypeError: tensor() takes 1 positional argument but 2 were given

When I checked the notebook, this line of code seems should work without error. I already update my fastai library to 1.0.15 and this error still occurs.

I am not familiar with the notebook, but please check the function signature, this can provide a lot of useful information.

To do so, while having your cursor on the right hand side of the leftmost parentheses, press shift+tab.

tensor( | <---- cursor, here is when you press shift + tab

You could also try executing a cell with only the following in it:
?tensor (one or two question marks - either produces different results).

Based on the code you shared, I do not know what tensor is. It might be a custom function defined in the notebook. Chances are it is a torch.tensor. Many of the torch methods take a tuple for a shape -> in this case tensor((3,2)) might work.

Still, would be great to take a look at the function signature to learn more.

Thanks @radek, here is what I found. This tensor function is from the fastai library.
def tensor(x:Any)->Tensor: "Like `torch.as_tensor`, but handle lists too" return torch.tensor(x) if is_listy(x) else as_tensor(x)

And also here is the document that I found but barely I understand for now.

1 Like

Ah great, you’ve done the necessary detective work :slight_smile: Yeah, the doc string is not very clear without context, but I would not worry about this too much.

Seems it takes something that is_listy and gives us a pytorch Tensor back. I googled torch.tensor and found this.

Might be the function worked with just 3,2 as input before some changes (seems the library is still being very actively developed!). Based on the additional information I think something like this might work:

torch.tensor([3, 2])

If it does not work, when it blows up, maybe it will give us some additional information.

1 Like

OK I know the problem. You’re right @radek, I should make it tensor([3.,2]) to make it work. Thanks!

1 Like

We’ll learn about the lesson 2 notebooks tonight.

I also had this error while trying to run it during the live lesson. Can it be a torch version thing?
My torch version is 1.0.0.dev20181005’

To create a tensor, brackets are needed tensor([1.,1.]) and to plot the chart I needed to use y.detach().numpy() and (x@a).detach().numpy

No, it means your fastai version isn’t current.