为什么要求标签具有其他形状?
Why is it is asking for labels to have some other shape?
你好,我正在尝试获取一个 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 的输出中...它 returns 4D 序列,您需要在最终密集层之前降低维度以匹配目标维度 (2D)。您可以使用 Flatten
或 GlobalPooling
层来执行此操作。
如果你的问题是分类问题,我也建议你使用 categorical_crossentropy
(如果你有 one-hot 编码标签)或 sparse_categorical_crossentropy
(如果你有整数编码标签)。 mse
适用于回归问题
你好,我正在尝试获取一个 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 的输出中...它 returns 4D 序列,您需要在最终密集层之前降低维度以匹配目标维度 (2D)。您可以使用 Flatten
或 GlobalPooling
层来执行此操作。
如果你的问题是分类问题,我也建议你使用 categorical_crossentropy
(如果你有 one-hot 编码标签)或 sparse_categorical_crossentropy
(如果你有整数编码标签)。 mse
适用于回归问题