张板:如何查看Pytorch模型摘要?

发布于 2025-02-05 05:25:43 字数 1972 浏览 2 评论 0原文

我有以下网络。

import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter

class Net(nn.Module):
    def __init__(self,input_shape, num_classes):
        super(Net, self).__init__()

        self.conv = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),

            nn.Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),
        )

        x = self.conv(torch.rand(input_shape))
        in_features = np.prod(x.shape)

        self.classifier = nn.Sequential(
            nn.Linear(in_features=in_features, out_features=num_classes),
        )

    def forward(self, x):
        x = self.feature_extractor(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

net = Net(input_shape=(1,64,1292), num_classes=4)
print(net)

这打印了以下内容: -

Net(
  (conv): Sequential(
    (0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (4): ReLU(inplace=True)
    (5): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=320, out_features=4, bias=True)
  )
)

但是,我正在尝试各种实验,我想跟踪张量板上的网络体系结构。我知道有一个函数writer.add_graph(模型,input_to_model),但它需要输入,或者至少应该知道其形状。

因此,我尝试了writer.add_text(“模型”,str(模型)),但格式化在张板中拧紧。

我的问题是,有没有办法至少通过在张量板中使用打印功能可以看到我可以看到的方式?

I have the following network.

import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter

class Net(nn.Module):
    def __init__(self,input_shape, num_classes):
        super(Net, self).__init__()

        self.conv = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),

            nn.Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),
        )

        x = self.conv(torch.rand(input_shape))
        in_features = np.prod(x.shape)

        self.classifier = nn.Sequential(
            nn.Linear(in_features=in_features, out_features=num_classes),
        )

    def forward(self, x):
        x = self.feature_extractor(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

net = Net(input_shape=(1,64,1292), num_classes=4)
print(net)

This prints the following:-

Net(
  (conv): Sequential(
    (0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (4): ReLU(inplace=True)
    (5): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=320, out_features=4, bias=True)
  )
)

However, I am trying various experiments and I want to keep track of network architecture on Tensorboard. I know there is a function writer.add_graph(model, input_to_model) but it requires input, or at least its shape should be known.

So, I tried writer.add_text("model", str(model)), but formatting is screwed up in tensorboard.

enter image description here

My question is, is there a way to at least visualize the way I can see by using print function in the tensorboard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2025-02-12 05:25:44

我可以看到一切都正确,但是有一个格式的问题。 Tensorboard了解Markdown,因此您实际上可以用\ n< br/> & nbsp; >。

这是一个详细的演练。假设您有以下模型: -

import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter

class Net(nn.Module):
    def __init__(self,input_shape, num_classes):
        super(Net, self).__init__()

        self.conv = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),

            nn.Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),
        )

        x = self.conv(torch.rand(input_shape))
        in_features = np.prod(x.shape)

        self.classifier = nn.Sequential(
            nn.Linear(in_features=in_features, out_features=num_classes),
        )

    def forward(self, x):
        x = self.feature_extractor(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

net = Net(input_shape=(1,64,1292), num_classes=4)
print(net)

它打印以下以及是否可以在张量板中显示。

Net(
  (conv): Sequential(
    (0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (4): ReLU(inplace=True)
    (5): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=320, out_features=4, bias=True)
  )
)

add_graph(模型,输入)summaryWriter中有函数,但是您必须创建虚拟输入,在某些情况下,很难始终了解它们。取而代之的是以下: -

writer = SummaryWriter()

model_summary = str(model).replace( '\n', '<br/>').replace(' ', ' ')
writer.add_text("model", model_summary)

writer.close()

上面在张板中产生以下文本: -

”在此处输入图像描述”

I can see everything is going right but there is just a formatting issue. Tensorboard understands markdown so you can actually replace \n with <br/> and with  .

Here is a detailed walkthrough. Suppose you have the following model:-

import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter

class Net(nn.Module):
    def __init__(self,input_shape, num_classes):
        super(Net, self).__init__()

        self.conv = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),

            nn.Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=(4,4)),
        )

        x = self.conv(torch.rand(input_shape))
        in_features = np.prod(x.shape)

        self.classifier = nn.Sequential(
            nn.Linear(in_features=in_features, out_features=num_classes),
        )

    def forward(self, x):
        x = self.feature_extractor(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

net = Net(input_shape=(1,64,1292), num_classes=4)
print(net)

This prints the following and if can actually show it in the Tensorboard.

Net(
  (conv): Sequential(
    (0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (4): ReLU(inplace=True)
    (5): MaxPool2d(kernel_size=(4, 4), stride=(4, 4), padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=320, out_features=4, bias=True)
  )
)

There is function in add_graph(model, input) in SummaryWriter but you must create dummy input and in some cases it is difficult of to always know them. Instead do following:-

writer = SummaryWriter()

model_summary = str(model).replace( '\n', '<br/>').replace(' ', ' ')
writer.add_text("model", model_summary)

writer.close()

Above produces following text in tensorboard:-

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文