tensorflow中的卷积和池化解释

首先,卷积和池化的基本概念就不用多说了,写这个东西的太多了,这里主要说说tensorflow中的相关内容。


再看看tensorflow中关于这两个函数的接口定义:

tf.nn.conv2d(
    input,				#输入的数据size=[batch, in_height, in_width, in_channels]
    filter,				#卷积核size=[filter_height, filter_width, in_channels, out_channels],in_channels是输入卷积核通道数
					#相同,out_channels是输出卷积结果个数
    strides,				#strides[0]=strides[3]=1,而strides[1],strides[2]分别是x,y方向上的位移
    padding,				#"SAME", "VALID"
    use_cudnn_on_gpu=None,
    data_format=None,
    name=None
)


bias_add(
    value,
    bias,
    data_format=None,
    name=None
)
avg_pool(
    value,				#[batch, height, width, channels]
    ksize,				#The size of the window for each dimension of the input
    strides,				#The stride of the sliding window for each dimension
    padding,				#"SAME", "VALID"
    data_format='NHWC',			#'NHWC' and 'NCHW' 
    name=None
)


给出的示例代码:

import tensorflow as tf
import numpy as np

Mat=np.array([
		[[1],[4],[-2],[7],[2]],
		[[3],[8],[1],[0],[3]],
		[[5],[-6],[-1],[4],[0]],
		[[7],[-2],[4],[0],[1]],
		[[2],[6],[1],[3],[-1]]
	])

print("The matrix is:",Mat)

filtKernel=tf.get_variable("weight",[2,2,1,1],initializer=tf.constant_initializer([[1,-2],[0,4]]))
biases=tf.get_variable("biases",[1],initializer=tf.constant_initializer(2))

Mat=np.asarray(Mat, dtype='float32')
Mat=Mat.reshape(1,5,5,1)

x=tf.placeholder('float32',[1,None,None,1])
conv=tf.nn.conv2d(x,filtKernel,strides=[1,2,2,1],padding="SAME")
bias=tf.nn.bias_add(conv,biases)
pools=tf.nn.avg_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

with tf.Session() as sess:
	tf.global_variables_initializer().run()
	conM=sess.run(conv,feed_dict={x:Mat})
	
	#print("converlution matrix: ",conM)
	
	conMbias=sess.run(bias,feed_dict={x:Mat})
	#print("converlution with bias: ",conMbias)
	
	pool=sess.run(pools,feed_dict={x:Mat})
	print("the pooling result is: ",pool)

运行结果截图:

卷积



池化


另外,给出部分结果解析:

卷积



池化结果


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sophia_xw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值