tensorflow的日用函数

1.tf.layers.MaxPooling1D
Class MaxPooling1D:1D输入的最大池化层

参数:

pool_size:一个整数或者一个单个整数的tuple/list,表示池化窗口的大小
strides:一个整数或者一个单个整数的tuple/list,指定池化操作的移动步幅
padding:一个字符串。padding的方法:”valid”或者’same’
data_format:一个字符串,channels_last(默认)或channels_first中的一个,输入中维度的排序,channels_last对应于具有形状(batch, length, channels)的输入,而channels_first对应于具有形状(batch, channels, length)的输入。
name:一个字符串,表示层的名称。
 

x=tf.Variable(tf.random_normal([2,9,1]))

max_pool=tf.layers.MaxPooling1D(pool_size=2,strides=2,padding="SAME")

print(max_pool(x))

max_pool=tf.layers.MaxPooling1D(pool_size=2,strides=2)

print(max_pool(x))

输出:

Tensor("max_pooling1d_4/Squeeze:0", shape=(2, 5, 1), dtype=float32)

Tensor("max_pooling1d_5/Squeeze:0", shape=(2, 4, 1), dtype=float32)

2.tf.layers.MaxPooling2D
Class MaxPooling2D:2D输入的最大池化层

参数:

pool_size:一个整数或者2个整数的元组/列表:(pool_height,pool_width),指定池化窗口的大小。 可以是单个整数,以为所有空间维度指定相同值。
strides:一个整数或者2个整数的元组/列表,指定池操作的步幅。 可以是单个整数,以为所有空间维度指定相同值。
padding:字符串,“valid”或者”same”
data_format:一个字符串,channels_last(默认)或channels_first中的一个,输入中维度的排序,channels_last对应于具有形状(batch,height, width, channels)的输入,而channels_first对应于具有形状(batch, channels, height,width)的输入。
name:层的名称。
 

x=tf.Variable(tf.random_normal([10,28,28,3])) #[batch_szie,height,weight,channel]

max_pool=tf.layers.MaxPooling2D(pool_size=[2,2],strides=[2,2],padding='SAME')

print(max_pool(x))

max_pool=tf.layers.MaxPooling2D(pool_size=[2,2],strides=[2,1],padding='SAME')

print(max_pool(x))

输出:

Tensor("max_pooling2d_1/MaxPool:0", shape=(10, 14, 14, 3), dtype=float32)

Tensor("max_pooling2d_2/MaxPool:0", shape=(10, 14, 28, 3), dtype=float32)
3.tf.expand_dims

tf.expand_dims(input, axis=None, name=None, dim=None)

# '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]

input:张量。

axis:在第axis位置增加一个维度

4.tf.squeeze()

tf.squeeze(input, squeeze_dims=None, name=None)
从tensor中删除所有大小是1的维度

给定张量输入,此操作返回相同类型的张量,并删除所有尺寸为1的尺寸。 如果不想删除所有尺寸1尺寸,可以通过指定squeeze_dims来删除特定尺寸1尺寸。

# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
Or, to remove specific size 1 dimensions:
 
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]

5.tf.split

tf.split( value,num_or_size_splits,axis=0,num=None,name='split')\

这个函数是用来切割张量的。输入切割的张量和参数,返回切割的结果。 
value传入的就是需要切割的张量。 
这个函数有两种切割的方式:

import tensorflow as tf

x=tf.Variable(tf.random_normal([20,30,40]))
f0=tf.split(x,2,axis=0)
f1=tf.split(x,10,axis=1)
f2=tf.split(x,[10,5,25],axis=2)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(f0))#10 * 30 * 40
    print(sess.run(f1))#20*3*40
    print(sess.run(f2))#20 * 30 * 10、20 * 30 * 5、20 * 30 * 25

很显然,传入的这个向量各个分量加和必须等于axis所指示原张量维度的大小 (10 + 5 + 25 = 40)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值