你好,我试图得到一个包含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
来看看是否有区别,但没有效果。
如果你想要完整的代码 –
回答:
问题出在InceptionV3的输出上…它返回4D序列,你需要在最终的密集层之前降低维度以匹配目标维度(2D)。你可以使用Flatten
或GlobalPooling
层来实现这一点。
如果这是分类问题,我还建议你使用categorical_crossentropy
(如果你有一热编码标签)或sparse_categorical_crossentropy
(如果你有整数编码标签)。mse
适合回归问题