python类constant_initializer()的实例源码

grid_rnn_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def testGridRNNEdgeCasesLikeRelu(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          'root', initializer=init_ops.constant_initializer(0.5)):
        x = array_ops.zeros([3, 2])
        m = array_ops.zeros([0, 0])

        # this is equivalent to relu
        cell = grid_rnn_cell.GridRNNCell(
            num_units=2,
            num_dims=1,
            input_dims=0,
            output_dims=0,
            non_recurrent_dims=0,
            non_recurrent_fn=nn_ops.relu)
        g, s = cell(x, m)
        self.assertEqual(g.get_shape(), (3, 2))
        self.assertEqual(s.get_shape(), (0, 0))

        sess.run([variables.global_variables_initializer()])
        res = sess.run([g, s], {x: np.array([[1., -1.], [-2, 1], [2, -1]])})
        self.assertEqual(res[0].shape, (3, 2))
        self.assertEqual(res[1].shape, (0, 0))
        self.assertAllClose(res[0], [[0, 0], [0, 0], [0.5, 0.5]])
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def testRNNDecoder(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        inp = [constant_op.constant(0.5, shape=[2, 2])] * 2
        _, enc_state = core_rnn.static_rnn(
            core_rnn_cell_impl.GRUCell(2), inp, dtype=dtypes.float32)
        dec_inp = [constant_op.constant(0.4, shape=[2, 2])] * 3
        cell = core_rnn_cell_impl.OutputProjectionWrapper(
            core_rnn_cell_impl.GRUCell(2), 4)
        dec, mem = seq2seq_lib.rnn_decoder(dec_inp, enc_state, cell)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 4), res[0].shape)

        res = sess.run([mem])
        self.assertEqual((2, 2), res[0].shape)
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def testBasicRNNSeq2Seq(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        inp = [constant_op.constant(0.5, shape=[2, 2])] * 2
        dec_inp = [constant_op.constant(0.4, shape=[2, 2])] * 3
        cell = core_rnn_cell_impl.OutputProjectionWrapper(
            core_rnn_cell_impl.GRUCell(2), 4)
        dec, mem = seq2seq_lib.basic_rnn_seq2seq(inp, dec_inp, cell)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 4), res[0].shape)

        res = sess.run([mem])
        self.assertEqual((2, 2), res[0].shape)
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def testTiedRNNSeq2Seq(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        inp = [constant_op.constant(0.5, shape=[2, 2])] * 2
        dec_inp = [constant_op.constant(0.4, shape=[2, 2])] * 3
        cell = core_rnn_cell_impl.OutputProjectionWrapper(
            core_rnn_cell_impl.GRUCell(2), 4)
        dec, mem = seq2seq_lib.tied_rnn_seq2seq(inp, dec_inp, cell)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 4), res[0].shape)

        res = sess.run([mem])
        self.assertEqual(1, len(res))
        self.assertEqual((2, 2), res[0].shape)
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def testEmbeddingRNNDecoder(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        inp = [constant_op.constant(0.5, shape=[2, 2])] * 2
        cell = core_rnn_cell_impl.BasicLSTMCell(2, state_is_tuple=True)
        _, enc_state = core_rnn.static_rnn(cell, inp, dtype=dtypes.float32)
        dec_inp = [
            constant_op.constant(
                i, dtypes.int32, shape=[2]) for i in range(3)
        ]
        dec, mem = seq2seq_lib.embedding_rnn_decoder(
            dec_inp, enc_state, cell, num_symbols=4, embedding_size=2)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 2), res[0].shape)

        res = sess.run([mem])
        self.assertEqual(1, len(res))
        self.assertEqual((2, 2), res[0].c.shape)
        self.assertEqual((2, 2), res[0].h.shape)
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def testAttentionDecoder2(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        cell = core_rnn_cell_impl.GRUCell(2)
        inp = [constant_op.constant(0.5, shape=[2, 2])] * 2
        enc_outputs, enc_state = core_rnn.static_rnn(
            cell, inp, dtype=dtypes.float32)
        attn_states = array_ops.concat([
            array_ops.reshape(e, [-1, 1, cell.output_size]) for e in enc_outputs
        ], 1)
        dec_inp = [constant_op.constant(0.4, shape=[2, 2])] * 3
        dec, mem = seq2seq_lib.attention_decoder(
            dec_inp, enc_state, attn_states, cell, output_size=4, num_heads=2)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 4), res[0].shape)

        res = sess.run([mem])
        self.assertEqual((2, 2), res[0].shape)
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def testDynamicAttentionDecoder1(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        cell = core_rnn_cell_impl.GRUCell(2)
        inp = constant_op.constant(0.5, shape=[2, 2, 2])
        enc_outputs, enc_state = rnn.dynamic_rnn(
            cell, inp, dtype=dtypes.float32)
        attn_states = enc_outputs
        dec_inp = [constant_op.constant(0.4, shape=[2, 2])] * 3
        dec, mem = seq2seq_lib.attention_decoder(
            dec_inp, enc_state, attn_states, cell, output_size=4)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 4), res[0].shape)

        res = sess.run([mem])
        self.assertEqual((2, 2), res[0].shape)
