Пример #1
0
def stabilize(operand):
    scalar_constant = 4.0
    f = Constant.scalar(sanitize_dtype_cntk(np.float32), scalar_constant);
    fInv = Constant.scalar(sanitize_dtype_cntk(np.float32), 1.0 / scalar_constant)

    beta = element_times(fInv, log(Constant.scalar(sanitize_dtype_cntk(np.float32), 1.0) + exp(element_times(f, parameter(shape=(), value=0.99537863)))))
    return element_times(beta, operand)
Пример #2
0
def stabilize(operand):
    scalar_constant = 4.0
    f = constant(sanitize_dtype_cntk(np.float32), scalar_constant)
    fInv = constant(sanitize_dtype_cntk(np.float32), 1.0 / scalar_constant)

    beta = element_times(fInv, log(constant(sanitize_dtype_cntk(
        np.float32), 1.0) + exp(element_times(f, parameter(init=0.99537863)))))
    return element_times(beta, operand)
Пример #3
0
Файл: nn.py Проект: hahatt/CNTK
def stabilize(operand):
    scalar_constant = 4.0
    f = constant(sanitize_dtype_cntk(np.float32), scalar_constant)
    fInv = constant(sanitize_dtype_cntk(np.float32), 1.0 / scalar_constant)

    beta = element_times(fInv, log(constant(sanitize_dtype_cntk(
        np.float32), 1.0) + exp(element_times(f, parameter(init=0.99537863)))))
    return element_times(beta, operand)
Пример #4
0
def test_op_dropout(shape, dropout_rate, device_id, precision):
    from cntk import dropout

    count = 10
    resulted_non_zeros = 0

    # As the dropout node is stochastic, we run it a couple times and aggregate
    # over the results to get more stable tests.
    for i in range(count):
        value = np.ones(shape=shape, dtype=PRECISION_TO_TYPE[precision])

        a = I(shape=value.shape,
              dtype=sanitize_dtype_cntk(PRECISION_TO_TYPE[precision]),
              needs_gradient=True,
              name='a')

        dropout_node = dropout(a, dropout_rate=dropout_rate)

        value.shape = (1, 1) + value.shape
        forward_input = {a: value}

        forward, backward = cntk_eval(dropout_node,
                                      forward_input,
                                      precision,
                                      cntk_device(device_id),
                                      backward_pass=True)

        resulted_non_zeros += np.count_nonzero(forward[dropout_node.output])

    resulted_non_zeros /= count
    num_elements = np.multiply.reduce(shape)
    expected_non_zeros = num_elements * (1 - dropout_rate)
    max_off = 0.2 * num_elements

    assert (abs(resulted_non_zeros - expected_non_zeros) < max_off)
Пример #5
0
def test_op_dropout(shape, dropout_rate, device_id, precision):
    from cntk import dropout
    from cntk.utils import eval, sanitize_dtype_cntk, cntk_device

    count = 10
    resulted_non_zeros = 0

    # As the dropout node is stochastic, we run it a couple times and aggregate
    # over the results to get more stable tests.
    for i in range(count):
        value = np.ones(shape=shape, dtype=PRECISION_TO_TYPE[precision])

        a = I(
            shape=value.shape,
            data_type=sanitize_dtype_cntk(PRECISION_TO_TYPE[precision]),
            needs_gradient=True,
            name="a",
        )

        dropout_node = dropout(a, dropout_rate=dropout_rate)

        value.shape = (1, 1) + value.shape
        forward_input = {a: value}

        forward, backward = eval(dropout_node, forward_input, precision, cntk_device(device_id), backward_pass=True)

        resulted_non_zeros += np.count_nonzero(forward[dropout_node.output])

    resulted_non_zeros /= count
    num_elements = np.multiply.reduce(shape)
    expected_non_zeros = num_elements * (1 - dropout_rate)
    max_off = 0.2 * num_elements

    assert abs(resulted_non_zeros - expected_non_zeros) < max_off
Пример #6
0
    def __init__(self, shape=None, dtype=None, needs_gradient=False, is_sparse=False,
                 dynamic_axes=[cntk_py.Axis.default_dynamic_axis(), cntk_py.Axis.default_batch_axis()], name=''):
        shape = utils.sanitize_shape(shape)

        if dtype is None:
            dtype = np.float32
        dtype = utils.sanitize_dtype_cntk(dtype)

        super(Variable, self).__init__(shape, is_sparse, dtype, needs_gradient, name,
                         dynamic_axes)
Пример #7
0
def load_model(data_type, filename, device=None):
    '''
    Load the network in `model_file`, that has been saved using
    `:func:save_model`.

    Args:
        data_type ('float' or 'double', or NumPy type): data type of the operation
        filename (`str`): filename to load the model from
        device (:class:`cntk.DeviceDescriptor`, default to default device): instance of DeviceDescriptor

    Returns:
        root node
    '''
    from cntk.utils import sanitize_dtype_cntk
    data_type = sanitize_dtype_cntk(data_type)
    if not device:
        device = cntk_py.DeviceDescriptor.use_default_device()
    return cntk_py.load_legacy_model(data_type, filename)
