Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras张量的标引_Python_Tensorflow_Keras_Keras Layer - Fatal编程技术网

Python Keras张量的标引

Python Keras张量的标引,python,tensorflow,keras,keras-layer,Python,Tensorflow,Keras,Keras Layer,我的Keras功能模型的输出层是一个维度为(None,1344,2)的张量x。我希望从x的第二维度中提取n

我的Keras功能模型的输出层是一个维度为
(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']