pytorch: Parameter 的数据结构实例

yipeiwu_com6年前Python基础

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

相关文章

Python学习笔记之lambda表达式用法详解

本文实例讲述了Python学习笔记之lambda表达式用法。分享给大家供大家参考,具体如下: Lambda 表达式 使用 Lambda 表达式创建匿名函数,即没有名称的函数。lambda...

python判断图片宽度和高度后删除图片的方法

本文实例讲述了python判断图片宽度和高度后删除图片的方法。分享给大家供大家参考。具体分析如下: Image对象有open方法却没有close方法,如果打开图片,判断图片高度和宽度,判...

Python随机生成一个6位的验证码代码分享

1. 生成源码 复制代码 代码如下: # -*- coding: utf-8 -*- import random def generate_verification_code(): &n...

Python subprocess模块详细解读

Python subprocess模块详细解读

本文研究的主要是Python subprocess模块的相关内容,具体如下。 在学习这个模块前,我们先用Python的help()函数查看一下subprocess模块是干嘛的: DES...

python检测lvs real server状态

复制代码 代码如下:import httplibimport osimport time def check_http(i):    try: &...