浅谈tensorflow1.0 池化层(pooling)和全连接层(dense)

yipeiwu_com6年前Python基础

池化层定义在tensorflow/python/layers/pooling.py.

有最大值池化和均值池化。

1、tf.layers.max_pooling2d

max_pooling2d(
  inputs,
  pool_size,
  strides,
  padding='valid',
  data_format='channels_last',
  name=None
)

  1. inputs: 进行池化的数据。
  2. pool_size: 池化的核大小(pool_height, pool_width),如[3,3]. 如果长宽相等,也可以直接设置为一个数,如pool_size=3.
  3. strides: 池化的滑动步长。可以设置为[1,1]这样的两个整数. 也可以直接设置为一个数,如strides=2
  4. padding: 边缘填充,'same' 和'valid‘选其一。默认为valid
  5. data_format: 输入数据格式,默认为channels_last ,即 (batch, height, width, channels),也可以设置为channels_first 对应 (batch, channels, height, width).
  6. name: 层的名字。

例:

pool1=tf.layers.max_pooling2d(inputs=x, pool_size=[2, 2], strides=2)

一般是放在卷积层之后,如:

conv=tf.layers.conv2d(
   inputs=x,
   filters=32,
   kernel_size=[5, 5],
   padding="same",
   activation=tf.nn.relu)
pool=tf.layers.max_pooling2d(inputs=conv, pool_size=[2, 2], strides=2)

2.tf.layers.average_pooling2d

average_pooling2d(
  inputs,
  pool_size,
  strides,
  padding='valid',
  data_format='channels_last',
  name=None
)

参数和前面的最大值池化一样。

全连接dense层定义在 tensorflow/python/layers/core.py.

3、tf.layers.dense

dense(
  inputs,
  units,
  activation=None,
  use_bias=True,
  kernel_initializer=None,
  bias_initializer=tf.zeros_initializer(),
  kernel_regularizer=None,
  bias_regularizer=None,
  activity_regularizer=None,
  trainable=True,
  name=None,
  reuse=None
)
  1. inputs: 输入数据,2维tensor.
  2. units: 该层的神经单元结点数。
  3. activation: 激活函数.
  4. use_bias: Boolean型,是否使用偏置项.
  5. kernel_initializer: 卷积核的初始化器.
  6. bias_initializer: 偏置项的初始化器,默认初始化为0.
  7. kernel_regularizer: 卷积核化的正则化,可选.
  8. bias_regularizer: 偏置项的正则化,可选.
  9. activity_regularizer: 输出的正则化函数.
  10. trainable: Boolean型,表明该层的参数是否参与训练。如果为真则变量加入到图集合中 GraphKeys.TRAINABLE_VARIABLES (see tf.Variable).
  11. name: 层的名字.
  12. reuse: Boolean型, 是否重复使用参数.

全连接层执行操作 outputs = activation(inputs.kernel + bias)

如果执行结果不想进行激活操作,则设置activation=None。

例:

#全连接层
dense1 = tf.layers.dense(inputs=pool3, units=1024, activation=tf.nn.relu)
dense2= tf.layers.dense(inputs=dense1, units=512, activation=tf.nn.relu)
logits= tf.layers.dense(inputs=dense2, units=10, activation=None)

也可以对全连接层的参数进行正则化约束:

复制代码 代码如下:
dense1 = tf.layers.dense(inputs=pool3, units=1024, activation=tf.nn.relu,kernel_regularizer=tf.contrib.layers.l2_regularizer(0.003))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中将字典转换成其json字符串

#这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'], 'sub_dic': {...

python中pygame模块用法实例

python中pygame模块用法实例

本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下: import pygame, sys from pygame.locals import *...

Pyorch之numpy与torch之间相互转换方式

numpy中的ndarray转化成pytorch中的tensor : torch.from_numpy() pytorch中的tensor转化成numpy中的ndarray : nump...

Python openpyxl读取单元格字体颜色过程解析

问题 我试图打印some_cell.font.color.rgb并得到各种结果。 对于一些人,我得到了我想要的东西(比如“ FF000000”),但对于其他人,它给了我Value mus...

Python中字符编码简介、方法及使用建议

1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码。计算机世...