mikeizbicki / cmc-csci181-deeplearning

deep learning course materials
15 stars 6 forks source link

Runtime Error #28

Open ademuro20 opened 4 years ago

ademuro20 commented 4 years ago

I am not sure how to fix this error and what is exactly going on. My weight tensor looks like this

tensor([2.9661e-05, 3.8282e-05, 1.2768e-04,  ..., 2.1441e-05, 1.5605e-05,
        5.5294e-06], grad_fn=<AbsBackward>)

and the code to build my score/weight is as follows:

input_tensorCopy = line_tensor
input_tensorCopy[i] = 0
output_classW, output_nextcharsW = model(input_tensorCopy)
probsW = softmax(output_classW)
weight = torch.abs(probs[i]-probsW[i])
scores[i] = weight
Screen Shot 2020-04-22 at 9 07 48 AM
mikeizbicki commented 4 years ago

The error message is caused by pytorch's expand function. See the docs at: https://pytorch.org/docs/stable/tensors.html#torch.Tensor.expand

expand is related to the shapes of tensors, and my guess is that the shapes of your tensors are not what you think they are. You should print the shapes of all your tensors, and I think that might help. In particular, your weight tensor either has the wrong shape or you are using an incorrect formula to create it.

PS. I've edited your comment to improve formatting. When you're including multiline code blocks, you need to wrap the code in three backticks on their own line, not single backticks.

ademuro20 commented 4 years ago

thank you!

ademuro20 commented 4 years ago

should the shape of our scores tensor be the length of the input line x the number of categories/websites?

ademuro20 commented 4 years ago

Ok, I created my scores with the shape being the length of the input line x the number of categories/websites and now I'm getting this error:

File "project.py", line 535, in line2img
    im[i%maxwidth,im_height-i//maxwidth-1] = scores[i]
ValueError: only one element tensors can be converted to Python scalars

In that case, I think I'm still wrong about how I made my scores tensor. I also made another change like this scores[i] = torch.abs(probs-probsW)

If I keep scores[i] = torch.abs(probs[i]-probsW[i]), then the code only runs through my for loop once and I get an out of bounds IndexError

mikeizbicki commented 4 years ago

scores should be a vector and not a higher order tensor. The sample code has a correctly shaped scores vector that you can look at.