Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
""" Examples to demonstrate ops level randomization
2
Author: Chip Huyen
3
Prepared for the class CS 20SI: "TensorFlow for Deep Learning Research"
4
cs20si.stanford.edu
5
"""
6
import os
7
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
8
9
import tensorflow as tf
10
11
# Example 1: session is the thing that keeps track of random state
12
c = tf.random_uniform([], -10, 10, seed=2)
13
14
with tf.Session() as sess:
15
print(sess.run(c)) # >> 3.57493
16
print(sess.run(c)) # >> -5.97319
17
18
# Example 2: each new session will start the random state all over again.
19
c = tf.random_uniform([], -10, 10, seed=2)
20
21
with tf.Session() as sess:
22
print(sess.run(c)) # >> 3.57493
23
24
with tf.Session() as sess:
25
print(sess.run(c)) # >> 3.57493
26
27
# Example 3: with operation level random seed, each op keeps its own seed.
28
c = tf.random_uniform([], -10, 10, seed=2)
29
d = tf.random_uniform([], -10, 10, seed=2)
30
31
with tf.Session() as sess:
32
print(sess.run(c)) # >> 3.57493
33
print(sess.run(d)) # >> 3.57493
34
35
# Example 4: graph level random seed
36
tf.set_random_seed(2)
37
c = tf.random_uniform([], -10, 10)
38
d = tf.random_uniform([], -10, 10)
39
40
with tf.Session() as sess:
41
print(sess.run(c)) # >> 9.12393
42
print(sess.run(d)) # >> -4.53404
43
44