如何使用后端从 Keras 收集张量?

How to gather a tensor from Keras using its backend?

我试图在 Keras 中编译一个模型,输入是一个 2D numpy 数组。 我需要的是将向量放在这个二维数组的第 n 个位置,并将其用作其中一层的张量 1D 张量。 我该怎么做?

使用 lambda 层应该可以做到:

extracted_tensor = Lambda(lambda x: x[:,nth_index,:], output_shape=(1,dim_vector))(input)
extracted_tensor = Flatten()(extracted_tensor)

请注意,在 x 张量(lambda 函数)中,您考虑了批量维度,但在 output_shape 参数中没有。

希望对您有所帮助

使用tf.gather( input_tensor, indices, axis )沿着指定的axis收集indices