zoukankan      html  css  js  c++  java
  • 将mnist数据集保存成numpy格式

    import numpy as np
    from urllib import request
    import gzip
    import pickle
    
    filename = [
    ["training_images","train-images-idx3-ubyte.gz"],
    ["test_images","t10k-images-idx3-ubyte.gz"],
    ["training_labels","train-labels-idx1-ubyte.gz"],
    ["test_labels","t10k-labels-idx1-ubyte.gz"]
    ]
    
    def download_mnist():
        base_url = "http://yann.lecun.com/exdb/mnist/"
        for name in filename:
            print("Downloading "+name[1]+"...")
            request.urlretrieve(base_url+name[1], name[1])
        print("Download complete.")
    
    def save_mnist():
        mnist = {}
        for name in filename[:2]:
            with gzip.open(name[1], 'rb') as f:
                mnist[name[0]] = np.frombuffer(f.read(), np.uint8, offset=16).reshape(-1,28*28)
        for name in filename[-2:]:
            with gzip.open(name[1], 'rb') as f:
                mnist[name[0]] = np.frombuffer(f.read(), np.uint8, offset=8)
        with open("mnist.pkl", 'wb') as f:
            pickle.dump(mnist,f)
        print("Save complete.")
    
    def init():
        download_mnist()
        save_mnist()
    
    def load():
        with open("mnist.pkl",'rb') as f:
            mnist = pickle.load(f)
        return mnist["training_images"], mnist["training_labels"], mnist["test_images"], mnist["test_labels"]
    
    if __name__ == '__main__':
        init()
    

    代码地址:https://github.com/hsjeong5/MNIST-for-Numpy



    MARSGGBO原创





    2018-10-31



  • 相关阅读:
    react-路由简单封装
    promise 和 async / await
    数据结构 栈 、 队列 、 链表
    ES6 Symbol
    react-react常用包与对应使用
    node-egg的使用
    自我理解与概述-BFC(Block formatting context)
    Git
    MySQL优化技巧
    Shiro
  • 原文地址:https://www.cnblogs.com/marsggbo/p/9883247.html
Copyright © 2011-2022 走看看