【发布时间】: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 来查看它是否有所作为,但它没有。
如果你想要完整的代码 -
【问题讨论】:
-
使用 model.summary() 查看模型的实际输出形状,您可能在 softmax 层之前缺少一个 Flatten()
-
好的,谢谢@Dr.Snoopy。
标签: tensorflow machine-learning keras deep-learning classification