pytorch: Parameter 的数据结构实例

一般来说,pytorch 的Parameter是一个tensor,但是跟通常意义上的tensor有些不一样

1) 通常意义上的tensor 仅仅是数据

2) 而Parameter所对应的tensor 除了包含数据之外,还包含一个属性:requires_grad(=True/False)

在Parameter所对应的tensor中获取纯数据,可以通过以下操作:

param_data = Parameter.data

测试代码:

#-*-coding:utf-8-*-
import torch
import torch.nn as nn
 
## regression for the 3 * 2 affine matrix
fc_loc = nn.Sequential(
  nn.Linear(10 * 3 * 3, 32),
  nn.ReLU(True),
  nn.Linear(32, 3 * 2)
)
 
## initialize the weights/bias with identy transformation
fc_loc[2].weight.data.zero_()
fc_loc[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
# print(fc_loc)
print(fc_loc[2].weight)
print(fc_loc[2].weight.data)

以上这篇pytorch: Parameter 的数据结构实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持来客网。