mindspore.get_grad ================== .. image:: https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg :target: https://gitee.com/mindspore/mindspore/blob/master/docs/api/api_python/mindspore/mindspore.get_grad.rst :alt: 查看源文件 .. py:function:: mindspore.get_grad(gradients, identifier) 当 :func:`mindspore.grad` 或 :func:`mindspore.value_and_grad` 的 `return_ids` 参数设置为 ``True`` 时,将 `mindspore.grad` 的返回值,或 `mindspore.value_and_grad` 的第二个返回值作为 `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]) - 指定求导输入位置的索引或者在 :func:`mindspore.grad` 中被使用的网络变量。 返回: `identifier` 所指定的求导输入位置的索引所对应的Tensor的梯度值,或者网络变量所对应的Tensor的梯度值。 异常: - **RuntimeError** - 无法找到identifier所对应的梯度值。 - **TypeError** - 入参类型不符合要求。