2017-11-18 109 views
0

我有以下網絡的正常工作:Keras:密實與嵌入 - ValueError異常:輸入0是與層repeat_vector_9不相容:預期NDIM = 2,發現NDIM = 3

left = Sequential() 
left.add(Dense(EMBED_DIM,input_shape=(ENCODE_DIM,))) 
left.add(RepeatVector(look_back)) 

然而,我需要與嵌入層更換緻密層:

left = Sequential() 
left.add(Embedding(ENCODE_DIM, EMBED_DIM, input_length=1)) 
left.add(RepeatVector(look_back)) 

然後我得到了下面的錯誤,當我使用嵌入層:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-119-5a5f11c97e39> in <module>() 
    29 left.add(Embedding(ENCODE_DIM, EMBED_DIM, input_length=1)) 
---> 30 left.add(RepeatVector(look_back)) 
    31 
    32 leftOutput = left.output 

/usr/local/lib/python3.4/dist-packages/keras/models.py in add(self, layer) 
    467       output_shapes=[self.outputs[0]._keras_shape]) 
    468   else: 
--> 469    output_tensor = layer(self.outputs[0]) 
    470    if isinstance(output_tensor, list): 
    471     raise TypeError('All layers in a Sequential model ' 

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs) 
    550     # Raise exceptions in case the input is not compatible 
    551     # with the input_spec specified in the layer constructor. 
--> 552     self.assert_input_compatibility(inputs) 
    553 
    554     # Collect input shapes to build layer. 

/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs) 
    449          self.name + ': expected ndim=' + 
    450          str(spec.ndim) + ', found ndim=' + 
--> 451          str(K.ndim(x))) 
    452    if spec.max_ndim is not None: 
    453     ndim = K.ndim(x) 

ValueError: Input 0 is incompatible with layer repeat_vector_9: expected ndim=2, found ndim=3 

使用嵌入圖層替換密集圖層時還需要進行哪些其他更改?謝謝!

回答

2

Dense圖層的輸出形狀爲(None, EMBED_DIM)。但是,Embedding圖層的輸出形狀爲(None, input_length, EMBED_DIM)。與input_length=1,它將是(None, 1, EMBED_DIM)。您可以在Embedding圖層之後添加Flatten圖層以刪除軸1.

您可以打印輸出形狀以調試模型。例如,

EMBED_DIM = 128 
left = Sequential() 
left.add(Dense(EMBED_DIM, input_shape=(ENCODE_DIM,))) 
print(left.output_shape) 
(None, 128) 

left = Sequential() 
left.add(Embedding(ENCODE_DIM, EMBED_DIM, input_length=1)) 
print(left.output_shape) 
(None, 1, 128) 

left.add(Flatten()) 
print(left.output_shape) 
(None, 128) 
相關問題