pytorch5求导

import torch
from torch.nn import functional
x=torch.ones(1,requires_grad=True)
print(x)
w=torch.full([1],6,requires_grad=True)
mse=functional.mse_loss(x,x*w)
print(mse)#(1-6)**2
print(torch.autograd.grad(mse,[w]))#2*(1-6)*(-1)
mse=functional.mse_loss(x,x*w)
mse.backward()
print(w.grad)
a=torch.rand(3,requires_grad=True)
print(a)
p=functional.softmax(a,dim=0)
print(p)
print(torch.autograd.grad(p[1],[a],retain_graph=True))#-0.0.2751*0.4222,0.2751*(1-0.2751),-0.2751* 0.3027

结果:tensor([1.], requires_grad=True)
tensor(25., grad_fn=<MeanBackward0>)
(tensor([10.]),)
tensor([10.])
tensor([0.9570, 0.5285, 0.6240], requires_grad=True)
tensor([0.4222, 0.2751, 0.3027], grad_fn=<SoftmaxBackward>)
(tensor([-0.1162,  0.1994, -0.0833]),)

附图softmax求导结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值