random.set_seed() does not work on tensorflow-macos

Issue Type

Bug

Source

source

Tensorflow Version

2.10

Custom Code

No

OS Platform and Distribution

MacOS 13.0 Ventura Beta 7

Mobile device

No response

Python version

3.9.13

Bazel version

No response

GCC/Compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

M1 Metal

Current Behaviour?

using tf.random.set_seed works perfectly on google colab environment, yet using tensorflow-macos version, it does not seem to work at all. It's not giving me an error either. Everytime I run my code, I get different results.

Code I gave as an example uses numpy but even using data imported from pandas as DataFrames do not work. Which work on google colab.

My miniconda installation and python installation is all fresh. I was using python version 3.10 but the apple website does not mention that, so uninstalled miniconda and reinstalled it using an older version, effectively downgrading python, but that did not fix the problem either.

Standalone code to reproduce the issue

import tensorflow as tf
import numpy as np

X = np.array([-7.0, -4.0, -1.0, 2.0, 5.0, 8.0, 11.0, 14.0])
y = np.array([3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0])

tf.random.set_seed(42)

model = tf.keras.Sequential([
  tf.keras.layers.Dense(1)
])

model.compile(loss=tf.keras.losses.mae,
              optimizer=tf.keras.optimizers.SGD(),
              metrics=["mae"])

model.fit(tf.expand_dims(X, axis=-1), y, epochs=5)

Relevant log output

First Time:

Epoch 1/5 2022-09-30 18:25:08.200023: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled. 1/1 [==============================] - 1s 583ms/step - loss: 15.3376 - mae: 15.3376 Epoch 2/5 1/1 [==============================] - 0s 18ms/step - loss: 15.0563 - mae: 15.0563 Epoch 3/5 1/1 [==============================] - 0s 21ms/step - loss: 14.8426 - mae: 14.8426 Epoch 4/5 1/1 [==============================] - 0s 17ms/step - loss: 14.7101 - mae: 14.7101 Epoch 5/5 1/1 [==============================] - 0s 38ms/step - loss: 14.5776 - mae: 14.5776 <keras.callbacks.History at 0x2a0960730>

Second Time

Epoch 1/5 1/1 [==============================] - 0s 222ms/step - loss: 11.3347 - mae: 11.3347 Epoch 2/5 1/1 [==============================] - 0s 10ms/step - loss: 11.2022 - mae: 11.2022 Epoch 3/5 1/1 [==============================] - 0s 15ms/step - loss: 11.0697 - mae: 11.0697 Epoch 4/5 1/1 [==============================] - 0s 16ms/step - loss: 10.9372 - mae: 10.9372 Epoch 5/5 1/1 [==============================] - 0s 19ms/step - loss: 10.8047 - mae: 10.8047 2022-09-30 18:28:03.323935: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled. <keras.callbacks.History at 0x2a09f6c40>

Answered by denizgedik in 730838022

Replacing tf.random.set_seed() with tf.keras.utils.set_random_seed() fixes the issue.

Accepted Answer

Replacing tf.random.set_seed() with tf.keras.utils.set_random_seed() fixes the issue.

random.set_seed() does not work on tensorflow-macos
 
 
Q