Пример #8
0
    def __init__(self, shape=None, init=None, data_type=None, device=None, name=""):

        if data_type is None:
            if not isinstance(init, np.ndarray):
                data_type = FLOAT_32
            else:
                data_type = str(init.dtype)

        if init is None:
            init = 0

        if isinstance(init, (np.ndarray, list, float, int)):
            ndav = _sanitize_value(shape, init, data_type, device)
            super(Parameter, self).__init__(ndav, name)
        else:
            shape = utils.sanitize_shape(shape)
            data_type = utils.sanitize_dtype_cntk(data_type)
            super(Parameter, self).__init__(shape, data_type, init, device, name)
Пример #9
0
def load_model(data_type, filename, device=None):
    '''
    Load the network in `model_file`, that has been saved using
    `:func:save_model`.

    Args:
        data_type ('float' or 'double', or NumPy type): data type of the operation
        filename (`str`): filename to load the model from
        device (:class:`cntk.DeviceDescriptor`, default to default device): instance of DeviceDescriptor

    Returns:
        root node
    '''
    from cntk.utils import sanitize_dtype_cntk
    data_type = sanitize_dtype_cntk(data_type)
    if not device:
        device = use_default_device()
    return cntk_py.load_legacy_model(data_type, filename)
Пример #10
0
def _sanitize_value(shape, value, dtype, device):
    np_dtype = utils.sanitize_dtype_numpy(dtype)
    cntk_dtype = utils.sanitize_dtype_cntk(dtype)

    if value is None:
        if shape is None:
            raise ValueError('you need to specify at least shape or value')
        shape = utils.sanitize_shape(shape)
        ndav = utils.create_NDArrayView(shape, cntk_dtype, device)
    else:
        if not isinstance(value, np.ndarray) or value.dtype != np_dtype:
            if np.isscalar(value) and shape:
                value = np.full(shape, value, dtype=np_dtype)
            else:
                value = np.asarray(value, dtype=np_dtype)

        ndav = utils.create_NDArrayView_from_NumPy(value, device)

    return ndav
Пример #11
0
def _sanitize_value(shape, value, dtype, device):
    np_dtype = utils.sanitize_dtype_numpy(dtype)
    cntk_dtype = utils.sanitize_dtype_cntk(dtype)

    if value is None:
        if shape is None:
            raise ValueError("you need to specify at least shape or value")
        shape = utils.sanitize_shape(shape)
        ndav = utils.create_NDArrayView(shape, cntk_dtype, device)
    else:
        if not isinstance(value, np.ndarray) or value.dtype != np_dtype:
            if np.isscalar(value) and shape:
                value = np.full(shape, value, dtype=np_dtype)
            else:
                value = np.asarray(value, dtype=np_dtype)

        ndav = utils.create_NDArrayView_from_NumPy(value, device)

    return ndav
Пример #12
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 = utils.sanitize_shape(shape)
            cntk_dtype = utils.sanitize_dtype_cntk(dtype)
            super(Parameter, self).__init__(shape, cntk_dtype, init,
                    device, name)
Пример #13
0
def load_model(filename, dtype=np.float32, device=None):
    '''
    Load the network in ``filename``, that has been saved using
    `:func:save_model`.

    Args:
        filename (`str`): filename to load the model from
        dtype ('float', 'double', or NumPy type, default ``np.float32``): data
         type of the operation
        device (:class:`cntk.DeviceDescriptor`, default is the default device):
         instance of DeviceDescriptor

    Returns:
        root node
    '''
    from cntk.utils import sanitize_dtype_cntk
    dtype = sanitize_dtype_cntk(dtype)
    if not device:
        device = use_default_device()
    return cntk_py.Function.load_model(dtype, filename, device)
Пример #14
0
def load_model(filename, dtype=np.float32, device=None):
    '''
    Load the network in ``filename``, that has been saved using
    `:func:save_model`.

    Args:
        filename (`str`): filename to load the model from
        dtype ('float', 'double', or NumPy type, default ``np.float32``): data
         type of the operation
        device (:class:`cntk.DeviceDescriptor`, default is the default device):
         instance of DeviceDescriptor

    Returns:
        root node
    '''
    from cntk.utils import sanitize_dtype_cntk
    dtype = sanitize_dtype_cntk(dtype)
    if not device:
        device = use_default_device()
    return cntk_py.Function.load_model(dtype, filename, device)
Пример #15
0
    def __init__(self,
                 shape=None,
                 init=None,
                 data_type=None,
                 device=None,
                 name=''):

        if data_type is None:
            if not isinstance(init, np.ndarray):
                data_type = FLOAT_32
            else:
                data_type = str(init.dtype)

        if init is None:
            init = 0

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