tf.nn.conv2d实现卷积的过程

 

#coding=utf-8

import tensorflow as tf
#case 2
input = tf.Variable(tf.round(10 * tf.random_normal([1,3,3,2])))
filter = tf.Variable(tf.round(5 * tf.random_normal([1,1,2,1])))
op2 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
#对于filter,多个输入通道,变成一个输入通道,是对各个通道上的卷积值进行相加
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print("case 2")
    print("input: ", sess.run(input))
    print("filter: ", sess.run(filter))
    print("conv ", sess.run(op2))
# case 2
# input:  [[[[-14. -11.]
#    [  2.   2.]
#    [ 25.  18.]]
#
#   [[  8.  13.]
#    [ -7.  -7.]
#    [ 11.   6.]]
#
#   [[ -1.   8.]
#    [ 18.  10.]
#    [ -2.  19.]]]]
# filter:  [[[[-3.]
#    [ 2.]]]]

# conv  [[[[ 20.]
#    [ -2.]
#    [-39.]]
#
#   [[  2.]
#    [  7.]
#    [-21.]]
#
#   [[ 19.]
#    [-34.]
#    [ 44.]]]]

 

 
#转换:输入为3*3的2通道数据
#通道1:
#[-14 2 25],
#[8 -7 11],
#[-1 18 -2]
#通道2:
#[-11 2 18],
#[13 -7 6],
#[8 10 19]
#conv转换
#[20 -2 -39],
#[2 -7 -21],
#[9 -34 44]

#计算过程
#[-14 2 25],
#[8 -7 11],  *  [-3]  +     
#[-1 18 -2]
#[-11 2 18],
#[13 -7 6],  * [2]
#[8 10 19]
#result
#[20 -2 -39],
#[2 -7 -21],
#[9 -34 44]
 
 

 

posted on 2017-09-26 18:26  Maddock  阅读(2459)  评论(0编辑  收藏  举报

导航