Keras ValueError: logits and labels must have the same shape ((None, 32, 17) vs (None, 17))
Keras ValueError: logits and labels must have the same shape ((None, 32, 17) vs (None, 17))
我正在训练多标签分类器(IAM 数据集上的手写数字识别器)。
以下是我所有劈叉的形状:
>>> X_train.shape, X_val.shape, X_test.shape, Y_train.shape, Y_val.shape, Y_test.shape
((86583, 32, 128, 1),
(4558, 32, 128, 1),
(4797, 32, 128, 1),
(86583, 17),
(4558, 17),
(4797, 17))
这是 5 个目标变量示例的样子:
>>> Y_val[5:10]
array([[68, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[28, 67, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[42, 59, 59, 62, 56, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[73, 61, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[73, 61, 54, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0]], dtype=int32)
(在上面的数组中,每个数字对应于字符和字母列表中的索引。并且已经为所有目标向量填充了零以匹配最大目标词的大小。)
我在输出层使用 'sigmoid' 激活,有 17 个单元,每个单元对应一个可能的输出 character/alphabet,如下所示。
from tensorflow.keras import layers
.
.
layers.Dense(units=17, activation='sigmoid', kernel_initializer='he_uniform', kernel_regularizer='l2')
以 'binary_crossentropy' 作为损失,用于我的多标签分类问题。
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['categorical_accuracy'])
这是我的错误:
编辑 1:
我的模型的架构:
从你的第二个 LSTM 层中删除 return_sequences=True
,这样就可以了
我正在训练多标签分类器(IAM 数据集上的手写数字识别器)。
以下是我所有劈叉的形状:
>>> X_train.shape, X_val.shape, X_test.shape, Y_train.shape, Y_val.shape, Y_test.shape
((86583, 32, 128, 1),
(4558, 32, 128, 1),
(4797, 32, 128, 1),
(86583, 17),
(4558, 17),
(4797, 17))
这是 5 个目标变量示例的样子:
>>> Y_val[5:10]
array([[68, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[28, 67, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[42, 59, 59, 62, 56, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[73, 61, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[73, 61, 54, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0]], dtype=int32)
(在上面的数组中,每个数字对应于字符和字母列表中的索引。并且已经为所有目标向量填充了零以匹配最大目标词的大小。)
我在输出层使用 'sigmoid' 激活,有 17 个单元,每个单元对应一个可能的输出 character/alphabet,如下所示。
from tensorflow.keras import layers
.
.
layers.Dense(units=17, activation='sigmoid', kernel_initializer='he_uniform', kernel_regularizer='l2')
以 'binary_crossentropy' 作为损失,用于我的多标签分类问题。
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['categorical_accuracy'])
这是我的错误:
编辑 1:
我的模型的架构:
从你的第二个 LSTM 层中删除 return_sequences=True
,这样就可以了