mindspore.get_grad
==================

.. image:: https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r2.2/resource/_static/logo_source.svg
    :target: https://gitee.com/mindspore/mindspore/blob/r2.2/docs/api/api_python/mindspore/mindspore.get_grad.rst
    :alt: 查看源文件


.. py:function:: mindspore.get_grad(gradients, identifier)

    当 :func:`mindspore.grad` 的 `return_ids` 参数设置为True时,将其返回值作为 `gradients` 。再根据 `identifier` 在 `gradients` 中找到对应的梯度值。

    根据 `identifier` 查找梯度值包含以下两种场景:

    1. `identifier` 为指定求导输入位置的索引;
    2. `identifier` 为网络变量。

    参数:
        - **gradients** (Union[tuple[int, Tensor], tuple[tuple, tuple]]) - :func:`mindspore.grad` 参数 `return_ids` 为True时的返回值。
        - **identifier** (Union[int, Parameter]) - 指定求导输入位置的索引或者网络变量。

    返回:
        `identifier` 所指定的求导输入位置的索引所对应的Tensor的梯度值,或者网络变量所对应的Tensor的梯度值。

    异常:
        - **RuntimeError** - 无法找到identifier所对应的梯度值。
        - **TypeError** - 入参类型不符合要求。