def model_fn():
   inputs = constant_op.constant(2 * [2 * [[0.0, 1.0, 2.0, 3.0, 4.0]]])
   cell_fw = rnn_cell_impl.LSTMCell(300)
   cell_bw = rnn_cell_impl.LSTMCell(300)
   (outputs, _) = rnn.bidirectional_dynamic_rnn(
       cell_fw, cell_bw, inputs, dtype=dtypes.float32)
   return outputs
Esempio n. 2
0
    def testDynamicRnn(self):
        input_data = {
            "x":
            constant_op.constant(
                np.array(np.random.random_sample((3, 10, 10)),
                         dtype=np.float32))
        }

        cell = rnn_cell_impl.LSTMCell(10)

        @def_function.function(input_signature=[
            tensor_spec.TensorSpec(shape=[3, 10, 10], dtype=dtypes.float32)
        ])
        def model(x):
            return rnn.dynamic_rnn(cell, x, dtype=dtypes.float32)

        root = tracking.AutoTrackable()
        root.f = model
        input_func = root.f.get_concrete_function()

        output_func = convert_to_constants.convert_variables_to_constants_v2(
            input_func, lower_control_flow=False)
        constant_graph_def = output_func.graph.as_graph_def()
        self.assertEqual(0, self._getNumVariables(constant_graph_def))
        self.assertFalse(
            self._hasStatefulPartitionedCallOp(constant_graph_def))

        self._testConvertedFunction(root, root.f, output_func, input_data)
Esempio n. 3
0
 def testLSTMCell(self):
     with self.test_session() as sess:
         num_units = 8
         num_proj = 6
         state_size = num_units + num_proj
         batch_size = 3
         input_size = 2
         with variable_scope.variable_scope(
                 "root", initializer=init_ops.constant_initializer(0.5)):
             x = array_ops.zeros([batch_size, input_size])
             m = array_ops.zeros([batch_size, state_size])
             cell = rnn_cell_impl.LSTMCell(num_units=num_units,
                                           num_proj=num_proj,
                                           forget_bias=1.0,
                                           state_is_tuple=False)
             output, state = cell(x, m)
             sess.run([variables_lib.global_variables_initializer()])
             res = sess.run(
                 [output, state], {
                     x.name: np.array([[1., 1.], [2., 2.], [3., 3.]]),
                     m.name: 0.1 * np.ones((batch_size, state_size))
                 })
             self.assertEqual(len(res), 2)
             # The numbers in results were not calculated, this is mostly just a
             # smoke test.
             self.assertEqual(res[0].shape, (batch_size, num_proj))
             self.assertEqual(res[1].shape, (batch_size, state_size))
             # Different inputs so different outputs and states
             for i in range(1, batch_size):
                 self.assertTrue(
                     float(np.linalg.norm((res[0][0, :] -
                                           res[0][i, :]))) > 1e-6)
                 self.assertTrue(
                     float(np.linalg.norm((res[1][0, :] -
                                           res[1][i, :]))) > 1e-6)
Esempio n. 4
0
    def testStaticRnn(self):
        input_data = constant_op.constant(
            np.array(np.random.random_sample((3, 10)), dtype=np.float32))

        cell = rnn_cell_impl.LSTMCell(10)

        @def_function.function(input_signature=[
            tensor_spec.TensorSpec(shape=[3, 10], dtype=dtypes.float32)
        ])
        def model(x):
            seq = array_ops.split(x, 3, 0)
            return rnn.static_rnn(cell,
                                  seq,
                                  dtype=dtypes.float32,
                                  sequence_length=[1])

        concrete_func = model.get_concrete_function()

        # Convert model.
        converter = lite.TFLiteConverterV2.from_concrete_functions(
            [concrete_func])
        converter.experimental_new_converter = True
        tflite_model = converter.convert()

        # Check values from converted model.
        expected_value = concrete_func(input_data)[0]
        actual_value = self._evaluateTFLiteModel(tflite_model, [input_data])
        for expected, actual in zip(expected_value, actual_value):
            np.testing.assert_almost_equal(expected.numpy(), actual)
