Keras - TypeError:模型的输出张量必须是Keras张量 - 同时建模多输入,多输出网络

时间:2017-03-12 11:34:41

标签: python machine-learning neural-network keras keras-layer

我有一个9个2000维向量序列作为来自2个双向lstms的o / p。我将它们合并以获得9个4000 dim向量。

我需要获取每个4000维向量并将它们中的每一个馈送到共享的完全连接层。 我怎样才能做到这一点? 现在我正在整合合并o / p以提供给共享的完全连接层。但我不知道是否需要这个?

当我尝试对整个网络进行建模以获取多个i / p并生成多个o / p时,我收到此错误,如link

中所述

可以找到代码here

# we can then concatenate the two vectors:
N=3
merge_cv = merge([top_out, btm_out], mode='concat')#concat_axis=2 or -1 (last dim axis)
cv = Reshape((9,1, 4000))(merge_cv) # we want 9 vectors of dimension 4000 each for sharedfc_out below

#number of output classes per cell
n_classes = 80
sharedfc_out= Dense(output_dim=n_classes,input_dim=4000,activation='relu')
#partial counts
#pc = np.ndarray(shape=(1,n_classes), dtype=float) 
#cells_pc = np.array([[pc for j in range(N)] for i in range(N)])
outpc=[]

for i in range(N):
    for j in range(N):
        # cells_pc[i][j] = sharedfc_out(cv[N*i+j])
        outpc.append(sharedfc_out(cv[0][N*i+j]))

# out=merge(outpc,mode='concat')
# out2=Reshape(720)(out)

model = Model(input=cells_in, output=outpc)

bi的维度= lstm o / p

>>> merge_cv.shape
TensorShape([Dimension(1), Dimension(None), Dimension(4000)])
>>> cv.shape
TensorShape([Dimension(None), Dimension(9), Dimension(1), Dimension(4000)])

对于最后一行,我遇到了类型错误。

TypeError                                 Traceback (most recent call last)
 in ()
----> 1 model = Model(input=cells_in, output=outpc)

/home/jkl/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py in __init__(self, input, output, name)
   1814                 cls_name = self.__class__.__name__
   1815                 raise TypeError('Output tensors to a ' + cls_name + ' must be '
-> 1816                                 'Keras tensors. Found: ' + str(x))
   1817         # Build self.output_layers:
   1818         for x in self.outputs:

TypeError: Output tensors to a Model must be Keras tensors. Found: Tensor("Relu_9:0", shape=(1, 80), dtype=float32)

1 个答案:

答案 0 :(得分:2)

所以最后结果发现问题出现在错误的列表切片中,最终将None作为一个层传递给一个列表然后合并到一个输入中。在修复这个并使切片保持一致之后 - 问题就解决了。