运行 在tensorflow中使用tf.data.Dataset api恢复模型后开发数据集上的模型

Running the model on the development dataset after restoring the model while using tf.data.Dataset api in tensorflow

我在 tensorflow 中使用 tf.data.Dataset api 训练我的模型。我想演示我的部分代码:

# This corresponds to loading the data using the tf.data.Dataset api...
names_train, detected_train, arousal_train, valence_train, liking_train, is_talking_train, images_train,\
iterator_train_all = load_train_sewa_tfrecords(filenames_train, train_batch_size)

names_devel, detected_devel, arousal_devel, valence_devel, liking_devel, is_talking_devel, images_devel, \
iterator_dev_all = load_devel_sewa_tfrecords(filenames_dev, test_batch_size)

现在,我已经看到训练和测试模型应该通过创建模型然后重用它进行测试来完成。所以:

train_predictions, model_layers_name_shape = simpler_CNN(images_train, my_initializer, phase_train, dropout,
                                                         dopout_per_call_bool, mseed, reuse=False)

devel_predictions, _ = simpler_CNN(images_devel, my_initializer, phase_train, dropout, dopout_per_call_bool,
                                   mseed, reuse=True)

现在,这是我的问题:在创建模型时,我已将训练数据集图像作为输入传递给模型。另一方面,当涉及到从给定测试数据集的模型中提取某些特征时,tensorflow 会要求我初始化训练迭代器,因为它假设我正在提供训练数据集(考虑到我如何创建模型 "i.e. I have passed the training images as input to the model where the reuse was False").

现在我尝试使用如下条件:

# the train_dataset will tell whether we are using the training or the testing dataset
train_dataset = tf.placeholder(dtype=tf.bool, shape=(), name='train_dataset')
images = tf.cond(train_dataset, return_images_train, return_images_devel)

# and then passing the images to the simpler_CNN while reuse is False:
train_predictions, model_layers_name_shape = simpler_CNN(images, my_initializer, phase_train, dropout,
                                                     dopout_per_call_bool, mseed, reuse=False)

然而,这并没有解决问题,tensorflow 仍然会要求我初始化训练迭代器。

此外,如果我 运行 初始化训练和测试迭代器,然后 运行 测试数据集上的模型直到结束;现在,如果我尝试提供训练数据集,我将在训练迭代器 上得到 End of sequence,就好像在提供测试数据集时训练迭代器也是 运行ning 一样 (这种行为对我来说很奇怪)。

总而言之,我如何告诉模型我想要加载测试数据集?另外,请注意,我可以 运行 devel_prediction 并且 tensorflow 会知道我在说什么,但是 运行 在模型中设置隐藏层会产生问题。

因此,在第一次创建模型时,假设模型层的名称如下:

layer1/Relu:0
layer2/Relu:0
layer3/Relu:0
layer4/Relu:0
layer5/Relu:0
global_average_pooling:0
dense/BiasAdd:0

现在,在将模型重新用于测试数据集后,tensorflow 将创建另一个模型,但权重将参考首次创建模型时创建的初始权重。当打印出重用模型的层名称时,我得到以下信息:

layer1_1/Relu:0
layer2_1/Relu:0
layer3_1/Relu:0
layer4_1/Relu:0
layer5_1/Relu:0
global_average_pooling_1:0
dense_1/BiasAdd:0

因此,我发现在我的例子中,我是 运行 一个隐藏层,例如 layer5/Relu:0。因此,这将要求我们使用训练数据集,这就是 tensorflow 抱怨的原因。另一方面,我应该 运行 layer5_1/Relu:0 并且问题已解决!!