gpt4 book ai didi

tensorflow 警告-找到未跟踪的函数,例如lstm_cell_6_layer_call_and_return_conditional_losses

转载 作者:行者123 更新时间:2023-12-03 17:22:55 26 4
gpt4 key购买 nike

我正在使用tensorflow2.4,是tensorflow的新手
这是代码

model = Sequential()
model.add(LSTM(32, input_shape=(X_train.shape[1:])))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))

model.compile(optimizer='rmsprop', loss='mean_absolute_error', metrics='mae')
model.summary()

save_weights_at = 'basic_lstm_model'
save_best = ModelCheckpoint(save_weights_at, monitor='val_loss', verbose=0,
save_best_only=True, save_weights_only=False, mode='min',
period=1)
history = model.fit(x=X_train, y=y_train, batch_size=16, epochs=20,
verbose=1, callbacks=[save_best], validation_data=(X_val, y_val),
shuffle=True)
在某些时代,得到了以下警告:
enter image description here
您知道我为什么收到此警告吗?

最佳答案

以H5格式保存模型似乎对我有用。

model.save(filepath, save_format="h5")
这是将H5与模型检查点一起使用的方法(我尚未对此进行广泛的测试,请注意!)
from tensorflow.keras.callbacks import ModelCheckpoint

class ModelCheckpointH5(ModelCheckpoint):
# There is a bug saving models in TF 2.4
# https://github.com/tensorflow/tensorflow/issues/47479
# This forces the h5 format for saving
def __init__(self,
filepath,
monitor='val_loss',
verbose=0,
save_best_only=False,
save_weights_only=False,
mode='auto',
save_freq='epoch',
options=None,
**kwargs):
super(ModelCheckpointH5, self).__init__(filepath,
monitor='val_loss',
verbose=0,
save_best_only=False,
save_weights_only=False,
mode='auto',
save_freq='epoch',
options=None,
**kwargs)
def _save_model(self, epoch, logs):
from tensorflow.python.keras.utils import tf_utils

logs = logs or {}

if isinstance(self.save_freq,
int) or self.epochs_since_last_save >= self.period:
# Block only when saving interval is reached.
logs = tf_utils.to_numpy_or_python_type(logs)
self.epochs_since_last_save = 0
filepath = self._get_file_path(epoch, logs)

try:
if self.save_best_only:
current = logs.get(self.monitor)
if current is None:
logging.warning('Can save best model only with %s available, '
'skipping.', self.monitor)
else:
if self.monitor_op(current, self.best):
if self.verbose > 0:
print('\nEpoch %05d: %s improved from %0.5f to %0.5f,'
' saving model to %s' % (epoch + 1, self.monitor,
self.best, current, filepath))
self.best = current
if self.save_weights_only:
self.model.save_weights(
filepath, overwrite=True, options=self._options)
else:
self.model.save(filepath, overwrite=True, options=self._options,save_format="h5") # NK edited here
else:
if self.verbose > 0:
print('\nEpoch %05d: %s did not improve from %0.5f' %
(epoch + 1, self.monitor, self.best))
else:
if self.verbose > 0:
print('\nEpoch %05d: saving model to %s' % (epoch + 1, filepath))
if self.save_weights_only:
self.model.save_weights(
filepath, overwrite=True, options=self._options)
else:
self.model.save(filepath, overwrite=True, options=self._options,save_format="h5") # NK edited here

self._maybe_remove_file()
except IOError as e:
# `e.errno` appears to be `None` so checking the content of `e.args[0]`.
if 'is a directory' in six.ensure_str(e.args[0]).lower():
raise IOError('Please specify a non-directory filepath for '
'ModelCheckpoint. Filepath used is an existing '
'directory: {}'.format(filepath))
# Re-throw the error for any other causes.
raise

关于 tensorflow 警告-找到未跟踪的函数,例如lstm_cell_6_layer_call_and_return_conditional_losses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65697623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com