我是Tensorflow的新手,正在构建一个数据管道,在其中构建了两个迭代器,用于训练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_train的logits来获得训练精度。
但是当我打电话时要获得测试的准确性
logits_test,y = graph(X_video_test,X_audio_test,training=False)
acc,predict_proba = evaluation(logits_test,y)
它给我错误
ValueError:变量bidirectional_rnn / fw / fwd_lstm_1 /内核已 存在,不允许。您的意思是设置reuse = True还是 在VarScope中重用= tf.AUTO_REUSE? :
然后我在图形中传递了一个火车测试参数,该参数为火车和测试创建了一个新变量。但是我认为为测试集创建一个全新的图形。
我正在考虑使用Varscope Reuse,但是它还会创建新图形吗?而不是从经过训练的图形中获取logit?
我只是不明白如何将测试数据输入到图形中。
答案 0 :(得分:2)
由于在测试函数中重新定义图形而引发此错误。 您正在训练或测试模型的事实不应与图形有关。该图形应使用占位符作为输入定义一次。然后,您可以使用培训或测试数据填充此占位符。
一些操作(例如批标准化)会在测试时更改其行为。如果您的模型包含这些OP,则应将布尔值传递给Feed字典,如下所示:
# 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})