Create a TensorFlow object that returns x + y if x > y, and x - y otherwise.
tf.cond相当于其他编程语言的?,比较要用tf.greater
tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.subtract(x, y))tf.case
第一个参数是字典或者tuples都可以,只要是一对对,然后每一对第一个是判断条件,第二个是得到的值,必须是可调用的函数,返回tensor,每对之间是有先后顺序的,不是并行的。
###############################################################################
# 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)
x_add_y = lambda : x + y
x_sub_y = lambda : x - y
zero = lambda : tf.zeros([])
out = tf.case([(tf.less(x,y),x_add_y),(tf.greater(x,y),x_sub_y)],default=zero)
print(sess.run(x))
print(sess.run(y))
print(sess.run(out))
random 的区别
random_uniform是均匀分布
random_normal是正态分布tf.equal 每个元素进行比较
###############################################################################
# 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)
out = tf.equal(x,y)
print(sess.run(out))
- where 返回符合条件的index,gather返回位置为true的元素
###############################################################################
# 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])
out1 = tf.where(x>30)
out2 = tf.gather(x,out1)
print(sess.run(out1))
print(sess.run(out2))
- range diag
###############################################################################
# 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().
###############################################################################
out = tf.diag(tf.range(1,7))
print(sess.run(out))
-
Embedding Lookup
tf.nn.embedding_lookup(params, ids, partition_strategy='mod', name=None,
validate_indices=True, max_norm=None)
with tf.name_scope(name)
Name scope:Group nodes togethertf.gradients(y, [xs])
Take derivative of y with respect to each tensor in the list [xs]
import tensorflow as tf
x = tf.Variable(2.0)
y = x**2
z = y**3
gradz_x_y = tf.gradients(z,[x,y])
with tf.Session() as session:
session.run(x.initializer)
print(session.run(gradz_x_y))
tf.nn.embedding_lookup(embed_matrix,indexs)
把embed_matrix的对应indexs行取出来,一行行叠起来-
tf.train.Saver.save(sess, save_path,global_step=None...)
Only save variables, not graph
Checkpoints map variable names to tensors
tf.set_random_seed(seed)
Graph level seed-
不同文件不同的阅读器
-
队列管理
tf.Coordinator
tf.QueueRunner
tf.nn.conv2d