Python Keras张量的标引
我的Keras功能模型的输出层是一个维度为Python Keras张量的标引,python,tensorflow,keras,keras-layer,Python,Tensorflow,Keras,Keras Layer,我的Keras功能模型的输出层是一个维度为(None,1344,2)的张量x。我希望从x的第二维度中提取n
(None,1344,2)
的张量x
。我希望从x
的第二维度中提取n<1344
条目,并创建一个新的y
张量(无,n,2)
通过简单地访问x[:,:n,:]
,似乎可以直接提取n
连续条目,但是如果n
索引是非连续的,则(似乎)很难提取。Keras是否有一种干净的方法可以做到这一点
以下是我目前的做法
实验1(切片张量,连续索引,工作):
实验2(在任意索引下索引张量,失败)
Keras返回以下错误:
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'strided_slice_17/stack_1' (op:
'Pack') with input shapes: [], [5], [].
实验3(张量流后端函数)
我也尝试过K.backend.gather
,但是它的用法还不清楚,因为1)Keras文档说明索引应该是整数张量,没有与numpy等价的Keras。其中
如果我的目标是提取x
中满足特定条件的条目,2)K.backend.gather从axis=0
提取条目,而我想从x
的第二个维度提取条目,您要查找的维度将基于索引数组进行索引:
# From documentation
indices = [[0, 0], [1, 1]]
params = [['a', 'b'], ['c', 'd']]
output = ['a', 'd']
要在Keras模型中使用它,请确保将其包装在类似于Lambda
的层中
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'strided_slice_17/stack_1' (op:
'Pack') with input shapes: [], [5], [].
# From documentation
indices = [[0, 0], [1, 1]]
params = [['a', 'b'], ['c', 'd']]
output = ['a', 'd']