Complete the code to create a linear layer with 10 inputs and 5 outputs.
import torch.nn as nn layer = nn.Linear([1], 5)
The first argument to nn.Linear is the number of input features. Here, it should be 10.
Complete the code to pass input data through the linear layer.
import torch import torch.nn as nn layer = nn.Linear(4, 2) input_data = torch.randn(1, 4) output = layer([1])
To get the output, we pass the input data to the layer as a function call.
Fix the error in the code to correctly create a linear layer with 3 inputs and 1 output.
import torch.nn as nn layer = nn.Linear([1], 1)
The input size must match the number of input features, which is 3 here.
Fill both blanks to create a linear layer and pass a tensor through it.
import torch import torch.nn as nn layer = nn.Linear([1], [2]) input_tensor = torch.randn(2, 3) output = layer(input_tensor)
The input tensor has shape (2, 3), so input features = 3 and batch size = 2. The layer output size is 2.
Fill all three blanks to create a linear layer, pass input, and get output shape.
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
The input tensor has shape (4, 3), so input features = 3. The layer outputs 7 features. The input tensor's last dimension is 3.