📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""1Simple exercises to get used to TensorFlow API2You should thoroughly test your code.3TensorFlow's official documentation should be your best friend here4CS20: "TensorFlow for Deep Learning Research"5cs20.stanford.edu6Created by Chip Huyen ([email protected])7"""8import os9os.environ['TF_CPP_MIN_LOG_LEVEL']='2'1011import tensorflow as tf1213sess = tf.InteractiveSession()14###############################################################################15# 1a: Create two random 0-d tensors x and y of any distribution.16# Create a TensorFlow object that returns x + y if x > y, and x - y otherwise.17# Hint: look up tf.cond()18# I do the first problem for you19###############################################################################2021x = tf.random_uniform([]) # Empty array as shape creates a scalar.22y = tf.random_uniform([])23out = tf.cond(tf.greater(x, y), lambda: x + y, lambda: x - y)24print(sess.run(out))2526###############################################################################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###############################################################################3132# YOUR CODE3334###############################################################################35# 1c: Create the tensor x of the value [[0, -2, -1], [0, 1, 2]]36# and y as a tensor of zeros with the same shape as x.37# Return a boolean tensor that yields Trues if x equals y element-wise.38# Hint: Look up tf.equal().39###############################################################################4041# YOUR CODE4243###############################################################################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###############################################################################5556# YOUR CODE5758###############################################################################59# 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1,60# 2, ..., 661# Hint: Use tf.range() and tf.diag().62###############################################################################6364# YOUR CODE6566###############################################################################67# 1f: Create a random 2-d tensor of size 10 x 10 from any distribution.68# Calculate its determinant.69# Hint: Look at tf.matrix_determinant().70###############################################################################7172# YOUR CODE7374###############################################################################75# 1g: Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9].76# Return the unique elements in x77# Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.78###############################################################################7980# YOUR CODE8182###############################################################################83# 1h: Create two tensors x and y of shape 300 from any normal distribution,84# as long as they are from the same distribution.85# Use tf.cond() to return:86# - The mean squared error of (x - y) if the average of all elements in (x - y)87# is negative, or88# - The sum of absolute value of all elements in the tensor (x - y) otherwise.89# Hint: see the Huber loss function in the lecture slides 3.90###############################################################################9192# YOUR CODE9394