一.pytorch求导计算法
就是普通的微分求导,详见https://zhuanlan.zhihu.com/p/51385110
二.pytorch处理不可微的方式
没有被选择到的直接归零
import torch
#x = torch.tensor([[1.,2.,3.],[4.,5.,6.]],requires_grad=True)
x= torch.randn(2,3,requires_grad=True)
y = torch.add(x,1)
print(y)
z = 2*torch.pow(y,2)
print(z)
t = torch.argmax(z,dim=0).unsqueeze(0)
d = torch.gather(z,0,t)
f = torch.mean(d)
f.backward()
print(x.grad)