kerasTest - juedaiyuer/researchNote GitHub Wiki

keras测试

keras的安装

To install Keras, cd to the Keras folder and run the install command:

sudo python setup.py install

基本概念

张量

张量,或tensor,使用这个词汇的目的是为了表述统一,张量可以看作是向量、矩阵的自然推广,我们用张量来表示广泛的数据类型。

batch

深度学习的优化算法,说白了就是梯度下降。每次的参数更新有两种方式

  • 遍历全部数据集算一次损失函数,然后算函数对各个参数的梯度,更新梯度。这种方法每更新一次参数都要把数据集里的所有样本都看一遍,计算量开销大,计算速度慢,不支持在线学习,这称为Batch gradient descent,批梯度下降
  • 每看一个数据就算一下损失函数,然后求梯度更新参数,这个称为随机梯度下降,stochastic gradient descent。这个方法速度比较快,但是收敛性能不太好,可能在最优点附近晃来晃去,hit不到最优点。两次参数的更新也有可能互相抵消掉,造成目标函数震荡的比较剧烈

解决两种方法的缺点

基本上现在的梯度下降都是基于mini-batch的,所以Keras的模块中经常会出现batch_size,就是指这个

官方代码

这里运行需要解决一下全局代理,终端下翻墙

keras/examples/mnist_cnn.py

'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

batch_size = 128
num_classes = 10
epochs = 12

# input image dimensions
img_rows, img_cols = 28, 28

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

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()

#第一个卷积层,32个卷积核,每个卷积核大小3×3.
#input_shape中的1表示输入的图片的通道,灰度图为1通道;彩色图为3通道
#激活函数用relu
#还可以加上dropout的技巧:model.add(Dropout(0.5))
#border_mode可以是valid或者full
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
#第二个卷积层
#采用maxpooling,poolsize为(2,2)
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
#全连接层,现将前一层输出的二维特征图flatten为一维的
#Dense就是隐藏层
#全连接有128个神经元节点
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
          verbose=1, validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

代码注解

keras的后端,调用tf,theno...进行相关符号操作

from keras import backend as K

amazonaws屏蔽問題

 File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.2-py2.7.egg/keras/datasets/mnist.py", line 15, in load_data
 path = get_file(path, origin='https://s3.amazonaws.com/img-datasets/mnist.npz')

修改獲取的源

path = get_file(path, origin='https://github.com/juedaiyuer/codeworkplace/blob/master/data/mnist.npz')

读取数据问题,未解决

下载之后的包在 ~/.keras/datasets/

遇到问题

socket.error: [Errno 104] Connection reset by peer

无论是否FQ下载都很慢,下载数据到本地解压出pkl文件

import gzip
import sys
from six.moves import cPickle
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()

mnist_cnn_my.py

source