📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""1Simple exercises to get used to TensorFlow API2You should thoroughly test your code3"""4import os5os.environ['TF_CPP_MIN_LOG_LEVEL']='2'67import tensorflow as tf89sess = tf.InteractiveSession()10###############################################################################11# 1a: Create two random 0-d tensors x and y of any distribution.12# Create a TensorFlow object that returns x + y if x > y, and x - y otherwise.13# Hint: look up tf.cond()14# I do the first problem for you15###############################################################################1617x = tf.random_uniform([]) # Empty array as shape creates a scalar.18y = tf.random_uniform([])19out = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.subtract(x, y))20print(sess.run(out))2122###############################################################################23# 1b: Create two 0-d tensors x and y randomly selected from the range [-1, 1).24# Return x + y if x < y, x - y if x > y, 0 otherwise.25# Hint: Look up tf.case().26###############################################################################2728# YOUR CODE2930###############################################################################31# 1c: Create the tensor x of the value [[0, -2, -1], [0, 1, 2]]32# and y as a tensor of zeros with the same shape as x.33# Return a boolean tensor that yields Trues if x equals y element-wise.34# Hint: Look up tf.equal().35###############################################################################3637# YOUR CODE3839###############################################################################40# 1d: Create the tensor x of value41# [29.05088806, 27.61298943, 31.19073486, 29.35532951,42# 30.97266006, 26.67541885, 38.08450317, 20.74983215,43# 34.94445419, 34.45999146, 29.06485367, 36.01657104,44# 27.88236427, 20.56035233, 30.20379066, 29.51215172,45# 33.71149445, 28.59134293, 36.05556488, 28.66994858].46# Get the indices of elements in x whose values are greater than 30.47# Hint: Use tf.where().48# Then extract elements whose values are greater than 30.49# Hint: Use tf.gather().50###############################################################################5152# YOUR CODE5354###############################################################################55# 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1,56# 2, ..., 657# Hint: Use tf.range() and tf.diag().58###############################################################################5960# YOUR CODE6162###############################################################################63# 1f: Create a random 2-d tensor of size 10 x 10 from any distribution.64# Calculate its determinant.65# Hint: Look at tf.matrix_determinant().66###############################################################################6768# YOUR CODE6970###############################################################################71# 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9].72# Return the unique elements in x73# Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.74###############################################################################7576# YOUR CODE7778###############################################################################79# 1h: Create two tensors x and y of shape 300 from any normal distribution,80# as long as they are from the same distribution.81# Use tf.cond() to return:82# - The mean squared error of (x - y) if the average of all elements in (x - y)83# is negative, or84# - The sum of absolute value of all elements in the tensor (x - y) otherwise.85# Hint: see the Huber loss function in the lecture slides 3.86###############################################################################8788# YOUR CODE8990