Esempio n. 5
0
    def testDynamicRnn(self):
        input_data = constant_op.constant(
            np.array(np.random.random_sample((3, 10, 10)), dtype=np.float32))

        cell = rnn_cell_impl.LSTMCell(10)

        @def_function.function(input_signature=[
            tensor_spec.TensorSpec(shape=[3, 10, 10], dtype=dtypes.float32)
        ])
        def model(x):
            return rnn.dynamic_rnn(cell, x, dtype=dtypes.float32)

        concrete_func = model.get_concrete_function()

        # Convert model.
        converter = lite.TFLiteConverterV2.from_concrete_functions(
            [concrete_func])
        converter.experimental_new_converter = True
        tflite_model = converter.convert()

        # Check values from converted model.
        expected_value = concrete_func(input_data)
        actual_value = self._evaluateTFLiteModel(tflite_model, [input_data])
        for expected, actual in zip(expected_value, actual_value):
            if isinstance(expected, ops.EagerTensor):
                expected = expected.numpy()
            else:
                expected = expected.c.numpy()
            np.testing.assert_almost_equal(expected, actual)
    def _create_equivalent_canonical_rnn(self,
                                         cudnn_model,
                                         inputs,
                                         use_block_cell,
                                         scope="rnn"):
        if cudnn_model.rnn_mode is not "lstm":
            raise ValueError("%s is not supported!" % cudnn_model.rnn_mode)

        num_units = cudnn_model.num_units
        num_layers = cudnn_model.num_layers

        # To reuse cuDNN-trained models, must set
        # forget_bias, clip_cell = 0, False
        # In LSTMCell and LSTMBlockCell, forget_bias is added in addition to learned
        # bias, whereas cuDNN does not apply the additional bias.
        if use_block_cell:
            # pylint: disable=g-long-lambda
            single_cell = lambda: lstm_ops.LSTMBlockCell(
                num_units, forget_bias=0, clip_cell=False)
            # pylint: enable=g-long-lambda
        else:
            single_cell = lambda: rnn_cell_impl.LSTMCell(num_units,
                                                         forget_bias=0)
        cell = rnn_cell_impl.MultiRNNCell(
            [single_cell() for _ in range(num_layers)])
        return rnn.dynamic_rnn(cell,
                               inputs,
                               dtype=dtypes.float32,
                               time_major=True,
                               scope=scope)
Esempio n. 7
0
  def testRNNCellSerialization(self):
    for cell in [
        rnn_cell_impl.LSTMCell(32, use_peepholes=True, cell_clip=True),
        rnn_cell_impl.BasicLSTMCell(32, dtype=dtypes.float32),
        rnn_cell_impl.BasicRNNCell(32, activation="relu", dtype=dtypes.float32),
        rnn_cell_impl.GRUCell(32, dtype=dtypes.float32)
    ]:
      with self.cached_session():
        x = keras.Input((None, 5))
        layer = keras.layers.RNN(cell)
        y = layer(x)
        model = keras.models.Model(x, y)
        model.compile(optimizer="rmsprop", loss="mse")

        # Test basic case serialization.
        x_np = np.random.random((6, 5, 5))
        y_np = model.predict(x_np)
        weights = model.get_weights()
        config = layer.get_config()
        # The custom_objects is important here since rnn_cell_impl is
        # not visible as a Keras layer, and also has a name conflict with
        # keras.LSTMCell and GRUCell.
        layer = keras.layers.RNN.from_config(
            config,
            custom_objects={
                "BasicRNNCell": rnn_cell_impl.BasicRNNCell,
                "GRUCell": rnn_cell_impl.GRUCell,
                "LSTMCell": rnn_cell_impl.LSTMCell,
                "BasicLSTMCell": rnn_cell_impl.BasicLSTMCell
            })
        y = layer(x)
        model = keras.models.Model(x, y)
        model.set_weights(weights)
        y_np_2 = model.predict(x_np)
        self.assertAllClose(y_np, y_np_2, atol=1e-4)
