def reciprocal(x, name=''): ''' Computes the element-wise reciprocal of `x`: Example: >>> C.eval(C.abs([-1/3, 1/5, -2, 3])) [array([[ -3., 5., -1/2., 1/3.]])] Args: x: numpy array or any :class:`cntk.Function` that outputs a tensor name (str): the name of the node in the network Returns: :class:`cntk.Function` ''' from cntk import reciprocal x = sanitize_input(x) return reciprocal(x, name).output()
def reciprocal(x, name=''): ''' Computes the element-wise reciprocal of `x`: Example: >>> C.eval(C.abs([-1/3, 1/5, -2, 3])) [array([[ -3., 5., -1/2., 1/3.]])] Args: x: numpy array or any :class:`cntk.Function` that outputs a tensor name (str): the name of the node in the network Returns: :class:`cntk.Function` ''' from cntk import reciprocal x = sanitize_input(x) return reciprocal(x, name).output()
def gaussian_mdn_phi(target, mu, sigma, ndim: int): """ Calculates phi between the target tensor and the network prediction Does not assumes independence between components of target. Arguments: target: target tensor with shape (ndim, ) mu: means of gaussian mdn with shape (nmix, ndim) sigma: sigma of gaussian mdn nmix (int): number of mixtures ndim (int): number of dimensions in gaussian Returns: :class:`~cntk.ops.functions.Function` """ if not len(mu.shape) == 2: raise ValueError("mu {0} must have shape (nmix, ndim)".format(mu.shape)) t = C.expand_dims(target, axis=0) exp_term = C.exp(C.negate(C.square(C.reduce_l2(t - mu, axis=-1)) / (2 * C.square(sigma)))) factor = C.reciprocal((2 * pi) ** (ndim / 2) * C.pow(sigma, ndim)) return factor * exp_term
def test_Reciprocal(tmpdir): model = C.reciprocal([-1 / 3, 1 / 5, -2, 3]) verify_no_input(model, tmpdir, 'Reciprocal_0')
def test_Reciprocal(tmpdir, dtype): with C.default_options(dtype=dtype): model = C.reciprocal(np.array([-1 / 3, 1 / 5, -2, 3]).astype(dtype)) verify_no_input(model, tmpdir, 'Reciprocal_0')
def test_Reciprocal(tmpdir, dtype): with C.default_options(dtype = dtype): model = C.reciprocal(np.array([-1/3, 1/5, -2, 3]).astype(dtype)) verify_no_input(model, tmpdir, 'Reciprocal_0')
def test_reciprocal(): assert_cntk_ngraph_isclose(C.reciprocal([-1 / 3, 1 / 5, -2, 3])) assert_cntk_ngraph_isclose(C.reciprocal([[-1, 0.5], [-3, 4]])) assert_cntk_ngraph_isclose( C.reciprocal([[[1, 0.5], [-3, 0.33]], [[1, -2], [3, 4]]]))
def test_Reciprocal(tmpdir): model = C.reciprocal([-1/3, 1/5, -2, 3]) verify_no_input(model, tmpdir, 'Reciprocal_0')