网络编程
位置:首页>> 网络编程>> 网络编程>> pytorch: Parameter 的数据结构实例

pytorch: Parameter 的数据结构实例

作者:wzg2016  发布时间:2022-10-19 22:28:22 

标签: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)

来源:https://blog.csdn.net/Strive_For_Future/article/details/83244670

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com