淺談tensorflow1.0 池化層(pooling)和全串連層(dense)

來源:互聯網
上載者:User
本篇文章主要介紹了淺談tensorflow1.0 池化層(pooling)和全串連層(dense),現在分享給大家,也給大家做個參考。一起過來看看吧

池化層定義在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))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.