张量流中补丁的点积
Dot product of patches in tensorflow
我有两个大小相同的方阵和一个方块的尺寸。我想计算每对补丁之间的点积。本质上我想实现以下操作:
def patch_dot(A, B, patch_dim):
res_dim = A.shape[0] - patch_dim + 1
res = np.zeros([res_dim, res_dim, res_dim, res_dim])
for i in xrange(res_dim):
for j in xrange(res_dim):
for k in xrange(res_dim):
for l in xrange(res_dim):
res[i, j, k, l] = (A[i:i + patch_dim, j:j + patch_dim] *
B[k:k + patch_dim, l:l + patch_dim]).sum()
return res
显然这是一个非常低效的实现。 Tensorflow 的 tf.nn.conv2d 似乎是一个自然的解决方案,因为我本质上是在做一个卷积,但是我的过滤器矩阵不是固定的。在 Tensorflow 中是否有自然的解决方案,或者我应该开始考虑实现我自己的 tf-op?
执行此操作的自然方法是首先使用 tf.extract_image_patches, then to apply the tf.nn.conv2D function on A and each B sub-patch using tf.map_fn.
提取矩阵 B 的重叠图像块
注意使用前tf.extract_image_patches and tf.nn.conv2D you need to reshape your matrices as 4D tensors of shape [1, width, height, 1]
using tf.reshape.
此外,在使用之前 tf.map_fn, you would also need to use the tf.transpose op so that the B sub-patches are indexed by the first dimension of the tensor you use as the elems
argument of tf.map_fn。
我有两个大小相同的方阵和一个方块的尺寸。我想计算每对补丁之间的点积。本质上我想实现以下操作:
def patch_dot(A, B, patch_dim):
res_dim = A.shape[0] - patch_dim + 1
res = np.zeros([res_dim, res_dim, res_dim, res_dim])
for i in xrange(res_dim):
for j in xrange(res_dim):
for k in xrange(res_dim):
for l in xrange(res_dim):
res[i, j, k, l] = (A[i:i + patch_dim, j:j + patch_dim] *
B[k:k + patch_dim, l:l + patch_dim]).sum()
return res
显然这是一个非常低效的实现。 Tensorflow 的 tf.nn.conv2d 似乎是一个自然的解决方案,因为我本质上是在做一个卷积,但是我的过滤器矩阵不是固定的。在 Tensorflow 中是否有自然的解决方案,或者我应该开始考虑实现我自己的 tf-op?
执行此操作的自然方法是首先使用 tf.extract_image_patches, then to apply the tf.nn.conv2D function on A and each B sub-patch using tf.map_fn.
提取矩阵 B 的重叠图像块注意使用前tf.extract_image_patches and tf.nn.conv2D you need to reshape your matrices as 4D tensors of shape [1, width, height, 1]
using tf.reshape.
此外,在使用之前 tf.map_fn, you would also need to use the tf.transpose op so that the B sub-patches are indexed by the first dimension of the tensor you use as the elems
argument of tf.map_fn。