深入理解tensorflowTensorFlow技术帖Tensorflow从入门到精通

TensorFlow(3)CNN中的函数

2018-08-22  本文已影响7人  山阴少年

tf.nn.conv2d()函数

参数介绍:

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)

例子:

一张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)

参数是四个,和卷积函数很类似:

返回一个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)

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,表示自动计算此维度

上一篇 下一篇

猜你喜欢

热点阅读