我正在尝试建立用于文本分类的lstm模型,但收到错误。这是我尝试过的全部代码。
请让我知道错误背后的原因以及如何解决该错误。
input1.shape # text data integer coded
(37788, 130)
input2.shape # multiple category columns(one hot encoded) concatenated together
(37788, 104)
train_data = [input1, input2] # this is the train data.
i1 = Input(shape=(130,), name='input')
embeddings = Embedding(input_dim=20000, output_dim=100, input_length=130)(i1)
lstm = LSTM(100)(embeddings)
flatten = Flatten()(lstm)
i2 = Input(shape=(None, 104))
c1 = Conv1D(64, 2, padding='same', activation='relu', kernel_initializer='he_uniform')(i2)
c2 = Conv1D(32, kernel_size=3, activation='relu', kernel_initializer='he_uniform')(c1)
flatten1 = Flatten()(c2)
concat = concatenate([flatten, flatten1])
dense1 = Dense(32, 'relu', kernel_initializer='he_uniform')(concat)
我尝试打印conv1d层的形状,但对于扁平层却没有显示。我认为这可能是错误的原因。
Tensor("conv1d_81/Identity:0", shape=(None, None, 64), dtype=float32)
Tensor("conv1d_82/Identity:0", shape=(None, None, 32), dtype=float32)
Tensor("flatten_106/Identity:0", shape=(None, None), dtype=float32)
这是我遇到的错误。如何解决?
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-531-31a53fbf3d37> in <module>
14 concat = concatenate([flatten, flatten1])
---> 15 dense1 = Dense(32, 'relu', kernel_initializer='he_uniform')(concat)
16 drop = Dropout(0.5)(dense1)
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
614 # Build layer if applicable (if the `build` method has been
615 # overridden).
--> 616 self._maybe_build(inputs)
617
618 # Wrapping `call` function in autograph to allow for dynamic control
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _maybe_build(self, inputs)
1964 # operations.
1965 with tf_utils.maybe_init_scope(self):
-> 1966 self.build(input_shapes)
1967 # We must set self.built since user defined build functions are not
1968 # constrained to set self.built.
~\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\core.py in build(self, input_shape)
1003 input_shape = tensor_shape.TensorShape(input_shape)
1004 if tensor_shape.dimension_value(input_shape[-1]) is None:
-> 1005 raise ValueError('The last dimension of the inputs to `Dense` '
1006 'should be defined. Found `None`.')
1007 last_dim = tensor_shape.dimension_value(input_shape[-1])
ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.
答案 0 :(得分:1)
第二个模型中序列的长度为None
。
i2 = Input(shape=(None, 104))
您不能展平可变长度并具有已知大小。
您需要Dense
的已知大小。
或者使用固定长度而不是None
,或者使用GlobalMaxPooling1D
或GlobalAveragePooling1D
而不是Flatten
。
答案 1 :(得分:0)
对我来说,问题在于在输入函数中使用Tensor之前我没有对其进行重塑
image = tf.reshape(image, [400,400,3])