[SOLVED] "convolutional_block" gives different output at each run in the local environment

Environment:
Ubuntu 21.10
TensorFlow: 2.8.0
NVIDIA-SMI 510.54
CUDA Version: 11.6

I am trying to reproduce C4-W2A1 in my own environment, and this happened:

For the notebooks to work correctly, you have to be extremely careful that you’re using the same versions of all the tools that Coursera uses.

Do you get similar results if you run the notebook in Coursera’s environment?

I had all test passed in Coursera Environment

it seems to have something to do with reusing X1. Check this out.

np.random.seed(1)
X1 = np.ones((1,4,4,3)) * -1
X2 = np.ones((1,4,4,3)) * 1
X3 = np.ones((1,4,4,3)) * 3

X4 = np.concatenate((X1, X2, X3), axis=0).astype(np.float32)
X5 = np.concatenate((X1, X2, X3), axis=0).astype(np.float32)

X6 = tf.keras.layers.Conv2D(filters=2, 
           kernel_size = 1, 
           strides = (2,2),
           padding='valid',
           kernel_initializer=glorot_uniform(seed=0))(X4)

X7 = tf.keras.layers.Conv2D(filters=2, 
           kernel_size = 1, 
           strides = (2,2),
           padding='valid',
           kernel_initializer=glorot_uniform(seed=0))(X5)

assert(np.array_equiv(X6,X7))

that code passes the assertion

What’s happening with X1 in that block?

I had both piece failed.

I also tried:

X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3
X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)

y = Conv2D(filters = 2, kernel_size = 1, strides = (2,2), padding = 'valid')(X)
z = Conv2D(filters = 2, kernel_size = 1, strides = (2,2), padding = 'valid')(X)

assert(np.array_equiv(y,z))

and it called:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [204], in <cell line: 7>()
      5 y = Conv2D(filters = 2, kernel_size = 1, strides = (2,2), padding = 'valid')(X)
      6 z = Conv2D(filters = 2, kernel_size = 1, strides = (2,2), padding = 'valid')(X)
----> 7 assert(np.array_equiv(y,z))

AssertionError: 

Strange. I pasted that code into a cell in my notebook and the assertion evaluated True.

X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3
X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)

y = tf.keras.layers.Conv2D(filters = 2, kernel_size = 1, strides = (2,2), padding = 'valid')(X)
z = tf.keras.layers.Conv2D(filters = 2, kernel_size = 1, strides = (2,2), padding = 'valid')(X)

assert(np.array_equiv(y,z))

print(y)
print(z)

tf.Tensor(
[[[[ 0.90270627 -0.7846885 ]
   [ 0.90270627 -0.7846885 ]]

  [[ 0.90270627 -0.7846885 ]
   [ 0.90270627 -0.7846885 ]]]
...

tf.Tensor(
[[[[ 0.90270627 -0.7846885 ]
   [ 0.90270627 -0.7846885 ]]

  [[ 0.90270627 -0.7846885 ]
   [ 0.90270627 -0.7846885 ]]]

I removed all “assertion” tests after exercises in the notebook and succeed to reproduce model with accuracy >0.80 of several runs.

It seems Coursera environment always produce the same seed with np.random.seed(1) , and it isn’t the case for my local environment.