Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

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