我有一个方法train_model
,它获取keras模型对象作为输入并对其进行训练。我在代码的其他地方有一个循环,它在每次迭代中创建一个新模型并将其传递给这个方法。如果我不传递TensorBoard回调,我的代码就可以正常工作。但是,当我通过TensorBoard回调时,第一个网络得到训练,但是对于第二个网络,我得到以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'dense_7_target' with dtype float and shape [?,?] [[{{node dense_7_target}} = Placeholderdtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
在调用.fit
方法之后。(我正在构建的网络是5层)
还有更多意想不到的行为: 当我第二次运行此代码时,第一个网络将不需要训练(因为我已经保存了模型,它将只加载该模型),第二个网络得到训练,没有错误,但我得到了第三个网络相同的错误。你知道吗
在本例中,当我检查TensorBoard图时,我看到第一个网络已正确创建,但第二个网络的层是其应有层的两倍(就好像第一个模型已首先加载,然后第二个网络已在其上构建)。
这是我的train_model
方法:
def train_model(model, data, dataname, MODEL_DIR, LOG_DIR, BS, EP, callbacks):
X_train, Y_train, X_test, Y_test = data
if not os.path.exists(MODEL_DIR):
os.makedirs(MODEL_DIR)
model_callback = tf.keras.callbacks.ModelCheckpoint(MODEL_DIR + dataname + '/',
monitor='mse',
verbose=1,
save_weights_only=True)
tb_callback = tf.keras.callbacks.TensorBoard(log_dir=LOG_DIR + dataname,histogram_freq=10)
callbacks += [model_callback, tb_callback]
if not os.path.exists(MODEL_DIR + dataname + '/'):
model.fit(X_train, Y_train, batch_size=BS, epochs=EP, verbose=0, callbacks=callbacks, validation_split=0.2)
validation_split=0.2)
else:
model.load_weights(MODEL_DIR + dataname + '/')
return model
我已经尽了我所能,我真的不知道我的代码出了什么问题。你知道吗
感谢您的帮助。提前谢谢。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