Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras: Simplify recurrent state updates #26216

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions tensorflow/python/keras/layers/convolutional_recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ def step(inputs, states):
mask=mask,
input_length=timesteps)
if self.stateful:
updates = []
for i in range(len(states)):
updates.append(K.update(self.states[i], states[i]))
updates = [
K.update(self_state, state)
for self_state, state in zip(self.states, states)]
self.add_update(updates)

if self.return_sequences:
Expand Down
6 changes: 3 additions & 3 deletions tensorflow/python/keras/layers/cudnn_recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ def call(self, inputs, mask=None, training=None, initial_state=None):
output, states = self._process_batch(inputs, initial_state)

if self.stateful:
updates = []
for i in range(len(states)):
updates.append(state_ops.assign(self.states[i], states[i]))
updates = [
state_ops.assign(self_state, state)
for self_state, state in zip(self.states, states)]
self.add_update(updates)

if self.return_state:
Expand Down
20 changes: 11 additions & 9 deletions tensorflow/python/keras/layers/recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,17 @@ def _validate_state_spec(cell_state_sizes, init_state_specs):
'`cell.state_size`. Received `state_spec`={}; '
'however `cell.state_size` is '
'{}'.format(init_state_specs, cell_state_sizes))
flat_cell_state_size = nest.flatten(cell_state_sizes)
flat_state_spec = nest.flatten(init_state_specs)
flat_cell_state_sizes = nest.flatten(cell_state_sizes)
flat_state_specs = nest.flatten(init_state_specs)

if len(flat_cell_state_size) != len(flat_state_spec):
if len(flat_cell_state_sizes) != len(flat_state_specs):
raise validation_error
for i in range(len(flat_cell_state_size)):
for cell_state_spec, cell_state_size in zip(
flat_state_specs, flat_cell_state_sizes):
if not tensor_shape.TensorShape(
# Ignore the first axis for init_state which is for batch
flat_state_spec[i].shape[1:]).is_compatible_with(
tensor_shape.TensorShape(flat_cell_state_size[i])):
cell_state_spec.shape[1:]).is_compatible_with(
tensor_shape.TensorShape(cell_state_size)):
raise validation_error

@doc_controls.do_not_doc_inheritable
Expand Down Expand Up @@ -785,9 +786,10 @@ def step(inputs, states):
zero_output_for_mask=self.zero_output_for_mask)

if self.stateful:
updates = []
for state_, state in zip(nest.flatten(self.states), nest.flatten(states)):
updates.append(state_ops.assign(state_, state))
updates = [
state_ops.assign(self_state, state)
for self_state, state in zip(
nest.flatten(self.states), nest.flatten(states))]
self.add_update(updates)

if self.return_sequences:
Expand Down
6 changes: 3 additions & 3 deletions tensorflow/python/keras/layers/recurrent_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,9 +1152,9 @@ def step(inputs, states):
states = [new_h, new_c]

if self.stateful:
updates = []
for i in range(len(states)):
updates.append(state_ops.assign(self.states[i], states[i]))
updates = [
state_ops.assign(self_state, state)
for self_state, state in zip(self.states, states)]
self.add_update(updates)

if self.return_sequences:
Expand Down