【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
除去name参数用以指定该操作的name,与方法有关的一共五个参数:
第一个参数input:指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一

第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维

第三个参数strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4

第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式(后面会介绍)

第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true

结果返回一个Tensor,这个输出,就是我们常说的feature map
下面举一些例子:

1.考虑一种最简单的情况,现在有一张3×3单通道的图像(对应的shape:[1,3,3,1]),用一个1×1的卷积核(对应的shape:[1,1,1,1])去做卷积,最后会得到一张3×3的feature map

2.增加图片的通道数,使用一张3×3五通道的图像(对应的shape:[1,3,3,5]),用一个1×1的卷积核(对应的shape:[1,1,1,1])去做卷积,仍然是一张3×3的feature map,这就相当于每一个像素点,卷积核都与该像素点的每一个通道做点积
 

下面的例子老用到tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)这个函数解释一下  shape: 输出张量的形状,必选
    mean: 正态分布的均值,默认为0
    stddev: 正态分布的标准差,默认为1.0
    dtype: 输出的类型,默认为tf.float32
    seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
    name: 操作的名称

# [1,3,3,1]表示一张灰度图,长3,宽3,通道为1,每一列为一个通道的所有的值
input=tf.Variable(tf.random.normal([1,3,3,1]))
filter=tf.Variable(tf.random.normal([1,1,1,1]))#[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
op=tf.nn.conv2d(input=input,filter=filter,strides=[1,1,1,1],padding="VALID")
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("输入的图像是:",sess.run(input))
    print("卷积后的结果:",sess.run(op))

输出如下: (一张图片,三行三列,一个通道)

输入的图像是: 
[[[[-0.56106955]
   [-0.92634636]
   [ 1.7922001 ]]

  [[-0.5304697 ]
   [-2.101639  ]
   [ 0.58320063]]

  [[ 0.07622519]
   [ 1.6862288 ]
   [-0.80677295]]]]
卷积后的结果: [[[[ 0.5191843 ]
   [ 0.8571922 ]
   [-1.6584078 ]]

  [[ 0.4908688 ]
   [ 1.9447463 ]
   [-0.5396632 ]]

  [[-0.07053479]
   [-1.5603474 ]
   [ 0.7465453 ]]]]

例子2

