首页 > 其他分享 >Pytorch语法——torch.autograd.grad

Pytorch语法——torch.autograd.grad

时间:2023-05-11 17:25:51浏览次数:32  
标签:Case tensor autograd torch Pytorch outputs gradients grad

The torch.autograd.grad function is a part of PyTorch's automatic differentiation package and is used to compute the gradients of given outputs with respect to given inputs. This function is useful when you need to compute gradients explicitly, rather than accumulating them in the .grad attribute of the input tensors.

Parameters:

  1. outputs: A sequence of tensors representing the outputs of the differentiated function.
  2. inputs: A sequence of tensors for which gradients will be calculated.
  3. grad_outputs: The "vector" in the vector-Jacobian product, usually gradients with respect to each output. Default is None.
  4. retain_graph: If set to False, the computation graph will be freed. Default value depends on the create_graph parameter.
  5. create_graph: If set to True, the graph of the derivative will be constructed, allowing higher-order derivative products. Default is False.
  6. allow_unused: If set to False, specifying unused inputs when computing outputs will raise an error. Default is False.
  7. is_grads_batched: If set to True, the first dimension of each tensor in grad_outputs will be interpreted as the batch dimension. Default is False.

Return type:
A tuple containing the gradients with respect to each input tensor.

Example:

Consider a simple example of computing the gradient of a function y = x^2 with respect to x. Here, x is the input and y is the output.

import torch

# Define the input tensor and enable gradient tracking
x = torch.tensor(2.0, requires_grad=True)

# Define the function y = x^2
y = x ** 2

# Compute the gradient of y with respect to x
grads = torch.autograd.grad(outputs=y, inputs=x)

print(grads)  # Output: (tensor(4.0),)

In this example, we first define the input tensor x with a value of 2.0 and enable gradient tracking by setting requires_grad=True. Then, we define the function y = x^2. Next, we compute the gradient of y with respect to x using torch.autograd.grad(outputs=y, inputs=x). The result is a tuple containing the gradient (4.0 in this case), which is the derivative of x^2 with respect to x evaluated at x=2.


The grad_outputs parameter in the torch.autograd.grad function represents the "vector" in the vector-Jacobian product. It is a sequence of tensors containing the gradients with respect to each output. The grad_outputs parameter is used when you want to compute a specific vector-Jacobian product, instead of the full Jacobian matrix.

When the gradient is computed using torch.autograd.grad, PyTorch computes the dot product of the Jacobian matrix (the matrix of partial derivatives) and the provided grad_outputs vector. If grad_outputs is not provided (i.e., set to None), PyTorch assumes it to be a vector of ones with the same shape as the output tensor.

Here's an example to help illustrate the concept:

import torch

# Define input tensors and enable gradient tracking
x = torch.tensor(2.0, requires_grad=True)
y = torch.tensor(3.0, requires_grad=True)

# Define the output function: z = x^2 + y^2
z = x ** 2 + y ** 2

# Compute the gradients of z with respect to x and y using different grad_outputs values

# Case 1: Default grad_outputs (None)
grads1 = torch.autograd.grad(outputs=z, inputs=(x, y))
print("Case 1 - Default grad_outputs:", grads1)  # Output: (tensor(4.0), tensor(6.0))

# Case 2: Custom grad_outputs (scalar value)
grad_outputs_scalar = torch.tensor(2.0)
grads2 = torch.autograd.grad(outputs=z, inputs=(x, y), grad_outputs=grad_outputs_scalar)
print("Case 2 - Custom grad_outputs (scalar):", grads2)  # Output: (tensor(8.0), tensor(12.0))

# Case 3: Custom grad_outputs (tensor value)
grad_outputs_tensor = torch.tensor(3.0)
grads3 = torch.autograd.grad(outputs=z, inputs=(x, y), grad_outputs=grad_outputs_tensor)
print("Case 3 - Custom grad_outputs (tensor):", grads3)  # Output: (tensor(12.0), tensor(18.0))

In this example, we define two input tensors x and y with values 2.0 and 3.0 respectively, and enable gradient tracking by setting requires_grad=True. Then, we define the output function z = x^2 + y^2. We compute the gradients of z with respect to x and y using three different values for grad_outputs.

  1. Case 1 - Default grad_outputs: The gradients are (4.0, 6.0), which correspond to the partial derivatives of z with respect to x and y (2x and 2y) evaluated at x=2 and y=3.
  2. Case 2 - Custom grad_outputs (scalar): We provide a scalar value of 2.0 as grad_outputs. The gradients are (8.0, 12.0), which are the original gradients (4.0, 6.0) multiplied by the scalar value 2.
  3. Case 3 - Custom grad_outputs (tensor): We provide a tensor value of 3.0 as grad_outputs. The gradients are (12.0, 18.0), which are the original gradients (4.0, 6.0) multiplied by the tensor value 3.

As you can see from the examples, providing different values for grad_outputs affects the resulting gradients, as it represents the vector in the vector-Jacobian product. This parameter can be useful when you want to weight the gradients differently, or when you need to compute a specific vector-Jacobian product.

Here's another example with a multi-output function to further illustrate the concept:

import torch

# Define input tensor and enable gradient tracking
x = torch.tensor([2.0, 3.0], requires_grad=True)

# Define the multi-output function: y = [x0^2, x1^2]
y = x ** 2

# Compute the gradients of y with respect to x using different grad_outputs values

