Python 如何交错分配4个子传感器到一个更大的张量?

Python 如何交错分配4个子传感器到一个更大的张量?,python,tensorflow,Python,Tensorflow,我想用交错索引的4个子传感器得到更大的张量 我的问题是: 带形状的四个输入[批次x 2m x 2n x 1] 输出形状为[批次x 4m x 4n x 1] 索引_y_0_1=[0,1,4,5,8,9…], 索引_y_2_3=[2,3,6,7,10,11…] 索引x_0_1=[0,1,4,5,8,9…], 指数x_2_3=[2,3,6,7,10,11…] 输出[index_y_0_1,index_x_0_1]=输入0 输出[index_y_0_1,index_x_2_3]=输入1 输出[index

我想用交错索引的4个子传感器得到更大的张量

我的问题是:

带形状的四个输入[批次x 2m x 2n x 1]

输出形状为[批次x 4m x 4n x 1]

索引_y_0_1=[0,1,4,5,8,9…], 索引_y_2_3=[2,3,6,7,10,11…]

索引x_0_1=[0,1,4,5,8,9…], 指数x_2_3=[2,3,6,7,10,11…]

输出[index_y_0_1,index_x_0_1]=输入0

输出[index_y_0_1,index_x_2_3]=输入1

输出[index_y_2_3,index_x_0_1]=输入2

输出[index_y_2_3,index_x_2_3]=输入3

下面是我关于python代码的问题:

import numpy as np

UpperLeft = np.ones((3,2,4,1))
UpperRight = np.ones((3,2,4,1))*2
BottonLeft = np.ones((3,2,4,1))*3
BottonRight = np.ones((3,2,4,1))*4

output = np.zeros((UpperLeft.shape[0], UpperLeft.shape[1]*2, UpperLeft.shape[2]*2, 1))

assert(output.shape[1]%4 == 0)
assert(output.shape[2]%4 == 0)

# UpperLeft Assignment
start_y = 0
start_x = 0
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = UpperLeft[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = UpperLeft[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = UpperLeft[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = UpperLeft[:,1::2, 1::2, :]

# UpperRight Assignment
start_y = 0
start_x = 2
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = UpperRight[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = UpperRight[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = UpperRight[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = UpperRight[:,1::2, 1::2, :]

# BottonLeft Assignment
start_y = 2
start_x = 0
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = BottonLeft[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = BottonLeft[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = BottonLeft[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = BottonLeft[:,1::2, 1::2, :]

# BottonRight Assignment
start_y = 2
start_x = 2
output[:,(start_y + 0)::4, (start_x + 0)::4, :] = BottonRight[:,0::2, 0::2, :]
output[:,(start_y + 0)::4, (start_x + 1)::4, :] = BottonRight[:,0::2, 1::2, :]
output[:,(start_y + 1)::4, (start_x + 0)::4, :] = BottonRight[:,1::2, 0::2, :]
output[:,(start_y + 1)::4, (start_x + 1)::4, :] = BottonRight[:,1::2, 1::2, :]

show_out = output[0,:,:,0]

如何在tensorflow上执行此操作?谢谢

你可以有一个函数,像这样在一个轴上交错张量

import tensorflow as tf

# Interlaves tensors across one axis
def interleave(tensors, size=1, axis=-1):
    # Reshape tensors
    tensors_res = []
    for tensor in tensors:
        s = tf.shape(tensor)
        new_s = tf.concat([s[:axis], [s[axis] // size, size], s[axis:][1:]], axis=0)
        tensors_res.append(tf.reshape(tensor, new_s))
    # Concatenate across new dimension
    if isinstance(axis, (tf.Tensor, tf.Variable)):
        selected_axis = tf.cond(tf.less(axis, 0), lambda: axis - 1, lambda: axis)
        concat_axis = tf.cond(tf.less(axis, 0), lambda: axis, lambda: axis + 1)
    else:
        selected_axis = (axis - 1) if axis < 0 else axis
        concat_axis = axis if axis < 0 else (axis + 1)
    tensors_concat = tf.concat(tensors_res, axis=concat_axis)
    # Reshape concatenation
    concat_s = tf.shape(tensors_concat)
    res_s = tf.concat([concat_s[:selected_axis], [-1], concat_s[concat_axis:][1:]], axis=0)
    return tf.reshape(tensors_concat, res_s)

你可以有一个函数,像这样在一个轴上交错张量

import tensorflow as tf

# Interlaves tensors across one axis
def interleave(tensors, size=1, axis=-1):
    # Reshape tensors
    tensors_res = []
    for tensor in tensors:
        s = tf.shape(tensor)
        new_s = tf.concat([s[:axis], [s[axis] // size, size], s[axis:][1:]], axis=0)
        tensors_res.append(tf.reshape(tensor, new_s))
    # Concatenate across new dimension
    if isinstance(axis, (tf.Tensor, tf.Variable)):
        selected_axis = tf.cond(tf.less(axis, 0), lambda: axis - 1, lambda: axis)
        concat_axis = tf.cond(tf.less(axis, 0), lambda: axis, lambda: axis + 1)
    else:
        selected_axis = (axis - 1) if axis < 0 else axis
        concat_axis = axis if axis < 0 else (axis + 1)
    tensors_concat = tf.concat(tensors_res, axis=concat_axis)
    # Reshape concatenation
    concat_s = tf.shape(tensors_concat)
    res_s = tf.concat([concat_s[:selected_axis], [-1], concat_s[concat_axis:][1:]], axis=0)
    return tf.reshape(tensors_concat, res_s)

哇,多好的解决方案啊。谢谢哇,多好的解决方案啊。谢谢