def uniform(name, shape, scale=0.05, dtype=tf.sg_floatx, summary=True): r"""Creates a tensor variable of which initial values are random numbers based on uniform distribution. Note that the default value of `scale` (=0.05) is different from the min/max values (=0.0, 1.0) of tf.random_uniform_initializer. Args: name: The name of the new variable. shape: A tuple/list of integers or an integer. If shape is an integer, it's converted to a list. scale: A Python scalar. All initial values should be in range `[-scale, scale)`. Default is .05. dtype: The data type. Only floating point types are supported. Default is float32. summary: If True, add this constant to tensor board summary. Returns: A `Variable`. """ shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.random_uniform_initializer( minval=-scale, maxval=scale)) # add summary if not tf.get_variable_scope().reuse and summary: tf.sg_summary_param(x) return x
def orthogonal(name, shape, scale=1.1, dtype=tf.sg_floatx, summary=True): r"""Creates a tensor variable of which initial values are of an orthogonal ndarray. See [Saxe et al. 2014.](http://arxiv.org/pdf/1312.6120.pdf) Args: name: The name of new variable. shape: A tuple/list of integers. scale: A Python scalar. dtype: Either float32 or float64. summary: If True, add this constant to tensor board summary. Returns: A `Variable`. """ flat_shape = (shape[0], np.prod(shape[1:])) a = np.random.normal(0.0, 1.0, flat_shape) u, _, v = np.linalg.svd(a, full_matrices=False) # pick the one with the correct shape q = u if u.shape == flat_shape else v q = q.reshape(shape) # create variable x = tf.get_variable(name, initializer=tf.constant(scale * q[:shape[0], :shape[1]], dtype=dtype)) # add summary if not tf.get_variable_scope().reuse and summary: tf.sg_summary_param(x) return x
def external(name, value, dtype=tf.sg_floatx, summary=True): r"""Creates a tensor variable of which initial values are `value`. For example, ``` external("external", [3,3,1,2]) => [3. 3. 1. 2.] ``` Args: name: The name of new variable. value: A constant value (or list) of output type `dtype`. dtype: The type of the elements of the resulting tensor. summary: If True, add this constant to tensor board summary. Returns: A `Variable`. Has the same contents as `value` of `dtype`. """ # create variable x = tf.get_variable(name, initializer=tf.constant(value, dtype=dtype)) # add summary if not tf.get_variable_scope().reuse and summary: tf.sg_summary_param(x) return x
def constant(name, shape, value=0, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True): r"""Creates a tensor variable of which initial values are `value` and shape is `shape`. Args: name: The name of new variable. shape: A tuple/list of integers or an integer. If shape is an integer, it is converted to a list. value: A Python scalar. All elements of the initialized variable will be set to this value. Default is 0. dtype: The data type. Only floating point types are supported. Default is float32. summary: If True, add this constant to tensor board summary. regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization trainable: If True, add this constant to trainable collection. Default is True. Returns: A `Variable`. """ shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.constant_initializer(value), regularizer=regularizer, trainable=trainable) # add summary if summary: tf.sg_summary_param(x) return x
def identity(name, dim, scale=1, dtype=tf.sg_floatx, summary=True): r"""Creates a tensor variable of which initial values are of an identity matrix. Note that the default value of `scale` (=0.05) is different from the min/max values (=0.0, 1.0) of tf.random_uniform_initializer. For example, ``` identity("identity", 3, 2) => [[2. 0. 0.] [0. 2. 0.] [0. 0. 2.]] ``` Args: name: The name of new variable. dim: An int. The size of the first and second dimension of the output tensor. scale: A Python scalar. The value on the diagonal. dtype: The type of the elements of the resulting tensor. summary: If True, add this constant to tensor board summary. Returns: A 2-D `Variable`. """ x = tf.get_variable(name, initializer=tf.constant(np.eye(dim) * scale, dtype=dtype)) # add summary if not tf.get_variable_scope().reuse and summary: tf.sg_summary_param(x) return x
def external(name, value, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True): r"""Creates a tensor variable of which initial values are `value`. For example, ``` external("external", [3,3,1,2]) => [3. 3. 1. 2.] ``` Args: name: The name of new variable. value: A constant value (or list) of output type `dtype`. dtype: The type of the elements of the resulting tensor. summary: If True, add this constant to tensor board summary. regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization trainable: If True, add this constant to trainable collection. Default is True. Returns: A `Variable`. Has the same contents as `value` of `dtype`. """ # create variable x = tf.get_variable(name, initializer=tf.constant(value, dtype=dtype), regularizer=regularizer, trainable=trainable) # add summary if summary: tf.sg_summary_param(x) return x
def uniform(name, shape, scale=0.05, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True): r"""Creates a tensor variable of which initial values are random numbers based on uniform distribution. Note that the default value of `scale` (=0.05) is different from the min/max values (=0.0, 1.0) of tf.random_uniform_initializer. Args: name: The name of the new variable. shape: A tuple/list of integers or an integer. If shape is an integer, it's converted to a list. scale: A Python scalar. All initial values should be in range `[-scale, scale)`. Default is .05. dtype: The data type. Only floating point types are supported. Default is float32. summary: If True, add this constant to tensor board summary. regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization trainable: If True, add this constant to trainable collection. Default is True. Returns: A `Variable`. """ shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.random_uniform_initializer(minval=-scale, maxval=scale), regularizer=regularizer, trainable=trainable) # add summary if summary: tf.sg_summary_param(x) return x
def orthogonal(name, shape, scale=1.1, dtype=tf.sg_floatx): r"""Returns a random orthogonal initializer. See Saxe et al. 2014 `http://arxiv.org/pdf/1312.6120.pdf` Args: name: A string. The name of the new or existing variable. shape: A list or tuple of integers. scale: A Python scalr. dtype = A float32 or float64. Returns: A `Tensor` variable. """ flat_shape = (shape[0], np.prod(shape[1:])) a = np.random.normal(0.0, 1.0, flat_shape) u, _, v = np.linalg.svd(a, full_matrices=False) # pick the one with the correct shape q = u if u.shape == flat_shape else v q = q.reshape(shape) # create variable x = tf.get_variable(name, initializer=tf.constant(scale * q[:shape[0], :shape[1]], dtype=dtype)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def orthogonal(name, shape, scale=1.1, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True): r"""Creates a tensor variable of which initial values are of an orthogonal ndarray. See [Saxe et al. 2014.](http://arxiv.org/pdf/1312.6120.pdf) Args: name: The name of new variable. shape: A tuple/list of integers. scale: A Python scalar. dtype: Either float32 or float64. summary: If True, add this constant to tensor board summary. regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization trainable: If True, add this constant to trainable collection. Default is True. Returns: A `Variable`. """ flat_shape = (shape[0], np.prod(shape[1:])) a = np.random.normal(0.0, 1.0, flat_shape) u, _, v = np.linalg.svd(a, full_matrices=False) # pick the one with the correct shape q = u if u.shape == flat_shape else v q = q.reshape(shape) # create variable x = tf.get_variable(name, initializer=tf.constant(scale * q[:shape[0], :shape[1]], dtype=dtype), regularizer=regularizer, trainable=trainable) # add summary if summary: tf.sg_summary_param(x) return x
def identity(name, dim, scale=1, dtype=tf.sg_floatx): x = tf.get_variable(name, initializer=tf.constant(np.eye(dim) * scale, dtype=dtype)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def constant(name, shape, value=0, dtype=tf.sg_floatx, summary=True): r"""Creates a tensor variable of which initial values are `value` and shape is `shape`. Args: name: The name of new variable. shape: A tuple/list of integers or an integer. If shape is an integer, it is converted to a list. value: A Python scalar. All elements of the initialized variable will be set to this value. Default is 0. dtype: The data type. Only floating point types are supported. Default is float32. summary: If True, add this constant to tensor board summary. Returns: A `Variable`. """ shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.constant_initializer(value)) # add summary if not tf.get_variable_scope().reuse and summary: tf.sg_summary_param(x) return x
def uniform(name, shape, scale=0.05, dtype=tf.sg_floatx): shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.random_uniform_initializer(minval=-scale, maxval=scale)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def external(name, value, dtype=tf.sg_floatx): # create variable x = tf.get_variable(name, initializer=tf.constant(value, dtype=dtype)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def constant(name, shape, value=0, dtype=tf.sg_floatx): shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.constant_initializer(value)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def constant(name, shape, value=0, dtype=tf.sg_floatx): r"""Returns an initializer of `shape` with all elements set to a scalar `value`. """ shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.constant_initializer(value)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def init_custom_embeddings(name, embeddings_matrix, summary=True, trainable=False): """ Initializes the embedding vector with custom preloaded embeddings """ embedding = np.array(embeddings_matrix) emb = tf.get_variable(name, shape=embedding.shape, initializer=tf.constant_initializer(embedding), trainable=trainable) if summary: tf.sg_summary_param(emb) return emb
def init_custom_embeddings(name, embeddings_matrix, summary=True, trainable=False): """ Initializes the embedding vector with custom preloaded embeddings """ embedding = np.array(embeddings_matrix) emb = tf.get_variable(name, shape=embedding.shape, initializer=tf.constant_initializer(embedding), trainable=trainable) if summary: tf.sg_summary_param(emb) return emb
def uniform(name, shape, scale=0.05, dtype=tf.sg_floatx): r"""Returns an initializer of random numbers based on uniform distribution. Note that the default value of `scale` (=0.05) is different from the min/max values (=0.0, 1.0) of tf.random_uniform_initializer. """ shape = shape if isinstance(shape, (tuple, list)) else [shape] x = tf.get_variable(name, shape, dtype=dtype, initializer=tf.random_uniform_initializer( minval=-scale, maxval=scale)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def orthogonal(name, shape, scale=1.1, dtype=tf.sg_floatx): # Sax et aE. ( http://arxiv.org/pdf/1312.6120.pdf ) flat_shape = (shape[0], np.prod(shape[1:])) a = np.random.normal(0.0, 1.0, flat_shape) u, _, v = np.linalg.svd(a, full_matrices=False) # pick the one with the correct shape q = u if u.shape == flat_shape else v q = q.reshape(shape) # create variable x = tf.get_variable(name, initializer=tf.constant(scale * q[:shape[0], :shape[1]], dtype=dtype)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def external(name, value, dtype=tf.sg_floatx): r"""Returns an initializer of `value`. Args: name: A string. The name of the new or existing variable. value: A constant value (or array) of output type `dtype`. dtype: The type of the elements of the resulting tensor. (optional) Returns: A `Tensor` variable. """ # create variable x = tf.get_variable(name, initializer=tf.constant(value, dtype=dtype)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x
def identity(name, dim, scale=1, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True): r"""Creates a tensor variable of which initial values are of an identity matrix. Note that the default value of `scale` (=0.05) is different from the min/max values (=0.0, 1.0) of tf.random_uniform_initializer. For example, ``` identity("identity", 3, 2) => [[2. 0. 0.] [0. 2. 0.] [0. 0. 2.]] ``` Args: name: The name of new variable. dim: An int. The size of the first and second dimension of the output tensor. scale: A Python scalar. The value on the diagonal. dtype: The type of the elements of the resulting tensor. summary: If True, add this constant to tensor board summary. regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization trainable: If True, add this constant to trainable collection. Default is True. Returns: A 2-D `Variable`. """ x = tf.get_variable(name, initializer=tf.constant(np.eye(dim) * scale, dtype=dtype), regularizer=regularizer, trainable=trainable) # add summary if summary: tf.sg_summary_param(x) return x
def identity(name, dim, scale=1, dtype=tf.sg_floatx): r"""Returns an initializer of a 2-D identity tensor. Args: name: A string. The name of the new or existing variable. dim: An int. The size of the first and second dimension of the output tensor scale: An int (optional). The value on the diagonal. shape: Shape of the new or existing variable. dtype: A tensor datatype. Returns: A 2-D tensor variable with the value of `scale` on the diagoanl and zeros elsewhere. """ x = tf.get_variable(name, initializer=tf.constant(np.eye(dim) * scale, dtype=dtype)) # add summary if not tf.get_variable_scope().reuse: tf.sg_summary_param(x) return x