代码拉取完成,页面将自动刷新
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
class TD(nn.Module):
def __init__(self):
super(TD, self).__init__()
# # 方法1,使用Sequential,将所有操作放到一个model里,
# self.model = Sequential(
# Conv2d(3, 32, 5, padding=2),
# MaxPool2d(2),
# Conv2d(32, 32, 5, padding=2),
# MaxPool2d(2),
# Conv2d(32, 64, 5, padding=2),
# MaxPool2d(2),
# Flatten(),
# Linear(1024, 64),
# Linear(64, 10),
# )
# 方法2,自己定义每一层,在forward里自行调用
self.conv1 = Conv2d(3, 32, 5, padding=2)
self.maxpool1 = MaxPool2d(2)
self.conv2 = Conv2d(32, 32, 5, padding=2)
self.maxpool2 = MaxPool2d(2)
self.conv3 = Conv2d(32, 64, 5, padding=2)
self.maxpool3 = MaxPool2d(2)
self.flaten = Flatten()
self.linear1 = Linear(1024, 64)
self.linear2 = Linear(64, 10)
def forward(self, x):
# # 方法1,直接调用即可
# x = self.model(x)
# 方法2,每一层都需要自行调用
x = self.conv1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.maxpool3(x)
x = self.flaten(x)
x = self.linear1(x)
x = self.linear2(x)
return x
td = TD()
input = torch.ones((64, 3, 32, 32))
output = td(input)
print(output.shape)
writer = SummaryWriter("logs_seq")
writer.add_graph(td, input)
writer.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。