0
0
PyTorchml~20 mins

Forward pass computation in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Forward Pass Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple forward pass in PyTorch
What is the output tensor after running this forward pass code?
PyTorch
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(2, 1, bias=False)

    def forward(self, x):
        return self.linear(x)

model = SimpleModel()
with torch.no_grad():
    model.linear.weight.copy_(torch.tensor([[2.0, 3.0]]))
    input_tensor = torch.tensor([[1.0, 2.0]])
    output = model(input_tensor)
    print(output)
Atensor([[8.]])
Btensor([[7.]])
Ctensor([[5.]])
Dtensor([[10.]])
Attempts:
2 left
💡 Hint
Remember the forward pass multiplies input by weights and sums them.
Model Choice
intermediate
1:30remaining
Choosing the correct model output shape
Given a PyTorch model with nn.Linear(4, 3) and input tensor shape (5, 4), what is the output shape after the forward pass?
A(5, 4)
B(4, 3)
C(3, 5)
D(5, 3)
Attempts:
2 left
💡 Hint
The linear layer changes the last dimension from input features to output features.
Hyperparameter
advanced
1:00remaining
Effect of activation function in forward pass
Which activation function will output only values between 0 and 1 during the forward pass?
AReLU
BSigmoid
CTanh
DLeakyReLU
Attempts:
2 left
💡 Hint
Think about which function squashes outputs into a probability-like range.
Metrics
advanced
1:30remaining
Calculating accuracy from model predictions
Given model predictions tensor([0, 1, 1, 0]) and true labels tensor([0, 0, 1, 0]), what is the accuracy?
A0.75
B0.5
C0.25
D1.0
Attempts:
2 left
💡 Hint
Accuracy is number of correct predictions divided by total predictions.
🔧 Debug
expert
2:00remaining
Identifying error in forward pass code
What error will this PyTorch forward pass code raise? import torch import torch.nn as nn class Model(nn.Module): def __init__(self): super().__init__() self.linear = nn.Linear(3, 2) def forward(self, x): return self.linear(x) model = Model() input_tensor = torch.randn(4, 4) output = model(input_tensor)
PyTorch
import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(3, 2)

    def forward(self, x):
        return self.linear(x)

model = Model()
input_tensor = torch.randn(4, 4)
output = model(input_tensor)
ANo error, outputs tensor of shape (4, 2)
BTypeError: forward() missing 1 required positional argument
CRuntimeError: size mismatch
DValueError: input tensor must be 1D
Attempts:
2 left
💡 Hint
Check if input tensor shape matches the expected input features of the linear layer.