A.I, Data and Software Engineering

TF2.0 Warm-up exercises (forked from @chipHuyen Repo)


Heard of Ms @huyen chip for her notable yet controversial travelling books back in the day. I enjoy reading but I am not really into travel memoirs. Nevertheless, she did surprise everyone by her achievements by getting in Stanford, teaching TensorFlow, and then became a computer/data scientist. Her story is definitely very inspiring.

For ones who don’t know about Ms Huyen, I added an interview vid at the end of this post. Now, let’s have some fun with TF2.X exercises extract from her public repo (it was originally written with TF.1.X). See this article for a quick comparison.

The TensorFlow exercises

Ex 0:

Enable TF2.0

Ex 1a.

 Create two random 0-D tensors x and y of any distribution. Create a TensorFlow object that returns x + y if x > y, and x - y otherwise.
Hint: lookup tf.cond()


Create two 0-d tensors x and y randomly selected from the range [-1, 1).
Return x + y if x < y, x - y if x > y, 0 otherwise.
Hint: Look up tf.case().


Create the tensor x of the value [[0, -2, -1], [0, 1, 2]] and y as a tensor of zeros with the same shape as x. Return a boolean tensor that yields Trues if x equals y element-wise.
Hint: Look up tf.equal()


Create the tensor x of value
[29.05088806, 27.61298943, 31.19073486, 29.35532951,
30.97266006, 26.67541885, 38.08450317, 20.74983215,
34.94445419, 34.45999146, 29.06485367, 36.01657104,
27.88236427, 20.56035233, 30.20379066, 29.51215172,
33.71149445, 28.59134293, 36.05556488, 28.66994858].

Get the indices of elements in x whose values are greater than 30.
Hint: Use tf.where().
Then extract elements whose values are greater than 30.
Hint: Use tf.gather(): Gather slices from params axis according to indices.


Create a diagonal 2-d tensor of size 6 x 6 with the diagonal values of 1, 2, ..., 6
Hint: Use tf.range() and tf.linalg.diag().


Create a random 2-d tensor of size 10 x 10 from any distribution. Calculate its determinant. Hint: Look at tf.linalg.det(): Computes the determinant of one or more square matrices.


Create tensor x with value [5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9].
Return the unique elements in x
Hint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.


Create two tensors x and y of shape 300 from any normal distribution, as long as they are from the same distribution. Use tf.cond() to return:

  • The mean squared error of (x – y) if the average of all elements in (x – y) is negative, or
  • The sum of the absolute value of all elements in the tensor (x – y) otherwise.

Hint: One application is the Huber loss function

The interview


A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest


You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.
%d bloggers like this: