将偏移量添加到张量的一个维度,偏移量是另一个张量的输入

时间:2018-02-21 12:59:22

标签: python tensorflow deep-learning

我正在寻找一种使用TensorFlow对下面的示例进行编码的有效方法。下面,它以最愚蠢的方式用numpy编码 我们的想法是使用另一个张量值来抵消高度维度上的张量(batch_sizeheightwidthchannels)的张量。换句话说:

tensor_2[i, j, k, l] = tensor_1[i, j + tensor_offset[i, j, k, l], k, l]

以下是我使用的代码:

import numpy as np 
import time

begin = time.time()
b, h, w ,c = 5, 256, 512, 20
offset = np.random.rand(b, h, w , c).astype(int)
image = np.ones((b, h, w ,c))
label = np.ones((b, h, w ,c))
label_offset = np.zeros ((b, h, w,c ))

loss = 0
count = 0

for i in range(b):
    for j in range (h):
        for k in range (w):
            for l in range (c): 
                offset_ = j + np.int(offset [i,j,k,l])
                if offset_ > 255:
                    pass
                else:
                    label_offset[i,j,k,l] = label [i,offset_,k,l]
                    loss =+ label_offset[i,j,k,l]*np.log(image [i,j,k,l])
                    count=+1


loss = loss/count
end = time.time()
print ('duree:', end - begin)

3 个答案:

答案 0 :(得分:1)

如果您只沿一个维度(上面为height)对数据进行抽样,则

tf.gather更容易使用:

indices = tf.mod(tf.range(h) + offset, h)
output = tf.gather(input_tensor, indices, axis=1)

答案 1 :(得分:0)

您可以使用indices[i, j, k, l]= [i, j+ tensor_offset [i,j,k,l], k, l] indices进行此操作(因此import numpy as np import tensorflow as tf b, h, w ,c = 2, 11, 13, 7 final_shape = [b, h, w, c] offset = np.random.randint(low=0, high=h, size=final_shape) image = np.random.randint(low=0, high=1000, size=final_shape) input_tensor = tf.constant(image) m1 = tf.transpose(tf.reshape(tf.tile(tf.range(b), multiples=[h * w * c]), [h, w, c, b]), perm=[3, 0, 1, 2]) m2 = tf.transpose(tf.reshape(tf.tile(tf.range(h), multiples=[b * w * c]), [b, w, c, h]), perm=[0, 3, 1, 2]) + offset not_too_big = tf.less(m2, h) m2_safe = tf.mod(m2, h) # Makes sure we don't go too far in the original array m3 = tf.transpose(tf.reshape(tf.tile(tf.range(w), multiples=[b * h * c]), [b, h, c, w]), perm=[0, 1, 3, 2]) m4 = tf.reshape(tf.tile(tf.range(c), multiples=[b * h * w]), [b, h, w, c]) # No transposition needed here indices = tf.stack([m1, m2_safe, m3, m4], axis=-1) tmp = tf.gather_nd(input_tensor, indices) output = tf.multiply(tmp, tf.cast(not_too_big, tmp.dtype)) # Sets all the values corresponding to j+offset>h to 0 的维度为5)。

您可以像这样构建它:

$q->whereIn('eat_categories.id', $kitchen);

编辑:这适用于换位。

答案 2 :(得分:0)

@gdelab 我对您的代码进行了以下改进:

def tensor_offset (input_tensor, offset_tensor, batch, nbcl):

    b, h, w ,c = batch, 256,512,nbcl

    m = tf.reshape(tf.tile(tf.range(b), multiples=[w*h*c]), [h,w,c, b])
    m1 = tf.reshape(tf.tile(tf.range(h), multiples=[w*b*c]), [b,w,c,h])
    m2 = tf.reshape(tf.tile(tf.range(w), multiples=[h*b*c]), [b,h,c,w])
    m2 = m2 +tf.transpose(tf.cast(offset_tensor,tf.int32),perm=[0, 1, 3, 2])
    m3 = tf.reshape(tf.tile(tf.range(c), multiples=[h*b*w]), [b,h,w,c])

    indices = tf.stack([tf.transpose(m,perm=[3,0,1,2]), tf.transpose(m1,perm=[0, 3, 1, 2]), tf.transpose(m2,perm=[0, 1, 3, 2]),m3], axis=-1)
    paddings = tf.constant([[0, 0], [0, 0], [0,100], [0,0]])
    output = tf.gather_nd(tf.pad(input_tensor, paddings), indices)

    return output