Esempio n. 8
0
 def _testDropoutWrapper(self, batch_size=None, time_steps=None,
                         parallel_iterations=None, **kwargs):
   with self.test_session() as sess:
     with variable_scope.variable_scope(
         "root", initializer=init_ops.constant_initializer(0.5)):
       if batch_size is None and time_steps is None:
         # 2 time steps, batch size 1, depth 3
         batch_size = 1
         time_steps = 2
         x = constant_op.constant(
             [[[2., 2., 2.]], [[1., 1., 1.]]], dtype=dtypes.float32)
         m = rnn_cell_impl.LSTMStateTuple(
             *[constant_op.constant([[0.1, 0.1, 0.1]], dtype=dtypes.float32)
              ] * 2)
       else:
         x = constant_op.constant(
             np.random.randn(time_steps, batch_size, 3).astype(np.float32))
         m = rnn_cell_impl.LSTMStateTuple(*[
             constant_op.constant(
                 [[0.1, 0.1, 0.1]] * batch_size, dtype=dtypes.float32)
         ] * 2)
       outputs, final_state = rnn.dynamic_rnn(
           cell=rnn_cell_impl.DropoutWrapper(
               rnn_cell_impl.LSTMCell(3), dtype=x.dtype, **kwargs),
           time_major=True,
           parallel_iterations=parallel_iterations,
           inputs=x,
           initial_state=m)
       sess.run([variables_lib.global_variables_initializer()])
       res = sess.run([outputs, final_state])
       self.assertEqual(res[0].shape, (time_steps, batch_size, 3))
       self.assertEqual(res[1].c.shape, (batch_size, 3))
       self.assertEqual(res[1].h.shape, (batch_size, 3))
       return res
Esempio n. 9
0
    def get_rnncell(cell_type, cell_size, keep_prob, num_layer):
        if cell_type == "gru":
            cell = rnn_cell.GRUCell(cell_size)
        else:
            cell = rnn_cell.LSTMCell(cell_size,
                                     use_peepholes=False,
                                     forget_bias=1.0)

        if keep_prob < 1.0:
            cell = rnn_cell.DropoutWrapper(cell, output_keep_prob=keep_prob)

        if num_layer > 1:
            cell = rnn_cell.MultiRNNCell([cell] * num_layer,
                                         state_is_tuple=True)

        return cell
Esempio n. 10
0
def _static_vs_dynamic_rnn_benchmark_dynamic(inputs_t, sequence_length):
  (unused_0, unused_1, input_size) = inputs_t.get_shape().as_list()
  initializer = init_ops.random_uniform_initializer(-0.01, 0.01, seed=127)
  cell = rnn_cell_impl.LSTMCell(
      num_units=input_size,
      use_peepholes=True,
      initializer=initializer,
      state_is_tuple=False)
  outputs, final_state = rnn.dynamic_rnn(
      cell, inputs_t, sequence_length=sequence_length, dtype=dtypes.float32)

  trainable_variables = ops_lib.get_collection(
      ops_lib.GraphKeys.TRAINABLE_VARIABLES)
  gradients = gradients_impl.gradients([outputs, final_state],
                                       trainable_variables)

  return control_flow_ops.group(final_state, outputs, *gradients)
    def testDynamicRnn(self):
        """Test a DynamicRnn containing While loops."""
        input_data = {
            "x":
            constant_op.constant(
                np.array(np.random.random_sample((3, 10, 10)),
                         dtype=np.float32))
        }

        cell = rnn_cell_impl.LSTMCell(10)

        @def_function.function(input_signature=[
            tensor_spec.TensorSpec(shape=[3, 10, 10], dtype=dtypes.float32)
        ])
        def model(x):
            return rnn.dynamic_rnn(cell, x, dtype=dtypes.float32)

        root, output_func = self._freezeModel(model)
        self._testConvertedFunction(root, root.f, output_func, input_data)
Esempio n. 12
0
    def get_rnncell(cell_type, cell_size, keep_prob, num_layer):
        # thanks for this solution from @dimeldo
        cells = []
        for _ in range(num_layer):
            if cell_type == "gru":
                cell = rnn_cell.GRUCell(cell_size)
            else:
                cell = rnn_cell.LSTMCell(cell_size, use_peepholes=False, forget_bias=1.0)

            if keep_prob < 1.0:
                cell = rnn_cell.DropoutWrapper(cell, output_keep_prob=keep_prob)

            cells.append(cell)

        if num_layer > 1:
            cell = rnn_cell.MultiRNNCell(cells, state_is_tuple=True)
        else:
            cell = cells[0]

        return cell
