コード例 #1
0
def one_hot(var, n_classes, dtype=None, name=None):
    """Convert to one-hot encoding.

    Parameters
    ----------
    n_classes : int
        Number of label to encode

    dtype : str
        The dtype of the resulting Tensor. Default to floatX

    name : str
        Name of operation

    Returns
    -------
    Tensor
        Tensor with shape ``(var.shape[0], n_classes)``

    Notes
    -----
    The Tensor must be either vector or 2D matrix
    """
    if not var.n_dim == 1:
        raise ValueError('Tensor must be 1D.')

    _dtype = dtype or luchador.get_nn_dtype()
    _tensor = tf.one_hot(
        var.unwrap(), depth=n_classes, dtype=_dtype, name=name)
    return Tensor(tensor=_tensor, name=name)
コード例 #2
0
ファイル: misc.py プロジェクト: mot0/luchador
def one_hot(var, n_classes, dtype=None, name=None):
    """Implement ``one_hot`` in Tensorflow backend.

    See :func:`luchador.nn.ops.one_hot` for the detail.
    """
    _dtype = dtype or luchador.get_nn_dtype()
    _tensor = tf.one_hot(
        var.unwrap(), depth=n_classes, dtype=_dtype, name=name)
    return Tensor(tensor=_tensor, name=name)
コード例 #3
0
ファイル: math.py プロジェクト: mot0/luchador
    def _build(self, input_tensor):
        dtype = input_tensor.dtype
        tensor = input_tensor.unwrap()
        if 'int' in input_tensor.dtype:
            dtype = luchador.get_nn_dtype()
            tensor = tf.cast(tensor, dtype)

        if self._denom is None:
            self._instantiate_denominator(dtype)

        output = tf.truediv(tensor, self._denom, 'ouptut')
        return Tensor(output, name='output')
コード例 #4
0
ファイル: math.py プロジェクト: mthrok/luchador
    def _build(self, input_tensor):
        dtype = input_tensor.dtype
        tensor = input_tensor.unwrap()
        if 'int' in input_tensor.dtype:
            dtype = luchador.get_nn_dtype()
            tensor = tf.cast(tensor, dtype)

        if self._denom is None:
            self._instantiate_denominator(dtype)

        output = tf.truediv(tensor, self._denom, 'ouptut')
        return Tensor(output, name='output')
コード例 #5
0
def get_variable(
        name, shape=None, dtype=None,
        initializer=None, regularizer=None, trainable=True, **kwargs):
    """Create Variable with the given configuration or retrieve existing one

    This function works mostly same as tf.get_variable, except when retrieving
    existing Variable, you only need name and need not to give shape and dtype.

    Mapping from name to VariableWrapper is internally cached so that you can
    retrieve variable with only name.

    Parameters
    ----------
    name : str
        Name of Variable to create or retrieve

    shape : list
        Used to create new Variable. Ignored when retrieving one

    dtype : str
        Used to create new Variable. Ignored when retrieving one

    initializer : luchador.nn.Initializer or tf.Initializer
        Initializer object

    kwargs
        Other arguments passed to ``tf.get_variable``
        See
        https://www.tensorflow.org/versions/master/api_docs/python/state_ops.html#get_variable
    """
    if isinstance(initializer, base_initializer.BaseInitializer):
        initializer = initializer.unwrap()

    scope = tf.get_variable_scope()
    if scope.reuse:
        name = '{}/{}'.format(scope.name, name) if scope.name else name
        var = base_wrapper.retrieve_variable(name)
        if var is None:
            raise ValueError(
                'Variable {} does not exist, disallowed. '
                'Did you mean to set reuse=None in VarScope?'
                .format(name)
            )
        return var
    else:
        dtype = dtype or luchador.get_nn_dtype()

        variable = tf.get_variable(
            name, shape=shape, dtype=dtype, initializer=initializer,
            regularizer=regularizer, trainable=trainable, **kwargs)

        return Variable(variable, trainable=trainable)
コード例 #6
0
ファイル: wrapper.py プロジェクト: mot0/luchador
def make_variable(name,
                  shape,
                  dtype=None,
                  initializer=None,
                  regularizer=None,
                  trainable=True,
                  **kwargs):
    """Create Variable with the given configuration

    Parameters
    ----------
    name : str
        Name of Variable to create.

    shape : list
        Used to create new Variable.

    dtype : str
        Used to create new Variable.

    initializer : luchador.nn.Initializer
        Initializer object

    kwargs
        Other arguments passed to ``tf.get_variable``
        See
        https://www.tensorflow.org/versions/master/api_docs/python/state_ops.html#get_variable
    """
    dtype = dtype or luchador.get_nn_dtype()

    if not initializer:
        initializer = fetch_initializer('NormalInitializer')(dtype=dtype)

    if isinstance(initializer, BaseInitializer):
        initializer = initializer.unwrap()

    return Variable(tf.get_variable(name,
                                    shape=shape,
                                    dtype=dtype,
                                    initializer=initializer,
                                    regularizer=regularizer,
                                    trainable=trainable,
                                    **kwargs),
                    name=name,
                    trainable=trainable)
