Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow:导入gzip mnist数据集_Python_Tensorflow - Fatal编程技术网

Python TensorFlow:导入gzip mnist数据集

Python TensorFlow:导入gzip mnist数据集,python,tensorflow,Python,Tensorflow,我目前正在学习ML,并完成了这项工作 一切都很好,很酷,但是mnist导入会抛出警告,并表示所使用的方法已被弃用 我不知道我是否应该“更新”这个,所以这将是我的第一个问题,但我还想稍后导入另一个数据集(它将再次是train-images.gz等) 所以,我需要的是一个从文件夹中读取.gz数据集并导入它们的方法。我读过关于tf.data.Dataset,但我认为我没有真正得到它,或者它不是我所需要的。根据TFv1.9上的可用文档,使用Tensorflow导入MNIST(作为numpy数组)并避免

我目前正在学习ML,并完成了这项工作

一切都很好,很酷,但是mnist导入会抛出警告,并表示所使用的方法已被弃用

我不知道我是否应该“更新”这个,所以这将是我的第一个问题,但我还想稍后导入另一个数据集(它将再次是train-images.gz等)


所以,我需要的是一个从文件夹中读取.gz数据集并导入它们的方法。我读过关于
tf.data.Dataset
,但我认为我没有真正得到它,或者它不是我所需要的。

根据TFv1.9上的可用文档,使用Tensorflow导入MNIST(作为numpy数组)并避免不推荐的警告的佳能方法是:

 mnist = tf.keras.datasets.mnist
 (x_train, y_train),(x_test, y_test) = mnist.load_data()
 x_train, x_test = x_train / 255.0, x_test / 255.0
因此,从现在起,您应该避免以下情况:

  • mnist=tf.contrib.learn.datasets.load\u dataset(“mnist”)
  • 来自tensorflow.examples.tutorials.mnist导入输入数据
    mnist=输入数据。读取数据集('mnist\U data',one\U hot=True)
无论如何,如果不是一个选项,那么可能调整下面的函数将起作用:

def extract_images(f):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth].
Args:
   f: A file object that can be passed into a gzip reader.
Returns:
   data: A 4D uint8 numpy array [index, y, x, depth].
Raises:
   ValueError: If the bytestream does not start with 2051.
"""
print('Extracting', f.name)
with gzip.GzipFile(fileobj=f) as bytestream:
   magic = _read32(bytestream)
   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

我明白你的意思,我今天早上真的试过了(但失败了),但我又试了一次:这似乎有效(它非常难看,需要一个解决办法;但它有效)


TF现在不是在r.1.9吗?我也不认为您可以拥有“通用”导入数据集:根据您正在阐述的任务,数据是不同的……我知道——但我将使用
emnist
mnist
数据集。两者都有相同的格式,但据我所知,这将始终从服务器下载作为npz(或nzp?)文件的数据集。我需要将它们放在文件管理器中,或者使用“标准gzip格式”,就像以前创建的方法一样
def _read32(bytestream):
  dt = np.dtype(np.uint32).newbyteorder('>')
  return np.frombuffer(bytestream.read(4), dtype=dt)[0]

def extract_images(f):
    print('Extracting', f.name)
    with gzip.GzipFile(fileobj=f) as bytestream:
        magic = _read32(bytestream)
        if magic != 2051:
            raise ValueError('Invalid magic number %d in MNIST image file: %s' %
                           (magic, f.name))
        num_images = _read32(bytestream)
        rows = _read32(bytestream)
        cols = _read32(bytestream)
        buf = bytestream.read(rows * cols * num_images)
        data = np.frombuffer(buf, dtype=np.uint8)
        data = data.reshape(num_images, rows, cols, 1)
        assert data.shape[3] == 1
        data = data.reshape(data.shape[0],data.shape[1] * data.shape[2])
        data = data.astype(np.float32)
        data = np.multiply(data, 1.0 / 255.0)
        return data
def extract_labels(f):
    with gzip.GzipFile(fileobj=f) as bytestream:
        magic = _read32(bytestream)
        if magic != 2049:
            raise ValueError('Invalid magic number %d in MNIST label file: %s' %
                           (magic, f.name))
        num_items = _read32(bytestream)
        buf = bytestream.read(num_items)
        labels = np.frombuffer(buf, dtype=np.uint8)
        return labels

with gfile.Open("MNIST_data/train-images-idx3-ubyte.gz", "rb") as f:
    train_images = extract_images(f)
with gfile.Open("MNIST_data/train-labels-idx1-ubyte.gz", "rb") as f:
    train_labels = extract_labels(f)
with gfile.Open("MNIST_data/t10k-images-idx3-ubyte.gz", "rb") as f:
    test_images = extract_images(f)
with gfile.Open("MNIST_data/t10k-labels-idx1-ubyte.gz", "rb") as f:   
    test_labels = extract_labels(f)