# Case 1: Default grad_outputs (None)
grads1 = torch.autograd.grad(outputs=y, inputs=x)
print("Case 1 - Default grad_outputs:", grads1)  # Output: (tensor([4., 6.]),)

# Case 2: Custom grad_outputs (tensor)
grad_outputs_tensor = torch.tensor([1.0, 2.0])
grads2 = torch.autograd.grad(outputs=y, inputs=x, grad_outputs=grad_outputs_tensor)
print("Case 2 - Custom grad_outputs (tensor):", grads2)  # Output: (tensor([ 4., 12.]),)

In this example, we define an input tensor x with two elements and enable gradient tracking. We then define a multi-output function y = [x0^2, x1^2]. We compute the gradients of y with respect to x using different values for grad_outputs.

  1. Case 1 - Default grad_outputs: The gradients are (4.0, 6.0), which correspond to the partial derivatives of y with respect to x (2x0 and 2x1) evaluated at x0=2 and x1=3.
  2. Case 2 - Custom grad_outputs (tensor): We provide a tensor with values [1.0, 2.0] as grad_outputs. The gradients are (4.0, 12.0), which are the original gradients (4.0, 6.0) multiplied element-wise by the grad_outputs tensor.

In the second case, the gradients are computed as the product of the Jacobian matrix and the provided grad_outputs tensor. This allows us to compute specific vector-Jacobian products or weight the gradients differently for each output.

标签:Case,tensor,autograd,torch,Pytorch,outputs,gradients,grad
From: https://www.cnblogs.com/maluyelang/p/17391661.html

相关文章

  • Pytorch数据预处理
    为了能用深度学习来解决现实世界的问题,我们经常从预处理原始数据开始,而不是从那些准备好的张量格式数据开始。首先我们准备一个人工数据集: 这是一个.csv格式(用逗号隔开)的数据文件。该数据集有四行三列。其中每行描述了房间数量(“NumRooms”)、巷子类型(“Alley”)和房屋价格(“P......
  • 官网使用conda&pip安装PyTorch命令总结(包含各版本)
    原网页https://pytorch.org/get-started/previous-versions/因为有时访问该网站比较慢,所以本博客记录该网页内容InstallingpreviousversionsofPyTorchWe’dpreferyouinstallthelatestversion,butoldbinariesandinstallationinstructionsareprovidedbelow......
  • 【pytorch】理解张量,了解张量的创建和操作
    深度学习的核心是卷积,卷积的核心是张量(Tensor)理解TensorTensor可以简单理解为是标量、向量、矩阵的高维扩展。你可以把张量看作多维数组,但相较于ndarray,Tensor包含了grad、requires_grad、grad_fn、device等属性,是为服务于神经网络而设计的类型,标量可以看作是零维张量、......
  • 【pytorch】土堆pytorch教程学习(六)神经网络的基本骨架——nn.module的使用
    torch.nn是pytorch的一个神经网络库(nn是neuralnetwork的简称)。Containerstorch.nn构建神经网络的模型容器(Containers,骨架)有以下六个:ModuleSequentialModuleListModuleDictParameterListParameterDict本博文将介绍神经网络的基本骨架——nn.module的使用。......
  • 安装python torch 遇到的问题
    一、解决torch指令安装时Couldnotfindaversionthatsatisfiestherequirementxxx(fromversions:none)1.进入torch的wheel下载网站https://download.pytorch.org/whl/torch_stable.html  先下载和系统对应的whl文件2.pycharm终端进入到whl文件的地址,执行下面的命令......
  • pytorch基础学习.md
    pytorch入门学习来源:https://www.bilibili.com/video/BV1hE411t7RN安装#1.已安装nvidia相关驱动#2.安装python-pytorch-cudansfoxer@ns-pc~/Temp>yay-Qipython-pytorch-cudanumactl基础使用DataSet数据集加载继承DataSet类,并实现get_item_fromtorch.utils......
  • conda 安装pytorch新环境
    1.创建环境condacreate-npytorch1.7.1python=3.72.安装pytorch安装pytorch官网上面安装指令https://pytorch.org/get-started/previous-versions/condainstallpytorch==1.7.1torchvision==0.8.2cudatoolkit=11.0-cpytorch##torchaudio==0.7.2语言的不需要......
  • Pytorch数据操作
    1.Pytorch中tensor的生成与访问可以使用arange()创建一个张量:如,torch.arange(12)创建0开始的前12个整数: 除非特殊指定,否则新的张量将存放在内存中,并采用CPU计算。 可以使用reshape()来改变张量的形状: 注意,reshape()的发起者是一个张量,比如这里的x.reshape(),x是一个张量......
  • PyTorch 1.0 中文文档:torch.utils.data
    译者:BXuan694classtorch.utils.data.Dataset表示数据集的抽象类。所有用到的数据集都必须是其子类。这些子类都必须重写以下方法:__len__:定义了数据集的规模;__getitem__:支持0到len(self)范围内的整数索引。classtorch.utils.data.TensorDataset(*tensors)用于张量封装的Dataset类......
  • PyTorch 1.0 中文文档:torch.utils.cpp_extension
    译者:belonHantorch.utils.cpp_extension.CppExtension(name,sources,*args,**kwargs)创建一个C++的setuptools.Extension。便捷地创建一个setuptools.Extension具有最小(但通常是足够)的参数来构建C++扩展的方法。所有参数都被转发给setuptools.Extension构造函数。例子>>>from......