# 目录

## 参考资料

①卷积核旋转180

②对应位置相乘，然后累加

 举例

[[2, 1, 0, 2, 3],

[9, 5, 2, 2, 0],

[2, 3, 4, 5, 6],

[1, 2, 3, 1, 0],

[0, 4, 4, 2, 8]]

[[1, 0, -1],

[1, 0, -1],

[1, 0, -1]]

import tensorflow as tf

# [batch, in_height, in_width, in_channels]
input = tf.constant([
[2, 1, 0, 2, 3],
[9, 5, 4, 2, 0],
[2, 3, 4, 5, 6],
[1, 2, 3, 1, 0],
[0, 4, 4, 2, 8],
], tf.float32)
input = tf.reshape(input, [1, 5, 5, 1])

#定义旋转180后的卷积核
# [filter_height, filter_width, in_channels, out_channels]
kernel = tf.constant([
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]
], tf.float32)
kernel = tf.reshape(kernel, [3, 3, 1, 1])

print(tf.Session().run(tf.nn.conv2d(input,kernel,[1,1,1,1],"VALID")))

[[[[-5.]   [ 0.]   [ 1.]]
[[-1.]   [-2.]   [-5.]]
[[ 8.]   [-1.]   [ 3.]]]]
View Code

import numpy as np
from scipy import signal

input = np.array([
[2, 1, 0, 2, 3],
[9, 5, 4, 2, 0],
[2, 3, 4, 5, 6],
[1, 2, 3, 1, 0],
[0, 4, 4, 2, 8]
])

#定义未经旋转的卷积核
kernel = np.array([
[1, 0, -1],
[1, 0, -1],
[1, 0, -1]
])
# kernel1 = np.flip(kernel1)
print(signal.convolve(input, kernel, mode="valid"))

[[-5  0  1]
[-1 -2 -5]
[ 8 -1  3]]
View Code

 结论

 参考资料

https://www.jianshu.com/p/8dfe02b61686

https://wenku.baidu.com/view/f55e1bc6f90f76c661371ac5.html

posted @ 2019-07-19 11:30  黎明程序员  阅读(2498)  评论(0编辑  收藏  举报