Keras:Dense vs.Embedding-ValueError:Input 0与layer repeat_vector_9不兼容:预期ndim=2,发现ndim=3
我有以下工作正常的网络:Keras:Dense vs.Embedding-ValueError:Input 0与layer repeat_vector_9不兼容:预期ndim=2,发现ndim=3,keras,embedding,keras-layer,word-embedding,keras-2,Keras,Embedding,Keras Layer,Word Embedding,Keras 2,我有以下工作正常的网络: 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))
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
当我使用嵌入层时,我得到了以下错误:
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
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
29左。添加(嵌入(编码尺寸,嵌入尺寸,输入长度=1))
--->30左。添加(重复向量(向后看))
31
32 leftOutput=left.output
/添加中的usr/local/lib/python3.4/dist-packages/keras/models.py(self,layer)
467输出形状=[self.outputs[0]。\u keras\u形状])
468其他:
-->469输出张量=层(自输出[0])
470如果存在(输出张量,列表):
471 raise TypeError('序列模型中的所有层'
/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in___调用(self,input,**kwargs)
550#在输入不兼容的情况下引发异常
551#具有图层构造函数中指定的输入规格。
-->552自我断言输入兼容性(输入)
553
554#收集输入形状以构建图层。
/assert\u input\u兼容性(self,inputs)中的usr/local/lib/python3.4/dist-packages/keras/engine/topology.py
449 self.name+':应为ndim=+
450 str(spec.ndim)+',发现ndim='+
-->451 str(K.ndim(x)))
452如果spec.max\u ndim不是无:
453 ndim=K.ndim(x)
ValueError:输入0与层重复向量9不兼容:预期ndim=2,发现ndim=3
用嵌入层替换密集层时,我还需要做哪些更改?谢谢 密集
层的输出形状为(无,嵌入尺寸)
。但是,嵌入
层的输出形状是(无,输入长度,嵌入尺寸)
。使用input\u length=1
,它将是(无,1,嵌入尺寸)
。您可以在嵌入
层之后添加一个展平
层以删除轴1
可以打印输出形状以调试模型。比如说,
EMBED_DIM=128
左=顺序()
添加(密集(嵌入尺寸,输入形状=(编码尺寸,))
打印(左。输出_形)
(无,128)
左=顺序()
添加(嵌入(编码尺寸,嵌入尺寸,输入长度=1))
打印(左。输出_形)
(无,1128)
左。添加(展平())
打印(左。输出_形)
(无,128)
密集层的输出形状为(无,嵌入尺寸)
。但是,嵌入
层的输出形状是(无,输入长度,嵌入尺寸)
。使用input\u length=1
,它将是(无,1,嵌入尺寸)
。您可以在嵌入
层之后添加一个展平
层以删除轴1
可以打印输出形状以调试模型。比如说,
EMBED_DIM=128
左=顺序()
添加(密集(嵌入尺寸,输入形状=(编码尺寸,))
打印(左。输出_形)
(无,128)
左=顺序()
添加(嵌入(编码尺寸,嵌入尺寸,输入长度=1))
打印(左。输出_形)
(无,1128)
左。添加(展平())
打印(左。输出_形)
(无,128)