【问题标题】:Error: ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`错误:ValueError:应定义“密集”输入的最后一个维度。找到`无`
【发布时间】: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


【解决方案1】:

您在第二个模型的序列长度中有None

i2  = Input(shape=(None, 104))

您不能展平可变长度并具有已知大小。
您需要知道Dense 的大小。

要么使用固定长度而不是None,要么使用GlobalMaxPooling1DGlobalAveragePooling1D 而不是Flatten

【讨论】:

  • 这里的None应该填什么?是我的 input2 形状吗?
  • 是的。它是 input2 形状。
  • 现在我在 model.fit ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (37788, 104) 期间收到此错误,为什么它期望我的输入 1 有 3 个维度?
  • 我将 input1 和 input2 合并为 train_data = [input1, input2] 并将其作为 x 传递给 model.fit
  • 那是因为 Conv1D 需要 3d 数组 (samples, length, features),而不是 2d。我不知道你想要实现什么,但你必须了解你的数据以及你想从中提取什么。
【解决方案2】:

对我来说,问题是在输入函数中使用之前我没有重塑张量

image = tf.reshape(image, [400,400,3])

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2020-05-23
    相关资源
    最近更新 更多