import torch.nn.functional as F a = torch.rand(3) # [batch, class_num] a.requires_grad_() print(a) p = F.softmax(a,dim=0)#在第0维上,也就是 class_num上,定义损失函数 print(p)
import torch.nn.functional as F a = torch.rand(3) # [batch, class_num] a.requires_grad_() print(a) p = F.softmax(a,dim=0)#在第0维上,也就是 class_num上,定义损失函数 print(p)
##p.backward() 报错 ,因为此时p为向量,没有办法进行求导 # pytorch: grad can be implicitly created only for scalar outputs ##只能对单个y进行自动求导, #所以单独列开,同时由于p[0]-p[2]都使用同一个计算图, # 但是pytorch的计算图如果没有显式声明要保存,计算一次之后会作废,所以要置retain_graph=True # retain_graph 有效次数是1次,即如果下次还需要用到计算图,还是需要置为True
retain_graph: Optional[bool] = None, # True--------the function will only return a list of gradients w.r.t the specified inputs. # False, will be accumulated into their .grad attribute.
create_graph: bool = False,
only_inputs: bool = True, # True--------the function will only return a list of gradients w.r.t the specified inputs. # False, will be accumulated into their .grad attribute.
import torch.nn.functional as F a = torch.rand(3) # [batch, class_num] a.requires_grad_() print(a) p = F.softmax(a,dim=0)#在第0维上,也就是 class_num上,定义损失函数 print(p)
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
修改方案: retain_graph
pytorch: grad can be implicitly created only for scalar outputs