Skip to content

tensorflow_avoid_using_nondeterministic_api.py#28

Open
Tanuj-Koduri wants to merge 5 commits intomainfrom
feature-branch
Open

tensorflow_avoid_using_nondeterministic_api.py#28
Tanuj-Koduri wants to merge 5 commits intomainfrom
feature-branch

Conversation

@Tanuj-Koduri
Copy link
Owner

Here are a few suggestions to improve reproducibility in the non-compliant code example:

  1. Set random seeds for TensorFlow, NumPy, and the Python random module at the start of the code:
import tensorflow as tf
import numpy as np
import random

tf.random.set_seed(42) 
np.random.seed(42)
random.seed(42)
  1. Avoid using tf.compat.v1.Session() in TensorFlow 2.x. Instead, use the tf.function decorator to make functions deterministic:
@tf.function
def my_func():
  # Function code
  1. Use deterministic versions of operations like tf.nn.dropout() and Keras layers like Dropout. For example:
tf.nn.dropout(x, rate=0.2, seed=42) 

tf.keras.layers.Dropout(rate=0.2, seed=42)
  1. Enable deterministic ops for the entire TensorFlow runtime:
tf.config.experimental.enable_op_determinism()
  1. Use deterministic initializers for layers like tf.keras.layers.Dense():
tf.keras.layers.Dense(10, kernel_initializer='glorot_uniform') 

This will help make the model training more reproducible across multiple runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant