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