Keras 自定义生成器 TypeError: ‘NoneType’ 对象不可调用

我编写了一个自定义生成器来加载我的数据集并将其馈送到 fit_generator 方法中。但我遇到了一个错误。question_trainimg_lis_trainanswers_train 都是字符串列表。我希望 mygen 能返回每批32个样本,格式为:

[images,questions] , answers

这是代码:

def mygen(questions_train,img_lis_train,answers_train):    start = 0      data_size = len(questions_train)    batch_size = 32            while True:                  if( start+batch_size <= data_size ):            batch_ques = questions_train[ start : start+batch_size ]             batch_ans = answers_train[ start : start+batch_size ]             batch_img_names = img_lis_train[ start : start+batch_size ]         elif(start < data_size):            batch_ques = questions_train[ start : ]             batch_ans = answers_train[ start : ]             batch_img_names = img_lis_train[ start : start+batch_size ]         else:            start = 0            continue               batch_img = []        for img_name in batch_img_names:            img = load_img('./dataset/images/' + str(img_name) + '.png' , target_size = (224,224))            img = img_to_array(img)               batch_img.append( preprocess_input(img) )            start += 32        print('start = ' + str(start))        yield [batch_img, batch_ques] ,batch_ansfc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)

这是我得到的错误:

  File "mycode.py", line 210, in <module>    fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)  File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper    return func(*args, **kwargs)  File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/models.py", line 1223, in fit_generator    initial_epoch=initial_epoch)  File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper    return func(*args, **kwargs)  File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/engine/training.py", line 2083, in fit_generator    generator_output = next(output_generator)StopIterationException ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f936527fcc0>>Traceback (most recent call last):  File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 712, in __del__  File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/framework/c_api_util.py", line 31, in __init__TypeError: 'NoneType' object is not callable

回答:

你需要通过调用生成器并传递你的数据来构建生成器,否则它如何生成批次?这是正确的方法:

fc_model.fit_generator(mygen(q_train, i_train, a_train), ...)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注