目录
3、tf.ones_like()&tf.zeros_like()
5、tf.stack&tf.concat&tf.unstack
1、tf.pad()
tf.pad(
tensor,
paddings,
mode='CONSTANT',
name=None
)
tensor:输入张量
padings 也是一个张量,代表每一维填充多少行/列,但是有一个要求它的rank一定要和tensor的rank是一样的
mode 可以取三个值,分别是"CONSTANT" ,"REFLECT","SYMMETRIC"
mode="CONSTANT" 是填充0
mode="REFLECT"是映射填充,上下(1维)填充顺序和paddings是相反的,左右(零维)顺序补齐
mode="SYMMETRIC"是对称填充,上下(1维)填充顺序是和paddings相同的,左右(零维)对称补齐
举个栗子:
import tensorflow as tf
a = [
[[1,2,3],
[4,5,6],
[7,8,9]],
[[1,2,3],
[4,5,6],
[7,8,9]],
[[1,2,3],
[4,5,6],
[7,8,9]],
]
b = tf.constant(a)
print(b.shape)
b1 = tf.pad(b,[[1,2],[2,1],[2,2]])
print(b1.shape)
sess = tf.Session()
print(sess.run(b1))
运行结果:
(3, 3, 3)
(6, 6, 7)
[[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 1 2 3 0 0]
[0 0 4 5 6 0 0]
[0 0 7 8 9 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 1 2 3 0 0]
[0 0 4 5 6 0 0]
[0 0 7 8 9 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 1 2 3 0 0]
[0 0 4 5 6 0 0]
[0 0 7 8 9 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]]]
2、tf.where()
where(condition, x=None, y=None, name=None)
condition, x, y 相同维度,condition是bool型值,True/False
返回值是对应元素,condition中元素为True的元素替换为x中的元素,为False的元素替换为y中对应元素
举个栗子:
import tensorflow as tf
sess = tf.Session()
a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.constant([[7, 8, 9], [0, 0, 0]])
c = tf.constant([[True,False,False],[False,True,False]])
print(sess.run(tf.where(c,a,b)))
结果:
[[1 8 9]
[0 5 0]]
c中true取相同a相同位置的值,false取b中的值。
3、tf.ones_like()&tf.zeros_like()
zeros_like(
tensor,
dtype=None,
name=None,
optimize=True
)
举个栗子
import tensorflow as tf
sess = tf.Session()
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
a = tf.ones_like(tensor)
print(sess.run(a))
结果:
[[1 1 1]
[1 1 1]]
简单的说就是定义一个shape和tensor相同但全为1的tensor。
4、tf.equal()
对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False,返回的值的矩阵维度和A是一样的。
import tensorflow as tf
sess = tf.Session()
a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.constant([[1, 8, 9], [0, 5, 0]])
print(sess.run(tf.equal(a,b)))
[[ True False False]
[False True False]]
5、tf.stack&tf.concat&tf.unstack
合并,举个栗子:
import tensorflow as tf
sess = tf.Session()
a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.constant([[1, 8, 9], [0, 5, 0]])
ab1 = tf.concat([a,b], axis=0)
ab2 = tf.concat([a,b], axis=1)
ab3 = tf.stack([a,b], axis=0)
print(sess.run(ab1))
print(sess.run(ab2))
print(sess.run(ab3))
结果:
[[1 2 3]
[4 5 6]
[1 8 9]
[0 5 0]]
[[1 2 3 1 8 9]
[4 5 6 0 5 0]]
[[[1 2 3]
[4 5 6]]
[[1 8 9]
[0 5 0]]]
关于stack,axis不同,tensor的shape也不同:
a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.constant([[1, 8, 9], [0, 5, 0]])
ab1 = tf.stack([a,b], axis=0)
ab3 = tf.stack([a,b], axis=2)
print(ab1.shape)
print(sess.run(ab1))
print(ab3.shape)
print(sess.run(ab3))
(2, 2, 3)
[[[1 2 3]
[4 5 6]]
[[1 8 9]
[0 5 0]]]
(2, 3, 2)
[[[1 1]
[2 8]
[3 9]]
[[4 0]
[5 5]
[6 0]]]
unstack则是将其分割:
a = tf.constant([[1, 2, 3], [4, 5, 6]])
b = tf.constant([[1, 8, 9], [0, 5, 0]])
ab1 = tf.stack([a,b], axis=0)
a1 = tf.unstack(ab1,axis=0)[0]
b1 = tf.unstack(ab1,axis=0)[1]
print(sess.run(a1))
print(sess.run(b1))
结果:
[[1 2 3]
[4 5 6]]
[[1 8 9]
[0 5 0]]
6、tf.slice()
tf.slice(inputs,begin,size,name=name)
input是你输入的tensor,list,array
begin:n维列表,每一个维度的起始位置
size:n维列表,每个维度使用的元素
举个栗子:
import tensorflow as tf
sess = tf.Session()
t = tf.constant([[[1, 1, 1], [2, 2, 2]],
[[3, 3, 3], [4, 4, 4]],
[[5, 5, 5], [6, 6, 6]]])
a = tf.slice(t, [1, 0, 0], [2, 1, 3])
b = tf.slice(t, [1, 0, 1], [2, 1, 2])
print(sess.run(a))
print(sess.run(b))
结果:
[[[3 3 3]]
[[5 5 5]]]
[[[3 3]]
[[5 5]]]
a中begin=[1,0,0],也就是取t中的
[
[[3,3,3],[4,4,4]],
[[5,5,5],[6,6,6]]
]
再取其中的size=[2,1,3],也就是
[[[3 3 3]]
[[5 5 5]]]
如果size=[1,2,3],就是
[[[3 3 3]]
[[4 4 4]]]