TensorFlow2.x 语法糖

tf.where

tf.where(condition, x=None, y=None, name=None)

如果x,y均为空,则返回满足条件的索引indices

1
2
a = tf.Variable([[1,2,3,4],[3,4,2,1]])
tf.where(a>3)

out:

1
2
3
4
<tf.Tensor: shape=(2, 2), dtype=int64, numpy=
array([[0, 3],
[1, 1]])>

如果x,y均不为空,则 满足条件位置的值为x相应位置的值,其余为y相应位置的值。(非常实用)

1
2
3
a = tf.Variable([[1,2,3,4],[3,4,2,1]])
b = tf.zeros_like(a)
tf.where(a>3, b, a)

out:

1
2
3
<tf.Tensor: shape=(2, 4), dtype=int32, numpy=
array([[1, 2, 3, 0],
[3, 0, 2, 1]], dtype=int32)>

tf.stack

tf.stack(values, axis=0, name=”stack”)

向量堆叠函数

values.shape = (A, B, C)

if axis == 0 then the output tensor will have the shape (N, A, B, C).
if axis == 1 then the output tensor will have the shape (A, N, B, C).

example:

1
2
3
4
5
6
7
a = tf.Variable([[1,2,0],[1,2,1]])   # shape = (2,3)
b = tf.Variable([[4,5,0],[4,5,1]]) # shape = (2,3)
c = tf.Variable([[7,8,0],[7,8,1]]) # shape = (2,3)
d = tf.Variable([[10,11,0],[10,11,1]]) # shape = (2,3)
stack_tensors_0 = tf.stack([a,b,c,d],axis=0) # shape = (4,2,3)
stack_tensors_1 = tf.stack([a,b,c,d],axis=1) # shape = (2,4,3)

out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
stack_tensors_0
<tf.Tensor: shape=(4, 2, 3), dtype=int32, numpy=
array([[[ 1, 2, 0],
[ 1, 2, 1]],
[[ 4, 5, 0],
[ 4, 5, 1]],
[[ 7, 8, 0],
[ 7, 8, 1]],
[[10, 11, 0],
[10, 11, 1]]], dtype=int32)>


stack_tensors_1
<tf.Tensor: shape=(2, 4, 3), dtype=int32, numpy=
array([[[ 1, 2, 0],
[ 4, 5, 0],
[ 7, 8, 0],
[10, 11, 0]],
[[ 1, 2, 1],
[ 4, 5, 1],
[ 7, 8, 1],
[10, 11, 1]]], dtype=int32)>

tf.gather

gather_v2(params,indices,validate_indices=None,axis=None,batch_dims=0,name=None)

根据索引,根据axis进行向量提取。只能根据一维度进行提取。

example:

1
2
a = tf.Variable([[1,1,1],[2,2,2],[3,3,3]])
gather_tensor = tf.gather(a,[2,1,1,0])

out:

1
2
3
4
5
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[3, 3, 3],
[2, 2, 2],
[2, 2, 2],
[1, 1, 1]], dtype=int32)>

tf.gather_nd

gather_nd_v2(params, indices, batch_dims=0, name=None)

根据多维度进行提取

example:

假设text 有2个sequence,每个sequence有2个单词,经过Embedding 后的维度为4维。
根据每个sequence的序列进行抽取。

第0个sequence抽取序列为[1,1,0,0]

第1个sequence抽取序列为[0,1,0,1]

1
2
3
text = tf.Variable([[[1,1,1,1],[2,2,2,2]],[[3,3,3,3],[4,4,4,4]]])  #shape (2,2,4)
index=tf.Variable([[[0,1],[0,1],[0,0],[0,0]],[[1,0],[1,1],[1,0],[1,1]]])
gather_nd_tensor = tf.gather_nd(text,index)

out:

1
2
3
4
5
6
7
8
9
10
gather_nd_tensor  #shape (2,4,4)
<tf.Tensor: shape=(2, 4, 4), dtype=int32, numpy=
array([[[2, 2, 2, 2],
[2, 2, 2, 2],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[3, 3, 3, 3],
[4, 4, 4, 4],
[3, 3, 3, 3],
[4, 4, 4, 4]]], dtype=int32)>

tf.slice

slice(input_, begin, size, name=None)

tf切片函数

example:

1
2
3
4
t = tf.constant([[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 1, 3]) #抽取 axis0=1,0<=axis1<1, 0<=axis2<3 的部分

out:

1
<tf.Tensor: shape=(1, 1, 3), dtype=int32, numpy=array([[[3, 3, 3]]], dtype=int32)>

tf.cond

tf.cond(pred, true_fn=None, false_fn=None, name=None)

tf条件函数

可以利用tf.cond来动态选择层。在TF2.x中 可以直接用if条件进行替代。

1
2
3
a=10
b=20
cond_layer = tf.cond(a>b,lambda :tf.keras.layers.Dense(a),lambda :tf.keras.layers.Dense(b))

tf.cumsum

tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)

按轴累加器

1
2
3
tf.cumsum([a,b,c]) = [a,a+b,a+b+c]
tf.cumsum([a,b,c],reverse=False) = [a+b+c,a+b,a]
tf.cumsum([a,b,c],exclusive=True) = [0,a,a+b]

example:

1
2
3
4
a = tf.ones((4,3))
cumsum_1 = tf.cumsum(a)
cumsum_2 = tf.cumsum(a, axis=1)
cumsum_3 = tf.cumsum(a,exclusive=True)

out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cumsum_1
<tf.Tensor: shape=(4, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.],
[4., 4., 4.]], dtype=float32)>

cumsum_2
<tf.Tensor: shape=(4, 3), dtype=float32, numpy=
array([[1., 2., 3.],
[1., 2., 3.],
[1., 2., 3.],
[1., 2., 3.]], dtype=float32)>

cumsum_3
<tf.Tensor: shape=(4, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]], dtype=float32)>

tf.clip_by_value

clip_by_value(t, clip_value_min, clip_value_max,name=None)

tf剪支函数

将t中 小于clip_value_min的替换成clip_value_min,大于 clip_value_max 替换成 clip_value_max

example:

1
2
t = tf.Variable([[1,2,3,4],[5,6,7,8]])
clip_tensor= tf.clip_by_value(t,3,5)

out:

1
2
3
4
clip_tensor
<tf.Tensor: shape=(2, 4), dtype=int32, numpy=
array([[3, 3, 3, 4],
[5, 5, 5, 5]], dtype=int32)>