在tensorflow中使用tf.data.Dataset api时,我已经在tensorflow中训练了我的模型。我想演示我的代码的一些部分:
# 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将要求我初始化训练迭代器,因为它假定我正在输入训练数据集(假定我是如何创建模型的,即我已将训练图像作为输入传递给重用为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
,就像输入测试数据集时训练迭代器也在运行一样(这种行为对我来说很奇怪)
总之,如何告诉模型我要加载测试数据集?另外,请注意,我可以运行devel_prediction
,tensorflow会知道我在说什么,但是在模型中运行隐藏层会产生问题
因此,在第一次创建模型时,假设模型层的名称如下:
现在,在为测试数据集重用模型之后,tensorflow将创建另一个模型,但权重将引用第一次创建模型时创建的初始权重。当打印出重用模型层的名称时,我得到了以下结果:
因此,我发现在我的例子中,我运行了一个隐藏层,比如
layer5/Relu:0
。因此,这将需要我们使用训练数据集,这就是为什么tensorflow抱怨的原因。另一方面,我应该运行layer5_1/Relu:0
并解决问题相关问题 更多 >
编程相关推荐