Hi,
When running this piece of code using GPU on google colab, I expect to receive “RuntimeError: mat1 and mat2 shapes cannot be multiplied”, however, I do not. Obviously, the shape of those consecutive Linear layers mismatch, and I get that RuntimeError while using CPU. Could anyone explain why?
import os
import torch
from torch import nn
device = 'cuda' if torch.cuda.is_available() else 'cpu'
output:
Using cuda device
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 513),
nn.ReLU(),
nn.Linear(512, 520),
nn.ReLU(),
nn.Linear(512, 10),
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = NeuralNetwork().to(device)
print(model)
output:
NeuralNetwork(
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear_relu_stack): Sequential(
(0): Linear(in_features=784, out_features=513, bias=True)
(1): ReLU()
(2): Linear(in_features=512, out_features=520, bias=True)
(3): ReLU()
(4): Linear(in_features=512, out_features=10, bias=True)
)
)
X = torch.rand(1, 28, 28, device=device)
logits = model(X)
pred_probab = nn.Softmax(dim=1)(logits)
y_pred = pred_probab.argmax(1)
print(f"Predicted class: {y_pred}")
Output:
Predicted class: tensor([5], device=‘cuda:0’)