Esempio n. 1
0
def switch(condition, then_expression, else_expression):
    logger.debug(
        'switch(condition: {}, then_expression: {}, else_expression: {})'.
        format(condition, then_expression, else_expression))
    return _KerasNode('switch',
                      tensor=edsl.select(condition.tensor,
                                         then_expression.tensor,
                                         else_expression.tensor))
Esempio n. 2
0
def toroidal_shell_integral(n, minval, maxval, eps, benchmark=False):
    coordvals = np.linspace(minval, maxval, n, dtype=np.float32)
    delta = (maxval - minval) / (n - 1)  # grid spacing

    X_data = coordvals.reshape(n, 1, 1)
    Y_data = coordvals.reshape(1, n, 1)
    Z_data = coordvals.reshape(1, 1, n)
    X = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, X_data.shape))
    Y = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, Y_data.shape))
    Z = edsl.Tensor(edsl.LogicalShape(plaidml.DType.FLOAT32, Z_data.shape))

    # f-rep of torodial shell f(x, y, z) = (sqrt(x^2 + y^2) - 1)^2 + z^2 + (0.1)^2
    F = sq(edsl.sqrt(sq(X) + sq(Y)) - 1.0) + sq(Z) - sq(0.1)
    # moment of inertia about z axis at each point g(x, y, z) = x^2 + y^2
    G = sq(X) + sq(Y)

    DFDX = partial(F, 'x', delta)
    DFDY = partial(F, 'y', delta)
    DFDZ = partial(F, 'z', delta)

    # chi: occupancy function: 1 inside the region (f<0), 0 outside the region (f>0)
    DCHIDX = partial_chi(F, 'x', delta)
    DCHIDY = partial_chi(F, 'y', delta)
    DCHIDZ = partial_chi(F, 'z', delta)

    NUMER = DFDX * DCHIDX + DFDY * DCHIDY + DFDZ * DCHIDZ
    DENOM = edsl.sqrt(sq(DFDX) + sq(DFDY) + sq(DFDZ))
    H = edsl.select(DENOM < eps, 0, NUMER / DENOM)
    O = sum(-G * H)

    program = edsl.Program('toroidal_shell_integral', [O])
    binder = plaidml_exec.Binder(program)
    executable = binder.compile()

    def run():
        binder.input(X).copy_from_ndarray(X_data)
        binder.input(Y).copy_from_ndarray(Y_data)
        binder.input(Z).copy_from_ndarray(Z_data)
        executable.run()
        return binder.output(O).as_ndarray()

    if benchmark:
        # the first run will compile and run
        print('compiling...')
        result = run()

        # subsequent runs should not include compile time
        print('running...')
        ITERATIONS = 100
        elapsed = timeit.timeit(run, number=ITERATIONS)
        print('runtime:', elapsed / ITERATIONS)
    else:
        result = run()

    return result * (delta**3)
Esempio n. 3
0
def partial_chi(F, wrt, delta):
    dims = edsl.TensorDims(3)
    x, y, z = edsl.TensorIndexes(3)
    F.bind_dims(*dims)
    DF_left = edsl.TensorOutput(*dims)
    DF_right = edsl.TensorOutput(*dims)

    if wrt == 'x':
        DF_right[x, y, z] = F[x + 1, y, z]
        DF_left[x, y, z] = F[x - 1, y, z]
    elif wrt == 'y':
        DF_right[x, y, z] = F[x, y + 1, z]
        DF_left[x, y, z] = F[x, y - 1, z]
    elif wrt == 'z':
        DF_right[x, y, z] = F[x, y, z + 1]
        DF_left[x, y, z] = F[x, y, z - 1]

    DF_chi_right = edsl.select(DF_right < 0, 1, 0)
    DF_chi_left = edsl.select(DF_left < 0, -1, 0)
    return (DF_chi_right + DF_chi_left) / (2.0 * delta)
Esempio n. 4
0
def dropout(x, level, noise_shape=None, seed=None):
    I = x.tensor
    if noise_shape is not None and len(noise_shape) != I.shape.ndims:
        raise ValueError('noise_shape ndims doesn\'t match input ndims')
    if noise_shape is None:
        shape = I.shape.dims
    else:
        shape = noise_shape
    rng_state = _make_rng_state(seed)
    R = 1.0 - level
    M = 1.0 / R
    T = edsl.prng(rng_state.tensor, shape)
    O = edsl.select(T < R, I * M, 0.0)
    return _KerasNode('dropout', tensor=O)
Esempio n. 5
0
def dropout(x, level, noise_shape=None, seed=None):
    logger.debug('dropout(x: {}, level: {}, noise_shape: {}, seed: {})'.format(
        x, level, noise_shape, seed))
    I = x.tensor
    if noise_shape is not None and len(noise_shape) != I.shape.ndims:
        raise ValueError("noise_shape ndims doesn't match input ndims")
    if noise_shape is None:
        shape = I.shape.dims
    else:
        shape = noise_shape
    rng_state = _make_rng_state(seed)
    R = 1.0 - level
    M = 1.0 / R
    T = edsl.prng(rng_state.tensor, shape)
    O = edsl.select(T < R, I * M, 0.0)
    return _KerasNode('dropout', tensor=O)
Esempio n. 6
0
def switch(condition, then_expression, else_expression):
    return _KerasNode('switch',
                      tensor=edsl.select(condition.tensor, then_expression.tensor,
                                         else_expression.tensor))
Esempio n. 7
0
def sign(x):
    intermediate = _KerasNode('sign_intermediate', tensor=edsl.select((x > 0).tensor, 1., -1.))
    return _KerasNode('sign', tensor=edsl.select((x.tensor == 0.), 0., intermediate.tensor))