pytorch 自定义参数不更新方式


Posted in Python onJanuary 06, 2020

nn.Module中定义参数:不需要加cuda,可以求导,反向传播

class BiFPN(nn.Module):
  def __init__(self, fpn_sizes):

  self.w1 = nn.Parameter(torch.rand(1))

  print("no---------------------------------------------------",self.w1.data, self.w1.grad)

下面这个例子说明中间变量可能没有梯度,但是最终变量有梯度:

cy1 cd都有梯度

import torch
 
xP=torch.Tensor([[ 3233.8557, 3239.0657, 3243.4355, 3234.4507, 3241.7087,
     3243.7292, 3234.6826, 3237.6609, 3249.7937, 3244.8623,
     3239.5349, 3241.4626, 3251.3457, 3247.4263, 3236.4924,
     3251.5735, 3246.4731, 3242.4692, 3239.4958, 3247.7283,
     3251.7134, 3249.0237, 3247.5637],
    [ 1619.9011, 1619.7140, 1620.4883, 1620.0642, 1620.2191,
     1619.9796, 1617.6597, 1621.1522, 1621.0869, 1620.9725,
     1620.7130, 1620.6071, 1620.7437, 1621.4825, 1620.5107,
     1621.1519, 1620.8462, 1620.5944, 1619.8038, 1621.3364,
     1620.7399, 1621.1178, 1618.7080],
    [ 1619.9330, 1619.8542, 1620.5176, 1620.1167, 1620.1577,
     1620.0579, 1617.7155, 1621.1718, 1621.1338, 1620.9572,
     1620.6288, 1620.6621, 1620.7074, 1621.5305, 1620.5656,
     1621.2281, 1620.8346, 1620.6021, 1619.8228, 1621.3936,
     1620.7616, 1621.1954, 1618.7983],
    [ 1922.6078, 1922.5680, 1923.1331, 1922.6604, 1922.9589,
     1922.8818, 1920.4602, 1923.8107, 1924.0142, 1923.6907,
     1923.4465, 1923.2820, 1923.5728, 1924.4071, 1922.8853,
     1924.1107, 1923.5465, 1923.5121, 1922.4673, 1924.1871,
     1923.6248, 1923.9086, 1921.9496],
    [ 1922.5948, 1922.5311, 1923.2850, 1922.6613, 1922.9734,
     1922.9271, 1920.5950, 1923.8757, 1924.0422, 1923.7318,
     1923.4889, 1923.3296, 1923.5752, 1924.4948, 1922.9866,
     1924.1642, 1923.6427, 1923.6067, 1922.5214, 1924.2761,
     1923.6636, 1923.9481, 1921.9005]])
 
yP=torch.Tensor([[ 2577.7729, 2590.9868, 2600.9712, 2579.0195, 2596.3684,
     2602.2771, 2584.0305, 2584.7749, 2615.4897, 2603.3164,
     2589.8406, 2595.3486, 2621.9116, 2608.2820, 2582.9534,
     2619.2073, 2607.1233, 2597.7888, 2591.5735, 2608.9060,
     2620.8992, 2613.3511, 2614.2195],
    [ 673.7830,  693.8904,  709.2661,  675.4254,  702.4049,
      711.2085,  683.1571,  684.6160,  731.3878,  712.7546,
      692.3011,  701.0069,  740.6815,  720.4229,  681.8199,
      736.9869,  718.5508,  704.3666,  695.0511,  721.5912,
      739.6672,  728.0584,  729.3143],
    [ 673.8367,  693.9529,  709.3196,  675.5266,  702.3820,
      711.2159,  683.2151,  684.6421,  731.5291,  712.6366,
      692.1913,  701.0057,  740.6229,  720.4082,  681.8656,
      737.0168,  718.4943,  704.2719,  695.0775,  721.5616,
      739.7233,  728.1235,  729.3387],
    [ 872.9419,  891.7061,  905.8004,  874.6565,  899.2053,
      907.5082,  881.5528,  883.0028,  926.3083,  908.9742,
      890.0403,  897.8606,  934.6913,  916.0902,  880.4689,
      931.3562,  914.4233,  901.2154,  892.5759,  916.9590,
      933.9291,  923.0745,  924.4461],
    [ 872.9661,  891.7683,  905.8128,  874.6301,  899.2887,
      907.5155,  881.6916,  883.0234,  926.3242,  908.9561,
      890.0731,  897.9221,  934.7324,  916.0806,  880.4300,
      931.3933,  914.5662,  901.2715,  892.5501,  916.9894,
      933.9813,  923.0823,  924.3654]])
 
 
shape=[4000, 6000]
cx,cy1=torch.rand(1,requires_grad=True),torch.rand(1,requires_grad=True)
 
