Comments (4)
So, I just ran into the issue and spent a bit of time figuring out what was going on. Turns out, it's relatively simple, but it became simple only after banging my head against the keyboard a hundred times LOL! Basically, the key thing to understand is that the model is designed to work only with BATCHES! So, if you feed it a batch, basically a tensor of shape [<batch_size>, <color_channels>, <height>, <width>]
, e.g., [32, 1, 28, 28]
, everything just works, and the output of the model is a tensor of shape [<batch_size>, <number_of_classes>]
, e.g., [32, 10]
in this notebook. Now, if you want to do a prediction on a single image, i.e. a tensor of shape [<color_channels>, <height>, <width>]
, it's not going to work. So, the trick is to artificially add a dimension to our image tensor before feeding it to our model. Basically, we just turn our image tensor into a batch of 1 image, i.e. a tensor of shape [1, 1, 28, 28]
. We do that with:
image.unsqueeze(0)
Now, the output of the model will be a batch containing 1 prediction, i.e. a tensor of shape [1, 10]
. In inference mode, use squeeze()
to remove the first dimension. So, here is the code:
model_X.to(device)
model_X.eval()
with torch.inference_mode():
prediction = model_X(image.unsqueeze(0).to(device)).squeeze()
prediction
And the output will be a tensor of shape [10]
, and you can use argmax(dim=1)
to get the index of the class with the highest probability.
Hopefully, this clarifies things. I think this issue can be closed (unless I missed something obvious, of course) but I do think that the course material may need to be updated to clarify this a bit.
from pytorch-deep-learning.
I've run into something similar in 03. Excersises - Running single dummy tensors torch.rand(size=(1, 28, 28)).unsqueeze(dim=0)
as you did in the solutions caused shape errors for me (again 49 vs 490) but did work in the training/test loops. Unsure where this comes from.. Could also be me misunderstanding something with shapes? still learning after all.
Note: model architecture was completely the same, even when running your code it prompted shape error on single tensors.
from pytorch-deep-learning.
Hello,
I ran into same issue, then I gave model.unsqueeze(dim=0) as suggested by aronvandepol, can someone explain what is the reason of such a problem?
Thanks.
from pytorch-deep-learning.
So, I just ran into the issue and spent a bit of time figuring out what was going on. Turns out, it's relatively simple, but it became simple only after banging my head against the keyboard a hundred times LOL! Basically, the key thing to understand is that the model is designed to work only with BATCHES! So, if you feed it a batch, basically a tensor of shape
[<batch_size>, <color_channels>, <height>, <width>]
, e.g.,[32, 1, 28, 28]
, everything just works, and the output of the model is a tensor of shape[<batch_size>, <number_of_classes>]
, e.g.,[32, 10]
in this notebook. Now, if you want to do a prediction on a single image, i.e. a tensor of shape[<color_channels>, <height>, <width>]
, it's not going to work. So, the trick is to artificially add a dimension to our image tensor before feeding it to our model. Basically, we just turn our image tensor into a batch of 1 image, i.e. a tensor of shape[1, 1, 28, 28]
. We do that with:
image.unsqueeze(0)
Now, the output of the model will be a batch containing 1 prediction, i.e. a tensor of shape
[1, 10]
. In inference mode, usesqueeze()
to remove the first dimension. So, here is the code:model_X.to(device) model_X.eval() with torch.inference_mode(): prediction = model_X(image.unsqueeze(0).to(device)).squeeze() predictionAnd the output will be a tensor of shape
[10]
, and you can useargmax(dim=1)
to get the index of the class with the highest probability.Hopefully, this clarifies things. I think this issue can be closed (unless I missed something obvious, of course) but I do think that the course material may need to be updated to clarify this a bit.
Thank you for making things simple as i was about to post the same question.
from pytorch-deep-learning.
Related Issues (20)
- Broken Link in PyTorch Custom Datasets Notebook HOT 1
- Hey i just reviewed one part of code.
- HuggingFace Spaces Runtime Error
- Getting Started with Tensor flor - scalar.ndim HOT 1
- an error on Batching FashionMNIST figure
- TypeError: 'Tensor' object is not callable HOT 1
- Fix Code Error
- Less accuracy on local machine compare to Colab HOT 1
- The Markdown table formatting is incorrect
- PyTorch
- 09 Model development: TypeError: AsyncConnectionPool.__init__() got an unexpected keyword argument 'socket_options'
- CIFAR 10 dataset
- 05_Pytorch going Modular model_builder.py
- 03_pytorch_computer_vision clarification on a comment
- Model not learning
- Where to go from here HOT 1
- Cant find solution to error - Model 2: Training Our First CNN and Evaluating Its Results HOT 1
- Help? Pls HOT 1
- 06 Error with pretrained Model
- Clarification for discussion of shared memory in `torch.from_numpy(ndarray)` and `torch.Tensor.numpy()` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytorch-deep-learning.