问题:老板让测试运行Keras官网里的Mnist-cnn.py,结果从下载数据就是一路坑……

 

当前环境:Ubuntu12.04、python2.7、Keras 1.1.1(不知道这个版本号对不对,在启动文件里查到的)

 

按遇到问题的先后逐个出解决方案:

1、load_data数据,下载老是报Errno 104 Connection reset by peer

解决:

  ①因为无论是否FQ下载都很慢,下载数据到本地并解压出pkl文件,绝对路径中不能有中文,

  ②重写数据加载函数,后面上代码,

 

2、运行代码时报错:

OverflowError: Range exceeds valid bounds
从异常跑出的栈里看是numpy的random函数有越界,
解决:有两种方法,就试了种简单的,另一种没试,
文件前面初始化设置:
from keras import backend
backend.set_image_dim_ordering('th')
查看具体解释。


下面是修改后的代码:如有问题还望能评论指出,谢谢,至少现在是能跑的,
import gzip

from six.moves import cPickle

from keras import backend
backend.set_image_dim_ordering('th')

batch_size = 128
nb_classes = 10
nb_epoch = 12

# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3

def load_data(path='mnist.pkl.gz'):
#    path = get_file(path, origin='https://s3.amazonaws.com/img-datasets/mnist.pkl.gz')
    path = r'/home/wh/mnist.pkl'

    if path.endswith('.gz'):
        f = gzip.open(path, 'rb')
    else:
        f = open(path, 'rb')
    f = open(path, 'rb')   
    data = cPickle.load(f)
f.close() return data # (X_train, y_train), (X_test, y_test) # the data, shuffled and split between train and test sets #(X_train, y_train), (X_test, y_test) = mnist.load_data() (X_train, y_train), (X_test, y_test) = load_data()

  

 

时间戳:2016-12-5 20:50:13