pytorch linear example
时间: 2023-10-11 13:09:52 浏览: 89
Here is an example of how to create a linear layer in PyTorch:
```python
import torch.nn as nn
# Create a linear layer with input size of 10 and output size of 5
linear_layer = nn.Linear(10, 5)
# Create a tensor of size (1,10)
input_tensor = torch.randn(1, 10)
# Pass the input tensor through the linear layer
output_tensor = linear_layer(input_tensor)
# Print the output tensor
print(output_tensor)
```
In this example, we first import the `nn` module from PyTorch for creating neural network layers. We then create a linear layer using the `nn.Linear` class with an input size of 10 and an output size of 5.
Next, we create an input tensor of size (1,10) using `torch.randn` and pass it through the linear layer using the `linear_layer` object. The output tensor is then printed to the console.
Note that the linear layer performs a matrix multiplication of the input tensor with a weight matrix of size (10,5) and adds a bias vector of size (5) to the result.
阅读全文