seq2seq_test.py 文件源码 项目:DeepLearning_VirtualReality_BigData_Project 作者: rashmitripathi 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def testDynamicAttentionDecoder2(self):
    with self.test_session() as sess:
      with variable_scope.variable_scope(
          "root", initializer=init_ops.constant_initializer(0.5)):
        cell = core_rnn_cell_impl.GRUCell(2)
        inp = constant_op.constant(0.5, shape=[2, 2, 2])
        enc_outputs, enc_state = rnn.dynamic_rnn(
            cell, inp, dtype=dtypes.float32)
        attn_states = enc_outputs
        dec_inp = [constant_op.constant(0.4, shape=[2, 2])] * 3
        dec, mem = seq2seq_lib.attention_decoder(
            dec_inp, enc_state, attn_states, cell, output_size=4, num_heads=2)
        sess.run([variables.global_variables_initializer()])
        res = sess.run(dec)
        self.assertEqual(3, len(res))
        self.assertEqual((2, 4), res[0].shape)

        res = sess.run([mem])
        self.assertEqual((2, 2), res[0].shape)
module_cell.py 文件源码 项目:dnnQuery 作者: richardxiong 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def call(self, inputs, state):
    """Gated recurrent unit (GRU) with nunits cells."""
    with vs.variable_scope("gates"):  # Reset gate and update gate.
      # We start with bias of 1.0 to not reset and not update.
      bias_ones = self._bias_initializer
      if self._bias_initializer is None:
        dtype = [a.dtype for a in [inputs, state]][0]
        bias_ones = init_ops.constant_initializer(1.0, dtype=dtype)
      value = math_ops.sigmoid(
          _linear([inputs, state], 2 * self._num_units, True, bias_ones,
                  self._kernel_initializer))
      r, u = array_ops.split(value=value, num_or_size_splits=2, axis=1)
    with vs.variable_scope("candidate"):
      c = self._activation(
          _linear([inputs, r * state], self._num_units, True,
                  self._bias_initializer, self._kernel_initializer))
    new_h = u * state + (1 - u) * c
    return new_h, new_h
rnn.py 文件源码 项目:seq2seq 作者: eske 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def call(self, inputs, state):
        inputs = tf.concat(inputs, axis=1)
        input_size = inputs.shape[1]
        state_size = state.shape[1]
        dtype = inputs.dtype

        with tf.variable_scope("gates"):
            bias_initializer = self._bias_initializer
            if self._bias_initializer is None and not self._layer_norm:  # bias of 1 for layer norm?
                bias_initializer = init_ops.constant_initializer(1.0, dtype=dtype)

            bias = tf.get_variable('bias', [2 * self._num_units], dtype=dtype, initializer=bias_initializer)
            weights = tf.get_variable('kernel', [input_size + state_size, 2 * self._num_units], dtype=dtype,
                                      initializer=self._kernel_initializer)

            inputs_ = tf.matmul(inputs, weights[:input_size])
            state_ = tf.matmul(state, weights[input_size:])

            if self._layer_norm:
                inputs_ = tf.contrib.layers.layer_norm(inputs_, scope='inputs')
                state_ = tf.contrib.layers.layer_norm(state_, scope='state')

            value = tf.nn.sigmoid(inputs_ + state_ + bias)
            r, u = tf.split(value=value, num_or_size_splits=2, axis=1)

        with tf.variable_scope("candidate"):
            bias = tf.get_variable('bias', [self._num_units], dtype=dtype, initializer=self._bias_initializer)
            weights = tf.get_variable('kernel', [input_size + state_size, self._num_units], dtype=dtype,
                                      initializer=self._kernel_initializer)

            c = tf.matmul(tf.concat([inputs, r * state], axis=1), weights)

            if self._layer_norm:
                c = tf.contrib.layers.layer_norm(c)

            c = self._activation(c + bias)

        new_h = u * state + (1 - u) * c
        return new_h, new_h
