大家好。我正在尝试使用CTC损失函数,但效果不佳。我不断遇到这个错误:
2020-11-04 07:28:53.647946: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.647977: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648009: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.647992: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648021: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648063: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648052: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648074: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648080: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.2020-11-04 07:28:53.648308: W ./tensorflow/core/util/ctc/ctc_loss_calculator.h:499] No valid path found.
我已经在网上搜索了很多关于这个错误的信息,但没有找到任何有用的内容。
这是相关的代码:
def loss_fn(self, y_true, y_pred): batch_len = tf.keras.backend.cast(tf.shape(y_true)[0], dtype="int64") input_length = tf.keras.backend.cast(tf.shape(y_pred)[1], dtype="int64") #结果为30 label_length = tf.keras.backend.cast(tf.shape(y_true)[1], dtype="int64") #结果为25 input_length = 30 * tf.ones(shape=(batch_len, 1), dtype="int64") #暂时硬编码为30 label_length = 25 * tf.ones(shape=(batch_len, 1), dtype="int64") #暂时硬编码为25 y_true = tf.keras.layers.Softmax()(y_true) y_pred = tf.keras.layers.Softmax()(y_pred) print("y_true shape %s" %y_true.shape) #输出 y_true shape (32, 25) print(y_true) #输出 Tensor("loss_fn/softmax/Softmax:0", shape=(32, 25), dtype=float32) print("y_pred shape %s" %y_pred.shape) #输出 y_pred shape (32, 30, 67) print(y_pred) #输出 Tensor("loss_fn/softmax_1/Softmax:0", shape=(32, 30, 67), dtype=float32) loss = tf.keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length) return tf.reduce_mean(loss)
损失函数在这里被调用:
…
def ResNet: ... out = tf.keras.layers.Reshape((out.shape[2], out.shape[3]))(out) print("out %s" %out.shape) #输出结果为:out (None, 30, 768) weight_initializer = tf.keras.initializers.he_uniform() bias_initializer = tf.keras.initializers.constant() logits = tf.keras.layers.Dense(67, kernel_initializer=weight_initializer, bias_initializer=bias_initializer, name="logits")(out) print("logits %s" %logits.shape) #输出结果为:logits (None, 30, 67) print("________________________") print(logits) model = tf.keras.Model(inputs=[input, labels], outputs=logits, name="full_model") model.compile(optimizer="RMSprop", loss=self.loss_fn) print(model.summary())
调用这个的主要函数是:
...... d = dataset.Dataset(confs) train_data = d.read_data(confs["trn_data_files"]) valid_data = d.read_data(confs["val_data_files"]) callbacks = [ tf.keras.callbacks.ModelCheckpoint("./model_checkpoint", monitor="val_loss") ] for x,y in train_data: history = model.fit( x=x, y=y, validation_data=valid_data, epochs=50, callbacks=callbacks, )
数据集有预处理步骤。
如您所见,标签的维度小于logits的维度。我知道如果不是这种情况,就会出现“未找到有效路径”的错误。
我做错了什么吗?请帮助我。非常感谢您的帮助。
回答:
在CTC中,您需要有比目标标签更多的隐藏状态。CTC实际上是学习如何有效地将目标标签与特殊的“空白”符号交错,使得标签最佳匹配隐藏状态。然而,当您的目标标签数量多于隐藏状态时,您无法对齐它们。
在CNN中,您可能过度减少了输入的维度,导致隐藏状态序列过短。您应该重新考虑在CNN中如何进行填充和池化,或者(这可能不是一个好主意),像在机器翻译中使用CTC时那样,进行某种状态分割投影。