Problem 1
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().
import tensorflow as tf
x=tf.random_uniform([], minval=-1, dtype=tf.float32)
y=tf.random_uniform([], minval=-1, dtype=tf.float32)
r=tf.case({tf.less(x,y):lambda:tf.add(x,y),tf.greater(x,y):lambda:tf.subtract(x,y)},default=lambda:tf.constant(0.0),exclusive=True)
sess=tf.Session()
print(sess.run([x,y,r]))
>>>>[0.6612725, -0.8899913, 1.5512638]
注:random_uniform中,float类型的,默认最大是1
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().
import tensorflow as tf
x=tf.constant([[0, -2, -1], [0, 1, 2]])
y=tf.zeros([2,3],tf.int32)
r=tf.equal(x,y)
sess=tf.Session()
print(sess.run(r))
>>>>[[ True False False]
[ True False False]]
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().
import tensorflow as tf
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])
y=tf.where(tf.greater(x,30))
sess=tf.Session()
print(sess.run(y))
>>>>
[[ 2]
[ 4]
[ 6]
[ 8]
[ 9]
[11]
[14]
[16]
[18]]
z=tf.gather(x,y)
print(sess.run(z))
>>>>
[[31.190735]
[30.97266 ]
[38.084503]
[34.944454]
[34.45999 ]
[36.01657 ]
[30.20379 ]
[33.711494]
[36.055565]]
1e: Create a diagnoal 2-d tensor of size 6 x 6 with the diagonal values of 1,
2, ..., 6Hint: Use tf.range() and tf.diag().
import tensorflow as tf
x=tf.range(1,7,1)
y=tf.diag(x)
sess=tf.Session()
print(sess.run(y))
>>>>[[1 0 0 0 0 0]
[0 2 0 0 0 0]
[0 0 3 0 0 0]
[0 0 0 4 0 0]
[0 0 0 0 5 0]
[0 0 0 0 0 6]]
1f: Create a random 2-d tensor of size 10 x 10 from any distribution.
Calculate its determinant.Hint: Look at tf.matrix_determinant().
import tensorflow as tf
x=tf.range(1.0,11.0,1.0)
y=tf.diag(x)
z=tf.matrix_determinant(y)
sess=tf.Session()
print(sess.run(y))
>>>>
[[ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 2. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 3. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 4. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 5. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 6. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 7. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 8. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 9. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 10.]]
print(sess.run(z))
>>>>3628798.2
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 xHint: use tf.unique(). Keep in mind that tf.unique() returns a tuple.
import tensorflow as tf
x=tf.constant([5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9])
y, idx = tf.unique(x)
sess=tf.Session()
print(sess.run(y))
>>>>[ 5 2 3 10 6 4 1 0 9]
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.
import tensorflow as tf
x=tf.random_normal([1,300],mean=0.0,stddev=1.0,dtype=tf.float32)
y=tf.random_normal([1,300],mean=0.0,stddev=1.0,dtype=tf.float32)
z=tf.cond(tf.reduce_mean(x-y)<0,lambda:tf.reduce_mean(tf.square(x-y)),lambda:tf.reduce_sum(tf.abs(x-y)))
sess=tf.Session()
print(sess.run(x-y))
>>>>[[-3.3259373 0.9305101 2.3369002 -0.58522654 -0.16759875 1.698406
-0.01081187 -0.38285708 1.111228 -2.0451121 0.72925764 -0.8763517
1.3866515 -0.7273851 -0.06797087 0.23708224 -0.4933051 0.55349827
0.2565738 1.3393756 -0.20295298 -0.37385103 1.4424682 -2.5382326
-1.4828734 1.7097936 2.525352 0.6961763 -1.6808228 -1.0919024
-1.7160004 2.0651374 0.08216521 -4.153361 1.1308309 -0.03906488
1.7629608 0.9418278 0.95242786 0.37612128 1.7995732 -1.8174422
0.68410325 1.1663442 -0.04805964 0.36871102 1.7314537 1.1640325
0.63783544 -1.4898503 -1.462168 -0.90315914 2.2180512 1.9490724
0.26858312 -0.7575321 -2.5997772 2.7222347 -0.27430427 0.14750266
-2.0072467 -0.8164351 0.3545426 1.8677943 -2.6482778 0.40069485
-0.27549598 -1.9823563 -0.7630851 -2.2795818 -2.0075269 0.48266673
0.6017053 2.1382527 0.74592113 0.52842444 0.32057723 0.26354694
-2.5238962 2.6234004 4.7944026 -1.6891562 1.2090372 -0.69063425
1.0070536 -0.9678581 0.5506362 -1.9133409 0.3949564 -0.24645442
0.37920615 -1.1195459 2.2429037 0.14914593 -1.7539029 2.558775
-0.6383449 -2.3427832 -0.09224594 -2.1342983 0.6265718 0.12264508
-1.0492 -0.7277551 1.5103204 -1.8051116 0.01116648 -1.8283169
0.8466557 -2.7166715 -1.6153333 -4.0796146 -1.8603649 -2.7653427
-1.2411971 -1.3313416 0.23849896 -1.4215461 -0.5723803 0.73624223
1.1147115 0.65772235 -2.1826386 1.0381981 2.572258 1.795631
0.37775195 0.8735537 0.14153415 -0.40607256 0.08104062 2.124076
-1.0336151 1.1516895 -0.9265008 -0.961225 -1.0439814 -3.0700302
1.8282708 2.992245 -1.7811463 2.088091 -2.154027 2.5574968
-1.7110771 0.15141404 2.189666 -0.8567083 -2.1762702 -0.42888075
3.3124957 0.946964 -1.6782995 1.3670723 0.16284399 1.9230621
-2.4625988 1.2417272 0.70477813 -0.9294472 0.39884406 -1.0117009
-0.84982616 -0.02290625 -2.5489736 -2.3589077 -0.7883153 0.05992937
-0.10870516 1.0378543 0.02198076 -0.46578634 -1.2035284 -2.4899156
-1.5938048 3.0138226 1.447144 -1.2254368 -0.07803547 -1.7945874
2.4832418 1.9498408 3.1883407 -0.25840232 -1.0816634 1.2577249
-0.43239972 0.9880579 -0.2810726 1.7623899 1.4618556 2.002539
0.34034097 -0.08139475 -0.70431256 1.1688669 0.6220495 0.05951071
2.2498481 -2.597868 1.3775531 0.80213875 -0.6430395 2.1536717
-0.21881579 -0.16325207 2.4478955 1.5137436 -0.2114343 -0.9714613
1.6356633 -0.2199794 -2.7332892 0.75113165 -3.7493258 -1.754692
-0.3192181 -0.2781701 1.024087 -4.010352 1.9129378 1.718173
1.3617702 -1.0772389 -0.40157917 -0.01206875 -1.4310075 0.38276523
-0.7008718 -1.0611877 -1.1379039 3.802422 -2.7665658 -1.0831838
-0.03419453 0.54294604 0.27439943 -0.07699788 0.6292731 2.9984536
0.87841225 -0.4740056 -0.04971766 -1.8112401 -0.01951165 0.9263905
0.22594452 0.3790323 -0.41561016 1.1175281 -1.425831 0.3882755
0.6556697 -0.61765623 -1.5916424 -0.6630892 -1.7987186 0.7655849
-0.5913538 -0.73421514 0.3323573 0.3332346 4.45142 -4.320508
1.4509175 -2.7783024 2.027264 0.4835115 -2.8464792 -0.31837556
-0.05154544 1.2853687 1.30814 -1.3599446 -0.10480693 0.03862008
-1.6713247 0.5779135 -1.0575099 -2.2695613 0.815313 -1.2145096
-0.60316 2.198226 -0.9850508 -0.7066151 -2.173722 1.5004234
-1.622364 -0.99073625 1.4605755 -0.18946862 2.4435315 0.3853131
1.6368191 0.22564127 -0.9333099 2.6863937 -2.6122236 -0.06404543]]
print(sess.run(z))
>>>>311.44037