|
21 | 21 | import numpy as np
|
22 | 22 |
|
23 | 23 | from tensorflow.python.framework import ops
|
| 24 | +from tensorflow.python.framework import test_util as tf_test_util |
24 | 25 | from tensorflow.python.keras._impl import keras
|
25 | 26 | from tensorflow.python.keras._impl.keras import testing_utils
|
26 | 27 | from tensorflow.python.platform import test
|
@@ -625,6 +626,30 @@ def test_class_weight_invalid_use_case(self):
|
625 | 626 | model.fit(x_np, [y_np, y_np], epochs=1, sample_weight={'1': bad_w_np})
|
626 | 627 |
|
627 | 628 |
|
| 629 | +class CorrectnessTest(test.TestCase): |
| 630 | + |
| 631 | + @tf_test_util.run_in_graph_and_eager_modes() |
| 632 | + def test_loss_correctness(self): |
| 633 | + # Test that training loss is the same in eager and graph |
| 634 | + # (by comparing it to a reference value in a deterministic case) |
| 635 | + model = keras.Sequential() |
| 636 | + model.add(keras.layers.Dense(3, |
| 637 | + activation='relu', |
| 638 | + input_dim=4, |
| 639 | + kernel_initializer='ones')) |
| 640 | + model.add(keras.layers.Dense(2, |
| 641 | + activation='softmax', |
| 642 | + kernel_initializer='ones')) |
| 643 | + model.compile(loss='sparse_categorical_crossentropy', |
| 644 | + optimizer=RMSPropOptimizer(learning_rate=0.001)) |
| 645 | + x = np.ones((100, 4)) |
| 646 | + np.random.seed(123) |
| 647 | + y = np.random.randint(0, 1, size=(100, 1)) |
| 648 | + history = model.fit(x, y, epochs=1, batch_size=10) |
| 649 | + self.assertEqual( |
| 650 | + np.around(history.history['loss'][-1], decimals=4), 0.6173) |
| 651 | + |
| 652 | + |
628 | 653 | if __name__ == '__main__':
|
629 | 654 | ops.enable_eager_execution()
|
630 | 655 | test.main()
|
0 commit comments