|
| 1 | +**Keras**是一个上层的神经网络API,它由纯Python编写而成并基Tensorflow、CNTK或者Theano为后端,其项目地址位于 https://github.com/fchollet/keras 。 |
| 2 | + |
| 3 | +Keras的核心数据结构是**"模型"**,这是一种组织网络层的方式。最简单的模型是**Sequential**模型,这是一系列网络层按顺序构成的栈。 |
| 4 | + |
| 5 | +## Keras环境搭建 |
| 6 | + |
| 7 | +#### Keras依赖下面几个库: |
| 8 | + |
| 9 | + numpy, scipy |
| 10 | + yaml |
| 11 | + HDF5 and h5py (Optional) |
| 12 | + cuDNN (Optional) |
| 13 | + |
| 14 | +#### 具体搭建步骤: |
| 15 | + |
| 16 | +* 安装开发包 |
| 17 | + |
| 18 | + |
| 19 | + sudo apt install -y python-dev python-pip python-nose gcc g++ git gfortran vim |
| 20 | + |
| 21 | +* 安装运算加速库 |
| 22 | + |
| 23 | + |
| 24 | + sudo apt install -y libopenblas-dev liblapack-dev libatlas-base-dev |
| 25 | + |
| 26 | +* 安装keras |
| 27 | + |
| 28 | + |
| 29 | + pip install keras |
| 30 | + |
| 31 | +* 安装tensorflow(使用TensorFlow为后端) |
| 32 | + |
| 33 | + |
| 34 | + pip install tensorflow # for Python 2.7 |
| 35 | + pip install tensorflow-gpu # for Python 2.7 and GPU |
| 36 | + |
| 37 | +* keras中mnist数据集测试 |
| 38 | + |
| 39 | + |
| 40 | + git clone https://github.com/fchollet/keras.git |
| 41 | + cd keras/examples/ |
| 42 | + python mnist_mlp.py |
| 43 | + |
| 44 | +## Keras上层API使用 |
| 45 | + |
| 46 | +#### 搭建模型 |
| 47 | + |
| 48 | +搭建一个Sequential模型: |
| 49 | + |
| 50 | + from keras.models import Sequential |
| 51 | + from keras.layers import Dense, Activation |
| 52 | + |
| 53 | +可以通过向Sequential模型传递一个layer的list来构造该模型: |
| 54 | + |
| 55 | + model = Sequential([ |
| 56 | + Dense(units=64, input_shape=(100,)), |
| 57 | + Activation('relu'), |
| 58 | + Dense(units=10), |
| 59 | + Activation('softmax'), |
| 60 | + ]) |
| 61 | + |
| 62 | +也可以通过.add()方法一个个的将layer加入Sequential模型中: |
| 63 | + |
| 64 | + model = Sequential() |
| 65 | + model.add(Dense(units=64, input_dim=100)) |
| 66 | + model.add(Activation('relu')) |
| 67 | + model.add(Dense(units=10)) |
| 68 | + model.add(Activation('softmax')) |
| 69 | + |
| 70 | +#### 编译模型 |
| 71 | + |
| 72 | +这一步用来配置模型的学习流程,编译模型时必须指明**损失函数、优化器、度量**: |
| 73 | + |
| 74 | + model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) |
| 75 | + |
| 76 | +* 损失函数 |
| 77 | + |
| 78 | + |
| 79 | + 该参数为模型试图最小化的目标函数,它可为预定义的损失函数名,如categorical_crossentropy、mse,也可以为一个损失函数。 |
| 80 | + |
| 81 | +* 优化器 |
| 82 | + |
| 83 | + |
| 84 | + 该参数可指定为已预定义的优化器名,如rmsprop、adagrad,或者为一个Optimizer类的对象。 |
| 85 | + |
| 86 | +* 度量 |
| 87 | + |
| 88 | + |
| 89 | + 对分类问题,一般将该列表设置为metrics=['accuracy']。指标可以是一个预定义指标的名字,也可以是一个用户定制的函数。 |
| 90 | + |
| 91 | + |
| 92 | +如果你需要的话,你可以自己定制损失函数: |
| 93 | + |
| 94 | + from keras.optimizers import SGD |
| 95 | + model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True)) |
| 96 | + |
| 97 | +#### 训练模型 |
| 98 | + |
| 99 | +我们在训练数据上按batch进行一定次数的迭代来训练网络: |
| 100 | + |
| 101 | + model.fit(x_train, y_train, epochs=5, batch_size=32) |
| 102 | + |
| 103 | +我们也可以手动将一个个batch的数据送入网络中训练: |
| 104 | + |
| 105 | + model.train_on_batch(x_batch, y_batch) |
| 106 | + |
| 107 | +#### 评估模型 |
| 108 | + |
| 109 | + loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128) |
| 110 | + |
| 111 | +#### 模型预测 |
| 112 | + |
| 113 | + classes = model.predict(x_test, batch_size=128) |
| 114 | + |
| 115 | +## Keras代码举例 |
| 116 | + |
| 117 | +以**"基于多层感知器(Multilayer Perceptron, MLP)的sigmoid二分类"**为例,代码如下: |
| 118 | + |
| 119 | + import keras |
| 120 | + from keras.models import Sequential |
| 121 | + from keras.layers import Dense, Dropout, Activation |
| 122 | + |
| 123 | + # Generate dummy data, with 2 classes (binary classification) |
| 124 | + import numpy as np |
| 125 | + x_train = np.random.random((1000, 20)) |
| 126 | + y_train = np.random.randint(2, size=(1000, 1)) |
| 127 | + x_test = np.random.random((100, 20)) |
| 128 | + y_test = np.random.randint(2, size=(100, 1)) |
| 129 | + |
| 130 | + model = Sequential() |
| 131 | + model.add(Dense(64, activation='relu', input_dim=20)) |
| 132 | + model.add(Dropout(0.5)) |
| 133 | + model.add(Dense(64, activation='relu')) |
| 134 | + model.add(Dropout(0.5)) |
| 135 | + model.add(Dense(1, activation='sigmoid')) |
| 136 | + |
| 137 | + model.compile(loss='binary_crossentropy', |
| 138 | + optimizer='sgd', |
| 139 | + metrics=['accuracy']) |
| 140 | + |
| 141 | + # Train the model, iterating on the data in batches of 128 samples |
| 142 | + model.fit(x_train, y_train, |
| 143 | + epochs=20, |
| 144 | + batch_size=128) |
| 145 | + |
| 146 | + score = model.evaluate(x_test, y_test, batch_size=128) |
| 147 | + print(score) |
| 148 | + |
| 149 | +#### 运行结果 |
| 150 | + |
| 151 | + |
0 commit comments