溫馨提示×

pytorch如何打印網(wǎng)絡(luò)結(jié)構(gòu)

小億
247
2024-04-08 13:55:43

要打印PyTorch網(wǎng)絡(luò)結(jié)構(gòu),可以使用print函數(shù)或者torchsummary庫來實(shí)現(xiàn)。

使用print函數(shù)來打印網(wǎng)絡(luò)結(jié)構(gòu)示例如下:

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庫來打印網(wǎng)絡(luò)結(jié)構(gòu)示例如下:

from torchsummary import summary

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

以上兩種方法都可以用來打印PyTorch網(wǎng)絡(luò)結(jié)構(gòu),可以根據(jù)需要選擇其中一種方法。

0