tensorflow構建CNN模型時的常用接口函數


(1)tf.nn.max_pool()函數

解釋:

tf.nn.max_pool(value, ksize, strides, padding, data_format='NHWC', name=None)

需要設置的參數主要有四個:

第一個參數value:需要池化的輸入,一般池化層接在卷積層后面,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape

第二個參數ksize:池化窗口的大小,取一個四維向量,一般是[1, height, width, 1],因為我們不想在batch和channels上做池化,所以這兩個維度設為了1

第三個參數strides:和卷積類似,窗口在每一個維度上滑動的步長,一般也是[1, stride,stride, 1]

第四個參數padding:和卷積類似,可以取'VALID' 或者'SAME'

返回一個Tensor,類型不變,shape仍然是[batch, height, width, channels]這種形式

 示例:

程序:

import tensorflow as tf  

a=tf.constant([  
        [[1.0,2.0,3.0,4.0],  
        [5.0,6.0,7.0,8.0],  
        [8.0,7.0,6.0,5.0],  
        [4.0,3.0,2.0,1.0]],  
        [[4.0,3.0,2.0,1.0],  
         [8.0,7.0,6.0,5.0],  
         [1.0,2.0,3.0,4.0],  
         [5.0,6.0,7.0,8.0]]  
    ])  

a=tf.reshape(a,[1,4,4,2])  

pooling=tf.nn.max_pool(a,[1,2,2,1],[1,1,1,1],padding='VALID')  
with tf.Session() as sess:  
    print("image:")  
    image=sess.run(a)  
    print (image)  
    print("reslut:")  
    result=sess.run(pooling)  
    print (result)  

運行結果:

image:  
[[[[ 1.  2.]  
   [ 3.  4.]  
   [ 5.  6.]  
   [ 7.  8.]]  

  [[ 8.  7.]  
   [ 6.  5.]  
   [ 4.  3.]  
   [ 2.  1.]]  

  [[ 4.  3.]  
   [ 2.  1.]  
   [ 8.  7.]  
   [ 6.  5.]]  

  [[ 1.  2.]  
   [ 3.  4.]  
   [ 5.  6.]  
   [ 7.  8.]]]]  
reslut:  
[[[[ 8.  7.]  
   [ 6.  6.]  
   [ 7.  8.]]  

  [[ 8.  7.]  
   [ 8.  7.]  
   [ 8.  7.]]  

  [[ 4.  4.]  
   [ 8.  7.]  
   [ 8.  8.]]]]

 

(2)tf.nn.dropout函數

解釋

tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)

此函數是為了防止在訓練中過擬合的操作,將訓練輸出按一定規則進行變換

參數:
x:輸入
keep_prob:保留比例。        取值 (0,1] 。每一個參數都將按這個比例隨機變更
noise_shape:干擾形狀。     此字段默認是None,表示第一個元素的操作都是獨立,但是也不一定。比例:數據的形狀是shape(x)=[k, l, m, n],而noise_shape=[k, 1, 1, n],則第1和4列是獨立保留或刪除,第2和3列是要么全部保留,要么全部刪除。
seed:整形變量,隨機數種子。
name:名字,沒啥用
返回:Tnesor

 

(3)tf.nn.local_response_normalization函數

公式說明

local response normalization最早是由Krizhevsky和Hinton在關於ImageNet的論文里面使用的一種數據標准化方法,即使現在,也依然會有不少CNN網絡會使用到這種正則手段,現在記錄一下lrn方法的計算流程以及tensorflow的實現,方便后面查閱

以上是這種歸一手段的公式,其中a的上標指該層的第幾個feature map,a的下標x,y表示feature map的像素位置,N指feature map的總數量,公式里的其它參數都是超參,需要自己指定的。

這種方法是受到神經科學的啟發,激活的神經元會抑制其鄰近神經元的活動(側抑制現象),至於為什么使用這種正則手段,以及它為什么有效,查閱了很多文獻似乎也沒有詳細的解釋,可能是由於后來提出的batch normalization手段太過火熱,漸漸的就把local response normalization掩蓋了吧

 解釋:

tf.nn.local_response_normalization(input, depth_radius=5, bias=1, alpha=1, beta=0.5, name=None)

除去name參數用以指定該操作的name,與方法有關的一共五個參數: 

第一個參數input:這個輸入就是feature map了,既然是feature map,那么它就具有[batch, height, width, channels]這樣的shape
第二個參數depth_radius:這個值需要自己指定,就是上述公式中的n/2
第三個參數bias:上述公式中的k
第四個參數alpha:上述公式中的α
第五個參數beta:上述公式中的β

 程序

import tensorflow as tf  

a = tf.constant([  
    [[1.0, 2.0, 3.0, 4.0],  
     [5.0, 6.0, 7.0, 8.0],  
     [8.0, 7.0, 6.0, 5.0],  
     [4.0, 3.0, 2.0, 1.0]],  
    [[4.0, 3.0, 2.0, 1.0],  
     [8.0, 7.0, 6.0, 5.0],  
     [1.0, 2.0, 3.0, 4.0],  
     [5.0, 6.0, 7.0, 8.0]]  
])  
#reshape a,get the feature map [batch:1 height:2 width:2 channels:8]  
a = tf.reshape(a, [1, 2, 2, 8])  

normal_a=tf.nn.local_response_normalization(a,2,0,1,1)  
with tf.Session() as sess:  
    print("feature map:")  
    image = sess.run(a)  
    print (image)  
    print("normalized feature map:")  
    normal = sess.run(normal_a)  
    print (normal) 

 輸出結果:

feature map:  
[[[[ 1.  2.  3.  4.  5.  6.  7.  8.]  
   [ 8.  7.  6.  5.  4.  3.  2.  1.]]  

  [[ 4.  3.  2.  1.  8.  7.  6.  5.]  
   [ 1.  2.  3.  4.  5.  6.  7.  8.]]]]  
normalized feature map:  
[[[[ 0.07142857  0.06666667  0.05454545  0.04444445  0.03703704  0.03157895  
     0.04022989  0.05369128]  
   [ 0.05369128  0.04022989  0.03157895  0.03703704  0.04444445  0.05454545  
     0.06666667  0.07142857]]  

  [[ 0.13793103  0.10000001  0.0212766   0.00787402  0.05194805  0.04  
     0.03448276  0.04545454]  
   [ 0.07142857  0.06666667  0.05454545  0.04444445  0.03703704  0.03157895  
     0.04022989  0.05369128]]]]  

 

(4)tf.get_variable函數

函數定義:

get_variable(
    name,
    shape=None,
    dtype=None,
    initializer=None,
    regularizer=None,
    trainable=True,
    collections=None,
    caching_device=None,
    partitioner=None,
    validate_shape=True,
    use_resource=None,
    custom_getter=None
)

 其中參數分別為:

參數:
name:新變量或現有變量的名稱。

shape:新變量或現有變量的形狀。

dtype:新變量或現有變量的類型(默認為 DT_FLOAT)。

initializer:創建變量的初始化器。

regularizer:一個函數(張量 - >張量或無);將其應用於新創建的變量的結果將被添加到集合 tf.GraphKeys.REGULARIZATION_LOSSES 中,並可用於正則化。

trainable:如果為 True,還將變量添加到圖形集合:GraphKeys.TRAINABLE_VARIABLES。
collections:要將變量添加到其中的圖形集合鍵的列表。默認為 [GraphKeys.LOCAL_VARIABLES]。

caching_device:可選的設備字符串或函數,描述變量應該被緩存以讀取的位置。默認為變量的設備,如果不是 None,則在其他設備上進行緩存。典型的用法的在使用該變量的操作所在的設備上進行緩存,通過 Switch 和其他條件語句來復制重復數據刪除。

partitioner:(可選)可調用性,它接受要創建的變量的完全定義的 TensorShape 和 dtype,並且返回每個坐標軸的分區列表(當前只能對一個坐標軸進行分區)。

validate_shape:如果為假,則允許使用未知形狀的值初始化變量。如果為真,則默認情況下,initial_value 的形狀必須是已知的。
use_resource:如果為假,則創建一個常規變量。如果為真,則創建一個實驗性的 ResourceVariable,而不是具有明確定義的語義。默認為假(稍后將更改為真)。

custom_getter:可調用的,將第一個參數作為真正的 getter,並允許覆蓋內部的 get_variable 方法。custom_getter 的簽名應該符合這種方法,但最經得起未來考驗的版本將允許更改:def custom_getter(getter, *args, **kwargs)。還允許直接訪問所有 get_variable 參數:def custom_getter(getter, name, *args, **kwargs)。創建具有修改的名稱的變量的簡單標識自定義 getter 是:python def custom_getter(getter, name, *args, **kwargs): return getter(name + '_suffix', *args, **kwargs) 

 使用例子

w = tf.get_variable("w", shape = [inputD, outputD], dtype = "float")
b = tf.get_variable("b", [outputD], dtype = "float")

 

(5)tf.variable_scope函數

函數原理

用於定義創建變量(層)的操作的上下文管理器。
此上下文管理器驗證(可選)values是否來自同一圖形,確保圖形是默認的圖形,並推送名稱范圍和變量范圍。
如果name_or_scope不是None,則使用as is。如果scope是None,則使用default_name。在這種情況下,如果以前在同一范圍內使用過相同的名稱,則通過添加_N來使其具有唯一性。
變量范圍允許您創建新變量並共享已創建的變量,同時提供檢查以防止意外創建或共享。在本文中我們提供了幾個基本示例。

如何創建一個新變量:

with tf.variable_scope("foo"):
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])
        assert v.name == "foo/bar/v:0"

 

(6)tf.nn.relu函數

解釋

這個函數的作用是計算激活函數relu,即max(features, 0)。即將矩陣中每行的非最大值置0。

類似的還有tf.sigmoid , tf.tanh

函數定義:

>>> help(tf.nn.relu)
Help on function relu in module tensorflow.python.ops.gen_nn_ops:

relu(features, name=None)
    Computes rectified linear: `max(features, 0)`.

    Args:
      features: A `Tensor`. Must be one of the following types: `float32`, `float64`, `int32`, `uint8`, `int16`, `int8`, `int64`, `bfloat16`, `uint16`, `half`, `uint32`, `uint64`.
      name: A name for the operation (optional).

    Returns:
      A `Tensor`. Has the same type as `features`.

 程序示例

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf

a = tf.constant([-1.0, 2.0])
with tf.Session() as sess:
    b = tf.nn.relu(a)
    print sess.run(b)

 運行結果:

[0. 2.]

 

(7)tf.nn.bias_add函數

函數定義

tf.nn.bias_add(value, bias, data_format=None, name=None)


對value加一偏置量
此函數為tf.add的特殊情況,bias僅為一維,
函數通過廣播機制進行與value求和,
數據格式可以與value不同,返回為與value相同格式

 官方釋義:

>>> help(tf.nn.bias_add)
Help on function bias_add in module tensorflow.python.ops.nn_ops:

bias_add(value, bias, data_format=None, name=None)
    Adds `bias` to `value`.

    This is (mostly) a special case of `tf.add` where `bias` is restricted to 1-D.
    Broadcasting is supported, so `value` may have any number of dimensions.
    Unlike `tf.add`, the type of `bias` is allowed to differ from `value` in the
    case where both types are quantized.

    Args:
      value: A `Tensor` with type `float`, `double`, `int64`, `int32`, `uint8`,
        `int16`, `int8`, `complex64`, or `complex128`.
      bias: A 1-D `Tensor` with size matching the last dimension of `value`.
        Must be the same type as `value` unless `value` is a quantized type,
        in which case a different quantized type may be used.
      data_format: A string. 'NHWC' and 'NCHW' are supported.
      name: A name for the operation (optional).

    Returns:
      A `Tensor` with the same type as `value`.

 使用示例:

out = tf.nn.bias_add(mergeFeatureMap, b)

 

(8)tf.nn.xw_plus_b函數

官方解釋:

>>> help(tf.nn.xw_plus_b)
Help on function xw_plus_b in module tensorflow.python.ops.nn_ops:

xw_plus_b(x, weights, biases, name=None)
    Computes matmul(x, weights) + biases.

    Args:
      x: a 2D tensor.  Dimensions typically: batch, in_units
      weights: a 2D tensor.  Dimensions typically: in_units, out_units
      biases: a 1D tensor.  Dimensions: out_units
      name: A name for the operation (optional).  If not specified
        "xw_plus_b" is used.

    Returns:
      A 2-D Tensor computing matmul(x, weights) + biases.
      Dimensions typically: batch, out_units.

 使用示例:

out = tf.nn.xw_plus_b(x, w, b, name = scope.name)

解釋:

xw_plus_b(x, weights, biases, name=None)相當於matmul(x, weights) + biases.

 

(9)tf.nn.conv2d函數

官方解釋:

>>> help(tf.nn.conv2d)
Help on function conv2d in module tensorflow.python.ops.gen_nn_ops:

