Python 无法向批次添加张量:元素数不匹配。形状是:[张量]:[125],[批次]:[149]

Python 无法向批次添加张量:元素数不匹配。形状是:[张量]:[125],[批次]:[149],python,tensorflow,tf.data.dataset,Python,Tensorflow,Tf.data.dataset,使用: TensorFlow:2.4.1 python:3.6.9 CUDA:11 CuDNN:8 使用tf.data.Dataset生成数据,如果批大小为1,代码运行良好,但如果我增加批大小,则会出现以下错误。Iam使用自定义CTC丢失功能,因为它是语音识别系统,所以如何解决此错误 class CTCLayer(layers.Layer): def __init__(self, name=None, **kwargs): super(CTCLayer, self).__

使用:

TensorFlow:2.4.1

python:3.6.9

CUDA:11

CuDNN:8

使用tf.data.Dataset生成数据,如果批大小为1,代码运行良好,但如果我增加批大小,则会出现以下错误。Iam使用自定义CTC丢失功能,因为它是语音识别系统,所以如何解决此错误

class CTCLayer(layers.Layer):
    def __init__(self, name=None, **kwargs):
        super(CTCLayer, self).__init__(name=name, **kwargs)
        self.loss_fn = keras.backend.ctc_batch_cost
    def call(self, y_true, y_pred):
        # Compute the training-time loss value and add it
        # to the layer using `self.add_loss()`.
        batch_len = tf.cast(tf.shape(y_true)[0], dtype="int64")
        input_length = tf.cast(tf.shape(y_pred)[1], dtype="int64")
        label_length = tf.cast(tf.shape(y_true)[1], dtype="int64")

        input_length = input_length * tf.ones(shape=(batch_len, 1), dtype="int64")
        label_length = label_length * tf.ones(shape=(batch_len, 1), dtype="int64")

        loss = self.loss_fn(y_true, y_pred, input_length, label_length)
        self.add_loss(loss)

        #acc = self.accuracy_fn(y_true, y_pred, input_length, label_length)
        #self.add_metric(cc, name="accuracy")
        # At test time, just return the computed predictions
        return y_pred
以下是完整的错误:

    Epoch 1/50
wTraceback (most recent call last):
  File "ctc-asr-v2/keras_model_train.py", line 348, in <module>
    batch_size=2,
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1100, in fit
    tmp_logs = self.train_function(iterator)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py", line 888, in _call
    return self._stateless_fn(*args, **kwds)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 2943, in __call__
    filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 1919, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py", line 560, in call
    ctx=ctx)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [125], [batch]: [149]
     [[node IteratorGetNext (defined at ctc-asr-v2/keras_model_train.py:348) ]]
  (1) Invalid argument:  Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [125], [batch]: [149]
     [[node IteratorGetNext (defined at ctc-asr-v2/keras_model_train.py:348) ]]
     [[model/ctc_loss/CTCLoss/_256]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_9169]

Function call stack:
train_function -> train_function
1/50纪元
wTraceback(最近一次呼叫最后一次):
文件“ctc-asr-v2/keras_model_train.py”,第348行
批次大小=2,
文件“/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py”,第1100行,适合
tmp_logs=self.train_函数(迭代器)
文件“/usr/local/lib/python3.6/dist packages/tensorflow/python/eager/def_function.py”,第828行,在调用中__
结果=自身调用(*args,**kwds)
文件“/usr/local/lib/python3.6/dist packages/tensorflow/python/eager/def_function.py”,第888行,在调用中
返回self.\u无状态\u fn(*args,**kwds)
文件“/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py”,第2943行,在__
过滤的参数,捕获的输入=图形函数。捕获的输入)#pylint:disable=受保护的访问
文件“/usr/local/lib/python3.6/dist packages/tensorflow/python/eager/function.py”,第1919行,在调用平面中
ctx,args,取消管理器=取消管理器)
文件“/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py”,第560行,在调用中
ctx=ctx)
文件“/usr/local/lib/python3.6/dist packages/tensorflow/python/eager/execute.py”,第60行,快速执行
输入、属性、数量(输出)
tensorflow.python.framework.errors\u impl.InvalidArgumentError:找到2个根错误。
(0)无效参数:无法向批次添加张量:元素数不匹配。形状是:[张量]:[125],[批次]:[149]
[[节点迭代器ETNEXT(定义见ctc-asr-v2/keras_model_train.py:348)]]
(1) 无效参数:无法向批次添加张量:元素数不匹配。形状是:[张量]:[125],[批次]:[149]
[[节点迭代器ETNEXT(定义见ctc-asr-v2/keras_model_train.py:348)]]
[[model/ctc_loss/CTCLoss/_256]]
0成功的操作。
忽略0个派生错误。[Op:uu推理u训练u功能9169]
函数调用堆栈:
列车功能->列车功能
参考此示例代码: