📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""1Solution to simple TensorFlow exercises2For the problems3"""4import tensorflow as tf56###############################################################################7# 1a: Create two random 0-d tensors x and y of any distribution.8# Create a TensorFlow object that returns x + y if x > y, and x - y otherwise.9# Hint: look up tf.cond()10# I do the first problem for you11###############################################################################1213x = tf.random_uniform([]) # Empty array as shape creates a scalar.14y = tf.random_uniform([])15out = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.subtract(x, y))1617###############################################################################18# 1b: Create two 0-d tensors x and y randomly selected from the range [-1, 1).19# Return x + y if x < y, x - y if x > y, 0 otherwise.20# Hint: Look up tf.case().21###############################################################################2223x = tf.random_uniform([], -1, 1, dtype=tf.float32)24y = tf.random_uniform([], -1, 1, dtype=tf.float32)25out = tf.case({tf.less(x, y): lambda: tf.add(x, y),26tf.greater(x, y): lambda: tf.subtract(x, y)},27default=lambda: tf.constant(0.0), exclusive=True)28print(x)29sess = tf.InteractiveSession()30print(sess.run(x))3132###############################################################################33# 1c: Create the tensor x of the value [[0, -2, -1], [0, 1, 2]]34# and y as a tensor of zeros with the same shape as x.35# Return a boolean tensor that yields Trues if x equals y element-wise.36# Hint: Look up tf.equal().37###############################################################################3839x = tf.constant([[0, -2, -1], [0, 1, 2]])40y = tf.zeros_like(x)41out = tf.equal(x, y)4243###############################################################################44# 1d: Create the tensor x of value45# [29.05088806, 27.61298943, 31.19073486, 29.35532951,46# 30.97266006, 26.67541885, 38.08450317, 20.74983215,47# 34.94445419, 34.45999146, 29.06485367, 36.01657104,48# 27.88236427, 20.56035233, 30.20379066, 29.51215172,49# 33.71149445, 28.59134293, 36.05556488, 28.66994858].50# Get the indices of elements in x whose values are greater than 30.51# Hint: Use tf.where().52# Then extract elements whose values are greater than 30.53# Hint: Use tf.gather().54###############################################################################5556x = tf.constant([29.05088806, 27.61298943, 31.19073486, 29.35532951,5730.97266006, 26.67541885, 38.08450317, 20.74983215,5834.94445419, 34.45999146, 29.06485367, 36.01657104,5927.88236427, 20.56035233, 30.20379066, 29.51215172,6033.71149445, 28.59134293, 36.05556488, 28.66994858])61indices = tf.where(x > 30)62out = tf.gather(x, indices)6364###############################################################################65# 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1,66# 2, ..., 667# Hint: Use tf.range() and tf.diag().68###############################################################################6970values = tf.range(1, 7)71out = tf.diag(values)7273###############################################################################74# 1f: Create a random 2-d tensor of size 10 x 10 from any distribution.75# Calculate its determinant.76# Hint: Look at tf.matrix_determinant().77###############################################################################7879m = tf.random_normal([10, 10], mean=10, stddev=1)80out = tf.matrix_determinant(m)8182###############################################################################83# 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9].84# Return the unique elements in x85# Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.86###############################################################################8788x = tf.constant([5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9])89unique_values, indices = tf.unique(x)9091###############################################################################92# 1h: Create two tensors x and y of shape 300 from any normal distribution,93# as long as they are from the same distribution.94# Use tf.cond() to return:95# - The mean squared error of (x - y) if the average of all elements in (x - y)96# is negative, or97# - The sum of absolute value of all elements in the tensor (x - y) otherwise.98# Hint: see the Huber loss function in the lecture slides 3.99###############################################################################100101x = tf.random_normal([300], mean=5, stddev=1)102y = tf.random_normal([300], mean=5, stddev=1)103average = tf.reduce_mean(x - y)104def f1(): return tf.reduce_mean(tf.square(x - y))105def f2(): return tf.reduce_sum(tf.abs(x - y))106out = tf.cond(average < 0, f1, f2)107108