tf.nn.conv2d()函數(shù)
參數(shù)介紹:
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
input:輸入?yún)?shù)姥闭,具有這樣的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]逮光,分別對應(yīng)[濾波器高度, 濾波器寬度, 接受圖像的通道數(shù), 卷積后通道數(shù)]酥泞,其中第三個參數(shù) in_channels需要與input中的第四個參數(shù) in_channels一致.
strides:代表步長央串,其值可以直接默認一個數(shù)尖殃,也可以是一個四維數(shù)如[1,2,1,1]以故,則其意思是水平方向卷積步長為第二個參數(shù)2坦弟,垂直方向步長為1.
padding:代表填充方式护锤,參數(shù)只有兩種,SAME和VALID酿傍,SAME比VALID的填充方式多了一列烙懦,比如一個3*3圖像用2*2的濾波器進行卷積,當(dāng)步長設(shè)為2的時候赤炒,會缺少一列氯析,則進行第二次卷積的時候,VALID發(fā)現(xiàn)余下的窗口不足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.]]]]
即卷積后的結(jié)果為:
* | * | * |
---|---|---|
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.]]]]
即卷積后的結(jié)果為:
* | * |
---|---|
19 | 37 |
25 | 43 |
tf.nn.max_pool()函數(shù)
tf.nn.max_pool(value, ksize, strides, padding, name=None)
參數(shù)是四個,和卷積函數(shù)很類似:
value:需要池化的輸入誊锭,一般池化層接在卷積層后面表悬,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape.
ksize:池化窗口的大小丧靡,取一個四維向量蟆沫,一般是[1, height, width, 1]籽暇,因為我們不想在batch和channels上做池化,所以這兩個維度設(shè)為了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:輸入?yún)?shù)
- keep_prob:保留比例惶看。 取值 (0,1] 捏顺。每一個參數(shù)都將按這個比例隨機變更
- noise_shape:干擾形狀。 此字段默認是None纬黎,表示第一個元素的操作都是獨立幅骄,但是也不一定。比例:數(shù)據(jù)的形狀是shape(x)=[k, l, m, n]本今,而noise_shape=[k, 1, 1, n]拆座,則第1和4列是獨立保留或刪除,第2和3列是要么全部保留冠息,要么全部刪除挪凑。
- seed:隨機數(shù)種子
- name: 命名空間
tensorflow中的dropout就是:shape不變,使輸入tensor中某些元素按照一定的概率變?yōu)?逛艰,其它沒變0的元素變?yōu)樵瓉淼?/keep_prob.
dropout層的作用: 防止神經(jīng)網(wǎng)絡(luò)的過擬合
例子:
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躏碳,表示自動計算此維度