tensorflow的几个常用函数(tf.tile(),tf.padding(),tf.expend_dims())

最近看tensorflow,有几个很常用的tensor操作函数,但又不是那么直观理解,看看资料,这里记录一下:

1.tf.tile():
函数参数:

tile(
    input,     #输入
    multiples,  #同一维度上复制的次数
    name=None
)
tile() 平铺之意,用于在同一维度上的复制
什么叫同一维度的复制呢,如果是多维该怎么复制呢,应该这么理解,就是input是多少维,mutiples中的参数就是讲input的每一个维度复制多少次,下面举例子说明吧:
a = tf.constant([1,2,3])#维度一维,shape=[3,]
b = tf.constant([[1,2,3],[2,3,4]])#维度2维,shape=[2,3]
c = tf.constant([[[1,2,3],[1,2,3]]])#维度3维,shape = [1,2,3]

#对a用tile()函数
a_tile = tf.tile(input =a,mutiples=[2])#mutiples中只能有一个参数,因为a是1维的,表示把第一维复制2次

#对b用tile()函数
b_tile = tf.tile(input = b,mutiples =[2,2])#mutiles中只能有2个参数,因为b是2维的,表示把第一维复制2次和第二维度复制2次

c的用法类推

2.tf.expand_dims()

函数参数:tf.expand_dims(input, axis=None, name=None, dim=None)

功能:给定张量输入,此操作在输入形状的维度索引轴处插入1的尺寸。 尺寸索引轴从零开始; 如果您指定轴的负数,则从最后向后计数

举例子:

# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

3.tf.padding()

函数参数:tf.pad(tensor, paddings, mode=”CONSTANT”, name=None, constant_values=0)

输入参数:
tensor:输入的tensor

paddings:设置填充的大小

mode:填充方式,默认是CONSTANT,还有REFLECT和SYMMETRIC

name:名称

constant_values:CONSTANT填充方式的填充值

具体例子可以参考这个博客:https://blog.csdn.net/sinat_29957455/article/details/80903913

猜你喜欢

转载自blog.csdn.net/CV_YOU/article/details/82503545