tf.nn.conv2d 和 tf.nn.max_pool 中 padding 分别为 'VALID' 和 'SAME' 的直觉上的经验和测试代码
这个地方一开始是迷糊的,写代码做比较分析,总结出直觉上的经验.
某人若想看精准的解释,移步这个网址(http://blog.csdn.net/fireflychh/article/details/73743849),但我觉得直觉上的经验更有用,如下:
直觉上的经验:
- 一件确定的事: padding 无论取 'SAME' 还是取 'VALID', 它在 conv2d 和 max_pool 上的表现是一致的;
- padding = 'SAME' 时,输出并不一定和原图size一致,但会保证覆盖原图所有像素,不会舍弃边上的莫些元素;
- padding = 'VALID' 时,输出的size总比原图的size小,有时不会覆盖原图所有元素(既,可能舍弃边上的某些元素).
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
def pooling_show():
a = tf.Variable(tf.random_normal(X))
pooling = tf.nn.max_pool(a, pooling_filter, pooling_strides, padding=pad)
# VALID (1, 2, 2, 7)
# SAME (1, 3, 3, 7)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print 'image: '
image = sess.run(a)
print image.shape
print 'pooling result: '
res = sess.run(pooling)
print res.shape
def conv2d_padding_show():
# [1, 13, 13, 2] ---> [m, height, width, channel]
input = tf.Variable(tf.random_normal(X))
# [6, 6, 2, 7] ---> [height, width, prev_channel, output_channel]
filter = tf.Variable(tf.random_normal(conv2d_filter))
op = tf.nn.conv2d(input, filter, strides=conv2d_strides, padding=pad)
# VALID (1, 2, 2, 7)
# SAME (1, 3, 3, 7)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print 'image: '
image = sess.run(input)
print image.shape
print 'result: '
res = sess.run(op)
print res.shape
pad = 'VALID'
# X ---> [m, height, width, channel]
# X = [1, 13, 13, 7]
X = [1, 8, 8, 3]
# ---> [1, f, f, 1]
# pooling_filter = [1, 6, 6, 1]
pooling_filter = [1, 2, 2, 1]
# ---> [1, s, s, 1]
# pooling_strides = [1, 5, 5, 1]
pooling_strides = [1, 2, 2, 1]
# ---> [height, width, prev_channel, output_channel]
# conv2d_filter = [6, 6, 7, 7]
conv2d_filter = [2, 2, 3, 3]
# ---> [1, s, s, 1]
# conv2d_strides = [1, 5, 5, 1]
conv2d_strides = [1, 2, 2, 1]
# 自己改改 X, fileter, strides 的值,配合直觉经验,会有更好的理解
conv2d_padding_show()
pooling_show()
tf.nn.conv2d 和 tf.nn.max_pool 中 padding 分别为 'VALID' 和 'SAME' 的直觉上的经验和测试代码的更多相关文章
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)
MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...
- Pytorch中nn.Conv2d的用法
Pytorch中nn.Conv2d的用法 nn.Conv2d是二维卷积方法,相对应的还有一维卷积方法nn.Conv1d,常用于文本数据的处理,而nn.Conv2d一般用于二维图像. 先看一下接口定义: ...
- 关于torch.nn.Conv2d的笔记
先看一下CLASS有哪些参数: torch.nn.Conv2d( in_channels, out_channels, kernel_size, stride=1, padding=0, dilati ...
- PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx
PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现一些功能重复的操作,比如卷积.激活.池化等操作.这些操作分别可 ...
- 深度学习原理与框架-Tensorflow卷积神经网络-卷积神经网络mnist分类 1.tf.nn.conv2d(卷积操作) 2.tf.nn.max_pool(最大池化操作) 3.tf.nn.dropout(执行dropout操作) 4.tf.nn.softmax_cross_entropy_with_logits(交叉熵损失) 5.tf.truncated_normal(两个标准差内的正态分布)
1. tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') # 对数据进行卷积操作 参数说明:x表示输入数据,w表示卷积核, stride ...
- tf.nn.conv2d函数和tf.nn.max_pool函数介绍
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 介绍参数: input:指卷积需要输入的 ...
- TF-卷积函数 tf.nn.conv2d 介绍
转自 http://www.cnblogs.com/welhzh/p/6607581.html 下面是这位博主自己的翻译加上测试心得 tf.nn.conv2d是TensorFlow里面实现卷积的函数, ...
- tf.nn.conv2d。卷积函数
tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...
- tf.nn.conv2d
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) input: 指需要做卷积的输入图像,它 ...
随机推荐
- Java课后练习
1.利用循环输出:************************* public class Shape { public static void main(String[] args) { for ...
- Java基础中一些容易被忽视的语法小细节总结
一:语法细节 1. Java中的命名规则: package:统一使用小写字母 class:首字母大写,使用驼峰标识 method:首字母小写,使用驼峰标识 field:首字母小写,使用驼峰标识 sta ...
- linux系统下的SVN安装
1.直接安装 # sudo apt-get install subversion 2. 创建版本库 # sudo mkdir /home/svn # sudo svnadmin create /hom ...
- c语言一个显示星号的函数(隐藏密码)
显示星号 void star(char p[]) //显示星号 { int j; while((p[j] = getch())!='\r') { if(p[j] !='\b') { pr ...
- SQL类型注入
前言: 继续进行未完成的sql注入学习 今天学习了各类型注入.前来进行总结. 目录: 数字型注入 字符型注入 提交注注入 GET注入 POST注入 COOKIE注入 正文: 数字型注入:www.xxx ...
- C#之FTP上传下载(二)
这个类几乎包含了对FTP常用的方法,有不对的地方,欢迎批评指正 public class FtpClient { #region 构造函数 /// <summary> /// 创建FTP工 ...
- java中包的定义
对包中的java程序进行编译(cmd) 编译:javac -d . Test.java 执行:java com.java.demo.Test package com.java public class ...
- leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...