错误:TypeError: Fetch参数None的类型无效
我认为这个错误是在使用回调函数ModelCheckpoint保存模型时发生的。查找这个错误时,找到了这个答案,但因为我在使用Keras,所以不能使用这个答案,因为我并没有在TensorFlow中显式调用sess.run()。此外,训练过程中的每个epoch都是完美无缺的,只有在保存模型时才会出现这个错误。
代码:
完整的模型位于Kaggle笔记本中,链接在这里:https://www.kaggle.com/aevinq/cnn-batchnormalization-0-1646/
错误出现的相关代码如下:
early_stopping = EarlyStopping(monitor='val_loss', patience=5, mode='min')mcp_save = ModelCheckpoint('md.hdf5', save_best_only=True, monitor='val_loss', mode='min')reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, epsilon=1e-4, mode='min')history = model.fit(train_X, train_y, batch_size=32, epochs=20, verbose=1, validation_split=0.25, callbacks=[early_stopping, reduce_lr_loss, mcp_save])
错误:
Train on 4413 samples, validate on 1471 samplesEpoch 1/204384/4413 [============================>.] - ETA: 1s - loss: 0.5157 - acc: 0.7696---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-11-97f0757a1e9c> in <module>() 2 mcp_save = ModelCheckpoint('md.hdf5', save_best_only=True, monitor='val_loss', mode='min') 3 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, epsilon=1e-4, mode='min')----> 4 history = model.fit(train_X, train_y, batch_size=32, epochs=20, verbose=1, validation_split=0.25, callbacks=[early_stopping, reduce_lr_loss, mcp_save])/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 970 initial_epoch=initial_epoch, 971 steps_per_epoch=steps_per_epoch,--> 972 validation_steps=validation_steps) 973 974 def evaluate(self, x=None, y=None,/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 1655 initial_epoch=initial_epoch, 1656 steps_per_epoch=steps_per_epoch,-> 1657 validation_steps=validation_steps) 1658 1659 def evaluate(self, x=None, y=None,/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps) 1231 for l, o in zip(out_labels, val_outs): 1232 epoch_logs['val_' + l] = o-> 1233 callbacks.on_epoch_end(epoch, epoch_logs) 1234 if callback_model.stop_training: 1235 break/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/callbacks.py in on_epoch_end(self, epoch, logs) 71 logs = logs or {} 72 for callback in self.callbacks:---> 73 callback.on_epoch_end(epoch, logs) 74 75 def on_batch_begin(self, batch, logs=None):/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/callbacks.py in on_epoch_end(self, epoch, logs) 413 self.model.save_weights(filepath, overwrite=True) 414 else:--> 415 self.model.save(filepath, overwrite=True) 416 else: 417 if self.verbose > 0:/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/engine/topology.py in save(self, filepath, overwrite, include_optimizer) 2563 """ 2564 from ..models import save_model-> 2565 save_model(self, filepath, overwrite, include_optimizer) 2566 2567 def save_weights(self, filepath, overwrite=True):/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/models.py in save_model(model, filepath, overwrite, include_optimizer) 145 if symbolic_weights: 146 optimizer_weights_group = f.create_group('optimizer_weights')--> 147 weight_values = K.batch_get_value(symbolic_weights) 148 weight_names = [] 149 for i, (w, val) in enumerate(zip(symbolic_weights,/opt/conda/lib/python3.6/site-packages/Keras-2.1.2-py3.6.egg/keras/backend/tensorflow_backend.py in batch_get_value(ops) 2208 """ 2209 if ops:-> 2210 return get_session().run(ops) 2211 else: 2212 return []/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 887 try: 888 result = self._run(None, fetches, feed_dict, options_ptr,--> 889 run_metadata_ptr) 890 if run_metadata: 891 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1103 # Create a fetch handler to take care of the structure of fetches. 1104 fetch_handler = _FetchHandler(-> 1105 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles) 1106 1107 # Run request and get response./opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in __init__(self, graph, fetches, feeds, feed_handles) 412 """ 413 with graph.as_default():--> 414 self._fetch_mapper = _FetchMapper.for_fetch(fetches) 415 self._fetches = [] 416 self._targets = []/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 232 elif isinstance(fetch, (list, tuple)): 233 # NOTE(touts): This is also the code path for namedtuples.--> 234 return _ListFetchMapper(fetch) 235 elif isinstance(fetch, dict): 236 return _DictFetchMapper(fetch)/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in __init__(self, fetches) 339 """ 340 self._fetch_type = type(fetches)--> 341 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 342 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 343 /opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in <listcomp>(.0) 339 """ 340 self._fetch_type = type(fetches)--> 341 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] 342 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers) 343 /opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in for_fetch(fetch) 229 if fetch is None: 230 raise TypeError('Fetch argument %r has invalid type %r' %--> 231 (fetch, type(fetch))) 232 elif isinstance(fetch, (list, tuple)): 233 # NOTE(touts): This is also the code path for namedtuples.TypeError: Fetch参数None的类型无效 <class 'NoneType'>
回答:
这是Keras中的一个bug。最近的更新后,model.optimizer.weights
中包含None
值,这导致在保存模型时调用K.batch_get_value
时出现错误。
我已经提交了一个PR来修复它,并且它已经被合并。你可以安装GitHub上的最新版本的Keras来解决这个问题。