原文及翻译:
add_module(name, module)
方法: add_module(name, module)
Adds a child module to the current module.
将一个子模块添加到当前模块中.
The module can be accessed as an attribute using the given name.
通过给定的名字,我们就可以以访问属性的方式来访问该模块.
Parameters 参数
name (string) – name of the child module. The child
module can be accessed from this module using the
given name
name (字符串) – 子模块的名字. 在当前模块中使用给定的这个name就
可以访问子模块.
module (Module) – child module to be added to the module.
module (模块) – 要添加到当前模块中的子模块.
代码实验展示:
import torch
import torch.nn as nn
torch.manual_seed(seed=20200910)
class Model(torch.nn.Module):
def __init__(self):
super(Model,self).__init__()
self.conv1=torch.nn.Sequential( # 输入torch.Size([64, 1, 28, 28])
torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
torch.nn.ReLU(), # 输出torch.Size([64, 64, 28, 28])
torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1), # 输出torch.Size([64, 128, 28, 28])
torch.nn.ReLU(),
torch.nn.MaxPool2d(stride=2,kernel_size=2) # 输出torch.Size([64, 128, 14, 14])
)
self.dense=torch.nn.Sequential( # 输入torch.Size([64, 14*14*128])
torch.nn.Linear(14*14*128,1024), # 输出torch.Size([64, 1024])
torch.nn.ReLU(),
torch.nn.Dropout(p=0.5),
torch.nn.Linear(1024,10) # 输出torch.Size([64, 10])
)
self.layer4cxq1 = torch.nn.Conv2d(2,33,4,4)
self.layer4cxq2 = torch.nn.ReLU()
self.layer4cxq3 = torch.nn.MaxPool2d(stride=2,kernel_size=2)
self.layer4cxq4 = torch.nn.Linear(14*14*128,1024)
self.layer4cxq5 = torch.nn.Dropout(p=0.8)
self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))
self.attribute4lzq = nn.Parameter(torch.tensor([2.0,3.0,4.0,5.0]))
self.attribute4hh = nn.Parameter(torch.randn(3,4,5,6))
self.attribute4wyf = nn.Parameter(torch.randn(7,8,9,10))
def forward(self,x): # torch.Size([64, 1, 28, 28])
x = self.conv1(x) # 输出torch.Size([64, 128, 14, 14])
x = x.view(-1,14*14*128) # torch.Size([64, 14*14*128])
x = self.dense(x) # 输出torch.Size([64, 10])
return x
print('cuda(GPU)是否可用:',torch.cuda.is_available())
print('torch的版本:',torch.__version__)
model = Model() #.cuda()
print("测试模型(CPU)".center(100,"-"))
print(type(model))
print("torch.nn.Module.add_module(name, module)方法调用之前".center(100,"-"))
for name, child in model.named_modules():
print('模块的名字是:', name, '###模块本身是:', child)
print("torch.nn.Module.add_module(name, module)方法调用之后".center(100,"-"))
model.add_module('WuYiFan', torch.nn.Conv2d(38,38,38,38))
for name, child in model.named_modules():
print('模块的名字是:', name, '###模块本身是:', child)
控制台输出结果:
import torch
import torch.nn as nn
torch.manual_seed(seed=20200910)
class Model(torch.nn.Module):
def __init__(self):
super(Model,self).__init__()
self.conv1=torch.nn.Sequential( # 输入torch.Size([64, 1, 28, 28])
torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1),
torch.nn.ReLU(), # 输出torch.Size([64, 64, 28, 28])
torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1), # 输出torch.Size([64, 128, 28, 28])
torch.nn.ReLU(),
torch.nn.MaxPool2d(stride=2,kernel_size=2) # 输出torch.Size([64, 128, 14, 14])
)
self.dense=torch.nn.Sequential( # 输入torch.Size([64, 14*14*128])
torch.nn.Linear(14*14*128,1024), # 输出torch.Size([64, 1024])
torch.nn.ReLU(),
torch.nn.Dropout(p=0.5),
torch.nn.Linear(1024,10) # 输出torch.Size([64, 10])
)
self.layer4cxq1 = torch.nn.Conv2d(2,33,4,4)
self.layer4cxq2 = torch.nn.ReLU()
self.layer4cxq3 = torch.nn.MaxPool2d(stride=2,kernel_size=2)
self.layer4cxq4 = torch.nn.Linear(14*14*128,1024)
self.layer4cxq5 = torch.nn.Dropout(p=0.8)
self.attribute4cxq = nn.Parameter(torch.tensor(20200910.0))
self.attribute4lzq = nn.Parameter(torch.tensor([2.0,3.0,4.0,5.0]))
self.attribute4hh = nn.Parameter(torch.randn(3,4,5,6))
self.attribute4wyf = nn.Parameter(torch.randn(7,8,9,10))
def forward(self,x): # torch.Size([64, 1, 28, 28])
x = self.conv1(x) # 输出torch.Size([64, 128, 14, 14])
x = x.view(-1,14*14*128) # torch.Size([64, 14*14*128])
x = self.dense(x) # 输出torch.Size([64, 10])
return x
print('cuda(GPU)是否可用:',torch.cuda.is_available())
print('torch的版本:',torch.__version__)
model = Model() #.cuda()
print("测试模型(CPU)".center(100,"-"))
print(type(model))
print("torch.nn.Module.add_module(name, module)方法调用之前".center(100,"-"))
for name, child in model.named_modules():
print('模块的名字是:', name, '###模块本身是:', child)
print("torch.nn.Module.add_module(name, module)方法调用之后".center(100,"-"))
model.add_module('WuYiFan', torch.nn.Conv2d(38,38,38,38))
for name, child in model.named_modules():
print('模块的名字是:', name, '###模块本身是:', child)
转:
https://blog.youkuaiyun.com/m0_46653437/article/details/112649366?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242