首页 > 解决方案 > ValueError:预期批量大小和模型输出批量大小不匹配

问题描述

目前我有一个工作模型定义为

model = tf.keras.Model(inputs=x, outputs=[y1,y2])

我想在这个模型的输出中添加一个函数,以便模型接受 [y1,y2] 并返回一个张量元组

当我尝试将模型重新定义为

model = tf.keras.Model(inputs=x, outputs=func([y1,y2]))

我在尝试运行 model.predict(input_data) 时看到以下错误


 File "my_path\backend\.venv\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 908, in predict
    use_multiprocessing=use_multiprocessing)
  File "my_path\backend\.venv\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 723, in predict
    callbacks=callbacks)
  File "my_path\backend\.venv\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 401, in model_iteration
    aggregator.aggregate(batch_outs, batch_start, batch_end)
  File "my_path\backend\.venv\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 341, in aggregate
    result.aggregate(batch_element, batch_start, batch_end)
  File "my_path\backend\.venv\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py", line 265, in aggregate
    batch_element.shape, self.results.shape))
ValueError: Mismatch between expected batch size and model output batch size. Output shape = (20, 4), expected output shape = shape (1, 4)

使用张量流==1.15.4

标签: tensorflowkerastf.kerastensorflow1.15

解决方案


推荐阅读