내가 정의하는 경우 std::vector<torch::nn::Linear> linear_layers;
와 일부이 벡터를 채우기 torch::nn::Linear
객체, 그때는 액세스 할 수 weight
및 bias
의해 값을 linear_layers[k].weight
하고 linear_layers[k].bias
. 같은 기능을 다른 레이어 유형 (예 : torch::nn::Conv2d
.
사용하여 내 네트워크를 작성하는 경우 nn::sequential
다음 중 하나 다시 밀어 Linear
또는 Conv2d
내가 액세스 할 수 없습니다 weight
및 bias
직접. 이제 내 질문은 내가 사용할 때 각 레이어의 가중치 및 바이어스 값에 어떻게 액세스 할 수 nn::sequential
있습니까?
고마워, Afshin
다음은 soultion입니다. [ https://discuss.pytorch.org/t/common-class-of-linear-conv-etc/39987/8 링크 참조 ]
네임 스페이스 토치 사용; 네임 스페이스 torch :: 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} ]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다