コード例 #7
0
ファイル: wrapper.py プロジェクト: mthrok/luchador
    def __init__(self, shape, name=None, dtype=None):
        """Creates Input object which wraps placeholder

        Parameters
        ----------
        shape : list
            The shape of the resulting object.
        name : str
            The name of the resulting object.
        dtype : NumPy dtype or None
            If None, default dtype is used
        """
        _dtype = dtype or luchador.get_nn_dtype()
        tensor = tf.placeholder(dtype=_dtype, shape=shape, name=name)
        name = _prefix_with_scope(name)
        dtype = _get_dtype_str(tensor)
        super(Input, self).__init__(
            tensor=tensor, shape=shape, name=name, dtype=dtype)
コード例 #8
0
    def __init__(self, shape, name=None, dtype=None):
        """Creates Input object which wraps placeholder

        Parameters
        ----------
        shape : list
            The shape of the resulting object.
        name : str
            The name of the resulting object.
        dtype : NumPy dtype or None
            If None, default dtype is used
        """
        _dtype = dtype or luchador.get_nn_dtype()
        tensor = tf.placeholder(dtype=_dtype, shape=shape, name=name)
        name = _prefix_with_scope(name)
        dtype = _get_dtype_str(tensor)
        super(Input, self).__init__(
            tensor=tensor, shape=shape, name=name, dtype=dtype)
コード例 #9
0
def _write_data_to_file(file_, data):
    for key, value in data.items():
        _LG.debug('  Saving: %10s %24s %s', value.dtype, value.shape, key)
        if key in file_:
            del file_[key]

        chunks = None if value.size == 1 else True
        file_.create_dataset(key, data=value, chunks=chunks)

    if 'LUCHADOR_NN_BACKEND' not in file_:
        data = np.string_(luchador.get_nn_backend())
        file_.create_dataset('LUCHADOR_NN_BACKEND', data=data, dtype='S10')
    if 'LUCHADOR_NN_CONV_FORMAT' not in file_:
        data = np.string_(luchador.get_nn_conv_format())
        file_.create_dataset('LUCHADOR_NN_CONV_FORMAT', data=data, dtype='S4')
    if 'LUCHADOR_NN_DTYPE' not in file_:
        data = np.string_(luchador.get_nn_dtype())
        file_.create_dataset('LUCHADOR_NN_DTYPE', data=data, dtype='S10')
    if 'LUCHADOR_VERSION' not in file_:
        data = np.string_(luchador.__version__)
        file_.create_dataset('LUCHADOR_VERSION', data=data)
    file_.flush()
コード例 #10
0
ファイル: saver.py プロジェクト: mthrok/luchador
def _write_data_to_file(file_, data):
    for key, value in data.items():
        _LG.debug('  Saving: %10s %24s %s', value.dtype, value.shape, key)
        if key in file_:
            del file_[key]

        chunks = None if value.size == 1 else True
        file_.create_dataset(key, data=value, chunks=chunks)

    if 'LUCHADOR_NN_BACKEND' not in file_:
        data = np.string_(luchador.get_nn_backend())
        file_.create_dataset('LUCHADOR_NN_BACKEND', data=data, dtype='S10')
    if 'LUCHADOR_NN_CONV_FORMAT' not in file_:
        data = np.string_(luchador.get_nn_conv_format())
        file_.create_dataset('LUCHADOR_NN_CONV_FORMAT', data=data, dtype='S4')
    if 'LUCHADOR_NN_DTYPE' not in file_:
        data = np.string_(luchador.get_nn_dtype())
        file_.create_dataset('LUCHADOR_NN_DTYPE', data=data, dtype='S10')
    if 'LUCHADOR_VERSION' not in file_:
        data = np.string_(luchador.__version__)
        file_.create_dataset('LUCHADOR_VERSION', data=data)
    file_.flush()
コード例 #11
0
ファイル: wrapper.py プロジェクト: mthrok/luchador
def make_variable(
        name, shape, dtype=None,
        initializer=None, regularizer=None, trainable=True, **kwargs):
    """Create Variable with the given configuration

    Parameters
    ----------
    name : str
        Name of Variable to create.

    shape : list
        Used to create new Variable.

    dtype : str
        Used to create new Variable.

    initializer : luchador.nn.Initializer
        Initializer object

    kwargs
        Other arguments passed to ``tf.get_variable``
        See
        https://www.tensorflow.org/versions/master/api_docs/python/state_ops.html#get_variable
    """
    dtype = dtype or luchador.get_nn_dtype()

    if not initializer:
        initializer = fetch_initializer('NormalInitializer')(dtype=dtype)

    if isinstance(initializer, BaseInitializer):
        initializer = initializer.unwrap()

    return Variable(
        tf.get_variable(
            name, shape=shape, dtype=dtype, initializer=initializer,
            regularizer=regularizer, trainable=trainable, **kwargs
        ), name=name, trainable=trainable
    )
コード例 #12
0
 def _get_dtype(self):
     return tf.as_dtype(self.args['dtype'] or luchador.get_nn_dtype())
コード例 #13
0
ファイル: initializer.py プロジェクト: mthrok/luchador
 def _get_dtype(self):
     return tf.as_dtype(self.args['dtype'] or luchador.get_nn_dtype())