- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 tf.train.shuffle_batch 来使用 TensorFlow 1.0 使用 TFRecord
文件中的批量数据。相关功能有:
def tfrecord_to_graph_ops(filenames_list):
file_queue = tf.train.string_input_producer(filenames_list)
reader = tf.TFRecordReader()
_, tfrecord = reader.read(file_queue)
tfrecord_features = tf.parse_single_example(
tfrecord,
features={'targets': tf.FixedLenFeature([], tf.string)}
)
## if no reshaping: `ValueError: All shapes must be fully defined` in
## `tf.train.shuffle_batch`
targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8)
## if using `strided_slice`, always get the first record
# targets = tf.cast(
# tf.strided_slice(targets, [0], [1]),
# tf.int32
# )
## error on shapes being fully defined
# targets = tf.reshape(targets, [])
## get us: Invalid argument: Shape mismatch in tuple component 0.
## Expected [1], got [1000]
targets.set_shape([1])
return targets
def batch_generator(filenames_list, batch_size=BATCH_SIZE):
targets = tfrecord_to_graph_ops(filenames_list)
targets_batch = tf.train.shuffle_batch(
[targets],
batch_size=batch_size,
capacity=(20 * batch_size),
min_after_dequeue=(2 * batch_size)
)
targets_batch = tf.one_hot(
indices=targets_batch, depth=10, on_value=1, off_value=0
)
return targets_batch
def examine_batches(targets_batch):
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for _ in range(10):
targets = sess.run([targets_batch])
print(targets)
coord.request_stop()
coord.join(threads)
代码通过examine_batches()
进入,并收到batch_generator()
的输出。 batch_generator()
调用 tfrecord_to_graph_ops()
,我相信问题出在该函数中。
我正在打电话
targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8)
位于 1,000 字节(数字 0-9)的文件上。如果我在 session 中对此调用 eval()
,它会显示所有 1,000 个元素。但如果我尝试将其放入批处理生成器中,它就会崩溃。
如果我不 reshape 目标
,当tf.train.shuffle_batch
时,我会收到类似ValueError:所有形状必须完全定义
的错误> 被调用。如果我调用 targets.set_shape([1])
,让人想起 Google 的 CIFAR-10 example code ,我收到类似 Invalid argument: Shape Mismatch in tuple component 0. Expected [1], got [1000]
in tf.train.shuffle_batch
的错误。我还尝试使用 tf.strided_slice 来剪切一大块原始数据 - 这不会崩溃,但会导致一遍又一遍地获取第一个事件。
执行此操作的正确方法是什么?要从 TFRecord
文件中提取批处理?
注意,我可以手动编写一个函数来切碎原始字节数据并进行某种批处理 - 如果我使用 feed_dict
方法将数据获取到图表中,这尤其容易 - 但我我正在尝试学习如何使用 TensorFlow 的 TFRecord
文件以及如何使用其内置批处理函数。
谢谢!
最佳答案
Allen Lavoie 在评论中指出了正确的解决方案。重要的缺失部分是 enqueue_many=True
作为 tf.train.shuffle_batch()
的参数。编写这些函数的正确方法是:
def tfrecord_to_graph_ops(filenames_list):
file_queue = tf.train.string_input_producer(filenames_list)
reader = tf.TFRecordReader()
_, tfrecord = reader.read(file_queue)
tfrecord_features = tf.parse_single_example(
tfrecord,
features={'targets': tf.FixedLenFeature([], tf.string)}
)
targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8)
targets = tf.reshape(targets, [-1])
return targets
def batch_generator(filenames_list, batch_size=BATCH_SIZE):
targets = tfrecord_to_graph_ops(filenames_list)
targets_batch = tf.train.shuffle_batch(
[targets],
batch_size=batch_size,
capacity=(20 * batch_size),
min_after_dequeue=(2 * batch_size),
enqueue_many=True
)
return targets_batch
关于python - 在 TensorFlow 中读取 `tf.train.shuffle_batch` 文件时 `TFRecord` 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44437066/
我注意到,如果我将训练数据加载到内存中并将其作为 numpy 数组提供到图中,与使用相同大小的 shuffle 批次相比,速度会有很大差异,我的数据有大约 1000 个实例。 使用内存 1000 次迭
import tensorflow as tf sess = tf.Session() def add_to_batch(image): print('Adding to batch')
更新问题 我正在尝试使用 shuffle_batch() 函数将标签与 tensorflow 中的图像匹配,但是当我开始使用shuffle_batch() 函数。 1。我更新的问题 使用shuffle
它是在一个时期内进行一次洗牌,还是其他? tf.train.shuffle_batch 和 tf.train.batch 有什么区别? 有人可以解释一下吗?谢谢。 最佳答案 首先看一下文档( http
我正在尝试使用 TensorFlow 干净的方式 (tf.train.shuffle_batch) 处理我的输入数据,大部分代码是我从教程中收集的,并稍作修改,例如 decode_jpeg 函数。 s
在 Tensorflow tutorial ,它给出了以下关于 tf.train.shuffle_batch() 的示例: # Creates batches of 32 images and 32
我有一个训练数据文件,大约有 100K 行,并且我在每个训练步骤上运行一个简单的 tf.train.GradientDescentOptimizer。该设置本质上直接取自 Tensorflow 的 M
我有一个 tfrecords 文件,我希望从中创建批量数据。我正在使用 tf.train.shuffle_batch() 来创建单个批处理。在我的训练中,我想调用批处理并通过它们。这就是我被困住的地方
查看两个带有参数的函数签名 tf.train.shuffle_batch_join( tensors_list, batch_size, capacity, min_after_dequeue, se
我使用 Binary data训练 DNN。 但是 tf.train.shuffle_batch 和 tf.train.batch 让我很困惑。 这是我的代码,我将对其进行一些测试。 首先Using_
我正在尝试使用 tf.train.shuffle_batch 来使用 TensorFlow 1.0 使用 TFRecord 文件中的批量数据。相关功能有: def tfrecord_to_graph_
我是一名优秀的程序员,十分优秀!