def constant(x, name=None, shape=None, dtype=None): """Initialize a tensor with constant value. If dtype is ``None``, use ``config.floatX``. Parameters ---------- x : basic numerical type The constant value. name : str or None The name of Tensor. shape : list, tuple or None The shape of Tensor. dtype : str or None The data type of Tensor. Returns ------- Tensor The initialized tensor. """ if dtype is None: dtype = config.floatX else: if dtype not in _DATA_TYPES.keys(): raise TypeError("Unsupported data type: {}".format(dtype)) if shape is None: shape = () np_value = x * np.ones(shape, dtype=_DATA_TYPES[dtype]) output = Tensor(name=name, shape=shape, dtype=dtype) output.set_value(np_value) return output
def zeros(shape, dtype=None): """Initialize a tensor with zeros. If dtype is ``None``, use ``config.floatX``. Parameters ---------- shape : tuple or list The shape of Tensor. dtype : str or None The data type of Tensor. Returns ------- Tensor The initialized tensor. """ if dtype is None: dtype = config.floatX else: if dtype not in _DATA_TYPES.keys(): raise TypeError("Unsupported data type: {}".format(dtype)) np_value = np.zeros(shape, dtype=_DATA_TYPES[dtype]) output = Tensor(shape=shape, dtype=dtype) output.set_value(np_value) return output
def ones(shape, dtype=None): """Initialize a tensor with ones. If dtype is ``None``, use ``config.floatX``. Parameters ---------- shape : tuple or list The shape of Tensor. dtype : str or None The data type of Tensor. Returns ------- Tensor The initialized tensor. """ if dtype is None: dtype = config.floatX else: if dtype not in _DATA_TYPES.keys(): raise TypeError("Unsupported data type: {}".format(dtype)) np_value = np.ones(shape, dtype=_DATA_TYPES[dtype]) output = Tensor(shape=shape, dtype=dtype) output.set_value(np_value) return output
def convert_to_tensor(value, dtype=None, name=None, **kwargs): """Converts the given value to a Tensor. Parameters ---------- value : basic type, list or numpy.ndarray The value to convert. dtype : Dtype or None The data type. If ``None``, inferred from the type of `value`. name : str or None The Optional name. Returns ------- Tensor The output tensor. """ if dtype is not None: if not isinstance(dtype, str): if isinstance(dtype, dtypes.DType): dtype = dtype.name else: raise ValueError('The dtype should be a str of a tf.Dtype.') tensor = Tensor(name=name, dtype=dtype) tensor.set_value(value) return tensor
def WrapConstants(constants, dtype='float32'): if not isinstance(constants, Tensor): if not isinstance(constants, np.ndarray): constants = np.array(constants, dtype=dtype) tensor = Tensor() tensor.set_value(constants) tensor.shape = constants.shape constants = tensor return constants
def _set_param(self, layer_id, param_id, param_type, param): if not isinstance(param, Tensor): if isinstance(param, np.ndarray): paramT = Tensor('/tmp/rnn_param').Variable() paramT.set_value(param) param = paramT else: raise ValueError('Excepted a tensor or numpy array.') self.weights.expressions = dict() # Clear cached expressions outputs = RNNParamSet([self.weights, param], layer_id, param_id, param_type, rnn_mode=self.mode, input_size=self.input_size, hidden_size=self.hidden_size, num_layers=self.num_layers, num_directions=self.num_directions) for k, v in outputs.expressions.items(): dg.workspace.RunOperator(v)