Keras/Tensorflow 的 CTC 损失;形状不可配置

问题描述

我的项目涉及 keras 的 keras.backend.ctc_batch_cost() 包装函数。参数为:

  • y_pred:这里,我有一个带有 shape=(None,446,30)

    softmax 输出
  • 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 Part

问题是我的 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",dtype="int64")

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

通过将“encoded_labels_input”-Layer 的形状更改为类似 (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,) 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,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,line 505,in ctc_lambda_func
    return keras.backend.ctc_batch_cost(labels,y_pred,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 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函数怎么处理?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)