tensorflow 池化操作实例 tf.nn.max_pooling


#encoding:utf-8
import numpy as np
import tensorflow as tf


x_image = tf.placeholder(tf.float32, shape = [4,4])
x = tf.reshape(x_image, [1, 4, 4, 1 ])

ksize = [1, 2, 2, 1]
strides = [1, 2, 2 ,1]
padding = 'VALID'

#max_pooling
#x:池化操作的输入
#ksize:池化窗口的大小
#strides:窗口在每一个维度上滑动的步长,一般是[1, stride, stride, 1]
#padding:"VALID"or"SAME"
y = tf.nn.max_pool(x, ksize, strides, padding)
x_data = np.array([
    [4, 3, 1, 8],
    [7, 2, 6, 3],
    [2, 0, 1, 1],
    [3, 4, 2, 5]
])

with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        x = sess.run(x, feed_dict = {x_image : x_data})
        y = sess.run(y, feed_dict = {x_image : x_data})

print "The shape of x:", x.shape
print x.reshape(4, 4)
print ""

print "The shape pf y:", y.shape
print y.reshape(2,2)
print ""

输出:

The shape of x: (1, 4, 4, 1)
[[ 4.  3.  1.  8.]
 [ 7.  2.  6.  3.]
 [ 2.  0.  1.  1.]
 [ 3.  4.  2.  5.]]

The shape pf y: (1, 2, 2, 1)
[[ 7.  8.]
 [ 4.  5.]]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值