简体   繁体   English

使用Tensorflow Dataset API时如何使用设置输入标量

[英]How to feed scalar with settings while using Tensorflow Dataset API

I'm using TF Dataset API with a placeholder for file names which I feed when initializing the iterator (different files depending whether it's a training or validation set). 我正在使用带有占位符的TF Dataset API来存储在初始化迭代器时输入的文件名(不同的文件,取决于它是训练集还是验证集)。 I would also like to use additional placeholder indicating whether we're training or validating (to include in dropout layers). 我还想使用其他占位符,指示我们是在培训还是在验证(包括在辍学层中)。 However, I'm unable to feed values to this placeholder using the dataset initializer (what makes sense since this is not a part of the dataset). 但是,我无法使用数据集初始化程序将值提供给此占位符(这很有意义,因为这不是数据集的一部分)。 How to feed additional variable while using Dataset API then? 那么在使用Dataset API时如何提供其他变量?

Key code pieces: 关键代码段:

filenames_placeholder = tf.placeholder(tf.string, shape = (None))
is_training = tf.placeholder(tf.bool, shape = ()) # Error: You must feed a value for placeholder tensor 'Placeholder_1' with dtype bool
dataset = tf.data.TFRecordDataset(filenames_placeholder)
# (...) Many other dataset operations
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()

# Model code using "next_element"  as inputs including the dropout layer at some point 
# where I would like to let the model know if we're training or validating

tf.layers.dropout(x, training = is_training)

# Model execution
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(iterator.initializer, feed_dict = {filenames_placeholder: training_files, is_training: True})
# (...) Performing training
sess.run(iterator.initializer, feed_dict = {filenames_placeholder: training_files, is_training: False})
# (...) Performing validadtion

What I do in this case is have an additional placeholder with a default value: 在这种情况下,我要做的是使用默认值的附加占位符:

keep_prob = tf.placeholder_with_default(1.0, shape=())

And in the graph: 在图中:

tf.layers.dropout(inputs, rate=1-keep_prob)

Then while training: 然后在训练时:

sess.run(...,feed_dict={keep_prob:0.5})

While evaluating: 在评估时:

sess.run(...) # No feed_dict here since the keep_prob placeholder has a default value of 1

Note that feeding a placeholder while training, that provides an additional float value doesn't slow your training at all. 请注意,在训练过程中喂食占位符可提供额外的float值,这完全不会减慢您的训练速度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在tensorflow中将数据集与MonitoredTrainingSession一起使用时无法馈送 - Can not feed when using Dataset with MonitoredTrainingSession in tensorflow 如何将窗口数据集输入到 Tensorflow 中的 StringLookup 层 - How to feed windowed dataset into StringLookup layer in Tensorflow 如何通过TensorFlow提要字典传递标量 - How do I pass a scalar via a TensorFlow feed dictionary Keras LSTM-使用来自发生器的Tensorflow数据集API的进料序列数据 - Keras LSTM - feed sequence data with Tensorflow dataset API from the generator 在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 数据集,如何在每批上使用自定义窗口提供训练数据? - Tensorflow dataset, how to feed training data using a custom windowing on every batch? 如何使用colab中的Dataset API在tensorflow中加载本地csv文件 - how to load a local csv file in tensorflow using Dataset API in colab 如何使用来自大量wav文件的tensorflow.data.Dataset api创建数据集? - How should one create a dataset using the tensorflow.data.Dataset api from a large set of wav files? 使用数据集 API 在 Tensorflow 中批量滑动窗口 - Sliding window of a batch in Tensorflow using Dataset API 使用Tensorflow的对象检测API用我自己的数据集训练对象检测器时出错 - Error while training an object detector with my own dataset using Tensorflow's Object Detection API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM