pytorch: Parameter 的数据结构实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

pytorch: Parameter 的数据结构实例

  2021-04-02 我要评论

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

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们