【问题标题】:Train test dataset in Data Pipeline在 Data Pipeline 中训练测试数据集
【发布时间】:2019-03-23 20:32:58
【问题描述】:

我是 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.runtrain它。它训练模型,通过使用 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/kernel 已经 存在,不允许。你的意思是设置reuse=True还是 VarScope 中的重用=tf.AUTO_REUSE? :

然后我在图中传递了一个训练测试参数,它为训练和测试创建了一个新变量。但我认为为测试集创建一个全新的图表。

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

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

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    抛出此错误是因为您在测试函数中重新定义图形。 您正在训练或测试模型的事实不应与图表相关。该图应使用占位符作为输入定义一次。然后,您可以使用训练数据或测试数据填充此占位符。

    像批量标准化这样的一些操作会在测试时改变它们的行为。如果您的模型包含这些 OP,您应该像这样将布尔值传递给您的提要字典:

    # 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,这里是使用可馈送迭代器的改编代码 sn-p:

    # 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})
    

    【讨论】:

    • 我觉得我不够清楚。数据来自 one_shot_iterator。我可能错了,但我认为数据是以张量的形式出现的,而不是 numpy 数组。那么当你可以直接将数据传递给图形时,在这里使用 feed_dict 和占位符有什么用呢?
    • 编辑了我的答案
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2021-12-08
    • 2015-07-16
    • 2019-05-01
    • 2017-02-20
    • 1970-01-01
    • 2020-01-21
    • 2019-03-02
    相关资源
    最近更新 更多