【问题标题】:How to prepare my own data for tensorflow?如何为张量流准备我自己的数据?
【发布时间】:2016-08-17 17:50:51
【问题描述】:

我在 ubuntu 14.04 上安装了 Tensorflow。我完成了MNIST For ML Beginners 教程。我明白了。

也不,我尝试使用我自己的数据。我的训练数据为 T[1000][10]。标签为 L[2]、1 或 0。

如何访问我的数据 mnist.train.images

【问题讨论】:

  • 你检查了 input_data.py 吗?我想你会从文件中得到一些想法。
  • 我检查它。 github.com/tensorflow/tensorflow/blob/r0.8/tensorflow/examples/… 但是我不知道如何安装和解析数据。
  • 脚本自动下载和导入数据集。我想自己做。
  • 我给你一个答案。让我知道这对您是否有意义。

标签: tensorflow deep-learning mnist


【解决方案1】:

在 input_data.py 中,这两个函数完成了主要工作。

1。下载

def maybe_download(filename, work_directory):
    """Download the data from Yann's website, unless it's already here."""
    if not os.path.exists(work_directory):
        os.mkdir(work_directory)
    filepath = os.path.join(work_directory, filename)
    if not os.path.exists(filepath):
        filepath, _ = urlretrieve(SOURCE_URL + filename, filepath)
        statinfo = os.stat(filepath)
        print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
    return filepath

2 图像到 nparray

def extract_images(filename):
    """Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
    print('Extracting', filename)
    with gzip.open(filename) as bytestream:
        magic = _read32(bytestream)
        if magic != 2051:
            raise ValueError(
                'Invalid magic number %d in MNIST image file: %s' %
                (magic, filename))
        num_images = _read32(bytestream)
        rows = _read32(bytestream)
        cols = _read32(bytestream)
        buf = bytestream.read(rows * cols * num_images)
        data = numpy.frombuffer(buf, dtype=numpy.uint8)
        data = data.reshape(num_images, rows, cols, 1)
        return data

根据您的数据集和位置,您可以调用:

local_file = maybe_download(TRAIN_IMAGES, train_dir)
train_images = extract_images(local_file)

https://github.com/nlintz/TensorFlow-Tutorials/blob/master/input_data.py查看完整的源代码。

【讨论】:

  • 感谢您的回答。我的输入数据有两个文件。其中一个包括单词(mnist 使用图像序列),另一个包括标签(0 或 1)。我无法枚举我的输入。
  • “我无法枚举我的输入”是什么意思?请参阅示例文件中的 def extract_labels(filename, one_hot=False)。
  • 我的word文件有非英文字符。我无法在 extract_images 函数中提取诸如“数据”之类的矩阵。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
相关资源
最近更新 更多