Esempio n. 13
0
  def testStaticRnn(self):
    """Test a StaticRnn containing If ops."""
    input_data = {
        "x":
            constant_op.constant(
                np.array(np.random.random_sample((3, 10)), dtype=np.float32))
    }

    cell = rnn_cell_impl.LSTMCell(10)

    @def_function.function(input_signature=[
        tensor_spec.TensorSpec(shape=[3, 10], dtype=dtypes.float32)
    ])
    def model(x):
      seq = array_ops.split(x, 3, 0)
      return rnn.static_rnn(
          cell, seq, dtype=dtypes.float32, sequence_length=[1])

    root, output_func = self._freezeModel(model)
    self._testConvertedFunction(root, root.f, output_func, input_data)
Esempio n. 14
0
  def testBasicLSTMCellInterchangeWithLSTMCell(self):
    with self.test_session(graph=ops_lib.Graph()) as sess:
      basic_cell = rnn_cell_impl.BasicLSTMCell(1)
      basic_cell(array_ops.ones([1, 1]),
                 state=basic_cell.zero_state(batch_size=1,
                                             dtype=dtypes.float32))
      self.evaluate([v.initializer for v in basic_cell.variables])
      self.evaluate(basic_cell._bias.assign([10.] * 4))
      save = saver.Saver()
      prefix = os.path.join(self.get_temp_dir(), "ckpt")
      save_path = save.save(sess, prefix)

    with self.test_session(graph=ops_lib.Graph()) as sess:
      lstm_cell = rnn_cell_impl.LSTMCell(1, name="basic_lstm_cell")
      lstm_cell(array_ops.ones([1, 1]),
                state=lstm_cell.zero_state(batch_size=1,
                                           dtype=dtypes.float32))
      self.evaluate([v.initializer for v in lstm_cell.variables])
      save = saver.Saver()
      save.restore(sess, save_path)
      self.assertAllEqual([10.] * 4, self.evaluate(lstm_cell._bias))
Esempio n. 15
0
def _concat_state_vs_tuple_state_rnn_benchmark(inputs_list_t, sequence_length,
                                               state_is_tuple):
    (_, input_size) = inputs_list_t[0].get_shape().as_list()
    initializer = init_ops.random_uniform_initializer(-0.01, 0.01, seed=127)
    cell = rnn_cell_impl.LSTMCell(num_units=input_size,
                                  use_peepholes=True,
                                  initializer=initializer,
                                  state_is_tuple=state_is_tuple)
    outputs, final_state = rnn.static_rnn(cell,
                                          inputs_list_t,
                                          sequence_length=sequence_length,
                                          dtype=dtypes.float32)

    final_state = list(final_state) if state_is_tuple else [final_state]

    trainable_variables = ops_lib.get_collection(
        ops_lib.GraphKeys.TRAINABLE_VARIABLES)
    gradients = gradients_impl.gradients(outputs + final_state,
                                         trainable_variables)

    return control_flow_ops.group(*(final_state + gradients + outputs))
Esempio n. 16
0
 def testLSTMCellVariables(self):
     with self.test_session():
         num_units = 8
         num_proj = 6
         state_size = num_units + num_proj
         batch_size = 3
         input_size = 2
         with variable_scope.variable_scope(
                 "root", initializer=init_ops.constant_initializer(0.5)):
             x = array_ops.zeros([batch_size, input_size])
             m = array_ops.zeros([batch_size, state_size])
             cell = rnn_cell_impl.LSTMCell(num_units=num_units,
                                           num_proj=num_proj,
                                           forget_bias=1.0,
                                           state_is_tuple=False)
             cell(x, m)  # Execute to create variables
         variables = variables_lib.global_variables()
         self.assertEquals(variables[0].op.name, "root/lstm_cell/kernel")
         self.assertEquals(variables[1].op.name, "root/lstm_cell/bias")
         self.assertEquals(variables[2].op.name,
                           "root/lstm_cell/projection/kernel")
Esempio n. 17
0
 def testDropoutWrapperV2Build(self):
   cell = rnn_cell_impl.LSTMCell(10)
   wrapper = rnn_cell_impl.DropoutWrapperV2(cell)
   wrapper.build((1,))
   self.assertTrue(cell.built)
Esempio n. 18
0
def _CreateStackedLstmCell(*cell_sizes):
    subcells = [rnn_cell_impl.LSTMCell(cell_size) for cell_size in cell_sizes]
    return rnn_cell_impl.MultiRNNCell(subcells)
