在 tensorflow 中,我找不到使用周期性边界条件进行卷积(tf.nn.conv2d)的直接可能性。
例如取张量
[[1,2,3],
[4,5,6],
[7,8,9]]
Run Code Online (Sandbox Code Playgroud)
和任何 3x3 过滤器。原则上可以通过对 5x5 进行周期性填充来完成具有周期性边界条件的卷积
[[9,7,8,9,7],
[3,1,2,3,1],
[6,4,5,6,4],
[9,7,8,9,7],
[3,1,2,3,1]]
Run Code Online (Sandbox Code Playgroud)
然后在“有效”模式下与过滤器进行卷积。然而,不幸的是,函数tf.pad不支持周期性填充。
有简单的解决方法吗?
以下应该适用于您的情况:
import tensorflow as tf
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
b = tf.tile(a, [3, 3])
result = b[2:7, 2:7]
sess = tf.InteractiveSession()
print(result.eval())
# prints the following
array([[9, 7, 8, 9, 7],
[3, 1, 2, 3, 1],
[6, 4, 5, 6, 4],
[9, 7, 8, 9, 7],
[3, 1, 2, 3, 1]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)
正如评论中所指出的,这在内存方面有点低效。如果内存对您来说是一个问题,但愿意花费一些计算,以下也将起作用:
pre = tf.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]])
post = tf.transpose(pre)
result = tf.matmul(tf.matmul(pre, a), post)
print(result.eval())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2713 次 |
| 最近记录: |