You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CNN模型预测测试数据报错:GPU资源耗尽/CPU输入类型错误求助

CNN模型预测阶段报错的解决方法

报错信息

GPU运行时错误

(0) Resource exhausted:  2 root error(s) found.
(0) Resource exhausted: SameWorkerRecvDone unable to allocate output tensor. 
Key: /job:localhost/replica:0/task:0/device:CPU:0;7a6cf13ce274a521;
/job:localhost/replica:0/task:0/device:GPU:0;ret_1;0:0

CPU运行时错误

File "/home//programs/Neural/model_testing.py", line 224, in <module>
prediction_prob_matrix = model(test_generator,len(df_test), verbose=0)
File "/home/.conda/envs/my_env/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/home/.conda/envs/my_env/lib/python3.9/site-packages/keras/src/engine/input_spec.py", line 213, in assert_input_compatibility
raise TypeError(
TypeError: Inputs to a layer should be tensors. Got '<generator object generator at 0x7f7217fe3890>' (of type <class 'generator'>) as input for layer 'model'.

相关代码

train_generator = generator (df_train,tokenizer,onehot,label_encoder, n_classes,batch_size)
validation_generator = generator (df_valid,tokenizer,onehot,label_encoder, n_classes,batch_size)
test_generator = generator (df_test,tokenizer,onehot,label_encoder, n_classes,batch_size)
model.summary()

model.fit(train_generator, 
      validation_data=validation_generator,
      epochs=1,steps_per_epoch = len(df_train)//batch_size, validation_steps = len(df_valid)//batch_size, shuffle = True)

prediction = model.predict(test_generator, len(df_test), verbose=0)
# get the classes with the highest predicted probability, save them to our dataframe
df_test['lab'] = label_encoder.inverse_transform(prediction_prob_matrix)
# add the predicted probabilities
df_test['PREDICTED_PROB'] = prediction_prob_matrix.max(axis=1)
# take a look at what we've got
df_test.head()

解决方法

1. 修正CPU报错:规范model.predict用法

CPU报错核心是直接调用model(test_generator)导致的,必须使用model.predict()方法,同时代码存在变量名不一致问题(用prediction接收结果却调用prediction_prob_matrix),一并修正:

# 正确调用predict,设置steps参数并修正变量名
prediction_prob_matrix = model.predict(test_generator, steps=len(df_test)//batch_size, verbose=0)
# 若有剩余未整除的样本,可加1确保覆盖全部数据
# prediction_prob_matrix = model.predict(test_generator, steps=(len(df_test)//batch_size)+1, verbose=0)

注意:model.predict的第二个参数是steps(需执行的批次数),不是样本总数,需和生成器的batch_size匹配。

2. 解决GPU资源耗尽问题

GPU报错是显存不足导致,可通过以下方式缓解:

  • 降低测试生成器的batch_size:生成测试数据时使用更小的批次尺寸,减少单批次显存占用。
  • 开启TensorFlow显存增长模式:避免一次性占用全部GPU显存,按需分配:
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
if gpus:
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
    except RuntimeError as e:
        print(e)
  • 清理冗余进程:关闭GPU上其他占用资源的程序,释放显存空间。

3. 验证生成器输出格式

确保自定义generator函数每次迭代返回**(输入张量, 标签张量)**的元组,即使测试阶段不需要标签,也需返回(inputs, None),保证model.predict能正确解析输入数据。

内容的提问来源于stack exchange,提问作者borfo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 08:52:32