Esempio n. 19
0
def RunLSTM(sess,
            num_units,
            input_size,
            batch_size,
            time,
            num_layers=1,
            variable_seq_lengths=False,
            time_major=True,
            dynamic_shape_input=False,
            is_training=True,
            dropout=0.,
            num_dirs=True,
            dtype=dtypes.float32):
  # TODO(jamesqin): add multi-layer tests.
  # TODO(jamesqin): add multi-dir tests
  assert num_layers == 1
  assert num_dirs == 1
  if is_training and not np.isclose(dropout, 0):
    raise ValueError("dropout can not be 0. when test training.")

  # set graph level random seed and numpy random seed.
  random_seed.set_random_seed(0)
  np.random.seed(0)

  shape = ([time, batch_size, input_size]
           if time_major else [batch_size, time, input_size])
  inputs_np = np.random.rand(*shape).astype(dtype.as_numpy_dtype)
  inputs_static = variable_scope.get_variable(
      "inputs", initializer=inputs_np, dtype=dtype)
  inputs_dynamic = array_ops.placeholder(
      dtype, shape=[None, None, None], name="inputs")
  inputs = inputs_dynamic if dynamic_shape_input else inputs_static
  initial_h_op = variable_scope.get_variable(
      "initial_h_op",
      initializer=np.random.rand(batch_size,
                                 num_units).astype(dtype.as_numpy_dtype),
      dtype=dtype)
  initial_c_op = variable_scope.get_variable(
      "initial_c_op",
      initializer=np.random.rand(batch_size,
                                 num_units).astype(dtype.as_numpy_dtype),
      dtype=dtype)

  if variable_seq_lengths:
    lengths_v = np.random.randint(low=1, high=time + 1, size=batch_size)
    lengths_v[0] = time  # make sure the max sequence has 'time' elems
    lengths = ops.convert_to_tensor(lengths_v.astype(np.int32))
  else:
    lengths = None

  initializer = init_ops.random_uniform_initializer(
      -0.01, 0.01, dtype=dtype, seed=19980904)

  with variable_scope.variable_scope("test", initializer=initializer):
    w = variable_scope.get_variable(
        "rnn/lstm_cell/kernel",
        shape=[input_size + num_units, num_units * 4],
        dtype=dtype)
    b = variable_scope.get_variable(
        "rnn/lstm_cell/bias", shape=[num_units * 4], dtype=dtype)

    # canonical lstm. must set forget_bias to 0. to align with cudnn lstm.
    cell = rnn_cell_impl.LSTMCell(num_units, forget_bias=0., reuse=True)
    outputs_op, state_tuple_op = rnn.dynamic_rnn(
        cell,
        inputs_static,
        sequence_length=lengths,
        initial_state=rnn_cell_impl.LSTMStateTuple(
            h=initial_h_op, c=initial_c_op),
        dtype=dtype,
        time_major=time_major,
        scope=None)

  # Convert to cudnn opaque param.
  format_converter = cudnn_rnn_ops.CudnnParamsFormatConverterLSTM(
      num_layers, num_units, input_size)
  opaque_params = format_converter.tf_canonical_to_opaque([w, b])

  cu_initial_h_op = array_ops.expand_dims(
      initial_h_op, axis=(0 if time_major else 1))
  cu_initial_c_op = array_ops.expand_dims(
      initial_c_op, axis=(0 if time_major else 1))
  cu_outputs_op, cu_h_op, cu_c_op = cudnn_rnn_ops._cudnn_rnn(
      inputs,
      cu_initial_h_op,
      cu_initial_c_op,
      opaque_params,
      sequence_lengths=lengths,
      time_major=time_major,
      dropout=dropout,
      is_training=is_training,
      rnn_mode=cudnn_rnn_ops.CUDNN_LSTM)
  # Remove the trivial 1st dimension.
  cu_state_tuple_op = rnn_cell_impl.LSTMStateTuple(
      c=array_ops.squeeze(cu_c_op, axis=0 if time_major else 1),
      h=array_ops.squeeze(cu_h_op, axis=0 if time_major else 1))

  if is_training:
    (inp_grad_op, hgrad_op,
     cgrad_op, wgrad_op, bgrad_op) = gradients_impl.gradients(
         outputs_op, [inputs_static, initial_h_op, initial_c_op, w, b])

    (cu_inp_grad_op, cu_hgrad_op,
     cu_cgrad_op, opaque_grad_op) = gradients_impl.gradients(
         cu_outputs_op,
         [inputs, cu_initial_h_op, cu_initial_c_op, opaque_params])
    # Remove the trivial 1st dimension
    cu_hgrad_op = array_ops.squeeze(cu_hgrad_op, axis=0 if time_major else 1)
    # Remove the trivial 1st dimension
    cu_cgrad_op = array_ops.squeeze(cu_cgrad_op, axis=0 if time_major else 1)

    cu_wgrad_op, cu_bgrad_op = format_converter.opaque_to_tf_canonical(
        opaque_grad_op)
    cu_wgrad_op = cu_wgrad_op[0]
    cu_bgrad_op = cu_bgrad_op[0]
    # cudnn lstm has 2 biases each gate. When converting to tf canonical format,
    # the two biases are summed into one. Thus here bias gradient should be
    # halved when comparing with tf lstm.
    cu_bgrad_op *= 0.5

  init_op = variables.global_variables_initializer()
  sess.run(init_op)

  if is_training:
    outputs, state_tuple, inp_grad, state_grad, wgrad, bgrad = sess.run([
        outputs_op, state_tuple_op, inp_grad_op,
        (hgrad_op, cgrad_op), wgrad_op, bgrad_op
    ])
    (cu_outputs, cu_state_tuple, cu_inp_grad, cu_state_grad, cu_wgrad,
     cu_bgrad) = sess.run(
         [
             cu_outputs_op, cu_state_tuple_op, cu_inp_grad_op,
             (cu_hgrad_op, cu_cgrad_op), cu_wgrad_op, cu_bgrad_op
         ],
         feed_dict={inputs: inputs_np} if dynamic_shape_input else None)

    logging.vlog(1, "outputs: %s" % outputs)
    logging.vlog(1, "cu_outputs: %s" % cu_outputs)
    logging.vlog(1, "state_tuple: %s" % str(state_tuple))
    logging.vlog(1, "cu_state_tuple: %s" % str(cu_state_tuple))
    logging.vlog(1, "inp_grad: %s" % inp_grad)
    logging.vlog(1, "cu_inp_grad: %s" % cu_inp_grad)
    logging.vlog(1, "state_grad: %s" % str(state_grad))
    logging.vlog(1, "cu_state_grad: %s" % str(cu_state_grad))
    logging.vlog(1, "wgrad: %s" % str(wgrad))
    logging.vlog(1, "bgrad: %s" % str(bgrad))
    logging.vlog(1, "cu_wgrad: %s" % str(cu_wgrad))
    logging.vlog(1, "cu_bgrad: %s" % str(cu_bgrad))
    return (outputs, cu_outputs, state_tuple, cu_state_tuple, inp_grad,
            cu_inp_grad, state_grad, cu_state_grad, wgrad, bgrad, cu_wgrad,
            cu_bgrad)
  else:
    outputs, state_tuple = sess.run([outputs_op, state_tuple_op])
    cu_outputs, cu_state_tuple = sess.run([cu_outputs_op, cu_state_tuple_op],
                                          feed_dict=({
                                              inputs: inputs_np
                                          } if dynamic_shape_input else None))

    logging.vlog(1, "outputs: %s" % outputs)
    logging.vlog(1, "cu_outputs: %s" % cu_outputs)
    logging.vlog(1, "state_tuple: %s" % str(state_tuple))
    logging.vlog(1, "cu_state_tuple: %s" % str(cu_state_tuple))
  return outputs, cu_outputs, state_tuple, cu_state_tuple
