Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

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