您的位置:首页 > 理论基础 > 数据结构算法

pytorch: Parameter 的数据结构实例

2020-02-13 10:05 537 查看

一般来说,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 的数据结构实例就是小编分享给大家的全部内容了,希望能给大家一个参考

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息