十四. Tensorflow常用数学操作

本篇博客记录tensorflow在数学公式表达中常用的操作。

tf.multiply

element-wise 乘法 $\bigodot $

1
2
3
4
5
6
7
import tensorflow as tf
sess = tf.Session()

x = tf.reshape(tf.range(10), [2, 5])
y = tf.reshape(tf.range(10), [2, 5])
z = tf.multiply(x, y)
sess.run(z)
1
2
array([[ 0,  1,  4,  9, 16],
[25, 36, 49, 64, 81]], dtype=int32)

tf.reduce_sum

求和 $\sum$

1
2
3
m1 = tf.reshape(tf.range(15), [3,5])

sess.run(m1)
1
2
3
array([[ 0,  1,  2,  3,  4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]], dtype=int32)
1
sess.run(tf.reduce_sum(m1))
1
105
1
sess.run(tf.reduce_sum(m1, 1))  # 指定求和的维度
1
array([10, 35, 60], dtype=int32)
1
tf.reduce_sum(m1, 1).get_shape()
1
TensorShape([Dimension(3)])

tf.reduce_mean

求平均 $\frac{\sum_{i=1}^n{x_i}}{n}$

1
sess.run(tf.reduce_mean(m1, 1))
1
array([ 2,  7, 12], dtype=int32)

tf.equal

判断是否相等,返回bool的tensor。

1
2
3
4
5
x = tf.placeholder(tf.float32, [5])
y = tf.placeholder(tf.float32, [5])
z = tf.equal(x,y)

sess.run(z, feed_dict={x:[0,1,1,1,0], y:[1,1,2,0,0]})
1
array([ True,  True,  True,  True,  True], dtype=bool)
1
2
w = tf.cast(z, tf.int32)
sess.run(w, feed_dict={x:[0,1,1,1,0], y:[1,1,2,0,0]})
1
array([         0, 1065353216, 1065353216, 1065353216,          0], dtype=int32)

tf.nn.top_k

返回最后一维的前k个最大的数值,最小可以考虑加个负号。

tf.nn.top_k(input, k=1, sorted=True, name=None)

返回的是:(values, indices)。

1
2
3
4
5
top_k_values = tf.nn.top_k(y, 4, sorted=True)[0] # 返回y中前k大的值
top_k_indices = tf.nn.top_k(y, 4, sorted=True)[1] # 返回y中前k大的数对应的index

print sess.run(top_k_values, feed_dict={ y:[1,1,2,0,0]})
print sess.run(top_k_indices, feed_dict={ y:[1,1,2,0,0]})
1
2
[ 2.  1.  1.  0.]
[2 0 1 3]

tensorflow中暂时没有排序的功能,可以使用top_k做一个ugly的替代。

tf.unique

去掉重复的值,返回原序的独一无二的版本。

返回的是:(y, idx)

1
2
3
4
5
v = tf.placeholder(tf.float32, [5])
u = tf.unique(v)[0]
u_idx = tf.unique(v)[1]

print sess.run([u, u_idx], feed_dict={ v:[3, 8, 7, 7, 1]})
1
[array([ 3.,  8.,  7.,  1.], dtype=float32), array([0, 1, 2, 2, 3], dtype=int32)]

tf.diag

根据对角向量获取一个对角矩阵。

1
2
3
v = tf.range(5)
m = tf.diag(v)
sess.run(m)
1
2
3
4
5
array([[0, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 3, 0],
[0, 0, 0, 0, 4]], dtype=int32)

tf.diag_part

tf.diag的逆操作,获取一个矩阵的对角向量。

1
2
v_ = tf.diag_part(m)
sess.run(v)
1
array([0, 1, 2, 3, 4], dtype=int32)

tf.truncated_normal

产生随机数矩阵(正态分布)。

tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

1
2
t = tf.truncated_normal([2,3])
sess.run(t)
1
2
array([[ -1.71465219e+38,  -1.71465219e+38,  -1.71465219e+38],
[ -1.71465219e+38, -1.71465300e+38, -1.71465219e+38]], dtype=float32)

tf.nn.l2_normalize

L2正则, $||x||_2$

tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None)

1
2
3
x = tf.cast(tf.reshape(tf.range(50), [10,5]), tf.float32)
x_l2_nor = tf.nn.l2_normalize(x, 1) # 指定在第1维上做正则
sess.run(x_l2_nor)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
array([[  0.00000000e+00,   1.40129846e-45,   2.80259693e-45,
4.20389539e-45, 5.60519386e-45],
[ 7.00649232e-45, 8.40779079e-45, 9.80908925e-45,
1.12103877e-44, 1.26116862e-44],
[ 1.40129846e-44, 1.54142831e-44, 1.68155816e-44,
1.82168800e-44, 1.96181785e-44],
[ 2.10194770e-44, 2.24207754e-44, 2.38220739e-44,
2.52233724e-44, 2.66246708e-44],
[ 2.80259693e-44, 2.94272678e-44, 3.08285662e-44,
3.22298647e-44, 3.36311631e-44],
[ 3.50324616e-44, 3.64337601e-44, 3.78350585e-44,
3.92363570e-44, 4.06376555e-44],
[ 4.20389539e-44, 4.34402524e-44, 4.48415509e-44,
4.62428493e-44, 4.76441478e-44],
[ 4.90454463e-44, 5.04467447e-44, 5.18480432e-44,
5.32493416e-44, 5.46506401e-44],
[ 5.60519386e-44, 5.74532370e-44, 5.88545355e-44,
6.02558340e-44, 6.16571324e-44],
[ 6.30584309e-44, 6.44597294e-44, 6.58610278e-44,
6.72623263e-44, 6.86636248e-44]], dtype=float32)