Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value error #2

Open
Rukhmini opened this issue Apr 5, 2020 · 10 comments
Open

value error #2

Rukhmini opened this issue Apr 5, 2020 · 10 comments

Comments

@Rukhmini
Copy link

Rukhmini commented Apr 5, 2020

hello,
Whenever I am trying to run with iterator it is showing error

ValueError Traceback (most recent call last)
in ()
7 start_time = time.time()
8
----> 9 train_loss, train_acc = train(model, train_iterator, optimizer, criterion, device)
10 valid_loss, valid_acc = evaluate(model, valid_iterator, criterion, device)
11

in train(model, iterator, optimizer, criterion, device)
13 optimizer.zero_grad()
14
---> 15 y_pred, _ = model(x)
16
17 loss = criterion(y_pred, y)

ValueError: too many values to unpack (expected 2)

@bentrevett
Copy link
Owner

For which notebook is this? Only the first 3 notebooks should be considered complete, the rest are in the process of being re-written.

The model should return a tuple of tensors, in the first three we are returning both the final output and an intermediate layer so we can plot it with PCA/t-SNE. The error you are getting is due to the model only returning a single tensor, i.e. your model's forward method should end with return x, h, but yours is only doing return x (or something similar).

@Rukhmini
Copy link
Author

Rukhmini commented Apr 6, 2020

I have executed the resnet.ipynb file with my own dataset. The model has run successfully but I want to visualize the predicted labels as well as confusion matrix as it is shown in the "Lenet.ipynb" tutorial. So, whenever I am trying to execute "images, labels, probs = get_predictions(model, test_iterator)" this line of code it is throwing error like this,
RuntimeError: size mismatch, m1: [40 x 2048], m2: [512 x 2] at C:/w/1/s/tmp_conda_3.6_095855/conda/conda-bld/pytorch_1579082406639/work/aten/src\THC/generic/THCTensorMathBlas.cu:290.
and when I replace the test iterator with valid iterator "images, labels, probs = get_predictions(model, valid_iterator)" it is throwing the previous error
ValueError Traceback (most recent call last)
in ()
----> 1 images, labels, probs = get_predictions(model, valid_iterator)

in get_predictions(model, iterator)
13 x = x.to(device)
14
---> 15 y_pred, _ = model(x)
16
17 y_prob = F.softmax(y_pred, dim = -1)

ValueError: too many values to unpack (expected 2)

@bentrevett
Copy link
Owner

In the ResNet model, change the last few lines of the forward method to:

h = out.view(out.shape[0], -1)
out = self.fc(h)
return out, h

@Rukhmini
Copy link
Author

Rukhmini commented Apr 6, 2020

changing these line of code is throwing another error
AttributeError: 'tuple' object has no attribute 'log_softmax'

@bentrevett
Copy link
Owner

Can you upload the notebook to a GitHub gist? I'll have a quick look at it.

@Rukhmini
Copy link
Author

Rukhmini commented Apr 6, 2020 via email

@bentrevett
Copy link
Owner

In the train and evaluate functions you need to change fx = model(x) to fx, _ = model(x).

In both the ResNet18 and ResNet34 models, within the forward method you need to change:

out = out.view(out.shape[0], -1)
out = self.fc(out)
return out

to

h = out.view(out.shape[0], -1)
out = self.fc(h)
return out, h

@Rukhmini
Copy link
Author

Rukhmini commented Apr 6, 2020 via email

@bentrevett
Copy link
Owner

https://gist.github.com/bentrevett/3f899ce17781d91e8c9dbdad2c1a1cc9

Here is the code for the AlexNet notebook but with the model changed to ResNet18 - might be useful for you.

@Rukhmini
Copy link
Author

Rukhmini commented Apr 7, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants