tf.nn.conv2d()函數
參數介紹:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
input:輸入參數,具有這樣的shape[batch, in_height, in_width, in_channels],分別是[batch張圖片, 每張圖片高度為in_height, 每張圖片寬度為in_width, 圖像通道為in_channels].
filter:濾波器,濾波器的shape為[filter_height, filter_width, in_channels, out_channels],分別對應[濾波器高度, 濾波器寬度, 接受圖像的通道數, 卷積后通道數],其中第三個參數 in_channels需要與input中的第四個參數 in_channels一致.
strides:代表步長,其值可以直接默認一個數,也可以是一個四維數如[1,2,1,1],則其意思是水平方向卷積步長為第二個參數2,垂直方向步長為1.
padding:代表填充方式,參數只有兩種,SAME和VALID,SAME比VALID的填充方式多了一列,比如一個3*3圖像用2*2的濾波器進行卷積,當步長設為2的時候,會缺少一列,則進行第二次卷積的時候,VALID發現余下的窗口不足2*2會直接把第三列去掉,SAME則會填充一列,填充值為0.
use_cudnn_on_gpu:bool類型,是否使用cudnn加速,默認為true.
name:給返回的tensor命名。給輸出feature map起名字.
例子:
一張3*3的圖片,元素如下:
* | * | * |
---|---|---|
0 | 3 | 6 |
1 | 4 | 7 |
2 | 5 | 8 |
卷積核為1個2*2的卷積,如下:
* | * |
---|---|
0 | 2 |
1 | 3 |
TensorFlow代碼(padding為SAME):
import tensorflow as tf
import numpy as np
g = tf.Graph()
with g.as_default() as g:
input = tf.Variable(np.array(range(9), dtype=np.float32).reshape(1,3,3,1))
filter = tf.Variable(np.array(range(4), dtype=np.float32).reshape(2,2,1,1))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
a,b,c = sess.run([input, filter, op])
print(a)
print(b)
print(c)
輸出:
[[[[ 0.]
[ 1.]
[ 2.]]
[[ 3.]
[ 4.]
[ 5.]]
[[ 6.]
[ 7.]
[ 8.]]]]
[[[[ 0.]]
[[ 1.]]]
[[[ 2.]]
[[ 3.]]]]
[[[[ 19.]
[ 25.]
[ 10.]]
[[ 37.]
[ 43.]
[ 16.]]
[[ 7.]
[ 8.]
[ 0.]]]]
即卷積后的結果為:
* | * | * |
---|---|---|
19 | 37 | 7 |
25 | 43 | 8 |
10 | 16 | 0 |
如果padding為VALID,則輸出如下:
[[[[ 0.]
[ 1.]
[ 2.]]
[[ 3.]
[ 4.]
[ 5.]]
[[ 6.]
[ 7.]
[ 8.]]]]
[[[[ 0.]]
[[ 1.]]]
[[[ 2.]]
[[ 3.]]]]
[[[[ 19.]
[ 25.]]
[[ 37.]
[ 43.]]]]
即卷積后的結果為:
* | * |
---|---|
19 | 37 |
25 | 43 |
tf.nn.max_pool()函數
tf.nn.max_pool(value, ksize, strides, padding, 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]這種形式.
TensorFlow代碼:
import tensorflow as tf
import numpy as np
g = tf.Graph()
with g.as_default() as g:
input = tf.Variable(np.array(range(9), dtype=np.float32).reshape(1,3,3,1))
filter = tf.Variable(np.array(range(4), dtype=np.float32).reshape(2,2,1,1))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
pool = tf.nn.max_pool(op, [1,2,2,1], [1,1,1,1], padding='SAME')
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
PL = sess.run(pool)
print(PL)
輸出:
[[[[ 43.]
[ 43.]
[ 16.]]
[[ 43.]
[ 43.]
[ 16.]]
[[ 8.]
[ 8.]
[ 0.]]]]
* | * | * |
---|---|---|
43 | 43 | 8 |
43 | 43 | 8 |
16 | 16 | 0 |
tf.nn.avg_pool()
計算方法: 計算非padding的元素的平均值
例子:
import tensorflow as tf
import numpy as np
g = tf.Graph()
with g.as_default() as g:
input = tf.Variable(np.array(range(9), dtype=np.float32).reshape(1,3,3,1))
filter = tf.Variable(np.array(range(4), dtype=np.float32).reshape(2,2,1,1))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
pool = tf.nn.avg_pool(op, [1,2,2,1], [1,1,1,1], padding='SAME')
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
PL = sess.run(pool)
print(PL)
輸出為:
[[[[31. ]
[23.5 ]
[13. ]]
[[23.75]
[16.75]
[ 8. ]]
[[ 7.5 ]
[ 4. ]
[ 0. ]]]]
* | * | * |
---|---|---|
31 | 23.75 | 7.5 |
23.5 | 16.75 | 4. |
13. | 8. | 0. |
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: 命名空間
tensorflow中的dropout就是:shape不變,使輸入tensor中某些元素按照一定的概率變為0,其它沒變0的元素變為原來的1/keep_prob.
dropout層的作用: 防止神經網絡的過擬合
例子:
import tensorflow as tf
g = tf.Graph()
with g.as_default() as g:
mat = tf.Variable(tf.ones([10,10]))
dropout_mat = tf.nn.dropout(mat, keep_prob=0.5)
with tf.Session(graph=g) as sess:
sess.run(tf.global_variables_initializer())
output, dropout = sess.run([mat, dropout_mat])
print(output)
print(dropout)
輸出:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
[[2. 0. 0. 0. 2. 0. 2. 2. 0. 2.]
[0. 2. 0. 0. 2. 2. 0. 0. 0. 0.]
[2. 2. 2. 0. 0. 2. 0. 2. 0. 0.]
[2. 0. 0. 0. 2. 2. 2. 0. 2. 0.]
[0. 2. 2. 0. 2. 2. 2. 2. 0. 2.]
[2. 0. 0. 0. 2. 0. 0. 2. 0. 2.]
[2. 2. 0. 2. 2. 0. 0. 0. 2. 2.]
[2. 0. 0. 0. 0. 2. 0. 2. 0. 0.]
[2. 2. 0. 0. 0. 0. 0. 2. 0. 0.]
[2. 0. 2. 2. 2. 2. 0. 2. 0. 0.]]
tf.reshape()
shape里最多有一個維度的值可以填寫為-1,表示自動計算此維度