model.py 文件源码 项目:merlin 作者: CSTR-Edinburgh 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def layer_normalization(inputs,epsilon = 1e-5,scope=None):
    mean,var=tf.nn.moments(inputs,[1],keep_dims=True)
    with tf.variable_scope(scope+"LN",reuse=None):
          scale=tf.get_variable(name="scale",shape=[inputs.get_shape()[1]],initializer=tf.constant_initializer(1))
          shift=tf.get_variable(name="shift",shape=[inputs.get_shape()[1]],initializer=tf.constant_initializer(0))
    LN_output=scale*(inputs-mean)/tf.sqrt(var + epsilon) + shift
    return LN_output
alexnet_model.py 文件源码 项目:tensorflow_face 作者: ZhihengCV 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def alexnet_v2_arg_scope(weight_decay=0.0005):
    with arg_scope(
            [layers.conv2d, layers_lib.fully_connected],
            activation_fn=nn_ops.relu,
            biases_initializer=init_ops.constant_initializer(0.1),
            weights_regularizer=regularizers.l2_regularizer(weight_decay)):
        with arg_scope([layers.conv2d], padding='SAME'):
            with arg_scope([layers_lib.max_pool2d], padding='VALID') as arg_sc:
                return arg_sc
ops.py 文件源码 项目:mann-for-speech-separation 作者: KWTsou1220 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def get_biases(name, shape, value, trainable = True):
    return tf.get_variable('biases{}'.format(name), shape,
                           initializer = tf.constant_initializer(value),
                           trainable = trainable)
ops.py 文件源码 项目:mann-for-speech-separation 作者: KWTsou1220 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _bias_variable(shape, name='biases'):
    initializer = tf.constant_initializer(0.1)
    return tf.get_variable(name=name, shape=shape, initializer=initializer)
seq_labeling.py 文件源码 项目:joint-slu-lm 作者: HadoopIt 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def linear_transformation(_X, input_size, n_class):
    with variable_scope.variable_scope("linear"):
      bias_start = 0.0
      weight_out = variable_scope.get_variable("Weight_out", [input_size, n_class])         
      bias_out = variable_scope.get_variable("Bias_out", [n_class],
                                                  initializer=init_ops.constant_initializer(bias_start))

      output = tf.matmul(_X, weight_out) + bias_out
      #regularizers = tf.nn.l2_loss(weight_hidden) + tf.nn.l2_loss(bias_hidden) + tf.nn.l2_loss(weight_out) + tf.nn.l2_loss(bias_out) 
    return output
tf-keras-skeleton.py 文件源码 项目:LIE 作者: EmbraceLife 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def zeros(shape, dtype=None, name=None):
      """Instantiates an all-zeros variable and returns it.

      Arguments:
          shape: Tuple of integers, shape of returned Keras variable
          dtype: String, data type of returned Keras variable
          name: String, name of returned Keras variable

      Returns:
          A variable (including Keras metadata), filled with `0.0`.

      Example:
      ```python
          >>> from keras import backend as K
          >>> kvar = K.zeros((3,4))
          >>> K.eval(kvar)
          array([[ 0.,  0.,  0.,  0.],
                 [ 0.,  0.,  0.,  0.],
                 [ 0.,  0.,  0.,  0.]], dtype=float32)
"""
  if dtype is None:
    dtype = floatx()
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
  return variable(
      init_ops.constant_initializer(0., dtype=tf_dtype)(shape), dtype, name)

