【问题标题】:Tensorflow and reading binary data properlyTensorFlow 和正确读取二进制数据
【发布时间】:2018-01-10 14:49:59
【问题描述】:

我正在尝试根据this tutorial 的固定长度记录部分,并通过查看read_cifar10 函数here,将我自己的二进制数据正确读入Tensorflow。请注意,我是 tensorflow 的新手,所以我的理解可能不正确。

我的数据

我的文件是 float32 类型的二进制文件。第一个 32 位样本是标签,其余 256 个样本是数据。我想将最后的数据重塑为 [2, 128] 矩阵。

到目前为止我的代码:

import tensorflow as tf
import os


def read_data(filename_queue):
    item_type = tf.float32
    label_items = 1
    data_items = 256

    label_bytes = label_items * item_type.size
    data_bytes = data_items * item_type.size
    record_bytes = label_bytes + data_bytes

    reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
    key, value = reader.read(filename_queue)

    record_data = tf.decode_raw(value, item_type)

    # labels = tf.cast(tf.strided_slice(record_data, [0], [label_items]), tf.int32)
    label = tf.strided_slice(record_data, [0], [label_items])
    data0 = tf.strided_slice(record_data, [label_items], [label_items + data_items])
    data = tf.reshape(data0, [2, data_items/2])
    return data, label


if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # Set GPU device
    datafiles = ['train_0000.dat', 'train_0001.dat']
    num_epochs = 2
    filename_queue = tf.train.string_input_producer(datafiles, num_epochs=num_epochs, shuffle=True)
    data, label = read_data(filename_queue)
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        (x, y) = read_data(filename_queue)
        print(y.eval())

这段代码交给print(y.eval()),但我担心我还有比这更大的问题。

问题:

当我执行这个时,我得到一个 datalabel 返回张量。问题是我不太明白如何从张量中实际读取数据。例如,我了解自动编码器示例here,但是它有一个mnist.train.next_batch(batch_size) 函数,它被调用来读取下一批。我是否需要为我的函数编写它,或者它是否由我的 read_data() 函数内部的东西处理。如果我需要写那个函数,它是什么样子的?

我还缺少其他明显的东西吗?我使用这种方法的目的是减少 I/O 开销,而不是将所有数据都存储在内存中,因为我的文件很大。

提前致谢。

【问题讨论】:

    标签: python tensorflow tensorflow-datasets


    【解决方案1】:

    是的。你已经完成了。此时您需要:

    1) 编写您的神经网络模型model,它应该获取您的数据并返回一个标签。

    2) 写出你的成本函数C,它接受网络预测和真实标签,并给你一个成本。

    3) 选择和优化器。

    4) 将所有内容放在一起:

    opt = tf.AdamOptimizer(learning_rate=0.001)
    datafiles = ['train_0000.dat', 'train_0001.dat']
    num_epochs = 2
    
    
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)    
        filename_queue = tf.train.string_input_producer(datafiles, num_epochs=num_epochs, shuffle=True)
        data, label = read_data(filename_queue)
        example_batch, label_batch = tf.train.shuffle_batch(
      [data, label], batch_size=128)
    
        y_pred = model(data)
        loss = C(label, y_pred)
    

    之后你迭代并最小化损失:

    opt.minimize(loss)
    

    有关相关信息,另请参阅tf.train.string_input_producer behavior in a loop

    【讨论】:

    • 感谢您的回复,但我还是有点困惑。我的 datafiles 变量是tf.train.string_input_producer 的输入,所以我不确定迭代它是否正确。纪元数是 filename_queue 的输入。你是说我不需要tf.train.string_input_producer
    • 对不起,我确实跳过了tf.train.string_input_producer,你仍然需要它。
    • 编辑了答案以考虑到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2019-09-24
    • 2015-02-05
    • 2010-12-08
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多