Esempio n. 20
0
 def __init__(self, name=None):
   super(KerasNetworkTFRNNs, self).__init__(name=name)
   self._cell = rnn_cell_impl.MultiRNNCell(
       [rnn_cell_impl.LSTMCell(1) for _ in range(2)])
    def _testSingleLayerBidirectionalLSTMHelper(self, input_size, num_units,
                                                seq_length, batch_size):
        # Only tests single layer bi-Cudnn LSTM.
        num_layers = 1
        np.random.seed(1234)

        # canonical bidirectional lstm
        param_size = _MinLSTMParamSize(
            num_layers,
            num_units,
            input_size,
            direction=cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION)
        # np data
        input_data = np.random.randn(seq_length, batch_size,
                                     input_size).astype(np.float32)
        input_h = np.zeros(
            (num_layers * 2, batch_size, num_units)).astype(np.float32)
        input_c = np.zeros(
            (num_layers * 2, batch_size, num_units)).astype(np.float32)
        cudnn_params = np.random.randn(param_size).astype(np.float32)

        with ops.Graph().as_default():
            # cudnn bidirectional lstm graph
            cudnn_params_t = variables.Variable(cudnn_params)
            input_data_t = constant_op.constant(input_data,
                                                dtype=dtypes.float32)
            input_h_t = constant_op.constant(input_h, dtype=dtypes.float32)
            input_c_t = constant_op.constant(input_c, dtype=dtypes.float32)

            cudnn_lstm = _CreateModel(
                "lstm",
                num_layers,
                num_units,
                input_size,
                direction=cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION)
            cudnn_output, cudnn_output_h, cudnn_output_c = cudnn_lstm(
                input_data=input_data_t,
                input_h=input_h_t,
                input_c=input_c_t,
                params=cudnn_params_t)

            # canonical bidirectional lstm
            cell_fw = rnn_cell_impl.LSTMCell(num_units, forget_bias=0.)
            cell_bw = rnn_cell_impl.LSTMCell(num_units, forget_bias=0.)
            outputs, output_state_fw, output_state_bw = static_bidirectional_rnn(
                cell_fw,
                cell_bw,
                array_ops.unstack(input_data),
                dtype=dtypes.float32)

            weights_list, biases_list = _TransformBidirectionalCudnnLSTMParams(
                cudnn_lstm, cudnn_params_t)
            assert len(weights_list) == 2
            assert len(biases_list) == 2

            with vs.variable_scope("", reuse=True):
                cell_fw_kernel = vs.get_variable(
                    "bidirectional_rnn/fw/lstm_cell/kernel")
                cell_fw_bias = vs.get_variable(
                    "bidirectional_rnn/fw/lstm_cell/bias")
                cell_bw_kernel = vs.get_variable(
                    "bidirectional_rnn/bw/lstm_cell/kernel")
                cell_bw_bias = vs.get_variable(
                    "bidirectional_rnn/bw/lstm_cell/bias")

            assign_fw_kernel = state_ops.assign(cell_fw_kernel,
                                                weights_list[0])
            assign_fw_bias = state_ops.assign(cell_fw_bias, biases_list[0])

            assign_bw_kernel = state_ops.assign(cell_bw_kernel,
                                                weights_list[1])
            assign_bw_bias = state_ops.assign(cell_bw_bias, biases_list[1])
            assign_ops = control_flow_ops.group(assign_fw_kernel,
                                                assign_fw_bias,
                                                assign_bw_kernel,
                                                assign_bw_bias)

            with self.test_session(use_gpu=True,
                                   graph=ops.get_default_graph()) as sess:
                sess.run(variables.global_variables_initializer())
                cu_out, cu_h, cu_c = sess.run(
                    [cudnn_output, cudnn_output_h, cudnn_output_c])

                sess.run(assign_ops)
                out, fwd_s, bak_s = sess.run(
                    [outputs, output_state_fw, output_state_bw])

                out = np.stack(out)
                fwd_h, fwd_c = fwd_s.h, fwd_s.c
                bak_h, bak_c = bak_s.h, bak_s.c
                h = np.concatenate((fwd_h, bak_h), axis=1)
                c = np.concatenate((fwd_c, bak_c), axis=1)

                cu_h = [np.array(x) for x in cu_h]
                cu_c = [np.array(x) for x in cu_c]

                cu_h = np.concatenate(cu_h, axis=1)
                cu_c = np.concatenate(cu_c, axis=1)

                self.assertAllClose(out, cu_out)
                self.assertAllClose(h, cu_h)
                self.assertAllClose(c, cu_c)
 def testWrapperV2Build(self, wrapper):
   cell = rnn_cell_impl.LSTMCell(10)
   wrapper = wrapper(cell)
   wrapper.build((1,))
   self.assertTrue(cell.built)