Example #1
0
    def _RunLSTMLayer(self, basename, init_weights, m_init_scalar,
                      c_init_scalar, pad_scalar):
        with self.test_session() as sess:
            num_inputs = 1
            num_nodes = 1
            seq_length = 3

            weights = init_weights(
                lstm.LSTMCellWeightsShape(num_inputs, num_nodes))
            m_init = constant_op.constant([[m_init_scalar]] * self._batch_size)
            c_init = constant_op.constant([[c_init_scalar]] * self._batch_size)
            x_seq = [constant_op.constant(self._inputs)] * seq_length
            pad_seq = [
                constant_op.constant([[pad_scalar]] * self._batch_size)
            ] * seq_length

            out_seq = lstm.LSTMLayer('lstm', weights, m_init, c_init, x_seq,
                                     pad_seq)
            _DumpGraph(
                sess.graph, 'lstm_layer_%s_%d_%d_%d' %
                (basename, m_init_scalar, c_init_scalar, pad_scalar))

            # Initialize variables and run the unrolled LSTM layer.
            sess.run(variables.global_variables_initializer())
            return sess.run(out_seq)
Example #2
0
  def testLSTMLayerErrors(self):
    num_inputs = 1
    num_nodes = 1
    seq_length = 3

    weights = array_ops.zeros(lstm.LSTMCellWeightsShape(num_inputs, num_nodes))
    m = constant_op.constant([[0.]] * self._batch_size)
    c = constant_op.constant([[0.]] * self._batch_size)
    x_seq = [constant_op.constant(self._inputs)] * seq_length
    pad = constant_op.constant([[0.]] * self._batch_size)

    with self.assertRaisesWithPredicateMatch(ValueError, 'length of x_seq'):
      lstm.LSTMLayer('lstm', weights, m, c, x_seq, [pad])
    with self.assertRaisesWithPredicateMatch(ValueError, 'length of x_seq'):
      lstm.LSTMLayer('lstm', weights, m, c, x_seq, [pad] * 2)
    with self.assertRaisesWithPredicateMatch(ValueError, 'length of x_seq'):
      lstm.LSTMLayer('lstm', weights, m, c, x_seq, [pad] * 4)
Example #3
0
  def _RunLSTMCell(self, basename, init_weights, m_prev_scalar, c_prev_scalar,
                   pad_scalar):
    with self.cached_session() as sess:
      num_inputs = 1
      num_nodes = 1

      weights = init_weights(lstm.LSTMCellWeightsShape(num_inputs, num_nodes))
      m_prev = constant_op.constant([[m_prev_scalar]] * self._batch_size)
      c_prev = constant_op.constant([[c_prev_scalar]] * self._batch_size)
      x = constant_op.constant(self._inputs)
      pad = constant_op.constant([[pad_scalar]] * self._batch_size)

      m, c = lstm.LSTMCell(weights, m_prev, c_prev, x, pad)
      _DumpGraph(sess.graph, 'lstm_cell_%s_%d_%d_%d' %
                 (basename, m_prev_scalar, c_prev_scalar, pad_scalar))

      # Initialize variables and run the unrolled LSTM step.
      self.evaluate(variables.global_variables_initializer())
      return self.evaluate([m, c])