class torch.nn.Parameter()
Variable的一种,常被用于模块参数(module parameter)。
Parameters 是 Variable 的子类。Paramenters和Modules一起使用的时候会有一些特殊的属性,即:当Paramenters赋值给Module的属性的时候,他会自动的被加到 Module的 参数列表中(即:会出现在 parameters() 迭代器中)。将Varibale赋值给Module属性则不会有这样的影响。 这样做的原因是:我们有时候会需要缓存一些临时的状态(state), 比如:模型中RNN的最后一个隐状态。如果没有Parameter这个类的话,那么这些临时变量也会注册成为模型变量。
Variable 与 Parameter的另一个不同之处在于,Parameter不能被 volatile(即:无法设置volatile=True)而且默认requires_grad=True。Variable默认requires_grad=False。
class torch.nn.Module
add_module(name, module)
将一个 child module 添加到当前 modle。 被添加的module可以通过 name属性来获取。 例:
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.add_module("conv", nn.Conv2d(10, 20, 4))
#self.conv = nn.Conv2d(10, 20, 4) is the same as above
model = Model()
print(model.conv)
输出:
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
children()
Returns an iterator over immediate children modules. 返回当前模型 子模块的迭代器。
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.add_module("conv", nn.Conv2d(10, 20, 4))
self.add_module("conv1", nn.Conv2d(20 ,10, 4))
model = Model()
for sub_module in model.children():
print(sub_module)
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
Conv2d(20, 10, kernel_size=(4, 4), stride=(1, 1))
eval()
将模型设置成evaluation模式
仅仅当模型中有Dropout和BatchNorm是才会有影响。
load_state_dict(state_dict)
将state_dict中的parameters和buffers复制到此module和它的后代中。state_dict中的key必须和 model.state_dict()返回的key一致。 NOTE:用来加载模型参数。
modules()
返回一个包含 当前模型 所有模块的迭代器。
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.add_module("conv", nn.Conv2d(10, 20, 4))
self.add_module("conv1", nn.Conv2d(20 ,10, 4))
model = Model()
for module in model.modules():
print(module)
Model (
(conv): Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
(conv1): Conv2d(20, 10, kernel_size=(4, 4), stride=(1, 1))
)
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
Conv2d(20, 10, kernel_size=(4, 4), stride=(1, 1))
可以看出,modules()返回的iterator不止包含 子模块。这是和children()的不同。
重复的模块只被返回一次(children()也是)。 在下面的例子中, submodule 只会被返回一次:
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
submodule = nn.Conv2d(10, 20, 4)
self.add_module("conv", submodule)
self.add_module("conv1", submodule)
model = Model()
for module in model.modules():
print(module)
print("fenge")
for module in model.children():
print(module)
print(len(list(model.children())))
Model (
(conv): Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
(conv1): Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
)
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
fenge
Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
1
named_children()
返回 包含 模型当前子模块 的迭代器,yield 模块名字和模块本身。
例子:
for name, module in model.named_children():
if name in ['conv', 'conv1']:
print(module)
重复的模块由于只能调用一次,所以我们输出不了conv1。
named_modules(memo=None, prefix=”)
返回包含网络中所有模块的迭代器, yielding 模块名和模块本身。
parameters(memo=None)
返回一个 包含模型所有参数 的迭代器。
一般用来当作optimizer的参数。
例子:
for param in model.parameters():
print(type(param.data), param.size())
<class 'torch.FloatTensor'> (20L,)
<class 'torch.FloatTensor'> (20L, 1L, 5L, 5L)
nn.FractionalMaxPool2d(2, output_ratio=(0.5, 0.5))只接受输入为4D的tensor
nn.FractionalMaxPool2d(3, output_size=(13, 12))可以接受3D输入
只接受4D或5D的输入
想查看embedding的内容,可以用以下方式:
embedding = nn.Embedding(10, 3)
print(embedding.weight)
Parameter containing:
-0.0010 -1.4765 -0.6978
-0.4030 1.1792 -0.0820
1.0888 -0.3538 -0.2297
-1.1154 -0.3636 -0.2577
-0.3030 1.1560 -0.7921
-1.5605 -0.3990 -0.7551
0.3265 -0.2672 -0.8583
-0.8346 0.5437 0.6192
0.1881 2.0915 0.5066
1.9426 0.0619 0.6486
[torch.FloatTensor of size 10x3]
input = Variable(torch.LongTensor([[1,2,4,5],[4,3,2,9]]))
print(embedding(input))
Variable containing:
(0 ,.,.) =
-0.4030 1.1792 -0.0820
1.0888 -0.3538 -0.2297
-0.3030 1.1560 -0.7921
-1.5605 -0.3990 -0.7551
(1 ,.,.) =
-0.3030 1.1560 -0.7921
-1.1154 -0.3636 -0.2577
1.0888 -0.3538 -0.2297
1.9426 0.0619 0.6486
[torch.FloatTensor of size 2x4x3]