keras中無法下載 https://s3.amazonaws.com/img-datasets/mnist.npz 解決方法


網址:https://s3.amazonaws.com/img-datasets/mnist.npz,由於顯而易見的原因,無法訪問。
npz實際上是numpy提供的數組存儲方式,簡單的可看做是一系列npy數據的組合,利用np.load函數讀取后得到一個類似字典的對象,可以通過關鍵字進行值查詢,關鍵字對應的值其實就是一個npy數據。
如果用keras自帶的example(from keras.datasets import mnist,在mnist.py下的load_data函數),會使用這種格式。

 

我自己解決方法是在國外的vps機器上下載,然后傳到本地,假設保存為mnist.npz,則加載方法:

import numpy as np
def load_data(path='mnist.npz'):
    """Loads the MNIST dataset.
    # Arguments
        path: path where to cache the dataset locally
            (relative to ~/.keras/datasets).
    # Returns
        Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
    path = get_file(path,
                    origin='https://s3.amazonaws.com/img-datasets/mnist.npz',
                    file_hash='8a61469f7ea1b51cbae51d4f78837e45')
    """
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = load_data()

 

原來的是:

(x_train, y_train), (x_test, y_test) = mnist.load_data()

 替換下OK!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM