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)
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)
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')
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')
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)
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)
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)
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)
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()
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()
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 )
def _get_dtype(self): return tf.as_dtype(self.args['dtype'] or luchador.get_nn_dtype())
def _get_dtype(self): return tf.as_dtype(self.args['dtype'] or luchador.get_nn_dtype())