使用数组为训练网络建立索引(tensorflow)

Using an array for indexing for training network (tensorflow)

我对 tensorflow 比较陌生,运行正在尝试正确索引张量时遇到问题。在所示的底部,我正在尝试使用 x(它本身是一个包含数组形式的张量,例如 [[0,1], [2,3]])来索引 y_rt 张量(可以将其视为切片 y_rt 张量)。但是,我无法将张量转换为数组或列表。我知道有 .eval() 函数,但是我不能在这里使用它,因为显示的代码发生在 .运行() 调用之前。任何帮助将非常感激。

with tf.name_scope('placeholders'):
x_true = tf.placeholder(tf.float32, shape=[None, size, size, 1], name="x_true")
y_rt = tf.placeholder(tf.float32, shape=[None, operator.range.shape[0], operator.range.shape[1], 1], name="y_rt")
is_training = tf.placeholder(tf.bool, shape=(), name='is_training')
angle = tf.placeholder(tf.float32, shape=[n_batches, number], name="projection_order")
ordersel = tf.placeholder(tf.int32, shape=[n_batches, number], name='order_selection')
selection = tf.placeholder(tf.float32, shape=[number], name='iteration_selection')
dual = tf.placeholder(tf.float32, shape=[None, number, 183, 1], name='dual')
y_par = tf.placeholder(tf.float32, shape=[None, number, 183, 1], name='y_partial')

for i in range(n_batches): #iterations, the amount of projection batches we have
    with tf.variable_scope('my_iterate{}'.format(i)):

        value = layer_sub(primal[..., 1:2], epoch_angle[i])  
        x = (selection[i])
        y_partial = y_rt[:, selection, :, :] #y_rt is of form (?,total, 183, 1)

从版本 1.9 开始,TensorFlow 不支持使用切片表示法的数组索引。来自 tf.Tensor documentation for __getitem__:

This operation extracts the specified region from the tensor. The notation is similar to NumPy with the restriction that currently only support basic indexing. That means that using a non-scalar tensor as input is not currently allowed.

如果您想要比简单标量更高级的索引,tf.boolean_mask can help you select tensor elements using a boolean array and tf.gather_nd 可以帮助您 select 使用整数数组的元素。

请注意,在您的示例中,x 指定的索引将是具有一维 selection 张量的标量,如果您使用它,则适用于切片表示法:

x = selection[i]
y_partial = y_rt[:, x, :, :]

但是为每个训练批次索引到 selection 可能不是您想要的。