0

我的项目涉及keras.backend.ctc_batch_cost()keras 的包装器功能。参数是:

  • y_pred:在这里,我有一个 softmax 输出shape=(None, 446, 30)

  • y_true:(这是麻烦制造者)包含编码标签的张量,如下所示:

    tf.Tensor( [[ 5 10 6 ... 99 99 99] [ 5 10 6 ... 99 99 99] [ 5 10 6 ... 99 99 99] ... [ 8 6 16 ... 99 99 99] [10 14 1 ... 99 99 99] [20 10 6 ... 99 99 99]],形状=(1787, 150),dtype=int32)

  • input_lengths:包含 y_pred 长度的张量

  • label_lengths:包含 y_true 长度的张量

这是我绘制的模型的图像(CTC 部分) CTC 部分

问题是我的 y_true 的形状为 (1787, 150) ,即 (samples, max_label_length) 对应于文档,但是当我尝试运行它时,它会抛出此错误:

ValueError: Input 1 is incompatible with layer sr_training_v1: expected shape=(None, 150), found shape=(None, 1787, 150)

我还必须向您展示我的模型的 InputLayers:

max_label_length = 150
labels = Input(name="encoded_labels_input", shape=(max_label_length,), dtype="float32")
input_length = Input(name="input_lengths_input", shape=(1,), dtype="int64")
label_length = Input(name="label_lengths_input", shape=(1,), dtype="int64")

(我没有包括 Input 0 因为它工作正常)

通过将“encoded_labels_input”层的形状更改为类似 (1787, 150) 的形状,解决方案似乎很明显,但不幸的是,这不起作用。因为那样会引发错误:ValueError: Shape (None, 150) must have rank 1

好吧,有多个错误,但我不知道其中哪些是相关的:

Traceback (most recent call last):
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 975, in merge_with
    self.assert_same_rank(other)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1028, in assert_same_rank
    raise ValueError("Shapes %s and %s must have the same rank" %
ValueError: Shapes (None, 150) and (None,) must have the same rank

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1059, in with_rank
    return self.merge_with(unknown_shape(rank=rank))
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 982, in merge_with
    raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (None, 150) and (None,) are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 621, in <module>
    main()
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 645, in wrapper
    return func(*args, **kwargs)
  File "train.py", line 578, in main
    testing_model, training_model = build_model(input_shape)
  File "train.py", line 508, in build_model
    loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name="ctc")([y_pred, labels, input_length, label_length])
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 969, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1107, in _functional_construction_call
    outputs = self._keras_tensor_symbolic_call(
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 840, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 880, in _infer_output_signature
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\layers\core.py", line 919, in call
    result = self.function(inputs, **kwargs)
  File "train.py", line 505, in ctc_lambda_func
    return keras.backend.ctc_batch_cost(labels, y_pred, input_length, label_length)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\backend.py", line 6251, in ctc_batch_cost
    ctc_label_dense_to_sparse(y_true, label_length), dtypes_module.int32)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\keras\backend.py", line 6221, in ctc_label_dense_to_sparse
    return sparse_tensor.SparseTensor(
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 145, in __init__
    values_shape = values.shape.with_rank(1)
  File "C:\Users\pmhh\Documents\AI\E2EASRS\tensor_asr\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1061, in with_rank
    raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (None, 150) must have rank 1

从现在开始,我不知道我应该或可以做些什么来解决它......有没有人知道如何处理 ctc 功能?

4

0 回答 0