Skip to content

Commit

Permalink
Theano fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fchollet committed Aug 25, 2019
1 parent 8feac19 commit caceebc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
5 changes: 3 additions & 2 deletions keras/engine/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@ def _prepare_total_loss(self, masks=None):
y_true, y_pred, sample_weight=sample_weight)

if len(self.outputs) > 1:
self._output_loss_metrics[i](output_loss)
update_ops = self._output_loss_metrics[i].update_state(output_loss)
self._metric_updates += update_ops

if total_loss is None:
total_loss = loss_weight * output_loss
Expand All @@ -698,7 +699,7 @@ def _prepare_total_loss(self, masks=None):
for loss_tensor in self.losses:
total_loss += loss_tensor

return total_loss
return K.mean(total_loss)

def _get_training_eval_metrics(self):
"""Returns all the metrics that are to be reported.
Expand Down
12 changes: 7 additions & 5 deletions keras/engine/training_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,12 @@ def call_metric_function(metric_fn,
weights *= mask

if y_pred is not None:
update_op = metric_fn.update_state(y_true, y_pred, sample_weight=weights)
result = metric_fn.result()
update_ops = metric_fn.update_state(y_true, y_pred, sample_weight=weights)
with K.control_dependencies(update_ops):
result = metric_fn.result()
else:
# `Mean` metric only takes a single value.
update_op = metric_fn.update_state(y_true, sample_weight=weights)
result = metric_fn.result()
return result, update_op
update_ops = metric_fn.update_state(y_true, sample_weight=weights)
with K.control_dependencies(update_ops):
result = metric_fn.result()
return result, update_ops
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
'''

setup(name='Keras',
version='2.2.4',
version='2.3.0',
description='Deep Learning for humans',
long_description=long_description,
author='Francois Chollet',
author_email='francois.chollet@gmail.com',
url='https://github.com/keras-team/keras',
download_url='https://github.com/keras-team/keras/tarball/2.2.4',
download_url='https://github.com/keras-team/keras/tarball/2.3.0',
license='MIT',
install_requires=['numpy>=1.9.1',
'scipy>=0.14',
Expand Down
12 changes: 12 additions & 0 deletions tests/keras/losses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def test_loss_wrapper(self):
np.allclose(K.eval(loss), 16, atol=1e-2)


skipif_not_tf = pytest.mark.skipif(
K.backend() != 'tensorflow',
reason='Need TensorFlow to __call__ a loss')


class TestLossClasses(object):

@pytest.mark.parametrize('cls', all_classes)
Expand All @@ -180,6 +185,7 @@ def test_objective_shapes_2d(self, cls):
assert K.eval(objective_output).shape == ()


@skipif_not_tf
class TestMeanSquaredError:

def test_config(self):
Expand Down Expand Up @@ -256,6 +262,7 @@ def test_sum_reduction(self):
assert np.isclose(K.eval(loss), 227.69998, rtol=1e-3)


@skipif_not_tf
class TestMeanAbsoluteError(object):

def test_config(self):
Expand Down Expand Up @@ -332,6 +339,7 @@ def test_sum_reduction(self):
assert np.isclose(K.eval(loss), 25.29999, rtol=1e-3)


@skipif_not_tf
class TestMeanAbsolutePercentageError(object):

def test_config(self):
Expand Down Expand Up @@ -392,6 +400,7 @@ def test_no_reduction(self):
assert np.allclose(K.eval(loss), [621.8518, 352.6666], rtol=1e-3)


@skipif_not_tf
class TestMeanSquaredLogarithmicError(object):

def test_config(self):
Expand Down Expand Up @@ -438,6 +447,7 @@ def test_zero_weighted(self):
assert np.allclose(K.eval(loss), 0.0, rtol=1e-3)


@skipif_not_tf
class TestBinaryCrossentropy(object):

def test_config(self):
Expand Down Expand Up @@ -598,6 +608,7 @@ def test_label_smoothing(self):
assert np.isclose(K.eval(loss), expected_value, atol=1e-3)


@skipif_not_tf
class TestCategoricalCrossentropy(object):

def test_config(self):
Expand Down Expand Up @@ -693,6 +704,7 @@ def test_label_smoothing(self):
assert np.isclose(K.eval(loss), expected_value, atol=1e-3)


@skipif_not_tf
class TestSparseCategoricalCrossentropy(object):

def test_config(self):
Expand Down
5 changes: 5 additions & 0 deletions tests/keras/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from keras import backend as K


if K.backend() != 'tensorflow':
# Need TensorFlow to use metric.__call__
pytestmark = pytest.mark.skip


class TestSum(object):

def test_sum(self):
Expand Down

0 comments on commit caceebc

Please sign in to comment.