conv2d(input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', dilations=[1, 1, 1, 1], name=None)
    Computes a 2-D convolution given 4-D `input` and `filter` tensors.

    Given an input tensor of shape `[batch, in_height, in_width, in_channels]`

    and a filter / kernel tensor of shape

    `[filter_height, filter_width, in_channels, out_channels]`, this op

    performs the following:



    1. Flattens the filter to a 2-D matrix with shape

       `[filter_height * filter_width * in_channels, output_channels]`.

    2. Extracts image patches from the input tensor to form a *virtual*

       tensor of shape `[batch, out_height, out_width,

       filter_height * filter_width * in_channels]`.

    3. For each patch, right-multiplies the filter matrix and the image patch

       vector.



    In detail, with the default NHWC format,



        output[b, i, j, k] =

            sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] *

                            filter[di, dj, q, k]



    Must have `strides[0] = strides[3] = 1`.  For the most common case of the same

    horizontal and vertices strides, `strides = [1, stride, stride, 1]`.

    Args:
      input: A `Tensor`. Must be one of the following types: `half`, `bfloat16`, `float32`.
        A 4-D tensor. The dimension order is interpreted according to the value

        of `data_format`, see below for details.
      filter: A `Tensor`. Must have the same type as `input`.
        A 4-D tensor of shape

        `[filter_height, filter_width, in_channels, out_channels]`
      strides: A list of `ints`.
        1-D tensor of length 4.  The stride of the sliding window for each

        dimension of `input`. The dimension order is determined by the value of

        `data_format`, see below for details.
      padding: A `string` from: `"SAME", "VALID"`.
        The type of padding algorithm to use.
      use_cudnn_on_gpu: An optional `bool`. Defaults to `True`.
      data_format: An optional `string` from: `"NHWC", "NCHW"`. Defaults to `"NHWC"`.
        Specify the data format of the input and output data. With the

        default format "NHWC", the data is stored in the order of:

            [batch, height, width, channels].

        Alternatively, the format could be "NCHW", the data storage order of:

            [batch, channels, height, width].
      dilations: An optional list of `ints`. Defaults to `[1, 1, 1, 1]`.
        1-D tensor of length 4.  The dilation factor for each dimension of

        `input`. If set to k > 1, there will be k-1 skipped cells between each

        filter element on that dimension. The dimension order is determined by the

        value of `data_format`, see above for details. Dilations in the batch and

        depth dimensions must be 1.
      name: A name for the operation (optional).

    Returns:
      A `Tensor`. Has the same type as `input`.

 函數釋義

此函數的作用是在給定四維輸入(input)和權重W(filter)的情況下計算二維卷積。

參數解釋:
input:
一個Tensor,每個元素的格式必須為float32或float64.
input的形狀:[batch,in_height,in_width,in_channels],
batch為訓練過程中每迭代一次迭代的照片數。
in_height,in_width分別為圖片的高和寬
in_channels為圖片的道。
filter:
一個Tensor,每個元素的類型和input類型一致。
filter的形狀:[filter_height,filter_width,in_channels,out_channels]
分別為權重的height,width,輸入的channels和輸出的channels
stride:
長度為4的list,元素類型為int。表示每一維度滑動的步長。
需要注意的是,strides[0]=strides[3]=1.
padding:
可選參數為"Same","VALID"
邊距,一般設為0,即padding='SAME'
use_cudnn_on_gpu:
bool類型,有True和False兩種選擇。
name:
此操作的名字

 

函數執行以下操作:
1.將參數filter變為一個二維矩陣,形狀為:[filter_height*filter_width*in_channels,output_channels]
2.將輸入(input)轉化為一個具有如下形狀的Tensor,形狀為:[batch,out_height,out_width,filter_height * filter_width * in_channels]
3.將filter矩陣和步驟2得到的矩陣相乘。

 

函數的返回值:
元素類型和input相同。

output[b, i, j, k] =sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] * filter[di, dj, q, k]

 編程示例:

kernel = tf.Variable(tf.truncated_normal([3,3,384,256], dtype=tf.float32, stddev=1e-1), name='weights')
conv = tf.nn.conv2d(conv3, kernel, [1,1,1,1],padding='SAME')

 

(10)tf.constant函數

官方解釋:

>>> help(tf.constant)
Help on function constant in module tensorflow.python.framework.constant_op:

constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
    Creates a constant tensor.

    The resulting tensor is populated with values of type `dtype`, as
    specified by arguments `value` and (optionally) `shape` (see examples
    below).

    The argument `value` can be a constant value, or a list of values of type
    `dtype`. If `value` is a list, then the length of the list must be less
    than or equal to the number of elements implied by the `shape` argument (if
    specified). In the case where the list length is less than the number of
    elements specified by `shape`, the last element in the list will be used
    to fill the remaining entries.

    The argument `shape` is optional. If present, it specifies the dimensions of
    the resulting tensor. If not present, the shape of `value` is used.

    If the argument `dtype` is not specified, then the type is inferred from
    the type of `value`.

    For example:

    ```python
    # Constant 1-D Tensor populated with value list.
    tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

    # Constant 2-D tensor populated with scalar value -1.
    tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                                 [-1. -1. -1.]]
    ```

    Args:
      value:          A constant value (or list) of output type `dtype`.

      dtype:          The type of the elements of the resulting tensor.

      shape:          Optional dimensions of resulting tensor.

      name:           Optional name for the tensor.

      verify_shape:   Boolean that enables verification of a shape of values.

    Returns:
      A Constant Tensor.

    Raises:
      TypeError: if shape is incorrectly specified or unsupported.

 程序示例:

 https://blog.csdn.net/qq_26591517/article/details/80198471


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM