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

Fix stateful metrics when passing dict to compile #9894

Merged
merged 1 commit into from
Apr 13, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions keras/engine/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ def compile(self, optimizer, loss=None, metrics=None, loss_weights=None,
nested_weighted_metrics = _collect_metrics(weighted_metrics, self.output_names)
self.metrics_updates = []
self.stateful_metric_names = []
self.stateful_metric_functions = []
with K.name_scope('metrics'):
for i in range(len(self.outputs)):
if i in skip_target_indices:
Expand Down Expand Up @@ -929,6 +930,7 @@ def handle_metrics(metrics, weights=None):
# stateful metrics (i.e. metrics layers).
if isinstance(metric_fn, Layer) and metric_fn.stateful:
self.stateful_metric_names.append(metric_name)
self.stateful_metric_functions.append(metric_fn)
self.metrics_updates += metric_fn.updates

handle_metrics(output_metrics)
Expand Down Expand Up @@ -1174,9 +1176,8 @@ def _fit_loop(self, f, ins, out_labels=None, batch_size=None,

for epoch in range(initial_epoch, epochs):
# Reset stateful metrics
for m in self.metrics:
if isinstance(m, Layer) and m.stateful:
m.reset_states()
for m in self.stateful_metric_functions:
m.reset_states()
callbacks.on_epoch_begin(epoch)
epoch_logs = {}
if steps_per_epoch is not None:
Expand Down Expand Up @@ -1363,9 +1364,8 @@ def _test_loop(self, f, ins, batch_size=None, verbose=0, steps=None):
"""

if hasattr(self, 'metrics'):
for m in self.metrics:
if isinstance(m, Layer) and m.stateful:
m.reset_states()
for m in self.stateful_metric_functions:
m.reset_states()
stateful_metric_indices = [
i for i, name in enumerate(self.metrics_names)
if str(name) in self.stateful_metric_names]
Expand Down Expand Up @@ -2185,9 +2185,8 @@ def generate_arrays_from_file(path):
# Construct epoch logs.
epoch_logs = {}
while epoch < epochs:
for m in self.metrics:
if isinstance(m, Layer) and m.stateful:
m.reset_states()
for m in self.stateful_metric_functions:
m.reset_states()
callbacks.on_epoch_begin(epoch)
steps_done = 0
batch_index = 0
Expand Down Expand Up @@ -2331,9 +2330,8 @@ def evaluate_generator(self, generator, steps=None,

stateful_metric_indices = []
if hasattr(self, 'metrics'):
for i, m in enumerate(self.metrics):
if isinstance(m, Layer) and m.stateful:
m.reset_states()
for m in self.stateful_metric_functions:
m.reset_states()
stateful_metric_indices = [
i for i, name in enumerate(self.metrics_names)
if str(name) in self.stateful_metric_names]
Expand Down
17 changes: 12 additions & 5 deletions tests/keras/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def test_sparse_top_k_categorical_accuracy():


@keras_test
def test_stateful_metrics():
@pytest.mark.parametrize('metrics_mode', ['list', 'dict'])
def test_stateful_metrics(metrics_mode):
np.random.seed(1334)

class BinaryTruePositives(keras.layers.Layer):
Expand Down Expand Up @@ -155,11 +156,17 @@ def __call__(self, y_true, y_pred):

# Test on simple model
inputs = keras.Input(shape=(2,))
outputs = keras.layers.Dense(1, activation='sigmoid')(inputs)
outputs = keras.layers.Dense(1, activation='sigmoid', name='out')(inputs)
model = keras.Model(inputs, outputs)
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['acc', metric_fn])

if metrics_mode == 'list':
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['acc', metric_fn])
elif metrics_mode == 'dict':
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics={'out': ['acc', metric_fn]})

samples = 1000
x = np.random.random((samples, 2))
Expand Down