生成二层神经网络的代码
时间: 2023-11-14 13:02:11 浏览: 14
两层的神经网络的代码实例
我可以为您提供生成二层神经网络的代码示例,以下是Python代码:
import torch
import torch.nn as nn
class TwoLayerNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(TwoLayerNet, self).__init__()
self.layer1 = nn.Linear(input_size, hidden_size)
self.layer2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
out = self.layer1(x)
out = torch.relu(out)
out = self.layer2(out)
return out
# Example usage
input_size = 10
hidden_size = 50
output_size = 2
net = TwoLayerNet(input_size, hidden_size, output_size)
# Generate random input tensor
input_tensor = torch.randn(1, input_size)
# Forward pass
output = net(input_tensor)
print("Output:", output)
阅读全文