这一节我们主要学习

  • 利用 Keras 搭建一个简单的手写数字识别网络
# 导入相应的工具包

import numpy as np 
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
from keras.datasets import mnist
# 定义一个函数:从 MNIST 数据集中导入数据并处理好数据格式

def load_data():
    (x_train, y_train) , (x_test, y_test) = mnist.load_data()
    number = 10000
    x_train = x_train[0:number]
    y_train = y_train[0:number]
    x_train = x_train.reshape(number, 28*28)
    x_test = x_test.reshape(x_test.shape[0], 28*28)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    # convert class vectors to binary class matrices
    y_train = np_utils.to_categorical(y_train,10)
    y_test = np_utils.to_categorical(y_test,10)
    x_train = x_train
    x_test = x_test
    # x_test = np.random.normal(x_test)
    x_train = x_train / 255
    x_test = x_test / 255

    return (x_train, y_train), (x_test, y_test)
# 导入数据

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

# 观察一下数据输出格式

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 680s 59us/step

    ---------------------------------------------------------------------------

    error                                     Traceback (most recent call last)

    <ipython-input-4-f22c8af72fbf> in <module>
          1 # 导入数据
          2 
    ----> 3 (x_train,y_train), (x_test,y_test) = load_data()
          4 
          5 # 观察一下数据输出格式
    

    <ipython-input-2-195cd3046b15> in load_data()
          2 
          3 def load_data():
    ----> 4     (x_train, y_train) , (x_test, y_test) = mnist.load_data()
          5     number = 10000
          6     x_train = x_train[0:number]
    

    ~/.virtualenvs/basenv/lib/python3.5/site-packages/keras/datasets/mnist.py in load_data(path)
         24     f = np.load(path)
         25     x_train, y_train = f['x_train'], f['y_train']
    ---> 26     x_test, y_test = f['x_test'], f['y_test']
         27     f.close()
         28     return (x_train, y_train), (x_test, y_test)
    

    ~/.virtualenvs/basenv/lib/python3.5/site-packages/numpy/lib/npyio.py in __getitem__(self, key)
        256                 return format.read_array(bytes,
        257                                          allow_pickle=self.allow_pickle,
    --> 258                                          pickle_kwargs=self.pickle_kwargs)
        259             else:
        260                 return self.zip.read(key)
    

    ~/.virtualenvs/basenv/lib/python3.5/site-packages/numpy/lib/format.py in read_array(fp, allow_pickle, pickle_kwargs)
        724                     read_count = min(max_read_count, count - i)
        725                     read_size = int(read_count * dtype.itemsize)
    --> 726                     data = _read_bytes(fp, read_size, "array data")
        727                     array[i:i+read_count] = numpy.frombuffer(data, dtype=dtype,
        728                                                              count=read_count)
    

    ~/.virtualenvs/basenv/lib/python3.5/site-packages/numpy/lib/format.py in _read_bytes(fp, size, error_template)
        863         # done about that.  note that regular files can't be non-blocking
        864         try:
    --> 865             r = fp.read(size - len(data))
        866             data += r
        867             if len(r) == 0 or len(data) == size:
    

    /usr/lib/python3.5/zipfile.py in read(self, n)
        842         self._offset = 0
        843         while n > 0 and not self._eof:
    --> 844             data = self._read1(n)
        845             if n < len(data):
        846                 self._readbuffer = data
    

    /usr/lib/python3.5/zipfile.py in _read1(self, n)
        918         elif self._compress_type == ZIP_DEFLATED:
        919             n = max(n, self.MIN_READ_SIZE)
    --> 920             data = self._decompressor.decompress(data, n)
        921             self._eof = (self._decompressor.eof or
        922                          self._compress_left <= 0 and
    

    error: Error -3 while decompressing data: invalid literal/length code


# 构建网络模型并训练数据

model = Sequential()

# construct the network

model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=10,activation='softmax'))

# configuration and train

model.compile(loss='mse',optimizer=SGD(lr=0.1),metrics=['accuracy'])

model.fit(x_train,y_train,batch_size=100,epochs=20)
# validation set can be used in fit function to evaluate
# 对模型进行评估,并输出准确率

result = model.evaluate(x_test, y_test)

print("Test Acc:"+"{}".format(result[1]))
# 你会发现训练出的模型效果并不好,接下来可以对网络进行调整

# 可以调整每层神经元的数目
model.add(Dense(units=689,activation='sigmoid'))

# 可以增加网络的深度
for i in range(10):
    model.add(Dense(units=689,activation='sigmoid'))
    
# 效果依然不大好,接下来可以学习下一节课—— DNN 训练技巧,根据课程内容来优化你的网络,让它表现的更好。