Example #1
0
def triangular_matrix_seq(mode: int = 1):
    X = C.placeholder(1)
    ones = C.ones_like(X[0])
    perm_1 = C.layers.Recurrence(C.plus, return_full_state=True)(ones)
    perm_2 = C.layers.Recurrence(C.plus,
                                 go_backwards=True,
                                 return_full_state=True)(ones)

    arr_1 = C.sequence.unpack(perm_1, 0, True)
    arr_2 = C.sequence.unpack(perm_2, 0, True)

    mat = C.times_transpose(arr_1, arr_2)
    mat_c = arr_1 * arr_2

    diagonal_mat = mat - mat_c

    final_mat = diagonal_mat
    if mode == 0:
        final_mat = C.equal(final_mat, 0)
    elif mode == 1:
        final_mat = C.less_equal(final_mat, 0)
    elif mode == 2:
        final_mat = C.less(final_mat, 0)
    elif mode == -1:
        final_mat = C.greater_equal(final_mat, 0)
    elif mode == -2:
        final_mat = C.greater(final_mat, 0)

    result = C.as_block(final_mat, [(X, X)], 'triangular_matrix')

    return C.stop_gradient(result)
Example #2
0
    def _inner(x, y):
        length_x = length(x)
        positions_y = position(y) + 1  # +1 because position starts from zero

        valid_x = C.less_equal(positions_y,
                               C.sequence.broadcast_as(length_x, positions_y))
        padded = C.sequence.scatter(x, valid_x)
        return padded, valid_x
Example #3
0
 def gradFunc(self, arg):
     # create an input variable corresponding the inputs of the forward prop function
     gradIn = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
     # create an input variable for the gradient passed from the next stage
     gradRoot = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
     # first step is to take absolute value of input arg
     signGrad = C.abs(gradIn)
     # then compare its magnitude to 1
     signGrad = C.less_equal(signGrad, 1)
     # finish by multiplying this result with the input gradient
     return C.element_times(gradRoot, signGrad), gradIn, gradRoot
Example #4
0
 def gradFunc(self, arg):
     # create an input variable corresponding the inputs of the forward prop function
     gradIn = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
     # create an input variable for the gradient passed from the next stage
     gradRoot = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
     # first step is to take absolute value of input arg
     signGrad = C.abs(gradIn)
     # then compare its magnitude to 1
     signGrad = C.less_equal(signGrad, 1)
     # finish by multiplying this result with the input gradient
     return C.element_times(gradRoot, signGrad), gradIn, gradRoot
Example #5
0
def true_density(z):
    z1, z2 = z[0], z[1]

    w1 = lambda x: C.sin(2 * np.pi * x/4)
    u = 0.5 * C.square((z2 - w1(z1))/0.4)
    dummy = C.ones_like(u) * 1e7

    # u = C.element_select(C.less_equal(z1,4), u, dummy)
    cond = C.less_equal(z1,4)
    u = C.element_select(cond, u, dummy) # u = cond*u + (1-cond)*dummy

    return C.exp(-u)
    def gradFunc(self, arg):
        # create an input variable corresponding the inputs of the forward prop function
        gradIn = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
        # create an input variable for the gradient passed from the next stage
        gradRoot = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
        signGrad = C.abs(gradIn)
        # new idea, bound of clipping should be a function of the bit map since higher bits can represent higher numbers
        bit_map = C.constant(self.bit_map)
        signGrad = C.less_equal(signGrad, bit_map)
        outGrad = signGrad

        outGrad = element_times(gradRoot, outGrad)

        return outGrad, gradIn, gradRoot
Example #7
0
    def gradFunc(self, arg):
        # create an input variable corresponding the inputs of the forward prop function
        gradIn = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
        # create an input variable for the gradient passed from the next stage
        gradRoot = C.input(shape=arg.shape, dynamic_axes=arg.dynamic_axes)
        signGrad = C.abs(gradIn)
        # new idea, bound of clipping should be a function of the bit map since higher bits can represent higher numbers
        bit_map = C.constant(self.bit_map)
        signGrad = C.less_equal(signGrad, bit_map)
        outGrad = signGrad

        outGrad = element_times(gradRoot, outGrad)
    
        return outGrad, gradIn, gradRoot
Example #8
0
def less_equal(left, right, name=''):
    '''
    Elementwise 'less equal' comparison of two tensors. Result is 1 if left <= right else 0. 

    Example:
        >>> C.eval(C.less_equal([41., 42., 43.], [42., 42., 42.]))
        [array([[1., 1., 0.]])]
        
        >>> C.eval(C.eq([-1,0,1], [0]))
        [array([[1., 1., 0.]])]

    Args:
        left: left side tensor
        right: right side tensor
        name (str): the name of the node in the network            
    Returns:
        :class:`cntk.Function`
    '''
    from cntk import less_equal
    left = sanitize_input(left, get_data_type(right))
    right = sanitize_input(right, get_data_type(left))
    return less_equal(left, right, name).output()
Example #9
0
def less_equal(left, right, name=''):
    '''
    Elementwise 'less equal' comparison of two tensors. Result is 1 if left <= right else 0. 

    Example:
        >>> C.eval(C.less_equal([41., 42., 43.], [42., 42., 42.]))
        [array([[1., 1., 0.]])]
        
        >>> C.eval(C.eq([-1,0,1], [0]))
        [array([[1., 1., 0.]])]

    Args:
        left: left side tensor
        right: right side tensor
        name (str): the name of the node in the network            
    Returns:
        :class:`cntk.Function`
    '''
    from cntk import less_equal
    left = sanitize_input(left, get_data_type(right))
    right = sanitize_input(right, get_data_type(left))
    return less_equal(left, right, name).output()
Example #10
0
import cntk
A = [1, 3, 4]
B = [4, 3, 2]

print("less(A,B):")
less = cntk.less(A, B).eval()
print("{}\n".format(less))

print("equal(A,B):")
equal = cntk.equal(A, B).eval()
print("{}\n".format(equal))

print("greater(A,B)")
greater = cntk.greater(A, B).eval()
print("{}\n".format(greater))

print("greater_equal(A,B):")
greater_equal = cntk.greater_equal(A, B).eval()
print("{}\n".format(greater_equal))

print("not_equal(A,B):")
not_equal = cntk.not_equal(A, B).eval()
print("{}\n".format(not_equal))

print("less_equal(A,B):")
less_equal = cntk.less_equal(A, B).eval()
print("{}\n".format(less_equal))
Example #11
0
 def inner(a):
     p = position(a)
     integers = p / s  # every s sequence item will be an integer
     valid = C.less_equal(C.abs(C.sin(integers * pi)), tol)  # sin of integer multiple of pi will return close to zero
     result = C.sequence.gather(a, valid)
     return result