大家都知道keras在使用mnist时需要在线下载数据之后才能继续执行后续代码,而mnist数据集的下载地址已经被墙了。
即:在使用时会报错:
from keras.datasets import mnist
在本地使用方法步骤如下:
1.下载本地:
蓝奏云下载地址:链接
解压之后是一个.npz文件。
编辑mnist.py(我的位置是C:\Users\LFY\envs\deep\Lib\site-packages\keras\datasets)或者pycharm中按住Ctrl点击上面那句代码的mnist即可打开mnisy.py文件:
编辑此.py文件为(把原先的源代码注释掉,增加下面的即可):
(可以看到,源代码默认从亚马逊上下载mnist数据。)
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')
# with np.load(path, allow_pickle=True) as f:
# x_train, y_train = f['x_train'], f['y_train']
# x_test, y_test = f['x_test'], f['y_test']
# return (x_train, y_train), (x_test, y_test)
path = r'D:\Download\mnist.npz' #此处的path是你下载的mnist.py的目录。加r的原因是 让斜杠‘\’不转义,或者可以把\替换为/ 和开头加r的效果相同。
with np.load(path) as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
return (x_train, y_train), (x_test, y_test)
OK,可以加载了。