欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tensorflow随笔-tf.nn.conv2d卷积运算(1)

程序员文章站 2022-06-12 17:06:38
...
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

 
 
tf.nn.conv2d
"""

import tensorflow as tf

g=tf.Graph()

with g.as_default():
    x=tf.constant([
            [[[3.]]
            ]])
    kernel=tf.constant([[[[2.]]]])
    y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="SAME")

with tf.Session(graph=g) as sess:
    print sess.run(x)
    print sess.run(kernel)

    print x.get_shape()
    print kernel.get_shape()

    print sess.run(y)

[[[[3.]]]]
[[[[2.]]]]
(1, 1, 1, 1)
(1, 1, 1, 1)
[[[[6.]]]]

 #!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

 
 
"""

import tensorflow as tf

g=tf.Graph()

with g.as_default():
    x=tf.constant([
            [[[1.],[2.]],[[3.],[4.]],[[5.],[6.]]],
            [[[10.],[20.]],[[30.],[40.]],[[50.],[60.]]]
            ])
    kernel=tf.constant([[[[2.]]]])
    y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="SAME")

with tf.Session(graph=g) as sess:
    print x.get_shape()
    print sess.run(y)

(2, 3, 2, 1)
[[[[ 2.]
[ 4.]]

[[ 6.]
[ 8.]]

[[ 10.]
[ 12.]]]

[[[ 20.]
[ 40.]]

[[ 60.]
[ 80.]]

[[100.]
[120.]]]]

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  2 13:23:27 2018

 
 
tf.nn.conv2d
"""

import tensorflow as tf

g=tf.Graph()

with g.as_default():
    x=tf.constant([
            [[[1.],[2.]],[[3.],[4.]],[[5.],[6.]]],
            [[[10.],[20.]],[[30.],[40.]],[[50.],[60.]]]
            ])
    kernel=tf.constant([[[[2.,3.]]]])
    y=tf.nn.conv2d(x,kernel,strides=[1,1,1,1],padding="SAME")

with tf.Session(graph=g) as sess:
    print sess.run(x)
    print x.get_shape()
    print sess.run(y)

[[[[ 1.]
[ 2.]]

[[ 3.]
[ 4.]]

[[ 5.]
[ 6.]]]

[[[10.]
[20.]]

[[30.]
[40.]]

[[50.]
[60.]]]]
(2, 3, 2, 1)
[[[[ 2. 3.]
[ 4. 6.]]

[[ 6. 9.]
[ 8. 12.]]

[[ 10. 15.]
[ 12. 18.]]]

[[[ 20. 30.]
[ 40. 60.]]

[[ 60. 90.]
[ 80. 120.]]

[[100. 150.]
[120. 180.]]]]

相关标签: AI