# [1,3,3,5]表示一张图,长3,宽3,通道为5,每一列为一个通道的所有的值
input = tf.Variable(tf.random_normal([1,3,3,5]))
# [卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
filter = tf.Variable(tf.random_normal([3,3,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("image:")
    print(sess.run(input))
    print("卷积后的结果:")
    print(sess.run(op))

输出结果:# [1,3,3,5]表示一张图,长3,宽3,通道为5,每一列为一个通道的所有的值

image:
[[[[ 0.5132848  -1.4826995  -1.2568641  -0.5831219   0.7876634 ]
   [ 0.77361244  1.2108543  -0.9968469  -0.47282207  0.01052212]
   [-1.7480724   0.2090977   0.2047069  -0.24468827 -0.4472891 ]]

  [[-0.8105438   0.4982495   0.16561204 -0.18864419 -1.4639016 ]
   [-0.7106967   0.36177424 -1.6652572   0.23265974  1.394153  ]
   [-0.3832286  -0.26418412  0.8721776  -0.47917604 -0.42627367]]

  [[-1.6762115  -0.6360417  -0.6961393  -0.662259    1.2426376 ]
   [-0.89949626  0.41479877  0.414207    2.1668675   0.8219067 ]
   [-0.5728682  -0.7705654  -1.0622944  -0.9751777   0.39082402]]]]
卷积后的结果:
[[[[-11.548962]]]]

例子3

# [1,5,5,5]表示一张图,长5,宽5,通道为5,每一列为一个通道的所有的值 ,填充为不填充
# 卷积核为1个
input = tf.Variable(tf.random_normal([1,5,5,5]))
# [卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
filter = tf.Variable(tf.random_normal([3,3,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("image:")
    print(sess.run(input))
    print("卷积后的结果")
    print(sess.run(op))

结果如下:# [1,5,5,5]表示一张图,长5,宽5,通道为5,每一列为一个通道的所有的值 

image:
[[[[-0.4682449  -0.18028636 -0.04340089 -0.3701825   0.9828843 ]
   [ 0.04672529 -1.3158734   0.8865767  -1.6041739   0.14455956]
   [-0.75835234  0.2237678  -0.4788373  -0.7870056   1.0565816 ]
   [ 0.53504425  1.0913415  -1.1307987  -0.59806156  0.4798613 ]
   [ 0.7530046  -0.7013719   0.6349602  -0.4643517   1.2425092 ]]

  [[ 1.4131328   1.3182664   2.481456    0.8756981  -0.66990846]
   [-1.8685397   0.46309894  0.3368068   0.3862001  -0.03855935]
   [ 0.11134686  0.42550024  1.6448019  -1.0457219   0.12603214]
   [ 0.5551288  -0.5958016  -1.2349746   0.64116615  0.73918235]
   [ 1.2566805  -0.15052295 -0.88028395 -0.04307711  1.0945408 ]]

  [[ 1.1820707   1.0440774   0.84729743 -1.2455394   0.49069393]
   [-0.40630963  0.736282   -0.6686512  -1.1160136   1.0618513 ]
   [-0.19580077 -0.45660532  0.98971444  0.586482   -0.45692214]
   [ 0.3687279  -1.1561526  -1.9401842   0.16311368 -1.0008506 ]
   [ 1.641681    1.4354105   2.9591467   1.4867517  -1.8861685 ]]

  [[-0.73770297 -1.0871137  -1.3640782   0.468111   -0.624521  ]
   [ 0.97448033  1.2654406  -1.1626259  -0.17696889 -0.09259722]
   [ 0.5457276   1.509086   -0.6565353   1.4277203  -0.8421102 ]
   [-0.09781578 -0.40230936 -0.26226363 -0.8282829   1.0212438 ]
   [-1.0155399   1.0607773  -0.07317501 -0.24923332  1.222725  ]]

  [[-0.00398618 -0.4382571  -0.07638728  1.074729   -1.3572804 ]
   [-0.7621709  -1.493084    0.5480254   1.738965    0.8104816 ]
   [ 0.4774224  -1.2881348  -0.01758118  0.20779762 -1.1781982 ]
   [ 1.0424412  -2.2157788  -1.3187922   2.0893848  -0.07531907]
   [-0.08196148  0.29442054 -2.8640423   0.6671733  -0.3242342 ]]]]
卷积后的结果
[[[[ -2.4067125 ]
   [  6.375507  ]
   [  4.3147397 ]]

  [[-13.655651  ]
   [  8.784082  ]
   [-10.266729  ]]

  [[  4.2496934 ]
   [ 10.188244  ]
   [ -0.33550802]]]]

例子4 :# [1,5,5,5]表示一张灰度图,长5,宽5,通道为5,每一列为一个通道的所有的值,填充为SAME

# [1,5,5,5]表示一张灰度图,长5,宽5,通道为5,每一列为一个通道的所有的值,填充为SAME
input = tf.Variable(tf.random_normal([1,5,5,5]))
# [卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
filter = tf.Variable(tf.random_normal([3,3,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("image:")
    print(sess.run(input))
    print("卷积后的结果")
    print(sess.run(op))

结果如下:

image:
[[[[-1.1409752  -1.1237978   0.969993   -0.09330048 -0.30127382]
   [ 0.63473773 -1.6333404   1.0382167  -1.0538093   0.5920465 ]
   [ 0.26941565  2.5032096  -0.41017386 -0.35149956 -0.00662273]
   [ 1.3607591   0.3059951   1.4047183   0.5131309   0.17767662]
   [-0.33459455  0.3857227  -1.1943399  -0.7611989  -2.4287467 ]]

  [[ 0.3349859   0.89753264  0.8945862  -0.6194693   0.807246  ]
   [-0.20954335 -0.54819685 -0.68056875  0.9046989  -0.02225048]
   [-1.1926031  -1.5560101  -0.56738424  1.2402813   0.5743818 ]
   [-1.0525175  -1.7928473   0.16360243 -1.599834    1.4629184 ]
   [ 0.5839175  -1.1103965  -0.9763271  -0.7811771   0.26797712]]

  [[-0.19256076  1.2182405  -1.287789    0.90990317  0.00725219]
   [-1.0023963   0.04610913 -0.14161305  0.5849918   0.42820033]
   [ 1.7379142   0.6765723  -0.6650164  -1.0538291  -0.0482389 ]
   [-0.3708785   0.6836035  -0.2950405  -0.31865856  1.2135432 ]
   [ 0.6245332   0.26809996 -0.09483299  0.08727938  0.48154357]]

  [[ 0.6072183  -0.37852624 -0.4261594  -0.97623014  2.387473  ]
   [-2.0365734   0.9960824  -1.0964136   0.4029449   1.727248  ]
   [-1.0227396  -1.3240278  -0.17191216 -0.00259159 -0.54877645]
   [-1.1728064   0.5300268   0.41220528 -0.40797675  0.9998429 ]
   [ 2.049206   -0.64893293 -0.9790292  -0.2714394  -0.3230693 ]]

  [[ 0.35635975 -0.7298406   0.76442707 -0.2594927   0.9461474 ]
   [ 0.04276727  1.5086014  -0.5433718  -0.5811971   0.06988855]
   [-0.38529688  0.3048263   1.5012817  -0.45640737 -1.2598898 ]
   [ 0.07113422 -0.6659965   0.2967453  -0.53805673  1.1926779 ]
   [ 0.844934   -0.6964954  -1.001855    0.19012412 -1.1618123 ]]]]
卷积后的结果
[[[[  1.9395869 ]
   [  3.408945  ]
   [ 10.644713  ]
   [-10.05168   ]
   [  3.009207  ]]

  [[ -0.05803694]
   [ -6.5234036 ]
   [  1.5340072 ]
   [  0.23329571]
   [  5.222505  ]]

  [[ -8.625753  ]
   [  0.12694001]
   [ 11.025016  ]
   [ -0.6670954 ]
   [  2.7404003 ]]

  [[  0.85691464]
   [ -1.4353684 ]
   [  1.882115  ]
   [ -7.268232  ]
   [  3.8121133 ]]

  [[ -5.6740417 ]
   [  2.9583483 ]
   [  6.057668  ]
   [  2.5107303 ]
   [  4.8872213 ]]]]

例子5

# [1,5,5,4]表示一张图,长5,宽5,通道为4,每一列为一个通道的所有的值,填充为SAME
# 多个通道,一列对应一个通道
input = tf.Variable(tf.random_normal([1,5,5,4]))
# [卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
# 卷积核为4个,多个卷积核,一列对应一个卷积核的值
filter = tf.Variable(tf.random_normal([3,3,4,2])) 
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("image:")
    print(sess.run(input))
    print("卷积后的结果")
    print(sess.run(op))

结果如下

image:
[[[[-0.81038433  0.4908924   1.9690148  -0.8282694 ]
   [-0.16464615 -0.8573208   0.29004255  0.03818048]
   [-0.32927868  0.5398332  -1.9121236  -0.25695848]
   [-1.0202032  -0.68132657 -0.77879393  0.15370113]
   [ 1.0284289   0.56674284 -0.20789596  0.7484298 ]]

  [[-0.7928814   0.77266896 -0.20810893 -0.81787497]
   [ 0.23300223 -0.79838264  1.3215377   0.39917254]
   [-0.0618635  -0.75969124  1.0097345   0.25720242]
   [-1.0597924   0.42722633 -1.1346114   0.07566167]
   [-1.227777   -0.0230741   0.9681664  -0.9120347 ]]

  [[ 0.88350654  0.00969779 -0.29216427  0.54594785]
   [-0.0171537  -0.06930162  0.12149471  1.6519375 ]
   [ 1.0398278  -2.4265072  -0.58390397 -1.9859055 ]
   [-1.3154542  -0.7891132  -2.0217006   0.11550926]
   [-1.7240051  -0.07652733  1.0348661   0.0169086 ]]

  [[ 0.23951894 -0.24463224 -1.0696232   0.560231  ]
   [-0.06035039 -0.23435421 -1.1719884   0.38748133]
   [-0.3180004  -0.19286864 -1.3769702   1.3232151 ]
   [ 0.80730385  0.66791695 -0.8210284  -0.28847694]
   [ 1.3545961  -0.00695121  1.0915802  -1.7109725 ]]

  [[-0.8962259  -0.49197084 -0.07470534  0.72433543]
   [-2.207767   -0.2628684  -2.1513605   0.2071047 ]
   [ 0.13749672 -0.00349726  0.7467156   1.2117634 ]
   [-0.5633068  -0.36797187  2.0591488  -0.18073748]
   [ 0.5595831  -0.91768235  0.5489676  -0.47491565]]]]
卷积后的结果
[[[[ -0.9055767   -1.62395   ]
   [ -5.776704    -4.831062  ]
   [  4.050132    -1.535057  ]
   [  5.027299     3.244002  ]
   [  1.4259509    3.9830074 ]]

  [[ -1.1049969   -1.553334  ]
   [  0.56792307   1.1056217 ]
   [ -1.5550847   -3.5384884 ]
   [ 13.692962    -5.32364   ]
   [ -0.87802684  -4.1084433 ]]

  [[  0.8219489    2.9766183 ]
   [  4.6915445   -0.795457  ]
   [ -8.026212   -10.763276  ]
   [  1.9545813   -2.8438659 ]
   [  3.1292822    1.9184105 ]]

  [[  0.4767096    3.411282  ]
   [ -1.6467662  -10.085728  ]
   [ -5.008278     4.893556  ]
   [ -2.1377363   -3.2403936 ]
   [ -1.2786953   -8.594146  ]]

  [[ -2.6425986   -3.2640736 ]
   [ -2.5234895   -3.2555308 ]
   [  0.40479517   3.996325  ]
   [  1.924521    -4.2392616 ]
   [  0.46793342   2.3304608 ]]]]

例子6:

# 一次输入3张灰度图片(一个三维矩阵对应一个灰度图),长5,宽5,通道为4,每一列为一个通道的所有的值,填充为SAME
input = tf.Variable(tf.random_normal([3,5,5,4]))
# [卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
# 卷积核为2个,多个卷积核,一列对应一个卷积核的值
filter = tf.Variable(tf.random_normal([3,3,4,2]))
op = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("image:")
    print(sess.run(input))
    print("卷积后的结果")
    print(sess.run(op))

结果如下:

image:
[[[[ 1.53635308e-01  1.03884506e+00  1.07903707e+00  8.24316323e-01]
   [ 1.91776752e-01 -8.30539763e-01 -1.79751670e+00  4.87352520e-01]
   [ 1.06227541e+00  4.86781895e-02 -2.99694061e-01  7.24089921e-01]
   [ 1.43642557e+00  8.34476233e-01  6.72843814e-01  2.45144844e+00]
   [-1.65839088e+00 -2.23455876e-01  3.72853875e-01  1.05485475e+00]]

  [[-1.18803596e+00 -5.55876195e-01 -1.88906038e+00 -3.84862393e-01]
   [-3.17148089e-01  6.15328014e-01 -3.87931705e-01 -2.79249692e+00]
   [ 8.00744355e-01  2.23988724e+00  7.35527277e-01  5.42257547e-01]
   [-1.25806177e+00 -1.38351023e+00  3.27982903e-02  1.63545430e+00]
   [-2.35119417e-01  9.34464395e-01 -8.90825152e-01 -1.05454957e+00]]

  [[ 4.79633451e-01 -8.22432935e-01  1.20789969e+00  3.28979194e-02]
   [ 8.23050201e-01 -1.50640440e+00 -1.76254451e+00 -6.07840538e-01]
   [-8.12188148e-01 -4.81279343e-01  9.38147247e-01  9.09957409e-01]
   [-3.07247434e-02  5.97164452e-01 -1.55110085e+00 -1.44613457e+00]
   [ 5.88536441e-01  3.92024428e-01  2.15157986e-01  8.69177163e-01]]

  [[ 2.74412304e-01 -7.24911749e-01 -9.07158136e-01 -1.33607531e+00]
   [ 4.20112252e-01  6.25065491e-02  1.46902668e+00 -9.91747752e-02]
   [ 2.27860942e-01 -7.35325992e-01 -2.86795765e-01 -1.28156400e+00]
   [-1.34718478e-01 -1.83792040e-01 -2.65567213e-01 -6.81739450e-02]
   [-4.22643274e-01  6.54692128e-02  1.73036253e+00 -2.29927465e-01]]

  [[-1.34560549e+00  6.99492455e-01 -2.26296216e-01 -8.30034733e-01]
   [ 1.13504720e+00 -9.43745196e-01  8.65896642e-01 -2.60832876e-01]
   [-1.74425438e-01  9.23798978e-01  1.38949454e+00 -6.33741498e-01]
   [-1.01218009e+00 -5.10927856e-01  7.78521895e-01  1.02399111e+00]
   [ 8.77844989e-02  3.96178551e-02  1.20981254e-01 -3.44514638e-01]]]


 [[[ 2.00834942e+00 -1.98729503e+00 -1.74601208e-02  1.03592552e-01]
   [-5.09348214e-01 -5.62639534e-01  6.36668503e-03 -9.43195581e-01]
   [ 2.74394900e-01 -5.49671292e-01  8.75108302e-01  3.18915963e-01]
   [-6.15050375e-01  3.98222417e-01 -9.28766489e-01 -8.74403179e-01]
   [-1.28779352e+00 -1.12849522e+00  8.45810711e-01  5.06139994e-01]]

  [[ 1.17747772e+00  1.39069021e+00 -1.38835564e-01 -5.81767023e-01]
   [ 1.32587588e+00  1.71937212e-01  2.31155187e-01 -4.94351208e-01]
   [ 1.68254983e+00  1.23631978e+00 -2.94594723e-03 -1.20531964e+00]
   [ 3.74776632e-01 -1.42718172e+00  6.34151161e-01  1.82790589e+00]
   [ 7.94436514e-01 -6.62045538e-01  5.62724411e-01  2.16390148e-01]]

  [[-2.19735408e+00  4.96648639e-01 -4.79801059e-01  2.82878309e-01]
   [ 4.58469123e-01 -5.95887601e-01 -1.49449885e-01 -4.34755266e-01]
   [ 8.86520594e-02  1.03760886e+00 -7.89204121e-01 -6.03974722e-02]
   [-4.26606774e-01 -1.87084472e+00  1.02138984e+00 -1.48810400e-02]
   [-7.74623573e-01  4.07522678e-01 -1.05382375e-01  1.56563914e+00]]

  [[-1.27719581e+00  4.64721888e-01  3.45410645e-01  1.27466559e-01]
   [ 7.55401313e-01  2.28577718e-01  7.25611091e-01 -1.81044650e+00]
   [ 5.95962822e-01 -1.78135419e+00  5.87311924e-01  1.41913795e+00]
   [-8.80261719e-01 -4.43020165e-02  7.65748769e-02 -3.13030452e-01]
   [-1.06642449e+00  6.93855762e-01  2.51420283e+00 -1.65989697e+00]]

  [[ 1.14257634e+00  1.50013244e+00 -6.07369244e-01  6.79866850e-01]
   [ 9.16707039e-01  1.57703206e-01  1.39666975e-01 -2.15118027e+00]
   [ 6.47776783e-01 -1.46082926e+00  1.22409904e+00  1.34617329e+00]
   [ 1.22700036e-01  1.88744891e+00 -2.16918215e-01  2.65507042e-01]
   [ 6.94703460e-02  8.92494798e-01  5.25944605e-02  1.61747247e-01]]]


 [[[-3.96324992e-01 -3.33692223e-01  2.79586411e+00  1.97846547e-01]
   [ 2.06367278e+00  1.65573537e+00  2.51074135e-01 -1.12951994e+00]
   [ 3.54281723e-01  2.21664238e+00 -1.01315773e+00  1.67509580e+00]
   [-4.01435643e-01  2.36688542e+00  1.50528520e-01 -1.44664511e-01]
   [-4.52979535e-01  8.80505562e-01  8.18441153e-01 -6.89864516e-01]]

  [[-3.65744948e-01  2.18395066e+00 -4.51371878e-01  1.65358067e+00]
   [-9.33238447e-01 -8.84110034e-02 -8.56696725e-01  8.05212140e-01]
   [ 1.92164898e+00  1.29930937e+00  4.06517655e-01  9.35857534e-01]
   [ 3.10202837e-01 -8.91113043e-01  7.93851078e-01 -2.67490536e-01]
   [ 1.19806838e+00  6.39872193e-01 -2.95379423e-02  1.47858334e+00]]

  [[-3.60369414e-01 -1.31741583e-01 -1.65882838e+00  2.14929533e+00]
   [ 9.99533236e-01 -5.53711653e-01  2.02546939e-01 -2.15106583e+00]
   [-7.29170889e-02 -1.54819226e+00  1.04277983e-01 -5.02055705e-01]
   [ 2.95371592e-01 -8.27745736e-01  7.16369569e-01 -4.41060185e-01]
   [-1.26123261e+00 -6.45794034e-01  1.37882352e+00 -4.45511848e-01]]

  [[-3.89332145e-01  4.95256960e-01  1.16475010e+00  5.19814610e-01]
   [ 1.44140676e-01 -6.03462338e-01  2.40625329e-02  9.26429629e-01]
   [-2.26854563e+00 -9.44080427e-02  3.07964653e-01 -9.21386108e-02]
   [ 8.85409296e-01 -1.92358822e-01  6.95082843e-01  7.42532387e-02]
   [ 2.08116865e+00 -2.02707863e+00  1.21577561e+00  6.39305353e-01]]

  [[-2.65140057e-01  2.40486814e-03  2.85298437e-01 -2.45090425e-01]
   [-4.59114462e-01 -2.10925207e-01  2.90837348e-01  1.95346296e-01]
   [ 9.25775707e-01  1.46072721e+00  2.37411469e-01 -5.11007190e-01]
   [-1.99127340e+00 -1.65769428e-01 -1.75244510e+00  2.15708941e-01]
   [-1.55998278e+00 -9.17936116e-02  4.47341025e-01  7.22489417e-01]]]]
卷积后的结果
[[[[ 4.93046188e+00 -5.50716686e+00]
   [-4.75565815e+00  5.04524183e+00]
   [-5.97594404e+00  6.53879786e+00]
   [ 1.12639084e+01  2.90887141e+00]
   [ 5.39333940e-01 -4.60183620e+00]]

  [[ 2.63206124e+00  7.98155069e-01]
   [ 2.73889375e+00 -3.02457976e+00]
   [ 9.61839485e+00  4.30451012e+00]
   [-6.51957464e+00 -2.97327018e+00]
   [ 6.18201780e+00 -5.08061409e-01]]

  [[-1.75883102e+00 -9.98734093e+00]
   [-4.98765707e+00  9.21488762e+00]
   [-1.25066853e+00 -5.03425360e+00]
   [ 1.20456338e+00 -7.90365314e+00]
   [ 2.25546885e+00  7.73196602e+00]]

  [[ 2.30215144e+00 -1.69237316e+00]
   [ 5.04733706e+00 -4.51411772e+00]
   [-1.58419847e-01 -4.04315758e+00]
   [ 6.90670967e+00  6.67816925e+00]
   [-1.68379998e+00 -5.58459044e+00]]

  [[-3.18855286e+00 -5.78971565e-01]
   [-7.25815105e+00  1.29835498e+00]
   [ 6.77240896e+00 -1.29883111e+00]
   [-6.87898254e+00  6.85535371e-01]
   [-2.59345913e+00  2.50696516e+00]]]


 [[[-1.91889381e+00 -2.10183764e+00]
   [ 7.53384686e+00 -2.68040895e+00]
   [-7.36438942e+00  9.41266894e-01]
   [ 3.65440607e+00  2.32931995e+00]
   [-1.03806663e+00 -6.24378490e+00]]

  [[-2.40564561e+00  1.84347963e+00]
   [ 1.52958584e+00  1.88743567e+00]
   [-1.35406089e+00 -5.76866293e+00]
   [ 2.05691385e+00  1.58512306e+00]
   [ 5.51482487e+00 -5.29743814e+00]]

  [[-4.03985691e+00  1.65228307e+00]
   [-1.77604637e+01  5.18206739e+00]
   [ 1.74197006e+00 -3.63689482e-01]
   [ 2.08147645e+00 -1.71383018e+01]
   [ 4.05671787e+00  7.32192945e+00]]

  [[ 4.86477232e+00 -8.31753445e+00]
   [-6.63864040e+00  3.98017001e+00]
   [ 9.86388564e-01  3.43198419e+00]
   [ 8.47280979e+00 -8.12578773e+00]
   [ 5.77618504e+00  2.71020103e+00]]

  [[-3.52845478e+00 -7.11950421e-01]
   [-3.63057137e-01  2.59838796e+00]
   [-2.19249129e+00 -4.51054192e+00]
   [-1.29562402e+00  5.78010893e+00]
   [-5.77973080e+00  3.93492246e+00]]]


 [[[-5.60834503e+00  2.28914785e+00]
   [-1.37290084e+00  1.52926445e+01]
   [-4.75709581e+00  1.73869133e+00]
   [ 1.88377905e+00  7.39068890e+00]
   [-4.54392529e+00 -7.73543298e-01]]

  [[-1.66172922e+00 -1.04563999e+00]
   [-1.26027088e+01  1.13547745e+01]
   [-2.89508057e+00 -3.50523710e+00]
   [-1.02942638e+01 -4.43019724e+00]
   [-1.05159104e+00  1.44464660e+00]]

  [[-1.20190501e-01  3.84477258e-01]
   [ 6.81039429e+00 -9.35764503e+00]
   [-3.30731988e+00  1.42793655e-01]
   [ 3.88063288e+00 -5.41379118e+00]
   [ 6.13060236e-01 -1.47510588e+00]]

  [[ 3.59563828e+00 -5.29299736e+00]
   [-1.12149715e-02  6.62296200e+00]
   [-1.48255253e+00 -6.52119875e+00]
   [-7.03836250e+00  1.22576368e+00]
   [ 4.20157576e+00  7.05133021e-01]]

  [[ 3.06560969e+00 -9.46631074e-01]
   [-3.09154510e+00  2.09830940e-01]
   [ 8.33938408e+00  1.09179020e+00]
   [-1.52850890e+00  5.25922966e+00]
   [-3.61025739e+00 -1.59407270e+00]]]]

例子7

# [1,5,5,5]表示一张灰度图,长5,宽5,通道为4,每一列为一个通道的所有的值,填充为SAME
# 多个通道,一列对应一个通道
input = tf.Variable(tf.random_normal([1,5,5,4]))
# [卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
# 卷积核为2个,多个卷积核,一列对应一个卷积核的值
filter = tf.Variable(tf.random_normal([3,3,4,2]))
op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print("image:")
    print(sess.run(input))
    print("卷积后的结果")
    print(sess.run(op))
print(op.shape)

结果如下:

image:
[[[[-0.07282767 -0.12567247  1.2285757   0.6662424 ]
   [ 1.4098632   0.6767542  -0.7640455  -0.57512856]
   [-1.0696435   0.39211977  0.22243913 -0.08474918]
   [ 1.1162896   1.2884071  -0.06142229 -0.45730448]
   [-1.4525018   0.48255727  0.30358532 -1.0893219 ]]

  [[ 1.9596944   2.0726337  -1.1265371  -0.03498291]
   [ 0.5387959   0.20751393 -1.4679326   0.3267714 ]
   [ 0.42708012  0.42108545  0.32346714  0.42406213]
   [-1.7963724   1.2988467   1.7694956  -0.9542209 ]
   [-1.0659063   0.58493865 -0.9555197   0.7425708 ]]

  [[ 0.37199903  0.22023673  0.20848778 -0.55662507]
   [-1.1750736   0.06945211 -2.1414602   0.23686293]
   [ 1.8197973  -1.639262    0.86372995 -0.44620314]
   [ 1.0902803   0.3587005  -1.1201538   0.8774452 ]
   [-1.4613754   0.49779564  1.0938331  -0.46720052]]

  [[-1.0407486   1.2318851   0.6670653   0.19556291]
   [-2.118173   -1.5438284   0.87955725 -0.32061562]
   [-0.354328   -1.7943884  -1.0703725  -1.876893  ]
   [-1.0503411  -0.22178032 -0.20079158  1.4894155 ]
   [-1.5795097  -0.11479913  0.789702   -0.29888964]]

  [[-1.2016894  -0.2634017   0.6039658   0.77531904]
   [ 0.02828266  0.9416176  -0.17481273 -2.4545903 ]
   [ 0.44502407 -1.2004696   0.29415184  0.05564101]
   [-0.5162365  -0.29872933  1.7654521   0.59423524]
   [-0.98648417 -0.40785882 -1.5224134   0.42334327]]]]
卷积后的结果
[[[[ 3.994135    0.38079923]
   [-7.8799067  -5.377478  ]
   [-2.5858035  -7.0294294 ]]

  [[-4.7147813   1.0494363 ]
   [-3.59727    -5.7471647 ]
   [ 5.4788938   1.7078321 ]]

  [[-2.365794    3.4435854 ]
   [ 4.966667    4.106488  ]
   [-3.1391191   4.3564405 ]]]]
(1, 3, 3, 2)

 

参考博客 https://blog.csdn.net/mao_xiao_feng/article/details/53444333

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值