Cannot get MNIST database through Anaconda/jupyter

前端 未结 9 2577
情歌与酒
情歌与酒 2021-02-13 14:20

Hu guys,

I\'m new to python/anaconda/jupyter/numPy, panda, etc.... so please excuse me if it\'s a really stupid question. I\'m trying to obtain MNIST database by using a

相关标签:
9条回答
  • 2021-02-13 14:56

    this is for python 3.6.*

    import os
    from urllib.request import urlretrieve
    import numpy as np
    
    def download(filename, source='http://yann.lecun.com/exdb/mnist/'):
        print("Downloading %s" % filename)
        urlretrieve(source + filename, filename)
    
    # We then define functions for loading MNIST images and labels.
    # For convenience, they also download the requested files if needed.
    import gzip
    
    def load_mnist_images(filename):
        if not os.path.exists(filename):
            download(filename)
        # Read the inputs in Yann LeCun's binary format.
        with gzip.open(filename, 'rb') as f:
            data = np.frombuffer(f.read(), np.uint8, offset=16)
        # The inputs are vectors now, we reshape them to monochrome 2D images,
        # following the shape convention: (examples, channels, rows, columns)
        data = data.reshape(-1, 1, 28, 28)
        # The inputs come as bytes, we convert them to float32 in range [0,1].
        # (Actually to range [0, 255/256], for compatibility to the version
        # provided at http://deeplearning.net/data/mnist/mnist.pkl.gz.)
        return data / np.float32(256)
    
    def load_mnist_labels(filename):
        if not os.path.exists(filename):
            download(filename)
        # Read the labels in Yann LeCun's binary format.
        with gzip.open(filename, 'rb') as f:
            data = np.frombuffer(f.read(), np.uint8, offset=8)
        # The labels are vectors of integers now, that's exactly what we want.
        return data
    
    
    X_train = load_mnist_images('train-images-idx3-ubyte.gz')
    y_train = load_mnist_labels('train-labels-idx1-ubyte.gz')
    X_test = load_mnist_images('t10k-images-idx3-ubyte.gz')
    y_test = load_mnist_labels('t10k-labels-idx1-ubyte.gz')
    
    0 讨论(0)
  • 2021-02-13 15:03

    I also get the same error as you. Here are some possible solutions that do not require this server.

    If you have tensorflow installed, you can get MNIST data in the following way:

    import tensorflow.examples.tutorials.mnist.input_data as input_data
    m=input_data.read_data_sets("MNIST")
    

    Then for example len(m.train.images) is 55000.

    If you don't have tensorflow, you can get this dataset using the instructions here.

    0 讨论(0)
  • 2021-02-13 15:03

    How about you go to this link:

    https://anaconda.org/conda-forge/mnist

    Follow the instructions:

    1. Open Anaconda Prompt
    2. Type conda install -c conda-forge mnist
    3. Type conda install -c conda-forge/label/cf201901 mnist

    I had the same problem too, I followed these instructions and I don't have any errors again. Hope this is helpful. Sorry if this doesn't solve your problem.

    0 讨论(0)
提交回复
热议问题