Introduction
A linear layer connects every input to every output with a weight, helping the model learn simple relationships between data.
torch.nn.Linear(in_features, out_features, bias=True)layer = torch.nn.Linear(10, 5)
layer = torch.nn.Linear(3, 1, bias=False)
import torch import torch.nn as nn # Create a linear layer with 4 inputs and 2 outputs linear_layer = nn.Linear(4, 2) # Example input: batch of 3 data points, each with 4 features input_data = torch.tensor([[1.0, 2.0, 3.0, 4.0], [4.0, 3.0, 2.0, 1.0], [0.5, 0.5, 0.5, 0.5]]) # Pass input through the linear layer output = linear_layer(input_data) # Print output shape and values print(f"Output shape: {output.shape}") print(f"Output values:\n{output}")
torch.nn.Linear layer do in a neural network?torch.nn.Linear(in_features, out_features).in_features=10 and out_features=5.import torch layer = torch.nn.Linear(4, 3) input_tensor = torch.randn(2, 4) output = layer(input_tensor) print(output.shape)
import torch layer = torch.nn.Linear(3, 2) input_tensor = torch.randn(4, 4) output = layer(input_tensor)
nn.Linear(8, 4).nn.Linear(4, 2).Pass the test and this pattern moves unknown