def _div(x, y): r"""Computes the division of x by y for each element, scalar and broadcast promotation are supported. The formula is: .. math:: out = \frac{X}{Y} Args: x (Union[int, float, flow.Tensor]): X. y (Union[int, float, flow.Tensor]): Y. For example: .. code-block:: python import oneflow as flow # element-wise divide x = flow.Tensor(np.random.randn(2,3)) y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3) # scalar divide x = 5 y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3) # broadcast divide x = flow.Tensor(np.random.randn(1,1)) y = flow.Tensor(np.random.randn(2,3)) out = flow.div(x,y).numpy() print(out.shape) # (2,3) """ if isinstance(x, (int, float)): return ScalarMul(x)(flow.reciprocal(y)) elif isinstance(y, (int, float)): if y == 0 or y == 0.0: y = 0.0 else: y = 1.0 / (float(y)) return ScalarMul(y)(x) elif x.shape == y.shape: return BroadcastDiv()(x, y) elif y.shape == (1, ): return ScalarDivByTensor()(x, y) else: return BroadcastDiv()(x, y)
def test_reciprocal(test_case): x = flow.Tensor(np.random.randn(2, 3)) of_out = flow.reciprocal(x) np_out = np.reciprocal(x.numpy()) test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-4, 1e-4))
def _reciprocal(self): return flow.reciprocal(self)
def forward(self, x): return flow.reciprocal(x)