From f4301e75d285c8d149501e8c3da95d86f6796a55 Mon Sep 17 00:00:00 2001 From: Prashant Khurana Date: Thu, 30 Mar 2023 04:51:41 +0530 Subject: [PATCH] Fix lab1 assertion dense layer output assertion 1) tf.random.set_seed only sets the global seed https://www.tensorflow.org/api_docs/python/tf/random/set_seed 2) we need to set keras set_random_seed to make all seeds be deterministic. docs : https://www.tensorflow.org/api_docs/python/tf/config/experimental/enable_op_determinism, https://www.tensorflow.org/api_docs/python/tf/keras/utils/set_random_seed. enable_op_determinism might not be needed here but its good to have. --- lab1/Part1_TensorFlow.ipynb | 7 +++++-- mitdeeplearning/lab1.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lab1/Part1_TensorFlow.ipynb b/lab1/Part1_TensorFlow.ipynb index e9e43b1f..55d80c67 100644 --- a/lab1/Part1_TensorFlow.ipynb +++ b/lab1/Part1_TensorFlow.ipynb @@ -366,8 +366,11 @@ " y = # TODO\n", " return y\n", "\n", - "# Since layer parameters are initialized randomly, we will set a random seed for reproducibility\n", - "tf.random.set_seed(1)\n", + "# Since layer parameters are initialized randomly, we will set random seed for reproducibility \n", + "# and make operations deterministic\n", + "tf.keras.utils.set_random_seed(1)\n", + "tf.config.experimental.enable_op_determinism()\n", + "\n", "layer = OurDenseLayer(3)\n", "layer.build((1,2))\n", "x_input = tf.constant([[1,2.]], shape=(1,2))\n", diff --git a/mitdeeplearning/lab1.py b/mitdeeplearning/lab1.py index 19d448a3..9ff0b658 100644 --- a/mitdeeplearning/lab1.py +++ b/mitdeeplearning/lab1.py @@ -80,8 +80,8 @@ def test_batch_func_next_step(func, args): return True def test_custom_dense_layer_output(y): - true_y = np.array([[0.2697859, 0.45750418, 0.66536945]],dtype='float32') + true_y = np.array([[0.27064407, 0.1826951, 0.50374055]],dtype='float32') assert tf.shape(y).numpy().tolist() == list(true_y.shape), "[FAIL] output is of incorrect shape. expected {} but got {}".format(true_y.shape, y.numpy().shape) - np.testing.assert_almost_equal(y.numpy(), true_y, decimal=7, err_msg="[FAIL] output is of incorrect value. expected {} but got {}".format(y.numpy(), true_y), verbose=True) + np.testing.assert_almost_equal(y.numpy(), true_y, decimal=7, err_msg="[FAIL] output is of incorrect value. expected {} but got {}".format(true_y,y.numpy()), verbose=True) print("[PASS] test_custom_dense_layer_output") return True