带有 tensorflow 的 keras 运行良好,直到我添加回调

keras with tensorflow runs fine, until I add callbacks

我是 运行 使用 Keras 和 TensorFlow 后端的模型。一切都很完美:

model = Sequential()
model.add(Dense(dim, input_dim=dim, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(1, activation='linear'))

model.compile(loss='mse', optimizer='Adam', metrics=['mae'])

history = model.fit(X, Y, epochs=12, 
                    batch_size=100, 
                    validation_split=0.2, 
                    shuffle=True, 
                    verbose=2)

但是一旦我包含记录器和回调以便我可以记录 tensorboard,我就会得到

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_layer_input_2' with dtype float and shape [?,1329]...

这是我的代码:(实际上,它工作了 1 次,第一次,然后 ecer 因为出现了这个错误)

model = Sequential()
model.add(Dense(dim, input_dim=dim, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(1, activation='linear'))

model.compile(loss='mse', optimizer='Adam', metrics=['mae'])

logger = keras.callbacks.TensorBoard(log_dir='/tf_logs',
                                     write_graph=True,
                                     histogram_freq=1)

history = model.fit(X, Y, 
                    epochs=12,
                    batch_size=100,
                    validation_split=0.2,
                    shuffle=True,
                    verbose=2,
                    callbacks=[logger])

tensorboard 回调使用 tf.summary.merge_all 函数来收集所有张量以进行直方图计算。因此 - 您的摘要是从以前的模型中收集未从以前的模型运行中清除的张量。为了清除这些以前的模型尝试:

from keras import backend as K

K.clear_session()

model = Sequential()
model.add(Dense(dim, input_dim=dim, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(1, activation='linear'))

model.compile(loss='mse', optimizer='Adam', metrics=['mae'])

logger = keras.callbacks.TensorBoard(log_dir='/tf_logs',
                                 write_graph=True,
                                 histogram_freq=1)

history = model.fit(X, Y, 
                epochs=12,
                batch_size=100,
                validation_split=0.2,
                shuffle=True,
                verbose=2,
                callbacks=[logger])