Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

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