Skip to content

Commit

Permalink
Fix lab1 assertion dense layer output assertion
Browse files Browse the repository at this point in the history
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.

fixes 116
  • Loading branch information
prashantkhurana committed Mar 30, 2023
1 parent 3d7b511 commit 35eafb4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lab1/Part1_TensorFlow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions mitdeeplearning/lab1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 35eafb4

Please sign in to comment.