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



我正在使用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)

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

InvalidArgumentError (参见上面的回溯(:您必须为占位符张量 'input_layer_input_2' 输入一个带有 dtype float 和 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])

最新更新