当前位置:网站首页>Pytorch: a pit about the implementation of gradreverselayer
Pytorch: a pit about the implementation of gradreverselayer
2022-04-23 12:54:00 【Qing San】
I've been working on one recently domain adaptation Project , Yes gradreverselayer:`class GradReverse(Function):
def forward(ctx, x, alpha, **kwargs:None):
ctx.alpha = alpha
return x.view_as(x)
def backward(ctx, grad_output):
output = grad_output * -ctx.alpha
return output, None
There seems to be no problem , But keep reporting the wrong :

I haven't done it for a long time , So I made a stupid decision , I added one :
src_domain_loss=torch.tensor(src_domain_loss, requires_grad=True)
As a result, the error disappeared , I happily used this silly * The code took two months , Results the experimental results have been unsatisfactory , Until today, check the training gradient , It is found that the gradient is always zero !!!

Speechless , It must be the pot of this code , But without it, an error will be reported , I searched every corner of the Internet , No solution has been found , And paid people to consult ( cry ).
result , A flash of light , I found something missing in the code of gradient flip layer :
class GradReverse(Function):
@staticmethod
def forward(ctx, x, alpha, **kwargs:None):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output * -ctx.alpha
return output, None`
Have you found it? ?@staticmethod!!! I always thought this was a comment , Delete... Directly , Great speechless event , cause sb. to die of anger !!!
In fact, this is declaring a static method !
I hope you will take a warning .( cry )
版权声明
本文为[Qing San]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230616251505.html
边栏推荐
- [Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
- [daily question] chessboard question
- 网站首页文件被攻击篡改的形式有哪些
- SSM framework series - data source configuration day2-1
- 解锁OpenHarmony技术日!年度盛会,即将揭幕!
- 将新增和编辑的数据同步更新到列表
- SSM framework series - annotation development day2-2
- SSL certificate refund instructions
- Web17——EL与JSTL的使用
- Buuctf Web [gxyctf2019] no dolls
猜你喜欢

Sort out several uses of network IP agent

Remote access to raspberry pie at home (Part 1)
![leetcode:437. Path sum III [DFS selected or not selected?]](/img/e7/d0983a3251dfaf2cf33b56a12ac149.png)
leetcode:437. Path sum III [DFS selected or not selected?]

Kubernets Getting started tutoriel

Aviation core technology sharing | overview of safety characteristics of acm32 MCU

Unable to create servlet under SRC subfile of idea

如何实现点击一下物体播放一次动画

V-model binding value in El select, data echo only displays value, not label

leetcode:437. 路径总和 III【dfs 选还是不选?】

进程虚拟地址空间区域划分
随机推荐
Luogu p3236 [hnoi2014] picture frame solution
Unable to create servlet under SRC subfile of idea
Teach you to quickly develop a werewolf killing wechat applet (with source code)
Keyword interpretation and some APIs in RT thread
RT-thread中关键词解释及部分API
MySQL function - recursive function
[wechat applet] Z-index is invalid
进程虚拟地址空间区域划分
World Book Day: I'd like to recommend these books
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Buuctf Web [bjdctf2020] zjctf, but so
Remote sensing image classification and recognition system based on convolutional neural network
ZigBee CC2530 minimum system and register configuration (1)
Object. The disorder of key value array after keys
22. Bracket generation
C, calculation code of parameter points of two-dimensional Bezier curve
Web17——EL与JSTL的使用
STM32控制步进电机(ULN2003+28byj)
教你快速开发一个 狼人杀微信小程序(附源码)