通过多个学习率运行模型,但仅次于
learning_rate
,程序给出一个错误。我想知道epoch是否在第一次迭代后没有重新初始化,但是确实重新初始化了。
代码是:
from keras.callbacks import ModelCheckpoint, TensorBoard, EarlyStopping
import keras.backend as K
cp = ModelCheckpoint(filepath="attention_bot.h5", save_best_only=True,verbose=1)
tb = TensorBoard(log_dir='./Graph', histogram_freq=1, write_graph=True, write_images=True)
#fig, axs = plt.subplots(3, 2)
learn_rate=[0.001,0.01,0.0001] #, df_cat_train , df_cat_test
for i in range(0,len(learn_rate)):
K.clear_session()
AttentionLSTM = LSTM_attention_model(learn_rate[i])
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.2,cooldown=1,verbose=1,
patience=2, min_lr=0.000001)
lr_init = K.get_value(AttentionLSTM.optimizer.lr)
lr_decay=0.5
print(lr_init)
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1)
lr_decay = lr_decay_callback(lr_init, lr_decay)
epochs=0
history =AttentionLSTM.fit(X_train,y_train,epochs=30,
batch_size=64,verbose=1,validation_split=0.1,callbacks=
[tb,cp,es]).history
print("early stopping"+ str(epochs))
#evaluate the training model#reduce_lr,,reduce_lr,lr_decay
scores = AttentionLSTM.evaluate(X_train,y_train, verbose=1)
print("Accuracy:"+ str(scores[0]) + "\tPrecision: "+ str(scores[1]) +
"\tRecall = "+ str(scores[2]) + "\tF1 Score = "
+ str(scores[3])) #
list_result.append(("LSTM with Attention", scores[1]))
# summarize history for accuracy
plt.plot(history['acc'])
plt.plot(history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history['loss'])
plt.plot(history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
完整的错误堆栈:-
error msgs are : -
TypeError Traceback (most recent call last)
<ipython-input-136-a6eddc472bdf> in <module>()
21 lr_decay = lr_decay_callback(lr_init, lr_decay)
22 epochs=0
---> 23
history=AttentionLSTM.fit(X_train,y_train,epochs=30,
batch_size=64,verbose=1,
validation_split=0.1,callbacks=[tb,cp,es]).history
24 print("early stopping"+ str(epochs))
25 #evaluate the training model#reduce_lr,,reduce_lr,lr_decay
D:\Anaconda3\lib\site-packages\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)
1037
initial_epoch=initial_epoch,
1038
steps_per_epoch=steps_per_epoch,
-> 1039
validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
D:\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in
fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose,
callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch,
steps_per_epoch, validation_steps)
215 for l, o in zip(out_labels, val_outs):
216 epoch_logs['val_' + l] = o
--> 217 callbacks.on_epoch_end(epoch, epoch_logs)
218 if callback_model.stop_training:
219 break
D:\Anaconda3\lib\site-packages\keras\callbacks.py in on_epoch_end(self,
epoch, logs)
77 logs = logs or {}
78 for callback in self.callbacks:
---> 79 callback.on_epoch_end(epoch, logs)
80
81 def on_batch_begin(self, batch, logs=None):
D:\Anaconda3\lib\site-packages\keras\callbacks.py in on_epoch_end(self,
epoch, logs)
939 assert len(batch_val) == len(tensors)
940 feed_dict = dict(zip(tensors, batch_val))
--> 941 result = self.sess.run([self.merged],
feed_dict=feed_dict)
942 summary_str = result[0]
943 self.writer.add_summary(summary_str, epoch)
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
_run(self, handle, fetches, feed_dict, options, run_metadata)
1135 # Create a fetch handler to take care of the structure of
fetches.
1136 fetch_handler = _FetchHandler(
-> 1137 self._graph, fetches, feed_dict_tensor,
feed_handles=feed_handles)
1138
1139 # Run request and get response.
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
__init__(self, graph, fetches, feeds, feed_handles)
469 """
470 with graph.as_default():
--> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
472 self._fetches = []
473 self._targets = []
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
for_fetch(fetch)
259 elif isinstance(fetch, (list, tuple)):
260 # NOTE(touts): This is also the code path for namedtuples.
--> 261 return _ListFetchMapper(fetch)
262 elif isinstance(fetch, collections.Mapping):
263 return _DictFetchMapper(fetch)
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
__init__(self, fetches)
368 """
369 self._fetch_type = type(fetches)
--> 370 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in
fetches]
371 self._unique_fetches, self._value_indices =
_uniquify_fetches(self._mappers)
372
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
<listcomp>(.0)
368 """
369 self._fetch_type = type(fetches)
--> 370 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in
fetches]
371 self._unique_fetches, self._value_indices =
_uniquify_fetches(self._mappers)
372
D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in
for_fetch(fetch)
256 if fetch is None:
257 raise TypeError('Fetch argument %r has invalid type %r' %
(fetch,
--> 258
type(fetch)))
259 elif isinstance(fetch, (list, tuple)):
260 # NOTE(touts): This is also the code path for namedtuples.
TypeError: Fetch argument None has invalid type <class 'NoneType'>