这两天一直迷惑pytorch中关于requires_grad的设置,特别是在写cudaextension时。今天在pytorchgithub上看到个issue解释的很不错,姑且记录在这以备后用。
python关于__requires__的问题
首先安装scikit_learn,打开cmd执行下面的命令:
pipinstall-Uscikit-learn
接着安装配套的Scipy全家桶:
pipinstall--usernumpyscipymatplotlibipythonjupyterpandassympynose
Scipy全家桶的清单大致如下:
MarkupSafe-1.0
Send2Trash-1.5.0
backports-abc-0.5
backports.functools-lru-cache-1.5
backports.shutil-get-terminal-size-1.0.0
backports.shutil-which-3.5.1bleach-2.1.3
colorama-0.3.9configparser-3.5.0
cycler-0.10.0
decorator-4.2.1entrypoints-0.2.3
enum34-1.1.6
functools32-3.2.3.post2
关于pytorchrequires_grad
我的问题是在写cuda扩展时,我会在pythonautograd.Function的forward部分先声明好变量,就是先分配好内存,然后传入cuda的函数,cuda内做自定义操作,类似:
classmyfunc(torch.autograd.Function):
@staticmethod
defforward(ctx,x):
out=torch.zeros_like(x)
cuda_foward(x,out)
ctx.save_for_backward(x)
returnout
@staticmethod
defbackward(ctx,d_out):
x,=ctx.saved_tensors
d_x=torch.zeros_like(x)
cuda_backward(d_x,x,d_out)
returnd_x
总之,关于pytorchrequires给大家简单的介绍了一些,希望大家多看看。