这是灵魂:[参见链接https://discuss.pytorch.org/t/common-class-of-linear-conv-etc/39987/8 ]
包括
使用命名空间火炬;使用命名空间火炬::nn;
int main() { auto net = Sequential(Conv2d(1 /输入通道/, 1 /输出通道/, 2 /内核大小/), Conv2d(1, 1, 2));
for (auto& p : net->named_parameters()) {
NoGradGuard no_grad;
// Access name.
std::cout << p.key() << std::endl;
// Access weigth and bias.
p.value().zero_(); // set all zero
std::cout << p.value() << std::endl;
}
return 0;
}
顺序的层具有以下命名约定:.,例如查看控制台输出
0.weight # name of the layer
(1,1,.,.) =
0 0
0 0
[ Variable[CPUFloatType]{1,1,2,2} ]
0.bias
0
[ Variable[CPUFloatType]{1} ]
1.weight
(1,1,.,.) =
0 0
0 0
[ Variable[CPUFloatType]{1,1,2,2} ]
1.bias
0
[ Variable[CPUFloatType]{1} ]