mindspore.ops.Depend

class mindspore.ops.Depend[源代码]

用来处理操作间的依赖关系。

在大多数情况下,如果操作有作用在IO或内存上的副作用,它们将按照用户的指令依序执行。在某些情况下,如果两个操作A和B没有顺序上的依赖性,而A必须在B之前执行,我们建议使用Depend来指定它们的执行顺序。使用方法如下:

a = A(x)                --->        a = A(x)
b = B(y)                --->        y = Depend(y, a)
                        --->        b = B(y)
输入:
  • value (Tensor) - 应被Depend操作符返回的Tensor。

  • expr (Expression) - 应被执行的无输出的表达式。

输出:

Tensor,作为 value 传入的变量。

支持平台:

Ascend GPU CPU

样例:

>>> import numpy as np
>>> import mindspore
>>> import mindspore.nn as nn
>>> import mindspore.ops as ops
>>> from mindspore import Tensor
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.softmax = ops.Softmax()
...         self.depend = ops.Depend()
...
...     def construct(self, x, y):
...         mul = x * y
...         y = self.depend(y, mul)
...         ret = self.softmax(y)
...         return ret
...
>>> x = Tensor(np.ones([4, 5]), dtype=mindspore.float32)
>>> y = Tensor(np.ones([4, 5]), dtype=mindspore.float32)
>>> net = Net()
>>> output = net(x, y)
>>> print(output)
[[0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]
 [0.2 0.2 0.2 0.2 0.2]]