溫馨提示×

pytorch如何打印網絡結構

小億
290
2024-04-08 13:55:43
欄目: 深度學習

要打印PyTorch網絡結構,可以使用print函數或者torchsummary庫來實現。

使用print函數來打印網絡結構示例如下:

import torch
import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 3)
        self.fc1 = nn.Linear(16 * 6 * 6, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 6 * 6)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()
print(net)

使用torchsummary庫來打印網絡結構示例如下:

from torchsummary import summary

net = Net()
summary(net, input_size=(3, 32, 32))

以上兩種方法都可以用來打印PyTorch網絡結構,可以根據需要選擇其中一種方法。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女