1. 问题
在尝试利用keras进行手写体识别的过程中,出现了数据集导入失败的现象,源码和对应报错如下:
import numpy as np
import keras
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
2. 解决方案
导致下载失败的原因肯定是因为外网限制啦,解决方式是将对应的文件下载下来,有以下两种方式:
- 官网网址:https://s3.amazonaws.com/img-datasets/mnist.npz
- 百度网盘:链接https://pan.baidu.com/s/1ejVO2ePrmF93nYaXFz2wZQ ;提取码:y6w2
下载好了放在一个位置,我这里就放在keras里面,使用相对路径,在keras\datasets\mnist.py进行修改,load里面放存储的路径(不用解压)
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)`.
"""
f = np.load(r"..\data\mnist.npz")
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)