```

tf-keras-skeleton.py 文件源码 项目:LIE 作者: EmbraceLife 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def ones(shape, dtype=None, name=None):
      """Instantiates an all-ones tensor variable and returns it.

      Arguments:
          shape: Tuple of integers, shape of returned Keras variable.
          dtype: String, data type of returned Keras variable.
          name: String, name of returned Keras variable.

      Returns:
          A Keras variable, filled with `1.0`.

      Example:
      ```python
          >>> from keras import backend as K
          >>> kvar = K.ones((3,4))
          >>> K.eval(kvar)
          array([[ 1.,  1.,  1.,  1.],
                 [ 1.,  1.,  1.,  1.],
                 [ 1.,  1.,  1.,  1.]], dtype=float32)
"""
  if dtype is None:
    dtype = floatx()
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
  return variable(
      init_ops.constant_initializer(1., dtype=tf_dtype)(shape), dtype, name)

```

rnn_cell.py 文件源码 项目:lsdc 作者: febert 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _norm(self, inp, scope):
    with vs.variable_scope(scope) as scope:
      shape = inp.get_shape()[-1:]
      gamma_init = init_ops.constant_initializer(self._g)
      beta_init = init_ops.constant_initializer(self._b)
      gamma = vs.get_variable("gamma", shape=shape, initializer=gamma_init)  # pylint: disable=unused-variable
      beta = vs.get_variable("beta", shape=shape, initializer=beta_init)  # pylint: disable=unused-variable
      normalized = layers.layer_norm(inp, reuse=True, scope=scope)
      return normalized
gru_ops.py 文件源码 项目:lsdc 作者: febert 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __call__(self, x, h_prev, scope=None):
    """GRU cell."""
    with vs.variable_scope(scope or type(self).__name__):
      input_size = x.get_shape().with_rank(2)[1]

      # Check if the input size exist.
      if input_size is None:
        raise ValueError("Expecting input_size to be set.")

      # Check cell_size == state_size from h_prev.
      cell_size = h_prev.get_shape().with_rank(2)[1]
      if cell_size != self._cell_size:
        raise ValueError("Shape of h_prev[1] incorrect: cell_size %i vs %s" %
                         (self._cell_size, cell_size))

      if cell_size is None:
        raise ValueError("cell_size from `h_prev` should not be None.")

      w_ru = vs.get_variable("w_ru", [input_size + self._cell_size,
                                      self._cell_size * 2])
      b_ru = vs.get_variable(
          "b_ru", [self._cell_size * 2],
          initializer=init_ops.constant_initializer(1.0))
      w_c = vs.get_variable("w_c",
                            [input_size + self._cell_size, self._cell_size])
      b_c = vs.get_variable(
          "b_c", [self._cell_size],
          initializer=init_ops.constant_initializer(0.0))

      _gru_block_cell = _gru_ops_so.gru_block_cell  # pylint: disable=invalid-name
      _, _, _, new_h = _gru_block_cell(
          x=x, h_prev=h_prev, w_ru=w_ru, w_c=w_c, b_ru=b_ru, b_c=b_c)

      return new_h, new_h
lstm_ops.py 文件源码 项目:lsdc 作者: febert 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def __call__(self, x, states_prev, scope=None):
    """Long short-term memory cell (LSTM)."""
    with vs.variable_scope(scope or type(self).__name__):
      x_shape = x.get_shape().with_rank(2)
      if not x_shape[1]:
        raise ValueError("Expecting x_shape[1] to be sets: %s" % str(x_shape))
      if len(states_prev) != 2:
        raise ValueError("Expecting states_prev to be a tuple with length 2.")
      input_size = x_shape[1]
      w = vs.get_variable("W", [input_size + self._num_units,
                                self._num_units * 4])
      b = vs.get_variable("b", [w.get_shape().with_rank(2)[1]],
                          initializer=init_ops.constant_initializer(0.0))
      wci = vs.get_variable("wci", [self._num_units])
      wco = vs.get_variable("wco", [self._num_units])
      wcf = vs.get_variable("wcf", [self._num_units])
      (cs_prev, h_prev) = states_prev
      (_, cs, _, _, _, _, h) = _lstm_block_cell(x,
                                                cs_prev,
                                                h_prev,
                                                w,
                                                b,
                                                wci=wci,
                                                wco=wco,
                                                wcf=wcf,
                                                forget_bias=self._forget_bias,
                                                use_peephole=self._use_peephole)

      return (h, (cs, h))


问题


面经


文章

微信
公众号

扫码关注公众号