-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_ops.py
132 lines (104 loc) · 4.82 KB
/
basic_ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
sess = tf.InteractiveSession()
###############################################################################
# 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: look up tf.cond()
# I do the first problem for you
###############################################################################
'''x = tf.constant(3) # Empty array as shape creates a scalar.
#y = tf.random_uniform([])
y = tf.constant(5) # Empty array as shape creates a scalar.
out = tf.cond(tf.greater(x, y), lambda: x + y, lambda: x - y)
print (x.eval(),y.eval())
print(sess.run(out))'''
###############################################################################
# 1b: 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().
###############################################################################
x = tf.random_uniform([],-1,1)
y = tf.random_uniform([],-1,1)
def f1():return x+y,x,y
def f2():return x-y,x,y
def f3():return 0.0,x,y
out = tf.case({tf.less(x,y):f1,tf.greater(x,y):f2},default=f3,exclusive=True)
print sess.run(out)
#print (x.eval(),y.eval())
# YOUR CODE
###############################################################################
# 1c: 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().
###############################################################################
x = tf.constant([[0,-2,-1],[0,1,2]])
y = tf.zeros_like(x)
f = tf.equal(x,y)
print(sess.run(f))
# YOUR CODE
###############################################################################
# 1d: 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().
###############################################################################
x= tf.constant([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])
nd = tf.where(tf.greater(x,30))
y = tf.gather(x,nd)
#print (tf.greater(x,30))
print (sess.run(y))
# YOUR CODE
###############################################################################
# 1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1,
# 2, ..., 6
# Hint: Use tf.range() and tf.diag().
###############################################################################
x = tf.diag(tf.range(1,7))
print (sess.run(x))
# YOUR CODE
###############################################################################
# 1f: Create a random 2-d tensor of size 10 x 10 from any distribution.
# Calculate its determinant.
# Hint: Look at tf.matrix_determinant().
###############################################################################
x = tf.random_normal((10,10),mean=0.0,stddev=1.0)
d = tf.matrix_determinant(x)
print (sess.run(d))
# YOUR CODE
###############################################################################
# 1g: 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.
###############################################################################
# YOUR CODE
###############################################################################
# 1h: 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 absolute value of all elements in the tensor (x - y) otherwise.
# Hint: see the Huber loss function in the lecture slides 3.
##############################################################################
x = tf.random_normal((300,300),mean=0.0,stddev=1.0)
y = tf.random_normal((300,300),mean=0.0,stddev=1.0)
d = tf.subtract(x,y)
mean = tf.reduce_mean(d)
def f1(): return tf.reduce_mean(tf.square(x-y))
def f2(): return tf.reduce_sum(tf.abs(x-y))
out = tf.cond(tf.less(mean,0),f1,f2)
print (sess.run(out))
#