我最近在Keras构建了一个小的网格搜索功能(基本上只是一个通过几个不同的神经网络架构的循环)并遇到了一个相当奇怪的问题。
让我们举一个例子,我正在使用以下列表作为我的网格(它不是真正的网格,因为只有一个超参数,但我仍然称之为):
layerListGrid = []
layerListGrid.append([5,5,5])
layerListGrid.append([10,10,10])
layerListGrid.append([100,100,100,100,100])
换句话说,我想连续三个神经网络,每次都有如上所述的密集层。
要执行此操作,请执行以下操作
#start grid loop
for layerList in layerListGrid:
#define model structure
init = keras.initializers.TruncatedNormal(mean=initializerMean, stddev=initializerStdDev, seed=None)
model = Sequential()
firstEntry=True
for layer in layerList:
if firstEntry:
model.add(Dense(layer, activation=activation, input_shape=(inputSize,), kernel_initializer=init)) #first hidden layer
else:
model.add(Dense(layer, activation=activation, kernel_initializer=init))
if dropoutRate>0.0:
model.add(Dropout(dropoutRate))
firstEntry=False
model.add(Dense(1, activation=finalActivation, kernel_initializer=init)) #outputLayer
model.summary()
#compile model
sgd = keras.optimizers.SGD(lr=learningRate, momentum=momentum, decay=decay, nesterov=False)
model.compile(loss=loss,
optimizer=sgd,
metrics=['accuracy'])
#define callbacks
startTime = datetime.datetime.now()
startTimeText = startTime.strftime("%Y-%m-%d_%H-%M-%S")
tensorboard = keras.callbacks.TensorBoard(log_dir='./TFLogs/'+startTimeText, histogram_freq=1, write_graph=True, write_grads=True, write_images=False)
#fit model
history = model.fit(trainX, trainY,
batch_size=batchSize,
epochs=epochs,
verbose=1,
validation_data=validationData,
validation_split=validation_split,
callbacks=[tensorboard]
)
print('Fitting done')
score = model.evaluate(testX, testY, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
现在,第一次运行循环(即第一次“layerList”),代码正确执行。然后第二个循环训练它的第一个时期,然后在结束时终止(根据下面的错误日志,当张量板回调触发时)。然后它抛出一个错误,抱怨“dense_1”的输入格式不正确。 “dense_1”是Keras给出的第一个循环执行的第一个密集层的唯一名称!在第二个循环中,第一个密集层实际上称为“dense_4”。似乎某种内存指针在循环之外持续存在,但我无法弄清楚它是什么。有没有人遇到这样的事情,或者有人建议我如何解决这个问题?
下面是堆栈跟踪
File "<stdin>", line 1, in <module>
File "/home/username/Repo//dfc/Analytics/tf_modeling/20170919_DFC_TF_dev.py", line 174, in <module>
callbacks=[tensorboard]
File "/home/username/.local/lib/python2.7/site-packages/keras/models.py", line 867, in fit
initial_epoch=initial_epoch)
File "/home/username/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1598, in fit
validation_steps=validation_steps)
File "/home/username/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1203, in _fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/username/.local/lib/python2.7/site-packages/keras/callbacks.py", line 77, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "/home/username/.local/lib/python2.7/site-packages/keras/callbacks.py", line 777, in on_epoch_end
result = self.sess.run([self.merged], feed_dict=feed_dict)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'dense_1_input' with dtype float and shape [?,89]
[[Node: dense_1_input = Placeholder[dtype=DT_FLOAT, shape=[?,89], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]