【发布时间】:2019-11-16 23:49:47
【问题描述】:
我正在尝试构建用于文本分类的 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 图层的形状,但我得到 None 用于展平图层。我认为这可能是错误的原因。
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`.
【问题讨论】:
-
如果
Dense是第一层,那么您必须设置输入数据的形状/大小。您甚至可以在 Dense documentation 的示例中看到它 -
@furas 但是我在前面没有几个其他层,比如 LSTM,嵌入正如你在我上面的代码中看到的那样,在我来到 Dense 层之前,我还有两个 conv1d 层和两个 flatten 层。对吗?
-
当您在
Sequential()中使用Dense()时,它会自动从以前的Dense()获得形状,并且只有第一个Dense()需要形状 - 就像文档中的示例一样 - 但您可能必须设置这个手动塑造Dense(... input_shape=...) -
@furas 我怎么知道它通过嵌入层、lstm、flatten、conv1d、flatten、concatenate 层等所有之前的层后的形状?这不是我的第一层。我正在使用 keras 功能 api,它也像顺序模型一样自动从前一层获得形状。你只需要像我一样传递它。 Dense(100)(x) 这里 x 指的是前一层。我不知道为什么我在这里收到错误。
标签: python python-3.x tensorflow keras