Example #1
0
def one_hot(node, shape, one_hot_axis, name=None):  # type: (Node, TensorShape, int, str) -> Node
    """Create node performing one-hot encoding on input data.

    :param node: The input node providing data for operation.
    :param shape: The output node shape including the new one-hot axis.
    :param one_hot_axis: The index within the output shape of the new one-hot axis.
    :param name: The optional name for new output node.
    :return: New node performing one-hot operation.
    """
    return OneHot(node, Shape(shape), one_hot_axis)
Example #2
0
def test_onehot():

    element_type = Type.f32
    A = Parameter(element_type, Shape([3]))
    parameter_list = [A]
    function = Function(NodeVector([OneHot(A, Shape([3, 3]), 0)]), parameter_list, 'test')
    backend = Backend.create(pytest.config.getoption('backend'))

    a = backend.create_tensor(element_type, Shape([3]))
    result = backend.create_tensor(element_type, Shape([3, 3]))

    a.write(util.numpy_to_c(np.array([1, 0, 2], dtype=np.float32)), 0, 12)

    result_arr = np.zeros((3, 3), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 36)
    backend.call(backend.compile(function), [result], [a])
    result.read(util.numpy_to_c(result_arr), 0, 36)

    a_arr = np.array([1, 0, 2])
    result_arr_ref = np.eye(3)[a_arr]

    assert np.allclose(result_arr, result_arr_ref)
Example #3
0
def test_onehot():

    element_type = Type.f32
    A = Parameter(element_type, Shape([3]))
    parameter_list = [A]
    function = Function(NodeVector([OneHot(A, Shape([3, 3]), 0)]), parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    a = backend.make_primary_tensor_view(element_type, Shape([3]))
    result = backend.make_primary_tensor_view(element_type, Shape([3, 3]))

    a.write(util.numpy_to_c(np.array([1, 0, 2], dtype=np.float32)), 0, 12)

    result_arr = np.zeros((3, 3), dtype=np.float32)
    result.write(util.numpy_to_c(result_arr), 0, 36)
    cf.call([a], [result])
    result.read(util.numpy_to_c(result_arr), 0, 36)

    a_arr = np.array([1, 0, 2])
    result_arr_ref = np.eye(3)[a_arr]

    assert np.allclose(result_arr, result_arr_ref)
Example #4
0
def test_onehot():

    element_type = Type.i32
    A = Parameter(element_type, Shape([3]))
    parameter_list = [A]
    function = Function([OneHot(A, Shape([3, 3]), 0)], parameter_list, 'test')
    backend = Backend.create(test.BACKEND_NAME)

    a = backend.create_tensor(element_type, Shape([3]))
    result = backend.create_tensor(element_type, Shape([3, 3]))

    a.write(util.numpy_to_c(np.array([1, 0, 2], dtype=np.int32)), 12)

    result_arr = np.zeros((3, 3), dtype=np.int32)
    result.write(util.numpy_to_c(result_arr), 36)
    handle = backend.compile(function)
    handle.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 36)

    a_arr = np.array([1, 0, 2])
    result_arr_ref = np.eye(3)[a_arr]

    assert np.allclose(result_arr, result_arr_ref)
Example #5
0
from ngraph.impl import Node, Shape, AxisVector, AxisSet
from ngraph.impl.op import Parameter, Maximum, Reshape, Dot, Broadcast
from ngraph.impl.op import Constant, Exp, Log, Sum
from ngraph.impl.op import Greater, Convert, Reduce
from ngraph.impl.op import OneHot

from typing import List, Dict, Set

float_element_type = Type.f32
int_element_type = Type.i32
bz = 53
lr = 0.2

Input = Parameter(float_element_type, Shape([bz, 28, 28]))
Label = Parameter(int_element_type, Shape([bz]))
LabelOneHot = Convert((OneHot(Label, Shape([bz, 10]), 1)), float_element_type)

MaxParam1 = Parameter(float_element_type, Shape([]))
MaxParam2 = Parameter(float_element_type, Shape([]))
MaxFn = Function(Maximum(MaxParam1, MaxParam2), [MaxParam1, MaxParam2],
                 'mnist')


def make_scalar_constant(elem_type, scalar, shape=None, axis_set=None):
    # type: (int, float, List[int], Set[int]) -> float
    """Create a Constant node for scalar value."""
    if shape is None:
        shape = Shape([])
    if axis_set is None:
        axis_set = AxisSet(set())
    scalar_shape = Shape([])  # type: List[int]