举一个通pytorch搭建的三层神经网络的例子
时间: 2023-11-11 22:02:34 浏览: 33
好的,这个问题属于科技问题,我可以回答。一个基于PyTorch搭建的三层神经网络的例子可以是:
```python
import torch
import torch.nn as nn
# 定义网络
class ThreeLayerNN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(ThreeLayerNN, self).__init__()
self.layer1 = nn.Linear(input_dim, hidden_dim)
self.layer2 = nn.Linear(hidden_dim, hidden_dim)
self.layer3 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
x = torch.relu(self.layer1(x))
x = torch.relu(self.layer2(x))
x = self.layer3(x)
return x
# 使用网络
input_dim = 10
hidden_dim = 20
output_dim = 2
model = ThreeLayerNN(input_dim, hidden_dim, output_dim)
input_data = torch.randn(1, input_dim)
output = model(input_data)
print(output)
```
希望这个例子能够帮助您。如果您有更多相关的问题,请随时继续问我。