In-depth explanation of Pytorch function - torch.Tensor.backward

Category: General catalog of "Pytorch functions in simple terms"
Related articles:
· Pytorch functions in simple terms——torch.Tensor


Computes the gradient of the current tensor with respect to the graph, this function differentiates the graph using the chain rule. If the tensor is not a scalar (i.e. its data has multiple elements) and requires a gradient, the function also needs to specify the gradient. It should be a tensor of matching type and position, containing the gradient of the differentiated function. This function accumulates the gradient of each leaf node in the graph, before calling it, you may need to use zero.gradthe clear attribute or set it to None.

grammar

Tensor.backward(gradient=None, retain_graph=None, create_graph=False, inputs=None)

parameter

  • gradient: [ Tensor/ None] The gradient of the relative tensor. If it is a Tensor, it will be automatically converted to grada Tensor which is not needed unless create_graphit is True. For scalars or tensors that do not require gradients should be specified as None.
  • retain_graph: [ OPTIONAL , bool] If False, the graph used to compute gradients will be freed. Note that in almost all cases it is not necessary to set this option to Trueand can often be resolved in a more efficient manner. Defaults to create_graphthe value of .
  • create_graph: [optional, bool] If true True, a graph of derivatives will be constructed so that higher order derivative products can be computed, default is False.
  • inputs:[ List[Tensor]] The gradients of the input tensors will be accumulated as .grad. All other tensors are ignored. If not provided, gradients will be accumulated into all leaf tensors used for computation.

Guess you like

Origin blog.csdn.net/hy592070616/article/details/131968272