cd=torch.rand(1,requires_grad=True)
ox,oy=cx,cy1
print('cx:{},cy:{}'.format(id(cx),id(cy1)))
print('ox:{},oy:{}'.format(id(ox),id(oy)))
cx,cy=cx*shape[1],cy1*shape[0]
print('cx:{},cy:{}'.format(id(cx),id(cy)))
print('ox:{},oy:{}'.format(id(ox),id(oy)))
distance=torch.sqrt(torch.pow((xP-cx),2)+torch.pow((yP-cy),2))
mean=torch.mean(distance,1)
starsFC=cd*torch.pow((distance-mean[...,None]),2)
loss=torch.sum(torch.mean(starsFC,1).squeeze(),0)
loss.backward()
print(loss)
print(cx)
print(cy1)
print("cx",cx.grad)
print("cy",cy1.grad)
print("cd",cd.grad)
print(ox.grad)
print(oy.grad)
print('cx:{},cy:{}'.format(id(cx),id(cy)))
print('ox:{},oy:{}'.format(id(ox),id(oy)))

以上这篇pytorch 自定义参数不更新方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的Django框架中的表单处理示例
Jul 17 Python
windows下安装Python和pip终极图文教程
Mar 05 Python
Python存取XML的常见方法实例分析
Mar 21 Python
浅谈python爬虫使用Selenium模拟浏览器行为
Feb 23 Python
django1.11.1 models 数据库同步方法
May 30 Python
python异步存储数据详解
Mar 19 Python
对python中基于tcp协议的通信(数据传输)实例讲解
Jul 22 Python
Django 用户认证组件使用详解
Jul 23 Python
Python在OpenCV里实现极坐标变换功能
Sep 02 Python
django 实现手动存储文件到model的FileField
Mar 30 Python
Python中socket网络通信是干嘛的
May 27 Python
python 实现批量图片识别并翻译
Nov 02 Python
3种python调用其他脚本的方法
Jan 06 #Python
pytorch 实现模型不同层设置不同的学习率方式
Jan 06 #Python
浅析Python3 pip换源问题
Jan 06 #Python
通过实例学习Python Excel操作
Jan 06 #Python
pytorch载入预训练模型后,实现训练指定层
Jan 06 #Python
python与mysql数据库交互的实现
Jan 06 #Python
win10系统下python3安装及pip换源和使用教程
Jan 06 #Python
You might like
Linux fgetcsv取得的数组元素为空字符串的解决方法
2011/11/25 PHP
Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法
2016/07/02 PHP
2017年最好用的9个php开发工具推荐(超好用)
2017/10/23 PHP
PDO::prepare讲解
2019/01/29 PHP
制作特殊字的脚本
2006/06/26 Javascript
javascript 定义初始化数组函数
2009/09/07 Javascript
Jquery ajaxsubmit上传图片实现代码
2010/11/04 Javascript
Extjs407 getValue()和getRawValue()区别介绍
2013/05/21 Javascript
JavaScript中的函数重载深入理解
2014/08/04 Javascript
javascript入门之string对象【新手必看】
2016/11/22 Javascript
使用JS判断移动端手机横竖屏状态
2018/07/30 Javascript
Vuex的基本概念、项目搭建以及入坑点
2018/11/04 Javascript
vuex + keep-alive实现tab标签页面缓存功能
2019/10/17 Javascript
js实现文章目录索引导航(table of content)
2020/05/10 Javascript
微信小程序实现身份证取景框拍摄
2020/09/09 Javascript
React倒计时功能实现代码——解耦通用
2020/09/18 Javascript
[03:59]DOTA2英雄梦之声_第07期_水晶室女
2014/06/23 DOTA
[01:11:02]Secret vs Newbee 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python合并文本文件示例
2014/02/07 Python
python 解析XML python模块xml.dom解析xml实例代码
2014/02/07 Python
完美解决Python2操作中文名文件乱码的问题
2017/01/04 Python
使用Python对SQLite数据库操作
2017/04/06 Python
Python中的Numpy矩阵操作
2018/08/12 Python
python读取xlsx的方法
2018/12/25 Python
python中struct模块之字节型数据的处理方法
2019/08/27 Python
python对验证码降噪的实现示例代码
2019/11/12 Python
python 安装教程之Pycharm安装及配置字体主题,换行,自动更新
2020/03/13 Python
Python集成开发工具Pycharm的安装和使用详解
2020/03/18 Python
通过实例了解python__slots__使用方法
2020/09/14 Python
中国电子产品外贸网站:MiniIntheBox
2017/02/06 全球购物
英国最大的在线奢侈手表零售商:Jura Watches
2018/01/29 全球购物
鞋类设计与工艺专业销售求职信
2013/11/01 职场文书
中西医专业毕业生职业规划书
2014/02/24 职场文书
党的群众路线对照检查材料
2014/09/22 职场文书
2014年全国法制宣传日宣传活动方案
2014/11/02 职场文书
店长岗位职责
2015/02/11 职场文书