Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
"""
2
Solution to simple exercises to get used to TensorFlow API
3
You should thoroughly test your code.
4
TensorFlow's official documentation should be your best friend here
5
CS20: "TensorFlow for Deep Learning Research"
6
cs20.stanford.edu
7
Created by Chip Huyen ([email protected])
8
"""
9
import os
10
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
11
12
import tensorflow as tf
13
14
sess = tf.InteractiveSession()
15
###############################################################################
16
# 1a: Create two random 0-d tensors x and y of any distribution.
17
# Create a TensorFlow object that returns x + y if x > y, and x - y otherwise.
18
# Hint: look up tf.cond()
19
# I do the first problem for you
20
###############################################################################
21
22
x = tf.random_uniform([]) # Empty array as shape creates a scalar.
23
y = tf.random_uniform([])
24
out = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.subtract(x, y))
25
26
###############################################################################
27
# 1b: Create two 0-d tensors x and y randomly selected from the range [-1, 1).
28
# Return x + y if x < y, x - y if x > y, 0 otherwise.
29
# Hint: Look up tf.case().
30
###############################################################################
31
32
x = tf.random_uniform([], -1, 1, dtype=tf.float32)
33
y = tf.random_uniform([], -1, 1, dtype=tf.float32)
34
out = tf.case({tf.less(x, y): lambda: tf.add(x, y),
35
tf.greater(x, y): lambda: tf.subtract(x, y)},
36
default=lambda: tf.constant(0.0), exclusive=True)
37
38
39
###############################################################################
40
# 1c: Create the tensor x of the value [[0, -2, -1], [0, 1, 2]]
41
# and y as a tensor of zeros with the same shape as x.
42
# Return a boolean tensor that yields Trues if x equals y element-wise.
43
# Hint: Look up tf.equal().
44
###############################################################################
45
46
x = tf.constant([[0, -2, -1], [0, 1, 2]])
47
y = tf.zeros_like(x)
48
out = tf.equal(x, y)
49
50
###############################################################################
51
# 1d: Create the tensor x of value
52
# [29.05088806, 27.61298943, 31.19073486, 29.35532951,
53
# 30.97266006, 26.67541885, 38.08450317, 20.74983215,
54
# 34.94445419, 34.45999146, 29.06485367, 36.01657104,
55
# 27.88236427, 20.56035233, 30.20379066, 29.51215172,
56
# 33.71149445, 28.59134293, 36.05556488, 28.66994858].
57
# Get the indices of elements in x whose values are greater than 30.
58
# Hint: Use tf.where().
59
# Then extract elements whose values are greater than 30.
60
# Hint: Use tf.gather().
61
###############################################################################
62
63
x = tf.constant([29.05088806, 27.61298943, 31.19073486, 29.35532951,
64
30.97266006, 26.67541885, 38.08450317, 20.74983215,
65
34.94445419, 34.45999146, 29.06485367, 36.01657104,
66
27.88236427, 20.56035233, 30.20379066, 29.51215172,
67
33.71149445, 28.59134293, 36.05556488, 28.66994858])
68
indices = tf.where(x > 30)
69
out = tf.gather(x, indices)
70
71
###############################################################################
72
# 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1,
73
# 2, ..., 6
74
# Hint: Use tf.range() and tf.diag().
75
###############################################################################
76
77
values = tf.range(1, 7)
78
out = tf.diag(values)
79
80
###############################################################################
81
# 1f: Create a random 2-d tensor of size 10 x 10 from any distribution.
82
# Calculate its determinant.
83
# Hint: Look at tf.matrix_determinant().
84
###############################################################################
85
86
m = tf.random_normal([10, 10], mean=10, stddev=1)
87
out = tf.matrix_determinant(m)
88
89
###############################################################################
90
# 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9].
91
# Return the unique elements in x
92
# Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.
93
###############################################################################
94
95
x = tf.constant([5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9])
96
unique_values, indices = tf.unique(x)
97
98
###############################################################################
99
# 1h: Create two tensors x and y of shape 300 from any normal distribution,
100
# as long as they are from the same distribution.
101
# Use tf.cond() to return:
102
# - The mean squared error of (x - y) if the average of all elements in (x - y)
103
# is negative, or
104
# - The sum of absolute value of all elements in the tensor (x - y) otherwise.
105
# Hint: see the Huber loss function in the lecture slides 3.
106
###############################################################################
107
108
x = tf.random_normal([300], mean=5, stddev=1)
109
y = tf.random_normal([300], mean=5, stddev=1)
110
average = tf.reduce_mean(x - y)
111
def f1(): return tf.reduce_mean(tf.square(x - y))
112
def f2(): return tf.reduce_sum(tf.abs(x - y))
113
out = tf.cond(average < 0, f1, f2)
114