【问题标题】:Why is it is asking for labels to have some other shape?为什么要求标签具有其他形状?
【发布时间】:2020-11-28 00:45:35
【问题描述】:

您好,我正在尝试获取 7 个类的数组 的输出。但是当我运行我的代码时,它说它希望我的数据输出标签具有其他形状。这是我的代码-

    def make_model(self):
        self.model.add(InceptionV3(include_top=False, 
                                   input_shape=(self.WIDTH, self.HEIGHT, 3), 
                                   weights="imagenet"))
        self.model.add(Dense(7, activation='softmax'))
        self.model.layers[0].trainable = False

我的模型编译和装修部分

    def train(self):
        self.model.compile(optimizer=self.optimizer, loss='mse', metrics=['accuracy'])
        self.model.fit(x=x, y=y, batch_size=64, 
                       validation_split=0.15, shuffle=True, epochs=self.epochs, 
                       callbacks=[self.tensorboard, self.reducelr])

我得到了错误 -

File "model.py", line 60, in train
    callbacks=[self.tensorboard, self.reducelr])

ValueError: A target array with shape (23639, 7) was passed for an output of shape (None, 6, 13, 7) while using as loss `mean_squared_error`. This loss expects targets to have the same shape as the output.

现在这里说它期望 (None, 6, 13, 7) 但是我给了它标签 - (23639, 7)

  • 现在我们可以清楚地看到,在self.model.add(Dense(7, activation='softmax'))中我指定了7作为输出类别的数量

这是模型摘要 - 所以有人可以告诉我这里出了什么问题

顺便说一句,我确实尝试过使用 categorical_crossentropy 来查看它是否有所作为,但它没有。

如果你想要完整的代码 -

Full Code

【问题讨论】:

  • 使用 model.summary() 查看模型的实际输出形状,您可能在 softmax 层之前缺少一个 Flatten()
  • 好的,谢谢@Dr.Snoopy。

标签: tensorflow machine-learning keras deep-learning classification


【解决方案1】:

问题出在 InceptionV3 的输出中......它返回 4D 序列,您需要在最终密集层之前降低维度以匹配目标维度(2D)。您可以使用FlattenGlobalPooling 层来执行此操作。

如果您的问题是分类问题,我还建议您使用categorical_crossentropy(如果您有单热编码标签)或sparse_categorical_crossentropy(如果您有整数编码标签)。 mse 适用于回归问题

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 2017-06-07
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多