Exemplo n.º 1
0
    def __init__(self,
                 value=None,
                 shape=None,
                 dtype=None,
                 device=None,
                 name=''):

        if not device:
            device = use_default_device()

        if (np.isscalar(value) or isinstance(value, np.ndarray)) and not shape:
            shape = ()

        if dtype is not None:
            if isinstance(value, np.ndarray) and dtype != value.dtype:
                value = np.array(value, dtype=dtype)
        else:
            if isinstance(value, np.ndarray):
                dtype = value.dtype
            else:
                dtype = np.float32

        if np.isscalar(value):
            super(Constant, self).__init__(sanitize_shape(shape),
                                           sanitize_dtype_cntk(dtype), value,
                                           device, name)
        else:
            ndav = sanitize_value(shape, value, dtype, device)
            super(Constant, self).__init__(ndav, name)
Exemplo n.º 2
0
    def __init__(self,
                 shape=None,
                 init=None,
                 dtype=None,
                 device=None,
                 name=''):
        if not device:
            device = use_default_device()

        if dtype is not None:
            if isinstance(init, np.ndarray) and dtype != init.dtype:
                init = np.array(init, dtype=dtype)
        else:
            if np.isscalar(init) and not shape:
                shape = ()
            if isinstance(init, np.ndarray):
                dtype = init.dtype
            else:
                dtype = np.float32

        if init is None:
            init = 0

        if isinstance(init, (np.ndarray, list, float, int)):
            ndav = sanitize_value(shape, init, dtype, device)
            super(Parameter, self).__init__(ndav, name)
        else:
            shape = sanitize_shape(shape)
            cntk_dtype = sanitize_dtype_cntk(dtype)
            super(Parameter, self).__init__(shape, cntk_dtype, init, device,
                                            name)
Exemplo n.º 3
0
    def __init__(self, value=None, shape=None, dtype=None, device=None, name=''):

        if dtype is None:
            if isinstance(value, np.ndarray):
                dtype = value.dtype
            else:
                dtype = np.float32

        if device is None:
            device = DeviceDescriptor.use_default_device()

        if np.isscalar(value):
            super(Constant, self).__init__(sanitize_shape(shape),
                    sanitize_dtype_cntk(dtype), value, device, name)
        else:
            ndav = sanitize_value(shape, value, dtype, device)
            super(Constant, self).__init__(ndav, name)
def test_numpy_conversion():
    from cntk.internal import sanitize_value
    from ..cntk_py import Value

    # check NDArrayView
    ndav = sanitize_value((2, 3), 1, np.float32, None)
    assert np.all(ndav.asarray() == np.ones((2, 3)))

    # check Value
    assert np.all(Value(ndav).asarray() == np.ones((2, 3)))

    # check Constant
    c = C.constant(1, shape=(2, 3))
    assert np.all(c.asarray() == np.ones((2, 3)))

    #check Parameter
    p = C.parameter(shape=(2, 3), init=1)
    assert np.all(p.asarray() == np.ones((2, 3)))
Exemplo n.º 5
0
def test_numpy_conversion():
    from cntk.internal import sanitize_value
    from ..cntk_py import Value

    # check NDArrayView
    ndav = sanitize_value((2,3), 1, np.float32, None)
    assert np.all(ndav.asarray() == np.ones((2,3)))

    # check Value
    assert np.all(Value(ndav).asarray() == np.ones((2,3)))

    # check Constant
    c = C.constant(1, shape=(2,3))
    assert np.all(c.asarray() == np.ones((2,3)))
    
    #check Parameter
    p = C.parameter(shape=(2,3), init=1)
    assert np.all(p.asarray() == np.ones((2,3)))
Exemplo n.º 6
0
    def __init__(self, shape=None, init=None, dtype=None,
                 device=None, name=''):

        if dtype is None:
            if isinstance(init, np.ndarray):
                dtype = init.dtype
            else:
                dtype = np.float32

        if init is None:
            init = 0

        if isinstance(init, (np.ndarray, list, float, int)):
            ndav = sanitize_value(shape, init, dtype, device)
            super(Parameter, self).__init__(ndav, name)
        else:
            shape = sanitize_shape(shape)
            cntk_dtype = sanitize_dtype_cntk(dtype)
            super(Parameter, self).__init__(shape, cntk_dtype, init,
                    device, name)
Exemplo n.º 7
0
    def __init__(self,
                 value=None,
                 shape=None,
                 dtype=None,
                 device=None,
                 name=''):

        if dtype is None:
            if isinstance(value, np.ndarray):
                dtype = value.dtype
            else:
                dtype = np.float32

        if device is None:
            device = DeviceDescriptor.use_default_device()

        if np.isscalar(value):
            super(Constant, self).__init__(sanitize_shape(shape),
                                           sanitize_dtype_cntk(dtype), value,
                                           device, name)
        else:
            ndav = sanitize_value(shape, value, dtype, device)
            super(Constant, self).__init__(ndav, name)
Exemplo n.º 8
0
    def __init__(self,
                 shape=None,
                 init=None,
                 dtype=None,
                 device=None,
                 name=''):

        if dtype is None:
            if isinstance(init, np.ndarray):
                dtype = init.dtype
            else:
                dtype = np.float32

        if init is None:
            init = 0

        if isinstance(init, (np.ndarray, list, float, int)):
            ndav = sanitize_value(shape, init, dtype, device)
            super(Parameter, self).__init__(ndav, name)
        else:
            shape = sanitize_shape(shape)
            cntk_dtype = sanitize_dtype_cntk(dtype)
            super(Parameter, self).__init__(shape, cntk_dtype, init, device,
                                            name)