0
0
PyTorchml~10 mins

Linear (fully connected) layers in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a linear layer with 10 inputs and 5 outputs.

PyTorch
import torch.nn as nn

layer = nn.Linear([1], 5)
Drag options to blanks, or click blank then click option'
A10
B15
C5
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the output size as the input size.
Confusing input and output sizes.
2fill in blank
medium

Complete the code to pass input data through the linear layer.

PyTorch
import torch
import torch.nn as nn

layer = nn.Linear(4, 2)
input_data = torch.randn(1, 4)
output = layer([1])
Drag options to blanks, or click blank then click option'
Atorch.tensor([1, 2, 3, 4])
Blayer
Ctorch.randn(1, 2)
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the layer itself instead of the input data.
Passing a tensor with wrong shape.
3fill in blank
hard

Fix the error in the code to correctly create a linear layer with 3 inputs and 1 output.

PyTorch
import torch.nn as nn

layer = nn.Linear([1], 1)
Drag options to blanks, or click blank then click option'
A1
B3
C4
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using output size as input size.
Using zero or invalid sizes.
4fill in blank
hard

Fill both blanks to create a linear layer and pass a tensor through it.

PyTorch
import torch
import torch.nn as nn

layer = nn.Linear([1], [2])
input_tensor = torch.randn(2, 3)
output = layer(input_tensor)
Drag options to blanks, or click blank then click option'
A3
B2
C1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping input and output sizes.
Confusing batch size with input features.
5fill in blank
hard

Fill all three blanks to create a linear layer, pass input, and get output shape.

PyTorch
import torch
import torch.nn as nn

layer = nn.Linear([1], [2])
input_tensor = torch.randn(4, [3])
output = layer(input_tensor)
output_shape = output.shape
Drag options to blanks, or click blank then click option'
A5
B3
C7
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up input and output sizes.
Using wrong tensor shapes.