(self, max_time)
| 2056 | |
| 2057 | @test_util.run_v1_only("b/124229375") |
| 2058 | def _testRawRNN(self, max_time): |
| 2059 | with self.session(graph=ops.Graph()) as sess: |
| 2060 | batch_size = 16 |
| 2061 | input_depth = 4 |
| 2062 | num_units = 3 |
| 2063 | |
| 2064 | inputs = array_ops.placeholder( |
| 2065 | shape=(max_time, batch_size, input_depth), dtype=dtypes.float32) |
| 2066 | sequence_length = array_ops.placeholder( |
| 2067 | shape=(batch_size,), dtype=dtypes.int32) |
| 2068 | inputs_ta = tensor_array_ops.TensorArray( |
| 2069 | dtype=dtypes.float32, size=array_ops.shape(inputs)[0]) |
| 2070 | inputs_ta = inputs_ta.unstack(inputs) |
| 2071 | |
| 2072 | cell = rnn_cell.LSTMCell(num_units, state_is_tuple=True) |
| 2073 | |
| 2074 | def loop_fn(time_, cell_output, cell_state, unused_loop_state): |
| 2075 | emit_output = cell_output # == None for time == 0 |
| 2076 | if cell_output is None: # time == 0 |
| 2077 | next_state = cell.zero_state(batch_size, dtypes.float32) |
| 2078 | else: |
| 2079 | next_state = cell_state # copy state through |
| 2080 | elements_finished = (time_ >= sequence_length) |
| 2081 | finished = math_ops.reduce_all(elements_finished) |
| 2082 | # For the very final iteration, we must emit a dummy input |
| 2083 | next_input = control_flow_ops.cond( |
| 2084 | finished, |
| 2085 | lambda: array_ops.zeros([batch_size, input_depth], dtype=dtypes.float32), |
| 2086 | lambda: inputs_ta.read(time_)) |
| 2087 | return (elements_finished, next_input, next_state, emit_output, None) |
| 2088 | |
| 2089 | reuse_scope = variable_scope.get_variable_scope() |
| 2090 | |
| 2091 | outputs_ta, final_state, _ = rnn.raw_rnn(cell, loop_fn, scope=reuse_scope) |
| 2092 | outputs = outputs_ta.stack() |
| 2093 | |
| 2094 | reuse_scope.reuse_variables() |
| 2095 | outputs_dynamic_rnn, final_state_dynamic_rnn = rnn.dynamic_rnn( |
| 2096 | cell, |
| 2097 | inputs, |
| 2098 | time_major=True, |
| 2099 | dtype=dtypes.float32, |
| 2100 | sequence_length=sequence_length, |
| 2101 | scope=reuse_scope) |
| 2102 | |
| 2103 | variables = variables_lib.trainable_variables() |
| 2104 | gradients = gradients_impl.gradients([outputs, final_state], |
| 2105 | [inputs] + variables) |
| 2106 | gradients_dynamic_rnn = gradients_impl.gradients( |
| 2107 | [outputs_dynamic_rnn, final_state_dynamic_rnn], [inputs] + variables) |
| 2108 | |
| 2109 | variables_lib.global_variables_initializer().run() |
| 2110 | |
| 2111 | rand_input = np.random.randn(max_time, batch_size, input_depth) |
| 2112 | if max_time == 0: |
| 2113 | rand_seq_len = np.zeros(batch_size) |
| 2114 | else: |
| 2115 | rand_seq_len = np.random.randint(max_time, size=batch_size) |
no test coverage detected