Python 数据管道中的列车试验数据集

Python 数据管道中的列车试验数据集,python,tensorflow,Python,Tensorflow,我是tensorflow的新手,我正在构建一个数据管道,在这个管道中,我为train构建了两个迭代器,测试集来自tfrecord。培训工作正常,但将测试集输入到图形时出现问题 if __name__ == '__main__': X_video_train,X_audio_train,y = dataset('frame_sample/train.tfrecord') X_video_test,X_audio_test,y = dataset('frame_sample/te

我是tensorflow的新手,我正在构建一个数据管道,在这个管道中,我为train构建了两个迭代器,测试集来自tfrecord。培训工作正常,但将测试集输入到图形时出现问题

if __name__ == '__main__':
     X_video_train,X_audio_train,y = dataset('frame_sample/train.tfrecord')
     X_video_test,X_audio_test,y = dataset('frame_sample/test.tfrecord')

     #Input:Train Set
     logits_train = graph(X_video_train,X_audio_train,training=True)
     train = training(logits_train)
这个代码很好,之后我调用
sess.run
train
它。它对模型进行了训练,通过使用logits\u train的logits,我得到了训练精度

但是为了在我打电话的时候得到测试的准确度

logits_test,y = graph(X_video_test,X_audio_test,training=False)
acc,predict_proba = evaluation(logits_test,y)
这给了我错误

ValueError:变量双向\u rnn/fw/fwd\u lstm\u 1/内核已存在 存在,不允许。您的意思是将重用设置为True还是True 重用=tf.AUTO_在VarScope中重用?:

然后我在图中传递了一个train test参数,它为train和test创建了一个新变量。但我认为为测试集创建一个全新的图形是必要的

我正在考虑使用Varscope重用,但它是否也会创建新的图形?而不是从经过训练的图形中获取logit


我只是不明白如何将测试数据输入到图形中

抛出此错误是因为您正在测试函数中重新定义图形。
if __name__ == '__main__':
     X_video_train,X_audio_train,y = dataset('frame_sample/train.tfrecord')
     X_video_test,X_audio_test,y = dataset('frame_sample/test.tfrecord')

     #Input:Train Set
     logits_train = graph(X_video_train,X_audio_train,training=True)
     train = training(logits_train)
您正在训练或测试模型的事实不应该与该图相关。应使用占位符作为输入定义一次图形。然后,您可以使用列车或测试数据填充此占位符

一些操作(如批量标准化)在测试时会改变它们的行为。如果您的模型包含这些操作,您应该向提要字典传递一个布尔值,如下所示:

# Model definition
...
h = tf.layers.batch_normalization(h, training=is_training_pl)
... 

# Training
_, l = sess.run([train_op, loss], {x_pl: x_train_batch,
                                   y_pl: y_train_batch, 
                                   is_training_pl: True})
...
# Testing
l = sess.run(loss, {x_pl: x_test_batch,
                    is_training_pl: False})
在使用新的tf.data.Dataset API的情况下,下面是使用可反馈迭代器的经过调整的代码段:

# Define training and validation datasets with the same structure.
training_dataset = tf.data.Dataset ...
validation_dataset = tf.data.Dataset ...

# A feedable iterator is defined by a handle placeholder and its structure. We
# could use the `output_types` and `output_shapes` properties of either
# `training_dataset` or `validation_dataset` here, because they have
# identical structure.
handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(
    handle, training_dataset.output_types, training_dataset.output_shapes)

next_element = iterator.get_next() # THIS WILL BE USED AS OUR INPUT

# You can use feedable iterators with a variety of different kinds of iterator
# (such as one-shot and initializable iterators).
training_iterator = training_dataset.make_one_shot_iterator()
validation_iterator = validation_dataset.make_initializable_iterator()

# The `Iterator.string_handle()` method returns a tensor that can be evaluated
# and used to feed the `handle` placeholder.
training_handle = sess.run(training_iterator.string_handle())
validation_handle = sess.run(validation_iterator.string_handle())

...
# Model definition
input = next_element
...
h = tf.layers.batch_normalization(h, training=is_training_pl)
... 


# Training
_, l = sess.run([train_op, loss], {is_training_pl: True,
                                   handle: training_handle})

# Validation
sess.run(validation_iterator.initializer)
l = sess.run(loss, {is_training_pl: False,
                    handle: validation_handle})

我想我还不够清楚。数据来自单次迭代。我可能错了,但我认为数据是以张量的形式出现的,而不是numpy数组。那么,当您可以将数据直接传递到图形时,在此处使用提要和占位符有什么用呢?编辑了我的答案