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.grad
the 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 tograd
a Tensor which is not needed unlesscreate_graph
it isTrue
. For scalars or tensors that do not require gradients should be specified asNone
. retain_graph
: [ OPTIONAL ,bool
] IfFalse
, the graph used to compute gradients will be freed. Note that in almost all cases it is not necessary to set this option toTrue
and can often be resolved in a more efficient manner. Defaults tocreate_graph
the value of .create_graph
: [optional,bool
] If trueTrue
, a graph of derivatives will be constructed so that higher order derivative products can be computed, default isFalse
.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.