├── .gitignore ├── .ipynb_checkpoints ├── Untitled1-checkpoint.ipynb ├── keras_demo-checkpoint.ipynb └── 感知器-checkpoint.ipynb ├── README.md ├── 前馈神经网络 ├── .ipynb_checkpoints │ ├── 使用正则化技术提高泛化能力-checkpoint.ipynb │ ├── 关于隐层和隐层神经元的实验-checkpoint.ipynb │ ├── 实现一个单层神经网络-checkpoint.ipynb │ ├── 实现一个自动编码器-checkpoint.ipynb │ ├── 实现感知器-checkpoint.ipynb │ ├── 开始使用激活函数-checkpoint.ipynb │ ├── 构建一个多层神经网络-checkpoint.ipynb │ ├── 测试不同的优化器-checkpoint.ipynb │ ├── 添加Dropout以防止过拟合-checkpoint.ipynb │ └── 调整损失函数-checkpoint.ipynb ├── README.md ├── checkpoints │ └── multi_layer_best_model.h5 ├── data │ ├── .ipynb_checkpoints │ │ └── Untitled-checkpoint.ipynb │ ├── Bike-Sharing-Dataset.zip │ ├── Bike-Sharing-Dataset │ │ ├── Readme.txt │ │ ├── day.csv │ │ └── hour.csv │ ├── best_model_adadelta.h5 │ ├── best_model_adam.h5 │ ├── best_model_adamax.h5 │ ├── best_model_nadam.h5 │ ├── best_model_rmsprop-0001.h5 │ ├── best_model_rmsprop.h5 │ ├── best_model_sgd-0001.h5 │ ├── best_model_sgd.h5 │ ├── mnist.npz │ └── winequality-red.csv ├── 使用正则化技术提高泛化能力.ipynb ├── 关于隐层和隐层神经元的实验.ipynb ├── 实现一个单层神经网络.ipynb ├── 实现一个自动编码器.ipynb ├── 实现感知器.ipynb ├── 开始使用激活函数.ipynb ├── 构建一个多层神经网络.ipynb ├── 测试不同的优化器.ipynb ├── 添加Dropout以防止过拟合.ipynb └── 调整损失函数.ipynb ├── 卷积神经网络 ├── .ipynb_checkpoints │ ├── 使用批量标准化进行优化-checkpoint.ipynb │ ├── 使用滤波器和参数共享-checkpoint.ipynb │ ├── 实现卷积自动编码器-checkpoint.ipynb │ ├── 实验不同类型的初始化-checkpoint.ipynb │ ├── 将一维CNN应用于文本-checkpoint.ipynb │ ├── 应用层合并技术-checkpoint.ipynb │ └── 理解填充和步长-checkpoint.ipynb ├── README.md ├── data │ ├── .ipynb_checkpoints │ │ └── 使用批量标准化进行优化-checkpoint.ipynb │ ├── mnist.npz │ └── 使用批量标准化进行优化.ipynb ├── 使用批量标准化进行优化.ipynb ├── 使用滤波器和参数共享.ipynb ├── 实现卷积自动编码器.ipynb ├── 实验不同类型的初始化.ipynb ├── 将一维CNN应用于文本.ipynb ├── 应用层合并技术.ipynb └── 理解填充和步长.ipynb ├── 强化学习 ├── .ipynb_checkpoints │ ├── 实现深度Q学习算法-checkpoint.ipynb │ └── 实现策略梯度-checkpoint.ipynb ├── README.md ├── 实现深度Q学习算法.ipynb └── 实现策略梯度.ipynb ├── 循环神经网络 ├── .ipynb_checkpoints │ ├── Chapter 4 - Adding attention-checkpoint.ipynb │ ├── 使用GRU-checkpoint.ipynb │ ├── 字符级文本生成-checkpoint.ipynb │ ├── 实现一个简单的RNN-checkpoint.ipynb │ ├── 实现双向RNN-checkpoint.ipynb │ ├── 添加Attention-checkpoint.ipynb │ └── 添加LSTM-checkpoint.ipynb ├── README.md ├── 使用GRU.ipynb ├── 字符级文本生成.ipynb ├── 实现一个简单的RNN.ipynb ├── 实现双向RNN.ipynb ├── 添加Attention.ipynb └── 添加LSTM.ipynb ├── 时间序列和结构化数据 ├── .ipynb_checkpoints │ ├── 使用浅层神经网络进行二元分类-checkpoint.ipynb │ ├── 使用神经网络预测股票价格-checkpoint.ipynb │ └── 预测共享单车需求-checkpoint.ipynb ├── README.md ├── 使用浅层神经网络进行二元分类.ipynb ├── 使用神经网络预测股票价格.ipynb └── 预测共享单车需求.ipynb ├── 框架 └── keras_demo.ipynb ├── 游戏智能体和机器人 ├── .ipynb_checkpoints │ ├── 用GA优化超参数-checkpoint.ipynb │ ├── 通过深度强化学习来玩游戏-checkpoint.ipynb │ └── 通过端到端学习来驾驶汽车-checkpoint.ipynb ├── README.md ├── 用GA优化超参数.ipynb ├── 通过深度强化学习来玩游戏.ipynb └── 通过端到端学习来驾驶汽车.ipynb ├── 生成对抗网络 ├── .ipynb_checkpoints │ ├── 了解GAN-checkpoint.ipynb │ ├── 使用SRGAN-checkpoint.ipynb │ └── 实现DCGAN-checkpoint.ipynb ├── README.md ├── 了解GAN.ipynb ├── 使用SRGAN.ipynb └── 实现DCGAN.ipynb ├── 网络内部构造 ├── .ipynb_checkpoints │ ├── 冻结层-checkpoint.ipynb │ ├── 分析网络权重-checkpoint.ipynb │ ├── 存储网络结构并训练权重-checkpoint.ipynb │ └── 用TensorBoard可视化训练过程-checkpoint.ipynb ├── README.md ├── 冻结层.ipynb ├── 分析网络权重.ipynb ├── 存储网络结构并训练权重.ipynb └── 用TensorBoard可视化训练过程.ipynb ├── 自然语言处理 ├── .ipynb_checkpoints │ ├── 句子翻译-checkpoint.ipynb │ ├── 情绪分析-checkpoint.ipynb │ └── 文本摘要-checkpoint.ipynb ├── README.md ├── 句子翻译.ipynb ├── 情绪分析.ipynb └── 文本摘要.ipynb ├── 计算机视觉 ├── .ipynb_checkpoints │ ├── 人脸识别-checkpoint.ipynb │ ├── 图像中的目标分类-checkpoint.ipynb │ ├── 增广图像-checkpoint.ipynb │ ├── 寻找人脸面部关键点-checkpoint.ipynb │ ├── 将风格转化为图像-checkpoint.ipynb │ ├── 用U-net将图像分类-checkpoint.ipynb │ ├── 目标在图像中的本地化-checkpoint.ipynb │ └── 语义分割与场景理解-checkpoint.ipynb ├── README.md ├── 人脸识别.ipynb ├── 图像中的目标分类.ipynb ├── 增广图像.ipynb ├── 寻找人脸面部关键点.ipynb ├── 将风格转化为图像.ipynb ├── 用U-net将图像分类.ipynb ├── 目标在图像中的本地化.ipynb └── 语义分割与场景理解.ipynb ├── 语音识别和视频分析 ├── .ipynb_checkpoints │ ├── 从零开始实现语音识别流程-checkpoint.ipynb │ ├── 使用深度学习理解视频-checkpoint.ipynb │ └── 使用语音识别技术辨别讲话人-checkpoint.ipynb ├── README.md ├── 从零开始实现语音识别流程.ipynb ├── 使用深度学习理解视频.ipynb └── 使用语音识别技术辨别讲话人.ipynb ├── 超参数选择、调优和神经网络学习 ├── .ipynb_checkpoints │ ├── TensorBoard可视化训练过程-checkpoint.ipynb │ ├── 使用批量生成器-checkpoint.ipynb │ ├── 学习率及其调度-checkpoint.ipynb │ ├── 数据增广使模型更加鲁棒-checkpoint.ipynb │ ├── 比较不同的优化器-checkpoint.ipynb │ ├── 添加Dropout以防止过拟合-checkpoint.ipynb │ └── 网格搜索调参-checkpoint.ipynb ├── README.md ├── TensorBoard可视化训练过程.ipynb ├── 使用批量生成器.ipynb ├── 学习率及其调度.ipynb ├── 数据增广使模型更加鲁棒.ipynb ├── 比较不同的优化器.ipynb ├── 添加Dropout以防止过拟合.ipynb └── 网格搜索调参.ipynb └── 预训练模型 ├── .ipynb_checkpoints ├── 使用GoogleLeNet Inception进行大规模视觉识别-checkpoint.ipynb ├── 对新类别使用预训练的VGG模型-checkpoint.ipynb ├── 用ResNet提取瓶颈特征-checkpoint.ipynb └── 用Xception细调-checkpoint.ipynb ├── README.md ├── 使用GoogleLeNet Inception进行大规模视觉识别.ipynb ├── 对新类别使用预训练的VGG模型.ipynb ├── 用ResNet提取瓶颈特征.ipynb └── 用Xception细调.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | *.mat 2 | *.zip 3 | *.gz 4 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/keras_demo-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "Using TensorFlow backend.\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "from keras.models import Sequential\n", 18 | "from keras.layers import Dense" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import numpy as np\n", 28 | "x_input = np.array([[1, 2, 3, 4, 5]])\n", 29 | "y_input = np.array([[10]])" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "model = Sequential()\n", 39 | "model.add(Dense(units=32, input_dim=x_input.shape[1]))\n", 40 | "model.add(Dense(units=1))" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "model.compile(loss='mse',\n", 50 | " optimizer='sgd',\n", 51 | " metrics=['accuracy'])" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "_________________________________________________________________\n", 64 | "Layer (type) Output Shape Param # \n", 65 | "=================================================================\n", 66 | "dense_1 (Dense) (None, 32) 192 \n", 67 | "_________________________________________________________________\n", 68 | "dense_2 (Dense) (None, 1) 33 \n", 69 | "=================================================================\n", 70 | "Total params: 225\n", 71 | "Trainable params: 225\n", 72 | "Non-trainable params: 0\n", 73 | "_________________________________________________________________\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "model.summary()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 6, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "Epoch 1/10\n", 91 | "1/1 [==============================] - 0s 459ms/step - loss: 48.9001 - acc: 0.0000e+00\n", 92 | "Epoch 2/10\n", 93 | "1/1 [==============================] - 0s 2ms/step - loss: 2144.2153 - acc: 0.0000e+00\n", 94 | "Epoch 3/10\n", 95 | "1/1 [==============================] - 0s 4ms/step - loss: 3431307.2500 - acc: 0.0000e+00\n", 96 | "Epoch 4/10\n", 97 | "1/1 [==============================] - 0s 2ms/step - loss: 20133275527806976.0000 - acc: 0.0000e+00\n", 98 | "Epoch 5/10\n", 99 | "1/1 [==============================] - 0s 2ms/step - loss: inf - acc: 0.0000e+00\n", 100 | "Epoch 6/10\n", 101 | "1/1 [==============================] - 0s 3ms/step - loss: nan - acc: 0.0000e+00\n", 102 | "Epoch 7/10\n", 103 | "1/1 [==============================] - 0s 2ms/step - loss: nan - acc: 0.0000e+00\n", 104 | "Epoch 8/10\n", 105 | "1/1 [==============================] - 0s 4ms/step - loss: nan - acc: 0.0000e+00\n", 106 | "Epoch 9/10\n", 107 | "1/1 [==============================] - 0s 2ms/step - loss: nan - acc: 0.0000e+00\n", 108 | "Epoch 10/10\n", 109 | "1/1 [==============================] - 0s 2ms/step - loss: nan - acc: 0.0000e+00\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "history = model.fit(x_input, y_input, epochs=10, batch_size=32)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 7, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "pred = model.predict(x_input, batch_size=128)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 8, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[[nan]]\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print(pred)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [] 149 | } 150 | ], 151 | "metadata": { 152 | "kernelspec": { 153 | "display_name": "Python 3", 154 | "language": "python", 155 | "name": "python3" 156 | }, 157 | "language_info": { 158 | "codemirror_mode": { 159 | "name": "ipython", 160 | "version": 3 161 | }, 162 | "file_extension": ".py", 163 | "mimetype": "text/x-python", 164 | "name": "python", 165 | "nbconvert_exporter": "python", 166 | "pygments_lexer": "ipython3", 167 | "version": "3.6.8" 168 | } 169 | }, 170 | "nbformat": 4, 171 | "nbformat_minor": 2 172 | } 173 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/感知器-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 深度学习 2 | - 简述 3 | - 第一部分 4 | - 常见深度学习框架(Python API) 5 | - TensorFlow 6 | - Keras(对TensorFlow进一步封装) 7 | - PyTorch(FaceBook) 8 | - CNTK(微软) 9 | - MXNet 10 | - Gluon 11 | - 见文件夹:框架 12 | - 第二部分 13 | - 前馈神经网络 14 | - 感知器 15 | - 一个单层神经网络 16 | - 一个多层神经网络 17 | - 使用激活函数 18 | - 关于隐层和隐层神经元的实验 19 | - 实现一个自动编码器 20 | - 调整损失函数 21 | - 测试不同的优化器 22 | - 使用正则化技术提高泛化能力 23 | - 添加Dropout以防止过拟合 24 | - 见文件夹:前馈神经网络 25 | - 第三部分 26 | - 卷积神经网络 27 | - 滤波器和参数共享 28 | - 应用层合并技术 29 | - 使用批量标准化进行优化 30 | - 理解填充和步长 31 | - 实验不同类型的初始化 32 | - 实现卷积自动编码器 33 | - 将一维CNN应用于文本 34 | - 见文件夹:卷积神经网络 35 | - 第四部分 36 | - 循环神经网络 37 | - 实现一个简单的RNN 38 | - 添加LSTM 39 | - 使用GRU 40 | - 实现双向RNN 41 | - 字符级文本生成 42 | - 添加Attention 43 | - 见文件夹:循环神经网络 44 | - 第五部分 45 | - 强化学习 46 | - 实现策略梯度 47 | - 实现深度Q学习算法 48 | - 见文件夹:强化学习 49 | - 第六部分 50 | - 生成对抗网络 51 | - 了解GAN 52 | - 实现DCGAN 53 | - 使用SRGAN 54 | - 见文件夹:生成对抗网络 55 | - 第七部分 56 | - 计算机视觉 57 | - 增广图像 58 | - 图像中的目标分类 59 | - 目标在图像中的本地化 60 | - 实时检测框架 61 | - 用U-net将图像分类 62 | - 语义分割与场景理解 63 | - 寻找人脸面部关键点 64 | - 人脸识别 65 | - 将风格转化为图像 66 | - 见文件夹:计算机视觉 67 | - 第八部分 68 | - 自然语言处理 69 | - 情绪分析 70 | - 句子翻译 71 | - 文本摘要 72 | - 见文件夹:自然语言处理 73 | - 第九部分 74 | - 语音识别和视频分析 75 | - 从零开始实现语音识别流程 76 | - 使用语音识别技术辨别讲话人 77 | - 使用深度学习理解视频 78 | - 见文件夹:语音识别和视频分析 79 | - 第十部分 80 | - 时间序列和结构化数据 81 | - 使用神经网络预测股票价格 82 | - 预测共享单车需求 83 | - 使用浅层神经网络进行二元分类 84 | - 见文件夹:时间序列和结构化数据 85 | - 第十一部分 86 | - 游戏智能体与机器人 87 | - 通过端到端学习来驾驶汽车 88 | - 通过深度强化学习来玩游戏 89 | - 用GA优化超参数 90 | - 见文件夹:游戏智能体与机器人 91 | - 第十二部分 92 | - 超参数选择、调优和神经网络学习 93 | - TensorBoard可视化训练过程 94 | - 使用批量生成器 95 | - 网格搜索调参 96 | - 学习率及其调度 97 | - 比较不同的优化器 98 | - 确定网络深度 99 | - 添加Dropout以防止过拟合 100 | - 数据增广使模型更加鲁棒 101 | - 利用TTA来提高精度 102 | - 见文件夹:超参数选择、调优和神经网络学习 103 | - 第十三部分 104 | - 网络内部构造 105 | - 用TensorBoard可视化训练过程 106 | - 用TensorBoard可视化网络结构 107 | - 分析网络权重 108 | - 冻结层 109 | - 存储网络结构并训练权重 110 | - 见文件夹:网络内部构造 111 | - 第十四部分 112 | - 预训练模型 113 | - 使用GoogleLeNet/Inception进行大规模视觉识别 114 | - 用ResNet提取瓶颈特征 115 | - 对新类别使用预训练的VGG模型 116 | - 用Xception细调 117 | - 见文件夹:预训练模型 118 | - 补充说明 119 | - 参考书籍为《Python深度学习实战》英德拉-丹-巴克著 120 | - 如果将这本书以及这个项目作为深度学习入门,那么这将不会是一个好的选择,因为这里的东西需要不低的深度学习基础,建议先看入门书籍。(推荐《深度学习之PyTorch实战计算机视觉》) 121 | 122 | -------------------------------------------------------------------------------- /前馈神经网络/.ipynb_checkpoints/测试不同的优化器-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "Using TensorFlow backend.\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import numpy as np\n", 18 | "import pandas as pd\n", 19 | "\n", 20 | "from sklearn.model_selection import train_test_split\n", 21 | "from keras.models import Sequential\n", 22 | "from keras.layers import Dense, Dropout\n", 23 | "from keras.callbacks import EarlyStopping, ModelCheckpoint\n", 24 | "from keras.optimizers import SGD, Adadelta, Adam, RMSprop, Adagrad, Nadam, Adamax\n", 25 | "\n", 26 | "data = pd.read_csv('data/winequality-red.csv')\n", 27 | "y = data['quality']\n", 28 | "X = data.drop(['quality'], axis=1)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "# 拆分数据集\n", 38 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2017)\n", 39 | "X_train, X_val, y_tarin, y_val = train_test_split(X, y, test_size=0.2, random_state=2017)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "# 定义一个创建模型的函数\n", 49 | "def create_model(opt):\n", 50 | " model = Sequential()\n", 51 | " model.add(Dense(100, input_dim=X_train.shape[1], activation='relu'))\n", 52 | " model.add(Dense(50, activation='relu'))\n", 53 | " model.add(Dense(25, activation='relu'))\n", 54 | " model.add(Dense(10, activation='relu'))\n", 55 | " model.add(Dense(1, activation='linear'))\n", 56 | " return model\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "# 定义一个训练期间使用的回调函数\n", 66 | "def create_callbacks(opt):\n", 67 | " callbacks = [\n", 68 | " EarlyStopping(monitor='val_acc', patience=200, verbose=2),\n", 69 | " ModelCheckpoint('data/best_model_'+opt+'.h5', monitor='val_acc', save_best_only=True, verbose=0)\n", 70 | " \n", 71 | " ]\n", 72 | " return callbacks\n" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "opts = dict({\n", 82 | " 'sgd': SGD(),\n", 83 | " 'sgd-0001': SGD(lr=0.0001, decay=0.00001),\n", 84 | " 'adam': Adam(),\n", 85 | " 'adadelta': Adadelta(),\n", 86 | " 'rmsprop': RMSprop(),\n", 87 | " 'rmsprop-0001': RMSprop(lr=0.0001),\n", 88 | " 'nadam': Nadam(),\n", 89 | " 'adamax': Adamax(),\n", 90 | "}\n", 91 | ")" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "Epoch 00201: early stopping\n", 104 | "Epoch 00381: early stopping\n", 105 | "Epoch 00511: early stopping\n", 106 | "Epoch 00726: early stopping\n", 107 | "Epoch 00904: early stopping\n", 108 | "Epoch 01299: early stopping\n", 109 | "Epoch 00689: early stopping\n", 110 | "Epoch 01119: early stopping\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# 训练网络并存储结果\n", 116 | "results = []\n", 117 | "for opt in opts:\n", 118 | " model = create_model(opt)\n", 119 | " callbacks = create_callbacks(opt)\n", 120 | " model.compile(loss='mse', optimizer=opts[opt], metrics=['accuracy'])\n", 121 | " hist = model.fit(X_train.values, y_train, batch_size=128, epochs=5000, validation_data=(X_val.values, y_val), verbose=0, callbacks=callbacks)\n", 122 | " best_epoch = np.argmax(hist.history['val_acc'])\n", 123 | " best_acc = hist.history['val_acc'][best_epoch]\n", 124 | " best_model = create_model(opt)\n", 125 | " # 加载具有最高验证精度的模型\n", 126 | " best_model.load_weights('data/best_model_'+opt+'.h5')\n", 127 | " best_model.compile(loss='mse', optimizer=opts[opt], metrics=['accuracy'])\n", 128 | " score = best_model.evaluate(X_test.values, y_test, verbose=0)\n", 129 | " results.append([opt, best_epoch, best_acc, score[1]])" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 7, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | " optimizer epochs val_accuracy test_accuracy\n", 142 | "0 sgd 0 0.000000 0.000000\n", 143 | "1 sgd-0001 180 0.593750 0.593750\n", 144 | "2 adam 310 0.659375 0.659375\n", 145 | "3 adadelta 525 0.625000 0.625000\n", 146 | "4 rmsprop 703 0.653125 0.653125\n", 147 | "5 rmsprop-0001 1098 0.634375 0.634375\n", 148 | "6 nadam 488 0.650000 0.650000\n", 149 | "7 adamax 918 0.653125 0.653125\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# 比较结果\n", 155 | "res = pd.DataFrame(results)\n", 156 | "res.columns = ['optimizer', 'epochs', 'val_accuracy', 'test_accuracy']\n", 157 | "print(res)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "- 写于最后\n", 165 | " - 后面我会解释如何使用网格搜索来进行参数调优。\n", 166 | " - 网格搜索会寻找合适的优化器来调整神经网络学习过程。" 167 | ] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "language": "python", 174 | "name": "python3" 175 | }, 176 | "language_info": { 177 | "codemirror_mode": { 178 | "name": "ipython", 179 | "version": 3 180 | }, 181 | "file_extension": ".py", 182 | "mimetype": "text/x-python", 183 | "name": "python", 184 | "nbconvert_exporter": "python", 185 | "pygments_lexer": "ipython3", 186 | "version": "3.6.8" 187 | } 188 | }, 189 | "nbformat": 4, 190 | "nbformat_minor": 2 191 | } 192 | -------------------------------------------------------------------------------- /前馈神经网络/README.md: -------------------------------------------------------------------------------- 1 | # 前馈神经网络(FNN) 2 | - 实现感知器 3 | - 实现单层神经网络 4 | - 实现多层神经网络 5 | - 开始使用激活函数 6 | - 关于隐层和隐层神经元的实验 7 | - 实现自动编码器 8 | - 调整损失函数 9 | - 测试不同的优化器 10 | - 使用正则化技术提高泛化能力 11 | - 添加Dropout以防止过拟合 12 | -------------------------------------------------------------------------------- /前馈神经网络/checkpoints/multi_layer_best_model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/checkpoints/multi_layer_best_model.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.6.8" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 2 32 | } 33 | -------------------------------------------------------------------------------- /前馈神经网络/data/Bike-Sharing-Dataset.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/Bike-Sharing-Dataset.zip -------------------------------------------------------------------------------- /前馈神经网络/data/Bike-Sharing-Dataset/Readme.txt: -------------------------------------------------------------------------------- 1 | ========================================== 2 | Bike Sharing Dataset 3 | ========================================== 4 | 5 | Hadi Fanaee-T 6 | 7 | Laboratory of Artificial Intelligence and Decision Support (LIAAD), University of Porto 8 | INESC Porto, Campus da FEUP 9 | Rua Dr. Roberto Frias, 378 10 | 4200 - 465 Porto, Portugal 11 | 12 | 13 | ========================================= 14 | Background 15 | ========================================= 16 | 17 | Bike sharing systems are new generation of traditional bike rentals where whole process from membership, rental and return 18 | back has become automatic. Through these systems, user is able to easily rent a bike from a particular position and return 19 | back at another position. Currently, there are about over 500 bike-sharing programs around the world which is composed of 20 | over 500 thousands bicycles. Today, there exists great interest in these systems due to their important role in traffic, 21 | environmental and health issues. 22 | 23 | Apart from interesting real world applications of bike sharing systems, the characteristics of data being generated by 24 | these systems make them attractive for the research. Opposed to other transport services such as bus or subway, the duration 25 | of travel, departure and arrival position is explicitly recorded in these systems. This feature turns bike sharing system into 26 | a virtual sensor network that can be used for sensing mobility in the city. Hence, it is expected that most of important 27 | events in the city could be detected via monitoring these data. 28 | 29 | ========================================= 30 | Data Set 31 | ========================================= 32 | Bike-sharing rental process is highly correlated to the environmental and seasonal settings. For instance, weather conditions, 33 | precipitation, day of week, season, hour of the day, etc. can affect the rental behaviors. The core data set is related to 34 | the two-year historical log corresponding to years 2011 and 2012 from Capital Bikeshare system, Washington D.C., USA which is 35 | publicly available in http://capitalbikeshare.com/system-data. We aggregated the data on two hourly and daily basis and then 36 | extracted and added the corresponding weather and seasonal information. Weather information are extracted from http://www.freemeteo.com. 37 | 38 | ========================================= 39 | Associated tasks 40 | ========================================= 41 | 42 | - Regression: 43 | Predication of bike rental count hourly or daily based on the environmental and seasonal settings. 44 | 45 | - Event and Anomaly Detection: 46 | Count of rented bikes are also correlated to some events in the town which easily are traceable via search engines. 47 | For instance, query like "2012-10-30 washington d.c." in Google returns related results to Hurricane Sandy. Some of the important events are 48 | identified in [1]. Therefore the data can be used for validation of anomaly or event detection algorithms as well. 49 | 50 | 51 | ========================================= 52 | Files 53 | ========================================= 54 | 55 | - Readme.txt 56 | - hour.csv : bike sharing counts aggregated on hourly basis. Records: 17379 hours 57 | - day.csv - bike sharing counts aggregated on daily basis. Records: 731 days 58 | 59 | 60 | ========================================= 61 | Dataset characteristics 62 | ========================================= 63 | Both hour.csv and day.csv have the following fields, except hr which is not available in day.csv 64 | 65 | - instant: record index 66 | - dteday : date 67 | - season : season (1:springer, 2:summer, 3:fall, 4:winter) 68 | - yr : year (0: 2011, 1:2012) 69 | - mnth : month ( 1 to 12) 70 | - hr : hour (0 to 23) 71 | - holiday : weather day is holiday or not (extracted from http://dchr.dc.gov/page/holiday-schedule) 72 | - weekday : day of the week 73 | - workingday : if day is neither weekend nor holiday is 1, otherwise is 0. 74 | + weathersit : 75 | - 1: Clear, Few clouds, Partly cloudy, Partly cloudy 76 | - 2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist 77 | - 3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds 78 | - 4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog 79 | - temp : Normalized temperature in Celsius. The values are divided to 41 (max) 80 | - atemp: Normalized feeling temperature in Celsius. The values are divided to 50 (max) 81 | - hum: Normalized humidity. The values are divided to 100 (max) 82 | - windspeed: Normalized wind speed. The values are divided to 67 (max) 83 | - casual: count of casual users 84 | - registered: count of registered users 85 | - cnt: count of total rental bikes including both casual and registered 86 | 87 | ========================================= 88 | License 89 | ========================================= 90 | Use of this dataset in publications must be cited to the following publication: 91 | 92 | [1] Fanaee-T, Hadi, and Gama, Joao, "Event labeling combining ensemble detectors and background knowledge", Progress in Artificial Intelligence (2013): pp. 1-15, Springer Berlin Heidelberg, doi:10.1007/s13748-013-0040-3. 93 | 94 | @article{ 95 | year={2013}, 96 | issn={2192-6352}, 97 | journal={Progress in Artificial Intelligence}, 98 | doi={10.1007/s13748-013-0040-3}, 99 | title={Event labeling combining ensemble detectors and background knowledge}, 100 | url={http://dx.doi.org/10.1007/s13748-013-0040-3}, 101 | publisher={Springer Berlin Heidelberg}, 102 | keywords={Event labeling; Event detection; Ensemble learning; Background knowledge}, 103 | author={Fanaee-T, Hadi and Gama, Joao}, 104 | pages={1-15} 105 | } 106 | 107 | ========================================= 108 | Contact 109 | ========================================= 110 | 111 | For further information about this dataset please contact Hadi Fanaee-T (hadi.fanaee@fe.up.pt) 112 | -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_adadelta.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_adadelta.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_adam.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_adam.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_adamax.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_adamax.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_nadam.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_nadam.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_rmsprop-0001.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_rmsprop-0001.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_rmsprop.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_rmsprop.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_sgd-0001.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_sgd-0001.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/best_model_sgd.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/best_model_sgd.h5 -------------------------------------------------------------------------------- /前馈神经网络/data/mnist.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/前馈神经网络/data/mnist.npz -------------------------------------------------------------------------------- /前馈神经网络/测试不同的优化器.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "Using TensorFlow backend.\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import numpy as np\n", 18 | "import pandas as pd\n", 19 | "\n", 20 | "from sklearn.model_selection import train_test_split\n", 21 | "from keras.models import Sequential\n", 22 | "from keras.layers import Dense, Dropout\n", 23 | "from keras.callbacks import EarlyStopping, ModelCheckpoint\n", 24 | "from keras.optimizers import SGD, Adadelta, Adam, RMSprop, Adagrad, Nadam, Adamax\n", 25 | "\n", 26 | "data = pd.read_csv('data/winequality-red.csv')\n", 27 | "y = data['quality']\n", 28 | "X = data.drop(['quality'], axis=1)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "# 拆分数据集\n", 38 | "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2017)\n", 39 | "X_train, X_val, y_tarin, y_val = train_test_split(X, y, test_size=0.2, random_state=2017)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "# 定义一个创建模型的函数\n", 49 | "def create_model(opt):\n", 50 | " model = Sequential()\n", 51 | " model.add(Dense(100, input_dim=X_train.shape[1], activation='relu'))\n", 52 | " model.add(Dense(50, activation='relu'))\n", 53 | " model.add(Dense(25, activation='relu'))\n", 54 | " model.add(Dense(10, activation='relu'))\n", 55 | " model.add(Dense(1, activation='linear'))\n", 56 | " return model\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "# 定义一个训练期间使用的回调函数\n", 66 | "def create_callbacks(opt):\n", 67 | " callbacks = [\n", 68 | " EarlyStopping(monitor='val_acc', patience=200, verbose=2),\n", 69 | " ModelCheckpoint('data/best_model_'+opt+'.h5', monitor='val_acc', save_best_only=True, verbose=0)\n", 70 | " \n", 71 | " ]\n", 72 | " return callbacks\n" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "opts = dict({\n", 82 | " 'sgd': SGD(),\n", 83 | " 'sgd-0001': SGD(lr=0.0001, decay=0.00001),\n", 84 | " 'adam': Adam(),\n", 85 | " 'adadelta': Adadelta(),\n", 86 | " 'rmsprop': RMSprop(),\n", 87 | " 'rmsprop-0001': RMSprop(lr=0.0001),\n", 88 | " 'nadam': Nadam(),\n", 89 | " 'adamax': Adamax(),\n", 90 | "}\n", 91 | ")" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "Epoch 00201: early stopping\n", 104 | "Epoch 00381: early stopping\n", 105 | "Epoch 00511: early stopping\n", 106 | "Epoch 00726: early stopping\n", 107 | "Epoch 00904: early stopping\n", 108 | "Epoch 01299: early stopping\n", 109 | "Epoch 00689: early stopping\n", 110 | "Epoch 01119: early stopping\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# 训练网络并存储结果\n", 116 | "results = []\n", 117 | "for opt in opts:\n", 118 | " model = create_model(opt)\n", 119 | " callbacks = create_callbacks(opt)\n", 120 | " model.compile(loss='mse', optimizer=opts[opt], metrics=['accuracy'])\n", 121 | " hist = model.fit(X_train.values, y_train, batch_size=128, epochs=5000, validation_data=(X_val.values, y_val), verbose=0, callbacks=callbacks)\n", 122 | " best_epoch = np.argmax(hist.history['val_acc'])\n", 123 | " best_acc = hist.history['val_acc'][best_epoch]\n", 124 | " best_model = create_model(opt)\n", 125 | " # 加载具有最高验证精度的模型\n", 126 | " best_model.load_weights('data/best_model_'+opt+'.h5')\n", 127 | " best_model.compile(loss='mse', optimizer=opts[opt], metrics=['accuracy'])\n", 128 | " score = best_model.evaluate(X_test.values, y_test, verbose=0)\n", 129 | " results.append([opt, best_epoch, best_acc, score[1]])" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 7, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | " optimizer epochs val_accuracy test_accuracy\n", 142 | "0 sgd 0 0.000000 0.000000\n", 143 | "1 sgd-0001 180 0.593750 0.593750\n", 144 | "2 adam 310 0.659375 0.659375\n", 145 | "3 adadelta 525 0.625000 0.625000\n", 146 | "4 rmsprop 703 0.653125 0.653125\n", 147 | "5 rmsprop-0001 1098 0.634375 0.634375\n", 148 | "6 nadam 488 0.650000 0.650000\n", 149 | "7 adamax 918 0.653125 0.653125\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# 比较结果\n", 155 | "res = pd.DataFrame(results)\n", 156 | "res.columns = ['optimizer', 'epochs', 'val_accuracy', 'test_accuracy']\n", 157 | "print(res)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "- 写于最后\n", 165 | " - 后面我会解释如何使用网格搜索来进行参数调优。\n", 166 | " - 网格搜索会寻找合适的优化器来调整神经网络学习过程。" 167 | ] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "language": "python", 174 | "name": "python3" 175 | }, 176 | "language_info": { 177 | "codemirror_mode": { 178 | "name": "ipython", 179 | "version": 3 180 | }, 181 | "file_extension": ".py", 182 | "mimetype": "text/x-python", 183 | "name": "python", 184 | "nbconvert_exporter": "python", 185 | "pygments_lexer": "ipython3", 186 | "version": "3.6.8" 187 | } 188 | }, 189 | "nbformat": 4, 190 | "nbformat_minor": 2 191 | } 192 | -------------------------------------------------------------------------------- /卷积神经网络/README.md: -------------------------------------------------------------------------------- 1 | # 卷积神经网络 2 | - 使用滤波器和参数共享 3 | - 应用层合并技术 4 | - 使用批量标准化进行优化 5 | - 理解填充和步长 6 | - 实验不同类型的初始化 7 | - 实现卷积自动编码器 8 | - 将一维CNN应用于文本 9 | -------------------------------------------------------------------------------- /卷积神经网络/data/.ipynb_checkpoints/使用批量标准化进行优化-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 17, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "from keras.datasets import cifar10\n", 12 | "(X_train, y_train), (X_val, y_val) = cifar10.load_data()" 13 | ] 14 | } 15 | ], 16 | "metadata": { 17 | "kernelspec": { 18 | "display_name": "Python 3", 19 | "language": "python", 20 | "name": "python3" 21 | }, 22 | "language_info": { 23 | "codemirror_mode": { 24 | "name": "ipython", 25 | "version": 3 26 | }, 27 | "file_extension": ".py", 28 | "mimetype": "text/x-python", 29 | "name": "python", 30 | "nbconvert_exporter": "python", 31 | "pygments_lexer": "ipython3", 32 | "version": "3.6.8" 33 | } 34 | }, 35 | "nbformat": 4, 36 | "nbformat_minor": 2 37 | } 38 | -------------------------------------------------------------------------------- /卷积神经网络/data/mnist.npz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luanshiyinyang/DeepLearning/a1c749014c5a5193673500f38457781d574be35c/卷积神经网络/data/mnist.npz -------------------------------------------------------------------------------- /卷积神经网络/data/使用批量标准化进行优化.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 17, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "from keras.datasets import cifar10\n", 12 | "(X_train, y_train), (X_val, y_val) = cifar10.load_data()" 13 | ] 14 | } 15 | ], 16 | "metadata": { 17 | "kernelspec": { 18 | "display_name": "Python 3", 19 | "language": "python", 20 | "name": "python3" 21 | }, 22 | "language_info": { 23 | "codemirror_mode": { 24 | "name": "ipython", 25 | "version": 3 26 | }, 27 | "file_extension": ".py", 28 | "mimetype": "text/x-python", 29 | "name": "python", 30 | "nbconvert_exporter": "python", 31 | "pygments_lexer": "ipython3", 32 | "version": "3.6.8" 33 | } 34 | }, 35 | "nbformat": 4, 36 | "nbformat_minor": 2 37 | } 38 | -------------------------------------------------------------------------------- /强化学习/README.md: -------------------------------------------------------------------------------- 1 | # 强化学习 2 | - 实现策略梯度 3 | - 实现深度Q学习算法 -------------------------------------------------------------------------------- /循环神经网络/.ipynb_checkpoints/实现一个简单的RNN-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "X = []\n", 19 | "X.append([1,0,0,0])\n", 20 | "X.append([0,1,0,0])\n", 21 | "X.append([0,0,1,0])\n", 22 | "X.append([0,0,0,1])\n", 23 | "X.append([0,0,0,1])\n", 24 | "X.append([1,0,0,0])\n", 25 | "X.append([0,1,0,0])\n", 26 | "X.append([0,0,1,0])\n", 27 | "X.append([0,0,0,1])\n", 28 | "\n", 29 | "y = [0.20, 0.30, 0.40, 0.50, 0.05, 0.10, 0.20, 0.30, 0.40]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "def sigmoid(x):\n", 39 | " return 1 / (1 + np.exp(-x))\n", 40 | "\n", 41 | "def sigmoid_der(x):\n", 42 | " return 1.0 - x**2" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "layers = []\n", 52 | "# 四个输入变量,10个隐层,1个输出变量\n", 53 | "n_units = (4, 16, 1)\n", 54 | "n_layers = len(n_units)\n", 55 | "\n", 56 | "layers.append(np.ones(n_units[0]+1+n_units[1]))\n", 57 | "for i in range(1, n_layers):\n", 58 | " layers.append(np.ones(n_units[i]))\n", 59 | "\n", 60 | "weights = []\n", 61 | "for i in range(n_layers-1):\n", 62 | " weights.append(np.zeros((layers[i].size, layers[i+1].size)))\n", 63 | "\n", 64 | "weights_delta = [0,]*len(weights)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "def forwards(data):\n", 74 | " layers[0][:n_units[0]] = data\n", 75 | " layers[0][n_units[0]:-1] = layers[1]\n", 76 | "\n", 77 | " for i in range(1, n_layers):\n", 78 | " layers[i][...] = sigmoid(np.dot(layers[i-1], weights[i-1]))\n", 79 | "\n", 80 | " return layers[-1]" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "def backwards(target, learning_rate=0.1, momentum=0.1):\n", 90 | " deltas = []\n", 91 | " error = target - layers[-1]\n", 92 | " delta = error * sigmoid_der(layers[-1])\n", 93 | " deltas.append(delta)\n", 94 | "\n", 95 | " # 确定隐层误差\n", 96 | " for i in range(n_layers-2, 0, -1):\n", 97 | " delta = np.dot(deltas[0], weights[i].T) * sigmoid_der(layers[i])\n", 98 | " deltas.insert(0, delta)\n", 99 | "\n", 100 | " # 权重更新\n", 101 | " for i in range(len(weights)):\n", 102 | " layer = np.atleast_2d(layers[i])\n", 103 | " delta = np.atleast_2d(deltas[i])\n", 104 | " weights_delta_temp = np.dot(layer.T, delta)\n", 105 | " weights[i] += learning_rate*weights_delta_temp + momentum*weights_delta[i]\n", 106 | " weights_delta[i] = weights_delta_temp\n", 107 | "\n", 108 | " return (error**2).sum()" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 7, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "epoch 0 - loss: 0.3116\n", 121 | "epoch 1000 - loss: 0.1660\n", 122 | "epoch 2000 - loss: 0.1801\n", 123 | "epoch 3000 - loss: 0.1877\n", 124 | "epoch 4000 - loss: 0.1914\n", 125 | "epoch 5000 - loss: 0.1922\n", 126 | "epoch 6000 - loss: 0.1920\n", 127 | "epoch 7000 - loss: 0.1916\n", 128 | "epoch 8000 - loss: 0.1913\n", 129 | "epoch 9000 - loss: 0.1912\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "n_epochs = 10000\n", 135 | "\n", 136 | "for i in range(n_epochs):\n", 137 | " loss = 0\n", 138 | " for j in range(len(X)):\n", 139 | " forwards(X[j])\n", 140 | " backwards(y[j])\n", 141 | " loss += (y[j]-forwards(X[j]))**2\n", 142 | " if i%1000 == 0: print('epoch {} - loss: {:04.4f}'.format(i, loss[0]))" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 8, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "X: [1, 0, 0, 0]; y: 0.20; pred: 0.15\n", 155 | "X: [0, 1, 0, 0]; y: 0.30; pred: 0.39\n", 156 | "X: [0, 0, 1, 0]; y: 0.40; pred: 0.29\n", 157 | "X: [0, 0, 0, 1]; y: 0.50; pred: 0.34\n", 158 | "X: [0, 0, 0, 1]; y: 0.05; pred: 0.30\n", 159 | "X: [1, 0, 0, 0]; y: 0.10; pred: 0.16\n", 160 | "X: [0, 1, 0, 0]; y: 0.20; pred: 0.38\n", 161 | "X: [0, 0, 1, 0]; y: 0.30; pred: 0.30\n", 162 | "X: [0, 0, 0, 1]; y: 0.40; pred: 0.33\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "for i in range(len(X)):\n", 168 | " pred = forwards(X[i])\n", 169 | " loss = (y[i]-pred)**2\n", 170 | " print('X: {}; y: {:04.2f}; pred: {:04.2f}'.format(X[i], y[i], pred[0]))" 171 | ] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "Python 3", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.6.8" 191 | }, 192 | "widgets": { 193 | "state": {}, 194 | "version": "1.1.2" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 2 199 | } 200 | -------------------------------------------------------------------------------- /循环神经网络/README.md: -------------------------------------------------------------------------------- 1 | # 循环神经网络 2 | - 实现一个简单的RNN 3 | - 添加LSTM 4 | - 使用GRU 5 | - 实现双向RNN 6 | - 字符级文本生成 7 | - 添加Attention 8 | -------------------------------------------------------------------------------- /循环神经网络/实现一个简单的RNN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "X = []\n", 19 | "X.append([1,0,0,0])\n", 20 | "X.append([0,1,0,0])\n", 21 | "X.append([0,0,1,0])\n", 22 | "X.append([0,0,0,1])\n", 23 | "X.append([0,0,0,1])\n", 24 | "X.append([1,0,0,0])\n", 25 | "X.append([0,1,0,0])\n", 26 | "X.append([0,0,1,0])\n", 27 | "X.append([0,0,0,1])\n", 28 | "\n", 29 | "y = [0.20, 0.30, 0.40, 0.50, 0.05, 0.10, 0.20, 0.30, 0.40]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "def sigmoid(x):\n", 39 | " return 1 / (1 + np.exp(-x))\n", 40 | "\n", 41 | "def sigmoid_der(x):\n", 42 | " return 1.0 - x**2" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "layers = []\n", 52 | "# 四个输入变量,10个隐层,1个输出变量\n", 53 | "n_units = (4, 16, 1)\n", 54 | "n_layers = len(n_units)\n", 55 | "\n", 56 | "layers.append(np.ones(n_units[0]+1+n_units[1]))\n", 57 | "for i in range(1, n_layers):\n", 58 | " layers.append(np.ones(n_units[i]))\n", 59 | "\n", 60 | "weights = []\n", 61 | "for i in range(n_layers-1):\n", 62 | " weights.append(np.zeros((layers[i].size, layers[i+1].size)))\n", 63 | "\n", 64 | "weights_delta = [0,]*len(weights)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "def forwards(data):\n", 74 | " layers[0][:n_units[0]] = data\n", 75 | " layers[0][n_units[0]:-1] = layers[1]\n", 76 | "\n", 77 | " for i in range(1, n_layers):\n", 78 | " layers[i][...] = sigmoid(np.dot(layers[i-1], weights[i-1]))\n", 79 | "\n", 80 | " return layers[-1]" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "def backwards(target, learning_rate=0.1, momentum=0.1):\n", 90 | " deltas = []\n", 91 | " error = target - layers[-1]\n", 92 | " delta = error * sigmoid_der(layers[-1])\n", 93 | " deltas.append(delta)\n", 94 | "\n", 95 | " # 确定隐层误差\n", 96 | " for i in range(n_layers-2, 0, -1):\n", 97 | " delta = np.dot(deltas[0], weights[i].T) * sigmoid_der(layers[i])\n", 98 | " deltas.insert(0, delta)\n", 99 | "\n", 100 | " # 权重更新\n", 101 | " for i in range(len(weights)):\n", 102 | " layer = np.atleast_2d(layers[i])\n", 103 | " delta = np.atleast_2d(deltas[i])\n", 104 | " weights_delta_temp = np.dot(layer.T, delta)\n", 105 | " weights[i] += learning_rate*weights_delta_temp + momentum*weights_delta[i]\n", 106 | " weights_delta[i] = weights_delta_temp\n", 107 | "\n", 108 | " return (error**2).sum()" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 7, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "epoch 0 - loss: 0.3116\n", 121 | "epoch 1000 - loss: 0.1660\n", 122 | "epoch 2000 - loss: 0.1801\n", 123 | "epoch 3000 - loss: 0.1877\n", 124 | "epoch 4000 - loss: 0.1914\n", 125 | "epoch 5000 - loss: 0.1922\n", 126 | "epoch 6000 - loss: 0.1920\n", 127 | "epoch 7000 - loss: 0.1916\n", 128 | "epoch 8000 - loss: 0.1913\n", 129 | "epoch 9000 - loss: 0.1912\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "n_epochs = 10000\n", 135 | "\n", 136 | "for i in range(n_epochs):\n", 137 | " loss = 0\n", 138 | " for j in range(len(X)):\n", 139 | " forwards(X[j])\n", 140 | " backwards(y[j])\n", 141 | " loss += (y[j]-forwards(X[j]))**2\n", 142 | " if i%1000 == 0: print('epoch {} - loss: {:04.4f}'.format(i, loss[0]))" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 8, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "X: [1, 0, 0, 0]; y: 0.20; pred: 0.15\n", 155 | "X: [0, 1, 0, 0]; y: 0.30; pred: 0.39\n", 156 | "X: [0, 0, 1, 0]; y: 0.40; pred: 0.29\n", 157 | "X: [0, 0, 0, 1]; y: 0.50; pred: 0.34\n", 158 | "X: [0, 0, 0, 1]; y: 0.05; pred: 0.30\n", 159 | "X: [1, 0, 0, 0]; y: 0.10; pred: 0.16\n", 160 | "X: [0, 1, 0, 0]; y: 0.20; pred: 0.38\n", 161 | "X: [0, 0, 1, 0]; y: 0.30; pred: 0.30\n", 162 | "X: [0, 0, 0, 1]; y: 0.40; pred: 0.33\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "for i in range(len(X)):\n", 168 | " pred = forwards(X[i])\n", 169 | " loss = (y[i]-pred)**2\n", 170 | " print('X: {}; y: {:04.2f}; pred: {:04.2f}'.format(X[i], y[i], pred[0]))" 171 | ] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "Python 3", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.6.8" 191 | }, 192 | "widgets": { 193 | "state": {}, 194 | "version": "1.1.2" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 2 199 | } 200 | -------------------------------------------------------------------------------- /时间序列和结构化数据/.ipynb_checkpoints/使用神经网络预测股票价格-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import matplotlib.pyplot as plt\n", 10 | "import numpy as np\n", 11 | "import pandas as pd\n", 12 | "from sklearn.preprocessing import MinMaxScaler\n", 13 | "\n", 14 | "from keras.layers.core import Dense, Activation, Dropout\n", 15 | "from keras.layers.recurrent import LSTM\n", 16 | "from keras.models import Sequential" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "# Data at https://ca.finance.yahoo.com/quote/%5EIXIC/, you can replace it with any other index or stock" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "data = pd.read_csv('../Data/stock-data-2000-2017.csv')\n", 35 | "data = data[['Open', 'High', 'Low', 'Volume', 'Close']]\n", 36 | "data.head()" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "sequence_length = 21\n", 46 | "n_features = len(data.columns)\n", 47 | "val_ratio = 0.1\n", 48 | "n_epochs = 300\n", 49 | "batch_size = 512" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "data = data.as_matrix()\n", 59 | "data_processed = []\n", 60 | "for index in range(len(data) - sequence_length):\n", 61 | " data_processed.append(data[index : index + sequence_length])\n", 62 | "data_processed = np.array(data_processed)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "val_split = round((1-val_ratio) * data_processed.shape[0])\n", 72 | "train = data_processed[: int(val_split), :]\n", 73 | "val = data_processed[int(val_split) :, :]\n", 74 | "\n", 75 | "print('Training data: {}'.format(train.shape))\n", 76 | "print('Validation data: {}'.format(val.shape))" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "train_samples, train_nx, train_ny = train.shape\n", 86 | "val_samples, val_nx, val_ny = val.shape\n", 87 | "\n", 88 | "train = train.reshape((train_samples, train_nx * train_ny))\n", 89 | "val = val.reshape((val_samples, val_nx * val_ny))\n", 90 | "\n", 91 | "preprocessor = MinMaxScaler().fit(train)\n", 92 | "train = preprocessor.transform(train)\n", 93 | "val = preprocessor.transform(val)\n", 94 | "\n", 95 | "train = train.reshape((train_samples, train_nx, train_ny))\n", 96 | "val = val.reshape((val_samples, val_nx, val_ny))" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "X_train = train[:, : -1]\n", 106 | "y_train = train[:, -1][: ,-1]\n", 107 | "X_val = val[:, : -1]\n", 108 | "y_val = val[:, -1][ : ,-1]\n", 109 | "\n", 110 | "X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], n_features))\n", 111 | "X_val = np.reshape(X_val, (X_val.shape[0], X_val.shape[1], n_features))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "model = Sequential()\n", 121 | "model.add(LSTM(input_shape=(X_train.shape[1:]), units = 128, return_sequences=True))\n", 122 | "model.add(Dropout(0.5))\n", 123 | "model.add(LSTM(128, return_sequences=False))\n", 124 | "model.add(Dropout(0.25))\n", 125 | "model.add(Dense(units=1))\n", 126 | "model.add(Activation(\"linear\"))\n", 127 | "\n", 128 | "model.compile(loss=\"mse\", optimizer=\"adam\")" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "history = model.fit(\n", 138 | " X_train,\n", 139 | " y_train,\n", 140 | " batch_size=batch_size,\n", 141 | " epochs=n_epochs,\n", 142 | " verbose=1\n", 143 | " )" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "preds_val = model.predict(X_val)\n", 153 | "diff = []\n", 154 | "for i in range(len(y_val)):\n", 155 | " pred = preds_val[i][0]\n", 156 | " diff.append(y_val[i] - pred)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "real_min = preprocessor.data_min_[104]\n", 166 | "real_max = preprocessor.data_max_[104]\n", 167 | "print(preprocessor.data_min_[104])\n", 168 | "print(preprocessor.data_max_[104])\n", 169 | "\n", 170 | "preds_real = preds_val * (real_max - real_min) + real_min\n", 171 | "y_val_real = y_val * (real_max - real_min) + real_min" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "plt.plot(preds_real, label='Predictions')\n", 181 | "plt.plot(y_val_real, label='Actual values')\n", 182 | "plt.xlabel('test')\n", 183 | "plt.legend(loc=0)\n", 184 | "plt.show()" 185 | ] 186 | } 187 | ], 188 | "metadata": { 189 | "kernelspec": { 190 | "display_name": "Python 3", 191 | "language": "python", 192 | "name": "python3" 193 | }, 194 | "language_info": { 195 | "codemirror_mode": { 196 | "name": "ipython", 197 | "version": 3 198 | }, 199 | "file_extension": ".py", 200 | "mimetype": "text/x-python", 201 | "name": "python", 202 | "nbconvert_exporter": "python", 203 | "pygments_lexer": "ipython3", 204 | "version": "3.6.8" 205 | }, 206 | "widgets": { 207 | "state": {}, 208 | "version": "1.1.2" 209 | } 210 | }, 211 | "nbformat": 4, 212 | "nbformat_minor": 2 213 | } 214 | -------------------------------------------------------------------------------- /时间序列和结构化数据/.ipynb_checkpoints/预测共享单车需求-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from sklearn import preprocessing\n", 10 | "import pandas as pd\n", 11 | "import numpy as np\n", 12 | "from math import pi, sin, cos\n", 13 | "from datetime import datetime\n", 14 | "\n", 15 | "from keras.models import Sequential\n", 16 | "from keras.layers import Dense, Activation, Dropout\n", 17 | "from keras.optimizers import Adam\n", 18 | "from keras.callbacks import EarlyStopping\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# https://www.kaggle.com/c/bike-sharing-demand/data" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "train = pd.read_csv('../Data/bike-sharing/train.csv')\n", 37 | "test = pd.read_csv('../Data/bike-sharing/test.csv')\n", 38 | "data = pd.concat([train, test])\n", 39 | "test_split = train.shape[0]" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "data.head()" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "data['hour'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').hour, axis=1)\n", 58 | "data['weekday'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').weekday(), axis=1)\n", 59 | "data['month'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').month, axis=1)\n", 60 | "data['year'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').year, axis=1)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "data['hour_sin'] = data.apply(lambda x: sin(x['hour'] / 24.0 * 2 * pi), axis=1)\n", 70 | "data['hour_cos'] = data.apply(lambda x: cos(x['hour'] / 24.0 * 2 * pi), axis=1)\n", 71 | "data['weekday_sin'] = data.apply(lambda x: sin(x['weekday'] / 7.0 * 2 * pi), axis=1)\n", 72 | "data['weekday_cos'] = data.apply(lambda x: cos(x['weekday'] / 7.0 * 2 * pi), axis=1)\n", 73 | "data['month_sin'] = data.apply(lambda x: sin(((x['month'] - 5) % 12) / 12.0 * 2 * pi), axis=1)\n", 74 | "data['month_cos'] = data.apply(lambda x: cos(((x['month'] - 5) % 12) / 12.0 * 2 * pi), axis=1)\n", 75 | "data['season_sin'] = data.apply(lambda x: sin(((x['season'] - 3) % 4) / 4.0 * 2 * pi), axis=1)\n", 76 | "data['season_cos'] = data.apply(lambda x: cos(((x['season'] - 3) % 4) / 4.0 * 2 * pi), axis=1)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "X_train = data[:test_split].drop(['datetime', 'casual', 'registered', 'count'], inplace=False, axis=1)\n", 86 | "X_test = data[test_split:].drop(['datetime', 'casual', 'registered', 'count'], inplace=False, axis=1)\n", 87 | "y_train = data['count'][:test_split]\n", 88 | "y_test = data['count'][test_split:]" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "scaler = preprocessing.StandardScaler()\n", 98 | "X_train = scaler.fit_transform(X_train)\n", 99 | "X_test = scaler.transform(X_test)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "model = Sequential()\n", 109 | "model.add(Dense(200, input_dim=X_train.shape[1]))\n", 110 | "model.add(Activation('relu'))\n", 111 | "model.add(Dropout(0.1))\n", 112 | "model.add(Dense(200))\n", 113 | "model.add(Activation('relu'))\n", 114 | "model.add(Dropout(0.1))\n", 115 | "model.add(Dense(1))\n", 116 | "\n", 117 | "opt = Adam()\n", 118 | "model.compile(loss='mean_squared_logarithmic_error', optimizer=opt)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "n_epochs = 1000\n", 128 | "batch_size = 128" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "callbacks = [EarlyStopping(monitor='val_loss', patience=5)]\n", 138 | "history = model.fit(X_train, y_train, shuffle=True, epochs=n_epochs, batch_size=batch_size, validation_split=0.1, verbose=1)" 139 | ] 140 | } 141 | ], 142 | "metadata": { 143 | "kernelspec": { 144 | "display_name": "Python 3", 145 | "language": "python", 146 | "name": "python3" 147 | }, 148 | "language_info": { 149 | "codemirror_mode": { 150 | "name": "ipython", 151 | "version": 3 152 | }, 153 | "file_extension": ".py", 154 | "mimetype": "text/x-python", 155 | "name": "python", 156 | "nbconvert_exporter": "python", 157 | "pygments_lexer": "ipython3", 158 | "version": "3.6.8" 159 | }, 160 | "widgets": { 161 | "state": {}, 162 | "version": "1.1.2" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 2 167 | } 168 | -------------------------------------------------------------------------------- /时间序列和结构化数据/README.md: -------------------------------------------------------------------------------- 1 | # 时间序列和结构化数据 2 | - 使用神经网络预测股票价格 3 | - 预测共享单车需求 4 | - 使用浅层神经网络进行二元分类 -------------------------------------------------------------------------------- /时间序列和结构化数据/使用神经网络预测股票价格.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import matplotlib.pyplot as plt\n", 10 | "import numpy as np\n", 11 | "import pandas as pd\n", 12 | "from sklearn.preprocessing import MinMaxScaler\n", 13 | "\n", 14 | "from keras.layers.core import Dense, Activation, Dropout\n", 15 | "from keras.layers.recurrent import LSTM\n", 16 | "from keras.models import Sequential" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "# Data at https://ca.finance.yahoo.com/quote/%5EIXIC/, you can replace it with any other index or stock" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "data = pd.read_csv('../Data/stock-data-2000-2017.csv')\n", 35 | "data = data[['Open', 'High', 'Low', 'Volume', 'Close']]\n", 36 | "data.head()" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "sequence_length = 21\n", 46 | "n_features = len(data.columns)\n", 47 | "val_ratio = 0.1\n", 48 | "n_epochs = 300\n", 49 | "batch_size = 512" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "data = data.as_matrix()\n", 59 | "data_processed = []\n", 60 | "for index in range(len(data) - sequence_length):\n", 61 | " data_processed.append(data[index : index + sequence_length])\n", 62 | "data_processed = np.array(data_processed)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "val_split = round((1-val_ratio) * data_processed.shape[0])\n", 72 | "train = data_processed[: int(val_split), :]\n", 73 | "val = data_processed[int(val_split) :, :]\n", 74 | "\n", 75 | "print('Training data: {}'.format(train.shape))\n", 76 | "print('Validation data: {}'.format(val.shape))" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "train_samples, train_nx, train_ny = train.shape\n", 86 | "val_samples, val_nx, val_ny = val.shape\n", 87 | "\n", 88 | "train = train.reshape((train_samples, train_nx * train_ny))\n", 89 | "val = val.reshape((val_samples, val_nx * val_ny))\n", 90 | "\n", 91 | "preprocessor = MinMaxScaler().fit(train)\n", 92 | "train = preprocessor.transform(train)\n", 93 | "val = preprocessor.transform(val)\n", 94 | "\n", 95 | "train = train.reshape((train_samples, train_nx, train_ny))\n", 96 | "val = val.reshape((val_samples, val_nx, val_ny))" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "X_train = train[:, : -1]\n", 106 | "y_train = train[:, -1][: ,-1]\n", 107 | "X_val = val[:, : -1]\n", 108 | "y_val = val[:, -1][ : ,-1]\n", 109 | "\n", 110 | "X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], n_features))\n", 111 | "X_val = np.reshape(X_val, (X_val.shape[0], X_val.shape[1], n_features))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "model = Sequential()\n", 121 | "model.add(LSTM(input_shape=(X_train.shape[1:]), units = 128, return_sequences=True))\n", 122 | "model.add(Dropout(0.5))\n", 123 | "model.add(LSTM(128, return_sequences=False))\n", 124 | "model.add(Dropout(0.25))\n", 125 | "model.add(Dense(units=1))\n", 126 | "model.add(Activation(\"linear\"))\n", 127 | "\n", 128 | "model.compile(loss=\"mse\", optimizer=\"adam\")" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "history = model.fit(\n", 138 | " X_train,\n", 139 | " y_train,\n", 140 | " batch_size=batch_size,\n", 141 | " epochs=n_epochs,\n", 142 | " verbose=1\n", 143 | " )" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "preds_val = model.predict(X_val)\n", 153 | "diff = []\n", 154 | "for i in range(len(y_val)):\n", 155 | " pred = preds_val[i][0]\n", 156 | " diff.append(y_val[i] - pred)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "real_min = preprocessor.data_min_[104]\n", 166 | "real_max = preprocessor.data_max_[104]\n", 167 | "print(preprocessor.data_min_[104])\n", 168 | "print(preprocessor.data_max_[104])\n", 169 | "\n", 170 | "preds_real = preds_val * (real_max - real_min) + real_min\n", 171 | "y_val_real = y_val * (real_max - real_min) + real_min" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "plt.plot(preds_real, label='Predictions')\n", 181 | "plt.plot(y_val_real, label='Actual values')\n", 182 | "plt.xlabel('test')\n", 183 | "plt.legend(loc=0)\n", 184 | "plt.show()" 185 | ] 186 | } 187 | ], 188 | "metadata": { 189 | "kernelspec": { 190 | "display_name": "Python 3", 191 | "language": "python", 192 | "name": "python3" 193 | }, 194 | "language_info": { 195 | "codemirror_mode": { 196 | "name": "ipython", 197 | "version": 3 198 | }, 199 | "file_extension": ".py", 200 | "mimetype": "text/x-python", 201 | "name": "python", 202 | "nbconvert_exporter": "python", 203 | "pygments_lexer": "ipython3", 204 | "version": "3.6.8" 205 | }, 206 | "widgets": { 207 | "state": {}, 208 | "version": "1.1.2" 209 | } 210 | }, 211 | "nbformat": 4, 212 | "nbformat_minor": 2 213 | } 214 | -------------------------------------------------------------------------------- /时间序列和结构化数据/预测共享单车需求.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from sklearn import preprocessing\n", 10 | "import pandas as pd\n", 11 | "import numpy as np\n", 12 | "from math import pi, sin, cos\n", 13 | "from datetime import datetime\n", 14 | "\n", 15 | "from keras.models import Sequential\n", 16 | "from keras.layers import Dense, Activation, Dropout\n", 17 | "from keras.optimizers import Adam\n", 18 | "from keras.callbacks import EarlyStopping\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# https://www.kaggle.com/c/bike-sharing-demand/data" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "train = pd.read_csv('../Data/bike-sharing/train.csv')\n", 37 | "test = pd.read_csv('../Data/bike-sharing/test.csv')\n", 38 | "data = pd.concat([train, test])\n", 39 | "test_split = train.shape[0]" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "data.head()" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "data['hour'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').hour, axis=1)\n", 58 | "data['weekday'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').weekday(), axis=1)\n", 59 | "data['month'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').month, axis=1)\n", 60 | "data['year'] = data.apply(lambda x: datetime.strptime(x['datetime'], '%Y-%m-%d %H:%M:%S').year, axis=1)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "data['hour_sin'] = data.apply(lambda x: sin(x['hour'] / 24.0 * 2 * pi), axis=1)\n", 70 | "data['hour_cos'] = data.apply(lambda x: cos(x['hour'] / 24.0 * 2 * pi), axis=1)\n", 71 | "data['weekday_sin'] = data.apply(lambda x: sin(x['weekday'] / 7.0 * 2 * pi), axis=1)\n", 72 | "data['weekday_cos'] = data.apply(lambda x: cos(x['weekday'] / 7.0 * 2 * pi), axis=1)\n", 73 | "data['month_sin'] = data.apply(lambda x: sin(((x['month'] - 5) % 12) / 12.0 * 2 * pi), axis=1)\n", 74 | "data['month_cos'] = data.apply(lambda x: cos(((x['month'] - 5) % 12) / 12.0 * 2 * pi), axis=1)\n", 75 | "data['season_sin'] = data.apply(lambda x: sin(((x['season'] - 3) % 4) / 4.0 * 2 * pi), axis=1)\n", 76 | "data['season_cos'] = data.apply(lambda x: cos(((x['season'] - 3) % 4) / 4.0 * 2 * pi), axis=1)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "X_train = data[:test_split].drop(['datetime', 'casual', 'registered', 'count'], inplace=False, axis=1)\n", 86 | "X_test = data[test_split:].drop(['datetime', 'casual', 'registered', 'count'], inplace=False, axis=1)\n", 87 | "y_train = data['count'][:test_split]\n", 88 | "y_test = data['count'][test_split:]" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "scaler = preprocessing.StandardScaler()\n", 98 | "X_train = scaler.fit_transform(X_train)\n", 99 | "X_test = scaler.transform(X_test)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "model = Sequential()\n", 109 | "model.add(Dense(200, input_dim=X_train.shape[1]))\n", 110 | "model.add(Activation('relu'))\n", 111 | "model.add(Dropout(0.1))\n", 112 | "model.add(Dense(200))\n", 113 | "model.add(Activation('relu'))\n", 114 | "model.add(Dropout(0.1))\n", 115 | "model.add(Dense(1))\n", 116 | "\n", 117 | "opt = Adam()\n", 118 | "model.compile(loss='mean_squared_logarithmic_error', optimizer=opt)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "n_epochs = 1000\n", 128 | "batch_size = 128" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "callbacks = [EarlyStopping(monitor='val_loss', patience=5)]\n", 138 | "history = model.fit(X_train, y_train, shuffle=True, epochs=n_epochs, batch_size=batch_size, validation_split=0.1, verbose=1)" 139 | ] 140 | } 141 | ], 142 | "metadata": { 143 | "kernelspec": { 144 | "display_name": "Python 3", 145 | "language": "python", 146 | "name": "python3" 147 | }, 148 | "language_info": { 149 | "codemirror_mode": { 150 | "name": "ipython", 151 | "version": 3 152 | }, 153 | "file_extension": ".py", 154 | "mimetype": "text/x-python", 155 | "name": "python", 156 | "nbconvert_exporter": "python", 157 | "pygments_lexer": "ipython3", 158 | "version": "3.6.8" 159 | }, 160 | "widgets": { 161 | "state": {}, 162 | "version": "1.1.2" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 2 167 | } 168 | -------------------------------------------------------------------------------- /框架/keras_demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "Using TensorFlow backend.\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "from keras.models import Sequential\n", 18 | "from keras.layers import Dense" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import numpy as np\n", 28 | "x_input = np.array([[1, 2, 3, 4, 5]])\n", 29 | "y_input = np.array([[10]])" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "model = Sequential()\n", 39 | "model.add(Dense(units=32, input_dim=x_input.shape[1]))\n", 40 | "model.add(Dense(units=1))" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "model.compile(loss='mse',\n", 50 | " optimizer='sgd',\n", 51 | " metrics=['accuracy'])" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "_________________________________________________________________\n", 64 | "Layer (type) Output Shape Param # \n", 65 | "=================================================================\n", 66 | "dense_1 (Dense) (None, 32) 192 \n", 67 | "_________________________________________________________________\n", 68 | "dense_2 (Dense) (None, 1) 33 \n", 69 | "=================================================================\n", 70 | "Total params: 225\n", 71 | "Trainable params: 225\n", 72 | "Non-trainable params: 0\n", 73 | "_________________________________________________________________\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "model.summary()" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 6, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "Epoch 1/10\n", 91 | "1/1 [==============================] - 0s 459ms/step - loss: 48.9001 - acc: 0.0000e+00\n", 92 | "Epoch 2/10\n", 93 | "1/1 [==============================] - 0s 2ms/step - loss: 2144.2153 - acc: 0.0000e+00\n", 94 | "Epoch 3/10\n", 95 | "1/1 [==============================] - 0s 4ms/step - loss: 3431307.2500 - acc: 0.0000e+00\n", 96 | "Epoch 4/10\n", 97 | "1/1 [==============================] - 0s 2ms/step - loss: 20133275527806976.0000 - acc: 0.0000e+00\n", 98 | "Epoch 5/10\n", 99 | "1/1 [==============================] - 0s 2ms/step - loss: inf - acc: 0.0000e+00\n", 100 | "Epoch 6/10\n", 101 | "1/1 [==============================] - 0s 3ms/step - loss: nan - acc: 0.0000e+00\n", 102 | "Epoch 7/10\n", 103 | "1/1 [==============================] - 0s 2ms/step - loss: nan - acc: 0.0000e+00\n", 104 | "Epoch 8/10\n", 105 | "1/1 [==============================] - 0s 4ms/step - loss: nan - acc: 0.0000e+00\n", 106 | "Epoch 9/10\n", 107 | "1/1 [==============================] - 0s 2ms/step - loss: nan - acc: 0.0000e+00\n", 108 | "Epoch 10/10\n", 109 | "1/1 [==============================] - 0s 2ms/step - loss: nan - acc: 0.0000e+00\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "history = model.fit(x_input, y_input, epochs=10, batch_size=32)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 7, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "pred = model.predict(x_input, batch_size=128)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 8, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[[nan]]\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print(pred)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [] 149 | } 150 | ], 151 | "metadata": { 152 | "kernelspec": { 153 | "display_name": "Python 3", 154 | "language": "python", 155 | "name": "python3" 156 | }, 157 | "language_info": { 158 | "codemirror_mode": { 159 | "name": "ipython", 160 | "version": 3 161 | }, 162 | "file_extension": ".py", 163 | "mimetype": "text/x-python", 164 | "name": "python", 165 | "nbconvert_exporter": "python", 166 | "pygments_lexer": "ipython3", 167 | "version": "3.6.8" 168 | } 169 | }, 170 | "nbformat": 4, 171 | "nbformat_minor": 2 172 | } 173 | -------------------------------------------------------------------------------- /游戏智能体和机器人/README.md: -------------------------------------------------------------------------------- 1 | # 游戏智能体与机器人 2 | - 通过端到端学习来驾驶汽车 3 | - 通过深度强化学习来玩游戏 4 | - 用GA优化超参数 -------------------------------------------------------------------------------- /生成对抗网络/.ipynb_checkpoints/了解GAN-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "from keras.models import Sequential, Model\n", 11 | "from keras.layers import Input, Dense, Activation, Flatten, Reshape\n", 12 | "from keras.layers import Conv2D, Conv2DTranspose, UpSampling2D\n", 13 | "from keras.layers import LeakyReLU, Dropout\n", 14 | "from keras.layers import BatchNormalization\n", 15 | "from keras.optimizers import Adam\n", 16 | "from keras import initializers\n", 17 | "\n", 18 | "from keras.datasets import mnist\n", 19 | "\n", 20 | "import matplotlib.pyplot as plt" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", 30 | "\n", 31 | "img_rows, img_cols = X_train.shape[1:]\n", 32 | "\n", 33 | "X_train = (X_train.reshape(-1, img_rows*img_cols, 1).astype(np.float32)-127.5)/127.5" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "def discriminator_model(dropout=0.5):\n", 43 | " model = Sequential()\n", 44 | " model.add(Dense(1024, input_dim=784, kernel_initializer=initializers.RandomNormal(stddev=0.02)))\n", 45 | " model.add(LeakyReLU(0.2))\n", 46 | " model.add(Dropout(dropout))\n", 47 | " model.add(Dense(512))\n", 48 | " model.add(LeakyReLU(0.2))\n", 49 | " model.add(Dropout(dropout))\n", 50 | " model.add(Dense(256))\n", 51 | " model.add(LeakyReLU(0.2))\n", 52 | " model.add(Dropout(dropout))\n", 53 | " model.add(Dense(1, activation='sigmoid'))\n", 54 | " opt = Adam(lr=0.0001)\n", 55 | " model.compile(loss='binary_crossentropy', optimizer=opt)\n", 56 | " return model" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "def generator_model():\n", 66 | " model = Sequential()\n", 67 | " model.add(Dense(256, input_dim=100, kernel_initializer=initializers.RandomNormal(stddev=0.02)))\n", 68 | " model.add(LeakyReLU(0.2))\n", 69 | " model.add(Dense(512))\n", 70 | " model.add(LeakyReLU(0.2))\n", 71 | " model.add(Dense(1024))\n", 72 | " model.add(LeakyReLU(0.2))\n", 73 | " model.add(Dense(784, activation='tanh'))\n", 74 | " opt = Adam(lr=0.00005)\n", 75 | " model.compile(loss='binary_crossentropy', optimizer=opt)\n", 76 | " return model" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "discriminator = discriminator_model()\n", 86 | "generator = generator_model()\n", 87 | "discriminator.trainable = False\n", 88 | "gan_input = Input(shape=(100,))\n", 89 | "x = generator(gan_input)\n", 90 | "gan_output = discriminator(x)\n", 91 | "gan = Model(inputs=gan_input, outputs=gan_output)\n", 92 | "opt = Adam(lr=0.0001)\n", 93 | "gan.compile(loss='binary_crossentropy', optimizer=opt)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "def plot_images(samples=16, step=0):\n", 103 | " plt.figure(figsize=(5,5))\n", 104 | " for i in range(samples):\n", 105 | " noise = np.random.uniform(-1, 1, size=[batch_size, 100])#np.random.normal(0, 1, size=[batch_size, 100])\n", 106 | " images = generator.predict(noise)\n", 107 | " plt.subplot(4, 4, i+1)\n", 108 | " image = images[i, :,]\n", 109 | " image = np.reshape(image, [img_rows, img_cols])\n", 110 | " plt.imshow(image, cmap='gray')\n", 111 | " plt.axis('off')\n", 112 | " plt.show()" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "batch_size = 32\n", 122 | "n_steps = 100000\n", 123 | "plot_every = 1000\n", 124 | "\n", 125 | "noise_input = np.random.uniform(-1, 1, size=[16, 100])\n", 126 | "for step in range(n_steps):\n", 127 | " \n", 128 | " noise = np.random.uniform(-1, 1, size=[batch_size, 100])\n", 129 | " batch = X_train[np.random.randint(0, X_train.shape[0], size=batch_size)].reshape(batch_size, 784)\n", 130 | "\n", 131 | " gen_output = generator.predict(noise)\n", 132 | " X = np.concatenate([batch, gen_output])\n", 133 | "\n", 134 | " y_D = np.zeros(2*batch_size)\n", 135 | " y_D[:batch_size] = 1\n", 136 | "\n", 137 | " discriminator.trainable = True\n", 138 | " loss_D = discriminator.train_on_batch(X, y_D)\n", 139 | "\n", 140 | " noise = np.random.uniform(-1, 1, size=[batch_size, 100])\n", 141 | " y_G = np.ones(batch_size)\n", 142 | " discriminator.trainable = False\n", 143 | " loss_G = gan.train_on_batch(noise, y_G)\n", 144 | " \n", 145 | " if step % plot_every == 0:\n", 146 | " print('Step {}'.format(step))\n", 147 | " plot_images(samples=noise_input.shape[0], step=(step+1))\n", 148 | " " 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.8" 169 | }, 170 | "widgets": { 171 | "state": {}, 172 | "version": "1.1.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /生成对抗网络/README.md: -------------------------------------------------------------------------------- 1 | # 生成对抗网络 2 | - 了解GAN 3 | - 实现DCGAN 4 | - 使用SRGAN -------------------------------------------------------------------------------- /生成对抗网络/了解GAN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "from keras.models import Sequential, Model\n", 11 | "from keras.layers import Input, Dense, Activation, Flatten, Reshape\n", 12 | "from keras.layers import Conv2D, Conv2DTranspose, UpSampling2D\n", 13 | "from keras.layers import LeakyReLU, Dropout\n", 14 | "from keras.layers import BatchNormalization\n", 15 | "from keras.optimizers import Adam\n", 16 | "from keras import initializers\n", 17 | "\n", 18 | "from keras.datasets import mnist\n", 19 | "\n", 20 | "import matplotlib.pyplot as plt" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", 30 | "\n", 31 | "img_rows, img_cols = X_train.shape[1:]\n", 32 | "\n", 33 | "X_train = (X_train.reshape(-1, img_rows*img_cols, 1).astype(np.float32)-127.5)/127.5" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "def discriminator_model(dropout=0.5):\n", 43 | " model = Sequential()\n", 44 | " model.add(Dense(1024, input_dim=784, kernel_initializer=initializers.RandomNormal(stddev=0.02)))\n", 45 | " model.add(LeakyReLU(0.2))\n", 46 | " model.add(Dropout(dropout))\n", 47 | " model.add(Dense(512))\n", 48 | " model.add(LeakyReLU(0.2))\n", 49 | " model.add(Dropout(dropout))\n", 50 | " model.add(Dense(256))\n", 51 | " model.add(LeakyReLU(0.2))\n", 52 | " model.add(Dropout(dropout))\n", 53 | " model.add(Dense(1, activation='sigmoid'))\n", 54 | " opt = Adam(lr=0.0001)\n", 55 | " model.compile(loss='binary_crossentropy', optimizer=opt)\n", 56 | " return model" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "def generator_model():\n", 66 | " model = Sequential()\n", 67 | " model.add(Dense(256, input_dim=100, kernel_initializer=initializers.RandomNormal(stddev=0.02)))\n", 68 | " model.add(LeakyReLU(0.2))\n", 69 | " model.add(Dense(512))\n", 70 | " model.add(LeakyReLU(0.2))\n", 71 | " model.add(Dense(1024))\n", 72 | " model.add(LeakyReLU(0.2))\n", 73 | " model.add(Dense(784, activation='tanh'))\n", 74 | " opt = Adam(lr=0.00005)\n", 75 | " model.compile(loss='binary_crossentropy', optimizer=opt)\n", 76 | " return model" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "discriminator = discriminator_model()\n", 86 | "generator = generator_model()\n", 87 | "discriminator.trainable = False\n", 88 | "gan_input = Input(shape=(100,))\n", 89 | "x = generator(gan_input)\n", 90 | "gan_output = discriminator(x)\n", 91 | "gan = Model(inputs=gan_input, outputs=gan_output)\n", 92 | "opt = Adam(lr=0.0001)\n", 93 | "gan.compile(loss='binary_crossentropy', optimizer=opt)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "def plot_images(samples=16, step=0):\n", 103 | " plt.figure(figsize=(5,5))\n", 104 | " for i in range(samples):\n", 105 | " noise = np.random.uniform(-1, 1, size=[batch_size, 100])#np.random.normal(0, 1, size=[batch_size, 100])\n", 106 | " images = generator.predict(noise)\n", 107 | " plt.subplot(4, 4, i+1)\n", 108 | " image = images[i, :,]\n", 109 | " image = np.reshape(image, [img_rows, img_cols])\n", 110 | " plt.imshow(image, cmap='gray')\n", 111 | " plt.axis('off')\n", 112 | " plt.show()" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "batch_size = 32\n", 122 | "n_steps = 100000\n", 123 | "plot_every = 1000\n", 124 | "\n", 125 | "noise_input = np.random.uniform(-1, 1, size=[16, 100])\n", 126 | "for step in range(n_steps):\n", 127 | " \n", 128 | " noise = np.random.uniform(-1, 1, size=[batch_size, 100])\n", 129 | " batch = X_train[np.random.randint(0, X_train.shape[0], size=batch_size)].reshape(batch_size, 784)\n", 130 | "\n", 131 | " gen_output = generator.predict(noise)\n", 132 | " X = np.concatenate([batch, gen_output])\n", 133 | "\n", 134 | " y_D = np.zeros(2*batch_size)\n", 135 | " y_D[:batch_size] = 1\n", 136 | "\n", 137 | " discriminator.trainable = True\n", 138 | " loss_D = discriminator.train_on_batch(X, y_D)\n", 139 | "\n", 140 | " noise = np.random.uniform(-1, 1, size=[batch_size, 100])\n", 141 | " y_G = np.ones(batch_size)\n", 142 | " discriminator.trainable = False\n", 143 | " loss_G = gan.train_on_batch(noise, y_G)\n", 144 | " \n", 145 | " if step % plot_every == 0:\n", 146 | " print('Step {}'.format(step))\n", 147 | " plot_images(samples=noise_input.shape[0], step=(step+1))\n", 148 | " " 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.8" 169 | }, 170 | "widgets": { 171 | "state": {}, 172 | "version": "1.1.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /网络内部构造/README.md: -------------------------------------------------------------------------------- 1 | # 网络内部构造 2 | - 用TensorBoard可视化训练过程 3 | - 用TensorBoard可视化网络结构 4 | - 分析网络权重 5 | - 冻结层 6 | - 存储网络结构并训练权重 -------------------------------------------------------------------------------- /自然语言处理/.ipynb_checkpoints/情绪分析-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from nltk.tokenize import word_tokenize \n", 10 | "from nltk.stem import WordNetLemmatizer \n", 11 | "import numpy as np \n", 12 | "import random \n", 13 | "import pickle \n", 14 | "from collections import Counter \n", 15 | "\n", 16 | "import tensorflow as tf \n", 17 | "\n", 18 | "\n", 19 | "lemmatizer = WordNetLemmatizer() \n", 20 | "max_lines = 10000000 \n", 21 | "pos = 'Data/sentences/positive.txt'\n", 22 | "neg = 'Data/sentences/negative.txt'\n", 23 | "\n", 24 | "def create_lexicon(pos, neg): \n", 25 | " lexicon = [] \n", 26 | " for fi in [pos, neg]: \n", 27 | " with open(fi, 'r') as f: \n", 28 | " contents = f.readlines() \n", 29 | " for l in contents[:max_lines]: \n", 30 | " all_words = word_tokenize(l.lower()) \n", 31 | " lexicon += list(all_words) \n", 32 | " \n", 33 | " lexicon = [lemmatizer.lemmatize(i) for i in lexicon] \n", 34 | " w_counts = Counter(lexicon) \n", 35 | " \n", 36 | " l2 =[] \n", 37 | " for w in w_counts: \n", 38 | " if 1000 > w_counts[w] > 50: \n", 39 | " l2.append(w) \n", 40 | " return l2 \n", 41 | " \n", 42 | "def sample_handling(sample,lexicon,classification): \n", 43 | " featureset = [] \n", 44 | " with open(sample,'r') as f: \n", 45 | " contents = f.readlines() \n", 46 | " for l in contents[:max_lines]: \n", 47 | " current_words = word_tokenize(l.lower()) \n", 48 | " current_words = [lemmatizer.lemmatize(i) for i in current_words] \n", 49 | " features = np.zeros(len(lexicon)) \n", 50 | " for word in current_words: \n", 51 | " if word.lower() in lexicon: \n", 52 | " index_value = lexicon.index(word.lower()) \n", 53 | " features[index_value] += 1 \n", 54 | " \n", 55 | " features = list(features) \n", 56 | " featureset.append([features,classification]) \n", 57 | " \n", 58 | " return featureset \n", 59 | " \n", 60 | "lexicon = create_lexicon(pos,neg) \n", 61 | "features = [] \n", 62 | "features += sample_handling(pos, lexicon,[1,0]) \n", 63 | "features += sample_handling(neg, lexicon,[0,1]) \n", 64 | "random.shuffle(features) \n", 65 | "features = np.array(features) \n", 66 | "\n", 67 | "testing_size = int(0.1*len(features)) \n", 68 | "\n", 69 | "X_train = list(features[:,0][:-testing_size]) \n", 70 | "y_train = list(features[:,1][:-testing_size]) \n", 71 | "X_test = list(features[:,0][-testing_size:]) \n", 72 | "y_test = list(features[:,1][-testing_size:]) " 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "n_epochs = 10 \n", 82 | "batch_size = 128 \n", 83 | "h1 = 500 \n", 84 | "h2 = 500 \n", 85 | "n_classes = 2 " 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "x_input = tf.placeholder('float') \n", 95 | "y_input = tf.placeholder('float') \n", 96 | " \n", 97 | "hidden_1 = {'weight':tf.Variable(tf.random_normal([len(X_train[0]), h1])), \n", 98 | " 'bias':tf.Variable(tf.random_normal([h1]))} \n", 99 | " \n", 100 | "hidden_2 = {'weight':tf.Variable(tf.random_normal([h1, h2])), \n", 101 | " 'bias':tf.Variable(tf.random_normal([h2]))} \n", 102 | " \n", 103 | "output_layer = {'weight':tf.Variable(tf.random_normal([h2, n_classes])), \n", 104 | " 'bias':tf.Variable(tf.random_normal([n_classes])),} " 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "l1 = tf.add(tf.matmul(x_input, hidden_1['weight']), hidden_1['bias']) \n", 114 | "l1 = tf.nn.relu(l1) \n", 115 | "\n", 116 | "l2 = tf.add(tf.matmul(l1, hidden_2['weight']), hidden_2['bias']) \n", 117 | "l2 = tf.nn.relu(l2) \n", 118 | "\n", 119 | "output = tf.matmul(l2, output_layer['weight']) + output_layer['bias'] " 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y_input)) \n", 129 | "opt = tf.train.AdamOptimizer().minimize(loss) \n", 130 | "\n", 131 | "\n", 132 | "with tf.Session() as sess: \n", 133 | " sess.run(tf.global_variables_initializer()) \n", 134 | "\n", 135 | " for epoch in range(n_epochs): \n", 136 | " epoch_loss = 0 \n", 137 | " i = 0 \n", 138 | " while i < len(X_train): \n", 139 | " start = i \n", 140 | " end = i + batch_size \n", 141 | " batch_x = np.array(X_train[start:end]) \n", 142 | " batch_y = np.array(y_train[start:end]) \n", 143 | "\n", 144 | " _, batch_loss = sess.run([opt, loss], feed_dict={x_input: batch_x, y_input: batch_y}) \n", 145 | " epoch_loss += batch_loss\n", 146 | " i += batch_size \n", 147 | "\n", 148 | " print('Epoch {}: loss {}'.format(epoch, epoch_loss))" 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.8" 169 | }, 170 | "widgets": { 171 | "state": {}, 172 | "version": "1.1.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /自然语言处理/README.md: -------------------------------------------------------------------------------- 1 | # 自然语言处理 2 | - 情绪分析 3 | - 句子翻译 4 | - 文本摘要 -------------------------------------------------------------------------------- /自然语言处理/情绪分析.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from nltk.tokenize import word_tokenize \n", 10 | "from nltk.stem import WordNetLemmatizer \n", 11 | "import numpy as np \n", 12 | "import random \n", 13 | "import pickle \n", 14 | "from collections import Counter \n", 15 | "\n", 16 | "import tensorflow as tf \n", 17 | "\n", 18 | "\n", 19 | "lemmatizer = WordNetLemmatizer() \n", 20 | "max_lines = 10000000 \n", 21 | "pos = 'Data/sentences/positive.txt'\n", 22 | "neg = 'Data/sentences/negative.txt'\n", 23 | "\n", 24 | "def create_lexicon(pos, neg): \n", 25 | " lexicon = [] \n", 26 | " for fi in [pos, neg]: \n", 27 | " with open(fi, 'r') as f: \n", 28 | " contents = f.readlines() \n", 29 | " for l in contents[:max_lines]: \n", 30 | " all_words = word_tokenize(l.lower()) \n", 31 | " lexicon += list(all_words) \n", 32 | " \n", 33 | " lexicon = [lemmatizer.lemmatize(i) for i in lexicon] \n", 34 | " w_counts = Counter(lexicon) \n", 35 | " \n", 36 | " l2 =[] \n", 37 | " for w in w_counts: \n", 38 | " if 1000 > w_counts[w] > 50: \n", 39 | " l2.append(w) \n", 40 | " return l2 \n", 41 | " \n", 42 | "def sample_handling(sample,lexicon,classification): \n", 43 | " featureset = [] \n", 44 | " with open(sample,'r') as f: \n", 45 | " contents = f.readlines() \n", 46 | " for l in contents[:max_lines]: \n", 47 | " current_words = word_tokenize(l.lower()) \n", 48 | " current_words = [lemmatizer.lemmatize(i) for i in current_words] \n", 49 | " features = np.zeros(len(lexicon)) \n", 50 | " for word in current_words: \n", 51 | " if word.lower() in lexicon: \n", 52 | " index_value = lexicon.index(word.lower()) \n", 53 | " features[index_value] += 1 \n", 54 | " \n", 55 | " features = list(features) \n", 56 | " featureset.append([features,classification]) \n", 57 | " \n", 58 | " return featureset \n", 59 | " \n", 60 | "lexicon = create_lexicon(pos,neg) \n", 61 | "features = [] \n", 62 | "features += sample_handling(pos, lexicon,[1,0]) \n", 63 | "features += sample_handling(neg, lexicon,[0,1]) \n", 64 | "random.shuffle(features) \n", 65 | "features = np.array(features) \n", 66 | "\n", 67 | "testing_size = int(0.1*len(features)) \n", 68 | "\n", 69 | "X_train = list(features[:,0][:-testing_size]) \n", 70 | "y_train = list(features[:,1][:-testing_size]) \n", 71 | "X_test = list(features[:,0][-testing_size:]) \n", 72 | "y_test = list(features[:,1][-testing_size:]) " 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "n_epochs = 10 \n", 82 | "batch_size = 128 \n", 83 | "h1 = 500 \n", 84 | "h2 = 500 \n", 85 | "n_classes = 2 " 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "x_input = tf.placeholder('float') \n", 95 | "y_input = tf.placeholder('float') \n", 96 | " \n", 97 | "hidden_1 = {'weight':tf.Variable(tf.random_normal([len(X_train[0]), h1])), \n", 98 | " 'bias':tf.Variable(tf.random_normal([h1]))} \n", 99 | " \n", 100 | "hidden_2 = {'weight':tf.Variable(tf.random_normal([h1, h2])), \n", 101 | " 'bias':tf.Variable(tf.random_normal([h2]))} \n", 102 | " \n", 103 | "output_layer = {'weight':tf.Variable(tf.random_normal([h2, n_classes])), \n", 104 | " 'bias':tf.Variable(tf.random_normal([n_classes])),} " 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "l1 = tf.add(tf.matmul(x_input, hidden_1['weight']), hidden_1['bias']) \n", 114 | "l1 = tf.nn.relu(l1) \n", 115 | "\n", 116 | "l2 = tf.add(tf.matmul(l1, hidden_2['weight']), hidden_2['bias']) \n", 117 | "l2 = tf.nn.relu(l2) \n", 118 | "\n", 119 | "output = tf.matmul(l2, output_layer['weight']) + output_layer['bias'] " 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y_input)) \n", 129 | "opt = tf.train.AdamOptimizer().minimize(loss) \n", 130 | "\n", 131 | "\n", 132 | "with tf.Session() as sess: \n", 133 | " sess.run(tf.global_variables_initializer()) \n", 134 | "\n", 135 | " for epoch in range(n_epochs): \n", 136 | " epoch_loss = 0 \n", 137 | " i = 0 \n", 138 | " while i < len(X_train): \n", 139 | " start = i \n", 140 | " end = i + batch_size \n", 141 | " batch_x = np.array(X_train[start:end]) \n", 142 | " batch_y = np.array(y_train[start:end]) \n", 143 | "\n", 144 | " _, batch_loss = sess.run([opt, loss], feed_dict={x_input: batch_x, y_input: batch_y}) \n", 145 | " epoch_loss += batch_loss\n", 146 | " i += batch_size \n", 147 | "\n", 148 | " print('Epoch {}: loss {}'.format(epoch, epoch_loss))" 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.8" 169 | }, 170 | "widgets": { 171 | "state": {}, 172 | "version": "1.1.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /计算机视觉/.ipynb_checkpoints/增广图像-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import cv2\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "import glob" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "DATA_DIR = 'Data/augmentation/'\n", 22 | "images = glob.glob(DATA_DIR + '*')\n", 23 | "\n", 24 | "plt.figure(figsize=(10, 10))\n", 25 | "i = 1\n", 26 | "for image in images:\n", 27 | " img = cv2.imread(image)\n", 28 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", 29 | " plt.subplot(3, 3, i)\n", 30 | " plt.imshow(img)\n", 31 | " i += 1\n", 32 | " \n", 33 | "plt.show()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "def plot_images(image, function, *args):\n", 43 | " plt.figure(figsize=(10, 10))\n", 44 | " n_examples = 3\n", 45 | " for i in range(n_examples):\n", 46 | " img = cv2.imread(image)\n", 47 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", 48 | " img = function(img, *args)\n", 49 | " plt.subplot(3, 3, i+1)\n", 50 | " plt.imshow(img)\n", 51 | " plt.show()" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def rotate_image(image, rotate=20):\n", 61 | " width, height, _ = image.shape\n", 62 | " random_rotation = np.random.uniform(low=-rotate, high=rotate)\n", 63 | " M = cv2.getRotationMatrix2D((width/2, height/2), random_rotation, 1)\n", 64 | " return(cv2.warpAffine(image, M, (width, height)))\n", 65 | "\n", 66 | "plot_images(images[2], rotate_image, 40)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | " def adjust_brightness(image, brightness=60):\n", 76 | " rand_brightness = np.random.uniform(low=-brightness, high=brightness)\n", 77 | " return(cv2.add(image, rand_brightness))\n", 78 | "\n", 79 | "plot_images(images[0], adjust_brightness, 85)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "def random_shifts(image, shift_max_x=100, shift_max_y=100):\n", 89 | " width, height, _ = image.shape\n", 90 | " shift_x = np.random.randint(shift_max_x)\n", 91 | " shift_y = np.random.randint(shift_max_y)\n", 92 | " M = np.float32([[1, 0, shift_x],[0, 1, shift_y]])\n", 93 | " return (cv2.warpAffine(image, M, (height, width)))\n", 94 | "\n", 95 | "plot_images(images[1], random_shifts, 100, 20)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "def scale_image(image, scale_range=[0.6, 1.4]):\n", 105 | " width, height, _ = image.shape \n", 106 | " \n", 107 | " scale_x = np.random.uniform(low=scale_range[0], high=scale_range[1])\n", 108 | " scale_y = np.random.uniform(low=scale_range[0], high=scale_range[1])\n", 109 | " scale_matrix = np.array([[scale_x, 0., (1. - scale_x) * width / 2.], \n", 110 | " [0., scale_y, (1. - scale_y) * height / 2.]], \n", 111 | " dtype=np.float32)\n", 112 | " return(cv2.warpAffine(image, scale_matrix, (width, height), flags=cv2.INTER_LINEAR, \n", 113 | " borderMode=cv2.BORDER_REFLECT_101))\n", 114 | "\n", 115 | "plot_images(images[2], scale_image, [0.7, 1.3])" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "def random_flip(image, p_flip=0.5):\n", 125 | " rand = np.random.random()\n", 126 | " if rand < p_flip:\n", 127 | " image = cv2.flip(image, 1)\n", 128 | " return image\n", 129 | "\n", 130 | "plot_images(images[2], random_flip)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "plt.figure(figsize=(15, 15))\n", 140 | "image = images[1]\n", 141 | "for i in range(32):\n", 142 | " img = cv2.imread(image)\n", 143 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", 144 | " img = transform_image(img)\n", 145 | " plt.subplot(8, 8, i+1)\n", 146 | " plt.axis('off')\n", 147 | " plt.imshow(img, interpolation=\"nearest\")\n", 148 | "plt.show()" 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.8" 169 | }, 170 | "widgets": { 171 | "state": {}, 172 | "version": "1.1.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /计算机视觉/README.md: -------------------------------------------------------------------------------- 1 | # 计算机视觉 2 | - 增广图像 3 | - 图像中的目标分类 4 | - 目标在图像中的本地化 5 | - 实时检测框架 6 | - 用U-net将图像分类 7 | - 语义分割与场景理解 8 | - 寻找人脸面部关键点 9 | - 人脸识别 10 | - 将风格转化为图像 -------------------------------------------------------------------------------- /计算机视觉/增广图像.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import cv2\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "import glob" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "DATA_DIR = 'Data/augmentation/'\n", 22 | "images = glob.glob(DATA_DIR + '*')\n", 23 | "\n", 24 | "plt.figure(figsize=(10, 10))\n", 25 | "i = 1\n", 26 | "for image in images:\n", 27 | " img = cv2.imread(image)\n", 28 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", 29 | " plt.subplot(3, 3, i)\n", 30 | " plt.imshow(img)\n", 31 | " i += 1\n", 32 | " \n", 33 | "plt.show()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "def plot_images(image, function, *args):\n", 43 | " plt.figure(figsize=(10, 10))\n", 44 | " n_examples = 3\n", 45 | " for i in range(n_examples):\n", 46 | " img = cv2.imread(image)\n", 47 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", 48 | " img = function(img, *args)\n", 49 | " plt.subplot(3, 3, i+1)\n", 50 | " plt.imshow(img)\n", 51 | " plt.show()" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def rotate_image(image, rotate=20):\n", 61 | " width, height, _ = image.shape\n", 62 | " random_rotation = np.random.uniform(low=-rotate, high=rotate)\n", 63 | " M = cv2.getRotationMatrix2D((width/2, height/2), random_rotation, 1)\n", 64 | " return(cv2.warpAffine(image, M, (width, height)))\n", 65 | "\n", 66 | "plot_images(images[2], rotate_image, 40)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | " def adjust_brightness(image, brightness=60):\n", 76 | " rand_brightness = np.random.uniform(low=-brightness, high=brightness)\n", 77 | " return(cv2.add(image, rand_brightness))\n", 78 | "\n", 79 | "plot_images(images[0], adjust_brightness, 85)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "def random_shifts(image, shift_max_x=100, shift_max_y=100):\n", 89 | " width, height, _ = image.shape\n", 90 | " shift_x = np.random.randint(shift_max_x)\n", 91 | " shift_y = np.random.randint(shift_max_y)\n", 92 | " M = np.float32([[1, 0, shift_x],[0, 1, shift_y]])\n", 93 | " return (cv2.warpAffine(image, M, (height, width)))\n", 94 | "\n", 95 | "plot_images(images[1], random_shifts, 100, 20)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "def scale_image(image, scale_range=[0.6, 1.4]):\n", 105 | " width, height, _ = image.shape \n", 106 | " \n", 107 | " scale_x = np.random.uniform(low=scale_range[0], high=scale_range[1])\n", 108 | " scale_y = np.random.uniform(low=scale_range[0], high=scale_range[1])\n", 109 | " scale_matrix = np.array([[scale_x, 0., (1. - scale_x) * width / 2.], \n", 110 | " [0., scale_y, (1. - scale_y) * height / 2.]], \n", 111 | " dtype=np.float32)\n", 112 | " return(cv2.warpAffine(image, scale_matrix, (width, height), flags=cv2.INTER_LINEAR, \n", 113 | " borderMode=cv2.BORDER_REFLECT_101))\n", 114 | "\n", 115 | "plot_images(images[2], scale_image, [0.7, 1.3])" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "def random_flip(image, p_flip=0.5):\n", 125 | " rand = np.random.random()\n", 126 | " if rand < p_flip:\n", 127 | " image = cv2.flip(image, 1)\n", 128 | " return image\n", 129 | "\n", 130 | "plot_images(images[2], random_flip)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "plt.figure(figsize=(15, 15))\n", 140 | "image = images[1]\n", 141 | "for i in range(32):\n", 142 | " img = cv2.imread(image)\n", 143 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", 144 | " img = transform_image(img)\n", 145 | " plt.subplot(8, 8, i+1)\n", 146 | " plt.axis('off')\n", 147 | " plt.imshow(img, interpolation=\"nearest\")\n", 148 | "plt.show()" 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.6.8" 169 | }, 170 | "widgets": { 171 | "state": {}, 172 | "version": "1.1.2" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /语音识别和视频分析/.ipynb_checkpoints/从零开始实现语音识别流程-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "Using Theano backend.\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import glob\n", 18 | "import numpy as np\n", 19 | "import random\n", 20 | "import librosa\n", 21 | "from sklearn.model_selection import train_test_split\n", 22 | "\n", 23 | "import keras\n", 24 | "from keras.layers import LSTM, Dense, Dropout, Flatten\n", 25 | "from keras.models import Sequential\n", 26 | "from keras.optimizers import Adam\n", 27 | "from keras.callbacks import EarlyStopping, ModelCheckpoint" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": { 34 | "collapsed": true 35 | }, 36 | "outputs": [], 37 | "source": [ 38 | "# 数据下载 http://pannous.net/spoken_numbers.tar" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "SEED = 2017\n", 50 | "DATA_DIR = 'Data/spoken_numbers_pcm/' " 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "# Training examples: 0\n", 63 | "# Validation examples: 0\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "files = glob.glob(DATA_DIR + \"*.wav\")\n", 69 | "X_train, X_val = train_test_split(files, test_size=0.2, random_state=SEED)\n", 70 | "\n", 71 | "print('# Training examples: {}'.format(len(X_train)))\n", 72 | "print('# Validation examples: {}'.format(len(X_val)))" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": { 79 | "collapsed": true 80 | }, 81 | "outputs": [], 82 | "source": [ 83 | "n_features = 20\n", 84 | "max_length = 80\n", 85 | "n_classes = 10" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "def batch_generator(data, batch_size=16):\n", 97 | " while 1:\n", 98 | " random.shuffle(data)\n", 99 | " X, y = [], []\n", 100 | " for i in range(batch_size):\n", 101 | " wav = data[i]\n", 102 | " wave, sr = librosa.load(wav, mono=True)\n", 103 | " label = one_hot_encode(int(wav.split('/')[-1][0]), n_classes)\n", 104 | " y.append(label)\n", 105 | " mfcc = librosa.feature.mfcc(wave, sr)\n", 106 | " mfcc = np.pad(mfcc, ((0,0), (0, max_length-len(mfcc[0]))), mode='constant', constant_values=0) \n", 107 | " X.append(np.array(mfcc))\n", 108 | " yield np.array(X), np.array(y)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "def one_hot_encode(labels_dense, n_classes=10):\n", 120 | " return np.eye(n_classes)[labels_dense]" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "collapsed": true 128 | }, 129 | "outputs": [], 130 | "source": [ 131 | "X_example, y_example = next(batch_generator(X_train, batch_size=1))\n", 132 | "print('Shape of training example: {}'.format(X_example.shape))\n", 133 | "print('Shape of training example label: {}'.format(y_example.shape))" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "collapsed": true 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "learning_rate = 0.001\n", 145 | "batch_size = 64\n", 146 | "n_epochs = 50\n", 147 | "dropout = 0.5\n", 148 | "\n", 149 | "input_shape = X_example.shape[1:]\n", 150 | "steps_per_epoch = 50" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": { 157 | "collapsed": true 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "model = Sequential() model.add(LSTM(256, return_sequences=True, input_shape=input_shape, dropout=dropout)) \n", 162 | "model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(dropout)) \n", 163 | "model.add(Dense(n_classes, activation='softmax'))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "opt = Adam(lr=learning_rate) model.compile(loss='categorical_crossentropy', optimizer=opt, \n", 175 | "metrics=['accuracy']) model.summary()" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": { 182 | "collapsed": true 183 | }, 184 | "outputs": [], 185 | "source": [ 186 | "history = model.fit_generator(\n", 187 | " generator=batch_generator(X_train, batch_size),\n", 188 | " steps_per_epoch=steps_per_epoch,\n", 189 | " epochs=n_epochs,\n", 190 | " verbose=1,\n", 191 | " validation_data=batch_generator(X_val, 32),\n", 192 | " validation_steps=5\n", 193 | " )" 194 | ] 195 | } 196 | ], 197 | "metadata": { 198 | "kernelspec": { 199 | "display_name": "Python 3", 200 | "language": "python", 201 | "name": "python3" 202 | }, 203 | "language_info": { 204 | "codemirror_mode": { 205 | "name": "ipython", 206 | "version": 3 207 | }, 208 | "file_extension": ".py", 209 | "mimetype": "text/x-python", 210 | "name": "python", 211 | "nbconvert_exporter": "python", 212 | "pygments_lexer": "ipython3", 213 | "version": "3.6.8" 214 | } 215 | }, 216 | "nbformat": 4, 217 | "nbformat_minor": 2 218 | } 219 | -------------------------------------------------------------------------------- /语音识别和视频分析/README.md: -------------------------------------------------------------------------------- 1 | # 语音识别和视频分析 2 | - 从零开始实现语音识别流程 3 | - 使用语音识别技术辨别讲话人 4 | - 使用深度学习理解视频 -------------------------------------------------------------------------------- /语音识别和视频分析/从零开始实现语音识别流程.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "Using Theano backend.\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import glob\n", 18 | "import numpy as np\n", 19 | "import random\n", 20 | "import librosa\n", 21 | "from sklearn.model_selection import train_test_split\n", 22 | "\n", 23 | "import keras\n", 24 | "from keras.layers import LSTM, Dense, Dropout, Flatten\n", 25 | "from keras.models import Sequential\n", 26 | "from keras.optimizers import Adam\n", 27 | "from keras.callbacks import EarlyStopping, ModelCheckpoint" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": { 34 | "collapsed": true 35 | }, 36 | "outputs": [], 37 | "source": [ 38 | "# 数据下载 http://pannous.net/spoken_numbers.tar" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": { 45 | "collapsed": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "SEED = 2017\n", 50 | "DATA_DIR = 'Data/spoken_numbers_pcm/' " 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "# Training examples: 0\n", 63 | "# Validation examples: 0\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "files = glob.glob(DATA_DIR + \"*.wav\")\n", 69 | "X_train, X_val = train_test_split(files, test_size=0.2, random_state=SEED)\n", 70 | "\n", 71 | "print('# Training examples: {}'.format(len(X_train)))\n", 72 | "print('# Validation examples: {}'.format(len(X_val)))" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": { 79 | "collapsed": true 80 | }, 81 | "outputs": [], 82 | "source": [ 83 | "n_features = 20\n", 84 | "max_length = 80\n", 85 | "n_classes = 10" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": { 92 | "collapsed": true 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "def batch_generator(data, batch_size=16):\n", 97 | " while 1:\n", 98 | " random.shuffle(data)\n", 99 | " X, y = [], []\n", 100 | " for i in range(batch_size):\n", 101 | " wav = data[i]\n", 102 | " wave, sr = librosa.load(wav, mono=True)\n", 103 | " label = one_hot_encode(int(wav.split('/')[-1][0]), n_classes)\n", 104 | " y.append(label)\n", 105 | " mfcc = librosa.feature.mfcc(wave, sr)\n", 106 | " mfcc = np.pad(mfcc, ((0,0), (0, max_length-len(mfcc[0]))), mode='constant', constant_values=0) \n", 107 | " X.append(np.array(mfcc))\n", 108 | " yield np.array(X), np.array(y)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "def one_hot_encode(labels_dense, n_classes=10):\n", 120 | " return np.eye(n_classes)[labels_dense]" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "collapsed": true 128 | }, 129 | "outputs": [], 130 | "source": [ 131 | "X_example, y_example = next(batch_generator(X_train, batch_size=1))\n", 132 | "print('Shape of training example: {}'.format(X_example.shape))\n", 133 | "print('Shape of training example label: {}'.format(y_example.shape))" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "collapsed": true 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "learning_rate = 0.001\n", 145 | "batch_size = 64\n", 146 | "n_epochs = 50\n", 147 | "dropout = 0.5\n", 148 | "\n", 149 | "input_shape = X_example.shape[1:]\n", 150 | "steps_per_epoch = 50" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": { 157 | "collapsed": true 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "model = Sequential() model.add(LSTM(256, return_sequences=True, input_shape=input_shape, dropout=dropout)) \n", 162 | "model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(dropout)) \n", 163 | "model.add(Dense(n_classes, activation='softmax'))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "opt = Adam(lr=learning_rate) model.compile(loss='categorical_crossentropy', optimizer=opt, \n", 175 | "metrics=['accuracy']) model.summary()" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": { 182 | "collapsed": true 183 | }, 184 | "outputs": [], 185 | "source": [ 186 | "history = model.fit_generator(\n", 187 | " generator=batch_generator(X_train, batch_size),\n", 188 | " steps_per_epoch=steps_per_epoch,\n", 189 | " epochs=n_epochs,\n", 190 | " verbose=1,\n", 191 | " validation_data=batch_generator(X_val, 32),\n", 192 | " validation_steps=5\n", 193 | " )" 194 | ] 195 | } 196 | ], 197 | "metadata": { 198 | "kernelspec": { 199 | "display_name": "Python 3", 200 | "language": "python", 201 | "name": "python3" 202 | }, 203 | "language_info": { 204 | "codemirror_mode": { 205 | "name": "ipython", 206 | "version": 3 207 | }, 208 | "file_extension": ".py", 209 | "mimetype": "text/x-python", 210 | "name": "python", 211 | "nbconvert_exporter": "python", 212 | "pygments_lexer": "ipython3", 213 | "version": "3.6.8" 214 | } 215 | }, 216 | "nbformat": 4, 217 | "nbformat_minor": 2 218 | } 219 | -------------------------------------------------------------------------------- /语音识别和视频分析/使用语音识别技术辨别讲话人.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import glob\n", 12 | "import numpy as np\n", 13 | "import random\n", 14 | "import librosa\n", 15 | "from sklearn.model_selection import train_test_split\n", 16 | "from sklearn.preprocessing import LabelBinarizer\n", 17 | "\n", 18 | "import keras\n", 19 | "from keras.layers import LSTM, Dense, Dropout, Flatten\n", 20 | "from keras.models import Sequential\n", 21 | "from keras.optimizers import Adam\n", 22 | "from keras.callbacks import EarlyStopping, ModelCheckpoint" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": { 29 | "collapsed": true 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "# 数据下载 http://pannous.net/spoken_numbers.tar" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": { 40 | "collapsed": true 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "SEED = 2017\n", 45 | "DATA_DIR = '../Data/spoken_numbers_pcm/' " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "files = glob.glob(DATA_DIR + \"*.wav\")\n", 57 | "X_train, X_val = train_test_split(files, test_size=0.2, random_state=SEED)\n", 58 | "\n", 59 | "print('# Training examples: {}'.format(len(X_train)))\n", 60 | "print('# Validation examples: {}'.format(len(X_val)))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": { 67 | "collapsed": true 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "labels = []\n", 72 | "for i in range(len(X_train)):\n", 73 | " label = X_train[i].split('/')[-1].split('_')[1]\n", 74 | " if label not in labels:\n", 75 | " labels.append(label)\n", 76 | "print(labels)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": { 83 | "collapsed": true 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "label_binarizer = LabelBinarizer()\n", 88 | "label_binarizer.fit(list(set(labels)))\n", 89 | "\n", 90 | "def one_hot_encode(x): return label_binarizer.transform(x)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "n_features = 20\n", 102 | "max_length = 80\n", 103 | "n_classes = len(labels)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": { 110 | "collapsed": true 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "def batch_generator(data, batch_size=16):\n", 115 | " while 1:\n", 116 | " random.shuffle(data)\n", 117 | " X, y = [], []\n", 118 | " for i in range(batch_size):\n", 119 | " wav = data[i]\n", 120 | " wave, sr = librosa.load(wav, mono=True)\n", 121 | " label = wav.split('/')[-1].split('_')[1]\n", 122 | " y.append(label)\n", 123 | " mfcc = librosa.feature.mfcc(wave, sr)\n", 124 | " mfcc = np.pad(mfcc, ((0,0), (0, max_length-len(mfcc[0]))), mode='constant', constant_values=0) \n", 125 | " X.append(np.array(mfcc))\n", 126 | " yield np.array(X), np.array(one_hot_encode(y))" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": { 133 | "collapsed": true 134 | }, 135 | "outputs": [], 136 | "source": [ 137 | "learning_rate = 0.001\n", 138 | "batch_size = 64\n", 139 | "n_epochs = 50\n", 140 | "dropout = 0.5\n", 141 | "\n", 142 | "input_shape = (n_features, max_length)\n", 143 | "steps_per_epoch = 50" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": { 150 | "collapsed": true 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "model = Sequential()\n", 155 | "model.add(LSTM(256, return_sequences=True, input_shape=input_shape,\n", 156 | "dropout=dropout))\n", 157 | "model.add(Flatten())\n", 158 | "model.add(Dense(128, activation='relu'))\n", 159 | "model.add(Dropout(dropout))\n", 160 | "model.add(Dense(n_classes, activation='softmax'))" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": { 167 | "collapsed": true 168 | }, 169 | "outputs": [], 170 | "source": [ 171 | "opt = Adam(lr=learning_rate)\n", 172 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])\n", 173 | "model.summary()" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": { 180 | "collapsed": true 181 | }, 182 | "outputs": [], 183 | "source": [ 184 | "callbacks = [ModelCheckpoint('checkpoints/voice_recognition_best_model_{epoch:02d}.hdf5', save_best_only=True),\n", 185 | " EarlyStopping(monitor='val_acc', patience=2)]" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": { 192 | "collapsed": true 193 | }, 194 | "outputs": [], 195 | "source": [ 196 | "history = model.fit_generator(\n", 197 | " generator=batch_generator(X_train, batch_size),\n", 198 | " steps_per_epoch=steps_per_epoch,\n", 199 | " epochs=n_epochs,\n", 200 | " verbose=1,\n", 201 | " validation_data=batch_generator(X_val, 32),\n", 202 | " validation_steps=5,\n", 203 | " callbacks=callbacks\n", 204 | " )" 205 | ] 206 | } 207 | ], 208 | "metadata": { 209 | "kernelspec": { 210 | "display_name": "Python 3", 211 | "language": "python", 212 | "name": "python3" 213 | }, 214 | "language_info": { 215 | "codemirror_mode": { 216 | "name": "ipython", 217 | "version": 3 218 | }, 219 | "file_extension": ".py", 220 | "mimetype": "text/x-python", 221 | "name": "python", 222 | "nbconvert_exporter": "python", 223 | "pygments_lexer": "ipython3", 224 | "version": "3.6.8" 225 | } 226 | }, 227 | "nbformat": 4, 228 | "nbformat_minor": 2 229 | } 230 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/.ipynb_checkpoints/TensorBoard可视化训练过程-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.datasets import cifar10\n", 10 | "from keras.preprocessing.image import ImageDataGenerator\n", 11 | "from keras.models import Sequential\n", 12 | "from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n", 13 | "from keras.optimizers import Adam\n", 14 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint\n", 15 | "from keras.utils import to_categorical" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "(X_train, y_train), (X_val, y_val) = cifar10.load_data()\n", 25 | "\n", 26 | "X_train = X_train.astype('float32')\n", 27 | "X_train /=255.\n", 28 | "X_val = X_val.astype('float32')\n", 29 | "X_val /=255.\n", 30 | "\n", 31 | "n_classes = 10\n", 32 | "y_train = to_categorical(y_train, n_classes)\n", 33 | "y_val = to_categorical(y_val, n_classes)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "model = Sequential()\n", 43 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 44 | "model.add(Activation('relu'))\n", 45 | "model.add(Conv2D(32, (3, 3)))\n", 46 | "model.add(Activation('relu'))\n", 47 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 48 | "model.add(Dropout(0.25))\n", 49 | "\n", 50 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 51 | "model.add(Activation('relu'))\n", 52 | "model.add(Conv2D(64, (3, 3)))\n", 53 | "model.add(Activation('relu'))\n", 54 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 55 | "model.add(Dropout(0.25))\n", 56 | "\n", 57 | "model.add(Flatten())\n", 58 | "model.add(Dense(512))\n", 59 | "model.add(Activation('relu'))\n", 60 | "model.add(Dropout(0.5))\n", 61 | "model.add(Dense(n_classes))\n", 62 | "model.add(Activation('softmax'))" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "opt = Adam()\n", 72 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "data_gen = ImageDataGenerator(\n", 82 | " rotation_range=15,\n", 83 | " width_shift_range=0.15,\n", 84 | " height_shift_range=0.15,\n", 85 | " horizontal_flip=True,\n", 86 | " vertical_flip=False)\n", 87 | "\n", 88 | "data_gen.fit(X_train)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2), ModelCheckpoint('checkpoints/weights.{epoch:02d}.hdf5', save_best_only=True), TensorBoard('~/notebooks/logs', write_graph=True, write_grads=True, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)]" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "batch_size = 128\n", 107 | "n_epochs = 1000\n", 108 | "model.fit_generator(data_gen.flow(X_train, y_train, batch_size=batch_size),\n", 109 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 110 | " epochs=n_epochs,\n", 111 | " validation_data=(X_val, y_val),\n", 112 | " callbacks=callbacks\n", 113 | " )" 114 | ] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.6.8" 134 | }, 135 | "widgets": { 136 | "state": {}, 137 | "version": "1.1.2" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 2 142 | } 143 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/.ipynb_checkpoints/使用批量生成器-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.datasets import cifar10\n", 10 | "from keras.preprocessing.image import ImageDataGenerator\n", 11 | "from keras.models import Sequential\n", 12 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 13 | "from keras.layers import Conv2D, MaxPooling2D\n", 14 | "from keras.optimizers import Adam\n", 15 | "from keras.callbacks import EarlyStopping, ModelCheckpoint" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 25 | "\n", 26 | "X_train = X_train.astype('float32')/255.\n", 27 | "X_test = X_test.astype('float32')/255.\n", 28 | "n_classes = 10\n", 29 | "y_train = keras.utils.to_categorical(y_train, n_classes)\n", 30 | "y_test = keras.utils.to_categorical(y_test, n_classes)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "model = Sequential()\n", 40 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 41 | "model.add(Activation('relu'))\n", 42 | "model.add(Conv2D(32, (3, 3)))\n", 43 | "model.add(Activation('relu'))\n", 44 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 45 | "model.add(Dropout(0.25))\n", 46 | "\n", 47 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 48 | "model.add(Activation('relu'))\n", 49 | "model.add(Conv2D(64, (3, 3)))\n", 50 | "model.add(Activation('relu'))\n", 51 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 52 | "model.add(Dropout(0.25))\n", 53 | "\n", 54 | "model.add(Flatten())\n", 55 | "model.add(Dense(512))\n", 56 | "model.add(Activation('relu'))\n", 57 | "model.add(Dropout(0.5))\n", 58 | "model.add(Dense(n_classes))\n", 59 | "model.add(Activation('softmax'))" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "opt = Adam(lr=0.0001)\n", 69 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "datagen = ImageDataGenerator(\n", 79 | " rotation_range=15,\n", 80 | " width_shift_range=0.15,\n", 81 | " height_shift_range=0.15,\n", 82 | " horizontal_flip=True,\n", 83 | " vertical_flip=False)\n", 84 | "\n", 85 | "datagen.fit(x_train)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2), ModelCheckpoint('checkpoints/weights.{epoch:02d}-'+str(batch_size)+'.hdf5', save_best_only=True)]" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "batch_size = 32\n", 104 | "n_epochs = 1000\n", 105 | "history_32 = model.fit_generator(datagen.flow(X_train, y_train,\n", 106 | " batch_size=batch_size),\n", 107 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 108 | " epochs=epochs,\n", 109 | " validation_data=(X_val, y_val),\n", 110 | " callbacks=callbacks\n", 111 | " )" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "batch_size = 256\n", 130 | "history_32 = model.fit_generator(datagen.flow(X_train, y_train,\n", 131 | " batch_size=batch_size),\n", 132 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 133 | " epochs=epochs,\n", 134 | " validation_data=(X_val, y_val),\n", 135 | " callbacks=callbacks\n", 136 | " )" 137 | ] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.6.8" 157 | }, 158 | "widgets": { 159 | "state": {}, 160 | "version": "1.1.2" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 2 165 | } 166 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/.ipynb_checkpoints/学习率及其调度-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import math\n", 10 | "\n", 11 | "from keras.models import Sequential\n", 12 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 13 | "from keras.layers import Conv2D, MaxPooling2D\n", 14 | "from keras.optimizers import SGD\n", 15 | "from sklearn.model_selection import train_test_split\n", 16 | "from keras.utils import to_categorical\n", 17 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint, LearningRateScheduler, Callback\n", 18 | "from keras import backend as K\n", 19 | "\n", 20 | "from keras.datasets import cifar10" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 30 | "validation_split = 0.1\n", 31 | "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=validation_split, random_state=SEED)\n", 32 | "\n", 33 | "X_train = X_train.astype('float32')\n", 34 | "X_train /=255.\n", 35 | "X_val = X_val.astype('float32')\n", 36 | "X_val /=255.\n", 37 | "X_test = X_test.astype('float32')\n", 38 | "X_test /=255.\n", 39 | "\n", 40 | "n_classes = 10\n", 41 | "y_train = to_categorical(y_train, n_classes)\n", 42 | "y_val = to_categorical(y_val, n_classes)\n", 43 | "y_test = to_categorical(y_test, n_classes)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "learning_rate_schedule = {0: '0.1', 10: '0.01', 25: '0.0025'}\n", 53 | "\n", 54 | "class get_learning_rate(Callback):\n", 55 | " def on_epoch_end(self, epoch, logs={}):\n", 56 | " optimizer = self.model.optimizer\n", 57 | " if epoch in learning_rate_schedule:\n", 58 | " K.set_value(optimizer.lr, learning_rate_schedule[epoch])\n", 59 | " lr = K.eval(optimizer.lr)\n", 60 | " print('\\nlr: {:.4f}'.format(lr))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "callbacks =[EarlyStopping(monitor='val_acc', patience=5, verbose=2),\n", 70 | " ModelCheckpoint('checkpoints/{epoch:02d}.h5', \n", 71 | " save_best_only=True),\n", 72 | " TensorBoard('~/notebooks/logs-lrscheduler'),\n", 73 | " get_learning_rate()\n", 74 | " ]" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "model = Sequential()\n", 84 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 85 | "model.add(Activation('relu'))\n", 86 | "model.add(Conv2D(32, (3, 3)))\n", 87 | "model.add(Activation('relu'))\n", 88 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 89 | "model.add(Dropout(0.25))\n", 90 | "\n", 91 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 92 | "model.add(Activation('relu'))\n", 93 | "model.add(Conv2D(64, (3, 3)))\n", 94 | "model.add(Activation('relu'))\n", 95 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 96 | "model.add(Dropout(0.25))\n", 97 | "\n", 98 | "model.add(Flatten())\n", 99 | "model.add(Dense(512))\n", 100 | "model.add(Activation('relu'))\n", 101 | "model.add(Dropout(0.5))\n", 102 | "model.add(Dense(n_classes))\n", 103 | "model.add(Activation('softmax'))\n", 104 | "optimizer = SGD()\n", 105 | "model.compile(loss='categorical_crossentropy',\n", 106 | "optimizer=optimizer, metrics=['accuracy'])" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "n_epochs = 20\n", 116 | "batch_size = 128\n", 117 | "\n", 118 | "history = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, \n", 119 | " validation_data=[X_val, y_val],\n", 120 | " verbose = 1, callbacks=callbacks)" 121 | ] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.6.8" 141 | }, 142 | "widgets": { 143 | "state": {}, 144 | "version": "1.1.2" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 2 149 | } 150 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/.ipynb_checkpoints/数据增广使模型更加鲁棒-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.preprocessing.image import ImageDataGenerator\n", 10 | "from keras.models import Sequential\n", 11 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 12 | "from keras.layers import Conv2D, MaxPooling2D\n", 13 | "from keras.optimizers import Adam\n", 14 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint\n", 15 | "\n", 16 | "from keras.datasets import cifar10" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "(X_train, y_train), (X_val, y_val) = cifar10.load_data()\n", 26 | "\n", 27 | "X_train = X_train.astype('float32')/255.\n", 28 | "X_val = X_val.astype('float32')/255.\n", 29 | "n_classes = 10\n", 30 | "y_train = keras.utils.to_categorical(y_train, n_classes)\n", 31 | "y_val = keras.utils.to_categorical(y_val, n_classes)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "model = Sequential()\n", 41 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 42 | "model.add(Activation('relu'))\n", 43 | "model.add(Conv2D(32, (3, 3)))\n", 44 | "model.add(Activation('relu'))\n", 45 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 46 | "model.add(Dropout(0.25))\n", 47 | "\n", 48 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 49 | "model.add(Activation('relu'))\n", 50 | "model.add(Conv2D(64, (3, 3)))\n", 51 | "model.add(Activation('relu'))\n", 52 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 53 | "model.add(Dropout(0.25))\n", 54 | "\n", 55 | "model.add(Flatten())\n", 56 | "model.add(Dense(512))\n", 57 | "model.add(Activation('relu'))\n", 58 | "model.add(Dropout(0.5))\n", 59 | "model.add(Dense(n_classes))\n", 60 | "model.add(Activation('softmax'))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "opt = Adam(lr=0.0001)\n", 70 | "model.compile(loss='categorical_crossentropy',\n", 71 | "optimizer=opt, metrics=['accuracy'])" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "datagen = ImageDataGenerator(\n", 81 | " rotation_range=15,\n", 82 | " width_shift_range=0.15,\n", 83 | " height_shift_range=0.15,\n", 84 | " horizontal_flip=True,\n", 85 | " vertical_flip=False)\n", 86 | "\n", 87 | "datagen.fit(X_train)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2), ModelCheckpoint('checkpoints/weights.{epoch:02d}-'+str(batch_size)+'.hdf5', save_best_only=True),\n", 97 | " TensorBoard('~/notebooks/logs-lrscheduler')\n", 98 | " ]" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "batch_size = 128\n", 108 | "n_epochs = 1000\n", 109 | "history = model.fit_generator(datagen.flow(X_train, y_train,\n", 110 | " batch_size=batch_size),\n", 111 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 112 | " epochs=epochs,\n", 113 | " validation_data=(X_val, y_val),\n", 114 | " callbacks=callbacks\n", 115 | " )" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.6.8" 136 | }, 137 | "widgets": { 138 | "state": {}, 139 | "version": "1.1.2" 140 | } 141 | }, 142 | "nbformat": 4, 143 | "nbformat_minor": 2 144 | } 145 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/.ipynb_checkpoints/比较不同的优化器-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import pandas as pd\n", 11 | "from matplotlib import pyplot as plt\n", 12 | "\n", 13 | "from sklearn.model_selection import train_test_split\n", 14 | "from keras.models import Sequential\n", 15 | "from keras.layers import Dense, Dropout\n", 16 | "from keras.wrappers.scikit_learn import KerasRegressor\n", 17 | "from keras.callbacks import EarlyStopping, ModelCheckpoint\n", 18 | "from keras.optimizers import SGD, Adadelta, Adam, RMSprop, Adagrad, Nadam, Adamax" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 28 | "validation_split = 0.1\n", 29 | "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=validation_split, random_state=SEED)\n", 30 | "\n", 31 | "X_train = X_train.astype('float32')\n", 32 | "X_train /=255.\n", 33 | "X_val = X_val.astype('float32')\n", 34 | "X_val /=255.\n", 35 | "X_test = X_test.astype('float32')\n", 36 | "X_test /=255.\n", 37 | "\n", 38 | "n_classes = 10\n", 39 | "y_train = to_categorical(y_train, n_classes)\n", 40 | "y_val = to_categorical(y_val, n_classes)\n", 41 | "y_test = to_categorical(y_test, n_classes)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "def create_model(opt): \n", 51 | " model = Sequential()\n", 52 | " model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]))\n", 53 | " model.add(Activation('relu'))\n", 54 | " model.add(Conv2D(32, (3, 3)))\n", 55 | " model.add(Activation('relu'))\n", 56 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 57 | " model.add(Dropout(0.25))\n", 58 | "\n", 59 | " model.add(Conv2D(64, (3, 3), padding='same'))\n", 60 | " model.add(Activation('relu'))\n", 61 | " model.add(Conv2D(64, (3, 3)))\n", 62 | " model.add(Activation('relu'))\n", 63 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 64 | " model.add(Dropout(0.25))\n", 65 | "\n", 66 | " model.add(Flatten())\n", 67 | " model.add(Dense(512))\n", 68 | " model.add(Activation('relu'))\n", 69 | " model.add(Dropout(0.5))\n", 70 | " model.add(Dense(num_classes))\n", 71 | " model.add(Activation('softmax'))\n", 72 | " return model" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "def create_callbacks(opt):\n", 82 | " callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2),\n", 83 | " ModelCheckpoint('checkpoints/weights.{epoch:02d}-'+opt+'.h5', save_best_only=False, verbose=True),\n", 84 | " TensorBoard()]\n", 85 | " return callbacks" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "opts = dict({\n", 95 | " 'sgd': SGD(),\n", 96 | " 'sgd-0001': SGD(lr=0.0001, decay=0.00001),\n", 97 | " 'adam': Adam(),\n", 98 | " 'adam': Adam(lr=0.0001),\n", 99 | " 'adadelta': Adadelta(),\n", 100 | " 'rmsprop': RMSprop(),\n", 101 | " 'rmsprop-0001': RMSprop(lr=0.0001),\n", 102 | " 'nadam': Nadam(),\n", 103 | " 'adamax': Adamax()\n", 104 | " })" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "n_epochs = 1000\n", 114 | "batch_size = 128\n", 115 | "\n", 116 | "results = []\n", 117 | "for opt in opts:\n", 118 | " model = create_model(opt)\n", 119 | " callbacks = create_callbacks(opt)\n", 120 | " model.compile(loss='categorical_crossentropy', optimizer=opts[opt], metrics=['accuracy'])\n", 121 | " hist = model.fit(X_train, y_train, batch_size=batch_size, epochs=n_epochs,\n", 122 | " validation_data=(X_val, y_val),\n", 123 | " verbose=1,\n", 124 | " callbacks=callbacks)\n", 125 | " best_epoch = np.argmax(hist.history['val_acc'])\n", 126 | " best_acc = hist.history['val_acc'][best_epoch] \n", 127 | " best_model = create_model(opt)\n", 128 | " best_model.load_weights('checkpoints/weights.{:02d}-{}.h5'.format(best_epoch, opt))\n", 129 | " best_model.compile(loss='mse', optimizer=opts[opt], metrics=['accuracy'])\n", 130 | " score = best_model.evaluate(X_test, y_test, verbose=0)\n", 131 | " results.append([opt, best_epoch, best_acc, score[1]])" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "res = pd.DataFrame(results)\n", 141 | "res.columns = ['optimizer', 'epochs', 'val_accuracy', 'test_last', 'test_accuracy']\n", 142 | "res" 143 | ] 144 | } 145 | ], 146 | "metadata": { 147 | "kernelspec": { 148 | "display_name": "Python 3", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.6.8" 163 | }, 164 | "widgets": { 165 | "state": {}, 166 | "version": "1.1.2" 167 | } 168 | }, 169 | "nbformat": 4, 170 | "nbformat_minor": 2 171 | } 172 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/.ipynb_checkpoints/网格搜索调参-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sys\n", 10 | "import numpy as np\n", 11 | "\n", 12 | "from hyperopt import fmin, tpe, hp, STATUS_OK, Trials\n", 13 | "\n", 14 | "from keras.models import Sequential\n", 15 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 16 | "from keras.layers import Conv2D, MaxPooling2D\n", 17 | "from keras.optimizers import Adam\n", 18 | "from sklearn.model_selection import train_test_split\n", 19 | "from keras.utils import to_categorical\n", 20 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint\n", 21 | "\n", 22 | "from keras.datasets import cifar10" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 32 | "validation_split = 0.1\n", 33 | "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=validation_split, random_state=SEED)\n", 34 | "\n", 35 | "X_train = X_train.astype('float32')\n", 36 | "X_train /=255.\n", 37 | "X_val = X_val.astype('float32')\n", 38 | "X_val /=255.\n", 39 | "X_test = X_test.astype('float32')\n", 40 | "X_test /=255.\n", 41 | "\n", 42 | "n_classes = 10\n", 43 | "y_train = to_categorical(y_train, n_classes)\n", 44 | "y_val = to_categorical(y_val, n_classes)\n", 45 | "y_test = to_categorical(y_test, n_classes)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "space = {'batch_size' : hp.choice('batch_size', [32, 64, 128,256]), 'n_epochs' : 1000}" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "def get_callbacks(pars):\n", 64 | " callbacks =[EarlyStopping(monitor='val_acc', p atience=5, verbose=2),\n", 65 | " ModelCheckpoint('checkpoints/{}.h5'.format(pars['batch_size']), save_best_only=True),\n", 66 | " TensorBoard('~/notebooks/logs-gridsearch', write_graph=True, write_grads=True, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)]\n", 67 | " return callbacks" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "def f_nn(pars): \n", 77 | " print ('Parameters: ', pars)\n", 78 | " model = Sequential()\n", 79 | " model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 80 | " model.add(Activation('relu'))\n", 81 | " model.add(Conv2D(32, (3, 3)))\n", 82 | " model.add(Activation('relu'))\n", 83 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 84 | " model.add(Dropout(0.25))\n", 85 | "\n", 86 | " model.add(Conv2D(64, (3, 3), padding='same'))\n", 87 | " model.add(Activation('relu'))\n", 88 | " model.add(Conv2D(64, (3, 3)))\n", 89 | " model.add(Activation('relu'))\n", 90 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 91 | " model.add(Dropout(0.25))\n", 92 | "\n", 93 | " model.add(Flatten())\n", 94 | " model.add(Dense(512))\n", 95 | " model.add(Activation('relu'))\n", 96 | " model.add(Dropout(0.5))\n", 97 | " model.add(Dense(n_classes))\n", 98 | " model.add(Activation('softmax'))\n", 99 | " optimizer = Adam()\n", 100 | " model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])\n", 101 | " \n", 102 | " history = model.fit(X_train, y_train, epochs=pars['n_epochs'], batch_size=pars['batch_size'], \n", 103 | " validation_data=[X_val, y_val],\n", 104 | " verbose = 1, callbacks=get_callbacks(pars)) \n", 105 | " best_epoch = np.argmax(history.history['val_acc'])\n", 106 | " best_val_acc = np.max(history.history['val_acc'])\n", 107 | " print('Epoch {} - val acc: {}'.format(best_epoch, best_val_acc))\n", 108 | " sys.stdout.flush() \n", 109 | " return {'val_acc': best_val_acc, 'best_epoch': best_epoch, 'eval_time': time.time(), 'status': STATUS_OK}" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "trials = Trials()\n", 119 | "best = fmin(f_nn, space, algo=tpe.suggest, max_evals=50, trials=trials)\n", 120 | "print(best)" 121 | ] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.6.8" 141 | }, 142 | "widgets": { 143 | "state": {}, 144 | "version": "1.1.2" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 2 149 | } 150 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/README.md: -------------------------------------------------------------------------------- 1 | # 超参数选择、调优和神经网络学习 2 | - TensorBoard可视化训练过程 3 | - 使用批量生成器 4 | - 网格搜索调参 5 | - 学习率及其调度 6 | - 比较不同的优化器 7 | - 确定网络深度 8 | - 添加Dropout以防止过拟合 9 | - 数据增广使模型更加鲁棒 10 | - 利用TTA来提高精度 -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/TensorBoard可视化训练过程.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.datasets import cifar10\n", 10 | "from keras.preprocessing.image import ImageDataGenerator\n", 11 | "from keras.models import Sequential\n", 12 | "from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D\n", 13 | "from keras.optimizers import Adam\n", 14 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint\n", 15 | "from keras.utils import to_categorical" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "(X_train, y_train), (X_val, y_val) = cifar10.load_data()\n", 25 | "\n", 26 | "X_train = X_train.astype('float32')\n", 27 | "X_train /=255.\n", 28 | "X_val = X_val.astype('float32')\n", 29 | "X_val /=255.\n", 30 | "\n", 31 | "n_classes = 10\n", 32 | "y_train = to_categorical(y_train, n_classes)\n", 33 | "y_val = to_categorical(y_val, n_classes)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "model = Sequential()\n", 43 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 44 | "model.add(Activation('relu'))\n", 45 | "model.add(Conv2D(32, (3, 3)))\n", 46 | "model.add(Activation('relu'))\n", 47 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 48 | "model.add(Dropout(0.25))\n", 49 | "\n", 50 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 51 | "model.add(Activation('relu'))\n", 52 | "model.add(Conv2D(64, (3, 3)))\n", 53 | "model.add(Activation('relu'))\n", 54 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 55 | "model.add(Dropout(0.25))\n", 56 | "\n", 57 | "model.add(Flatten())\n", 58 | "model.add(Dense(512))\n", 59 | "model.add(Activation('relu'))\n", 60 | "model.add(Dropout(0.5))\n", 61 | "model.add(Dense(n_classes))\n", 62 | "model.add(Activation('softmax'))" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "opt = Adam()\n", 72 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "data_gen = ImageDataGenerator(\n", 82 | " rotation_range=15,\n", 83 | " width_shift_range=0.15,\n", 84 | " height_shift_range=0.15,\n", 85 | " horizontal_flip=True,\n", 86 | " vertical_flip=False)\n", 87 | "\n", 88 | "data_gen.fit(X_train)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2), ModelCheckpoint('checkpoints/weights.{epoch:02d}.hdf5', save_best_only=True), TensorBoard('~/notebooks/logs', write_graph=True, write_grads=True, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)]" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "batch_size = 128\n", 107 | "n_epochs = 1000\n", 108 | "model.fit_generator(data_gen.flow(X_train, y_train, batch_size=batch_size),\n", 109 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 110 | " epochs=n_epochs,\n", 111 | " validation_data=(X_val, y_val),\n", 112 | " callbacks=callbacks\n", 113 | " )" 114 | ] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.6.8" 134 | }, 135 | "widgets": { 136 | "state": {}, 137 | "version": "1.1.2" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 2 142 | } 143 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/使用批量生成器.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.datasets import cifar10\n", 10 | "from keras.preprocessing.image import ImageDataGenerator\n", 11 | "from keras.models import Sequential\n", 12 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 13 | "from keras.layers import Conv2D, MaxPooling2D\n", 14 | "from keras.optimizers import Adam\n", 15 | "from keras.callbacks import EarlyStopping, ModelCheckpoint" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 25 | "\n", 26 | "X_train = X_train.astype('float32')/255.\n", 27 | "X_test = X_test.astype('float32')/255.\n", 28 | "n_classes = 10\n", 29 | "y_train = keras.utils.to_categorical(y_train, n_classes)\n", 30 | "y_test = keras.utils.to_categorical(y_test, n_classes)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "model = Sequential()\n", 40 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 41 | "model.add(Activation('relu'))\n", 42 | "model.add(Conv2D(32, (3, 3)))\n", 43 | "model.add(Activation('relu'))\n", 44 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 45 | "model.add(Dropout(0.25))\n", 46 | "\n", 47 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 48 | "model.add(Activation('relu'))\n", 49 | "model.add(Conv2D(64, (3, 3)))\n", 50 | "model.add(Activation('relu'))\n", 51 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 52 | "model.add(Dropout(0.25))\n", 53 | "\n", 54 | "model.add(Flatten())\n", 55 | "model.add(Dense(512))\n", 56 | "model.add(Activation('relu'))\n", 57 | "model.add(Dropout(0.5))\n", 58 | "model.add(Dense(n_classes))\n", 59 | "model.add(Activation('softmax'))" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "opt = Adam(lr=0.0001)\n", 69 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "datagen = ImageDataGenerator(\n", 79 | " rotation_range=15,\n", 80 | " width_shift_range=0.15,\n", 81 | " height_shift_range=0.15,\n", 82 | " horizontal_flip=True,\n", 83 | " vertical_flip=False)\n", 84 | "\n", 85 | "datagen.fit(x_train)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2), ModelCheckpoint('checkpoints/weights.{epoch:02d}-'+str(batch_size)+'.hdf5', save_best_only=True)]" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "batch_size = 32\n", 104 | "n_epochs = 1000\n", 105 | "history_32 = model.fit_generator(datagen.flow(X_train, y_train,\n", 106 | " batch_size=batch_size),\n", 107 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 108 | " epochs=epochs,\n", 109 | " validation_data=(X_val, y_val),\n", 110 | " callbacks=callbacks\n", 111 | " )" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "batch_size = 256\n", 130 | "history_32 = model.fit_generator(datagen.flow(X_train, y_train,\n", 131 | " batch_size=batch_size),\n", 132 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 133 | " epochs=epochs,\n", 134 | " validation_data=(X_val, y_val),\n", 135 | " callbacks=callbacks\n", 136 | " )" 137 | ] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.6.8" 157 | }, 158 | "widgets": { 159 | "state": {}, 160 | "version": "1.1.2" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 2 165 | } 166 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/学习率及其调度.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import math\n", 10 | "\n", 11 | "from keras.models import Sequential\n", 12 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 13 | "from keras.layers import Conv2D, MaxPooling2D\n", 14 | "from keras.optimizers import SGD\n", 15 | "from sklearn.model_selection import train_test_split\n", 16 | "from keras.utils import to_categorical\n", 17 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint, LearningRateScheduler, Callback\n", 18 | "from keras import backend as K\n", 19 | "\n", 20 | "from keras.datasets import cifar10" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 30 | "validation_split = 0.1\n", 31 | "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=validation_split, random_state=SEED)\n", 32 | "\n", 33 | "X_train = X_train.astype('float32')\n", 34 | "X_train /=255.\n", 35 | "X_val = X_val.astype('float32')\n", 36 | "X_val /=255.\n", 37 | "X_test = X_test.astype('float32')\n", 38 | "X_test /=255.\n", 39 | "\n", 40 | "n_classes = 10\n", 41 | "y_train = to_categorical(y_train, n_classes)\n", 42 | "y_val = to_categorical(y_val, n_classes)\n", 43 | "y_test = to_categorical(y_test, n_classes)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "learning_rate_schedule = {0: '0.1', 10: '0.01', 25: '0.0025'}\n", 53 | "\n", 54 | "class get_learning_rate(Callback):\n", 55 | " def on_epoch_end(self, epoch, logs={}):\n", 56 | " optimizer = self.model.optimizer\n", 57 | " if epoch in learning_rate_schedule:\n", 58 | " K.set_value(optimizer.lr, learning_rate_schedule[epoch])\n", 59 | " lr = K.eval(optimizer.lr)\n", 60 | " print('\\nlr: {:.4f}'.format(lr))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "callbacks =[EarlyStopping(monitor='val_acc', patience=5, verbose=2),\n", 70 | " ModelCheckpoint('checkpoints/{epoch:02d}.h5', \n", 71 | " save_best_only=True),\n", 72 | " TensorBoard('~/notebooks/logs-lrscheduler'),\n", 73 | " get_learning_rate()\n", 74 | " ]" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "model = Sequential()\n", 84 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 85 | "model.add(Activation('relu'))\n", 86 | "model.add(Conv2D(32, (3, 3)))\n", 87 | "model.add(Activation('relu'))\n", 88 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 89 | "model.add(Dropout(0.25))\n", 90 | "\n", 91 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 92 | "model.add(Activation('relu'))\n", 93 | "model.add(Conv2D(64, (3, 3)))\n", 94 | "model.add(Activation('relu'))\n", 95 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 96 | "model.add(Dropout(0.25))\n", 97 | "\n", 98 | "model.add(Flatten())\n", 99 | "model.add(Dense(512))\n", 100 | "model.add(Activation('relu'))\n", 101 | "model.add(Dropout(0.5))\n", 102 | "model.add(Dense(n_classes))\n", 103 | "model.add(Activation('softmax'))\n", 104 | "optimizer = SGD()\n", 105 | "model.compile(loss='categorical_crossentropy',\n", 106 | "optimizer=optimizer, metrics=['accuracy'])" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "n_epochs = 20\n", 116 | "batch_size = 128\n", 117 | "\n", 118 | "history = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, \n", 119 | " validation_data=[X_val, y_val],\n", 120 | " verbose = 1, callbacks=callbacks)" 121 | ] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.6.8" 141 | }, 142 | "widgets": { 143 | "state": {}, 144 | "version": "1.1.2" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 2 149 | } 150 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/数据增广使模型更加鲁棒.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.preprocessing.image import ImageDataGenerator\n", 10 | "from keras.models import Sequential\n", 11 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 12 | "from keras.layers import Conv2D, MaxPooling2D\n", 13 | "from keras.optimizers import Adam\n", 14 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint\n", 15 | "\n", 16 | "from keras.datasets import cifar10" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "(X_train, y_train), (X_val, y_val) = cifar10.load_data()\n", 26 | "\n", 27 | "X_train = X_train.astype('float32')/255.\n", 28 | "X_val = X_val.astype('float32')/255.\n", 29 | "n_classes = 10\n", 30 | "y_train = keras.utils.to_categorical(y_train, n_classes)\n", 31 | "y_val = keras.utils.to_categorical(y_val, n_classes)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "model = Sequential()\n", 41 | "model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 42 | "model.add(Activation('relu'))\n", 43 | "model.add(Conv2D(32, (3, 3)))\n", 44 | "model.add(Activation('relu'))\n", 45 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 46 | "model.add(Dropout(0.25))\n", 47 | "\n", 48 | "model.add(Conv2D(64, (3, 3), padding='same'))\n", 49 | "model.add(Activation('relu'))\n", 50 | "model.add(Conv2D(64, (3, 3)))\n", 51 | "model.add(Activation('relu'))\n", 52 | "model.add(MaxPooling2D(pool_size=(2, 2)))\n", 53 | "model.add(Dropout(0.25))\n", 54 | "\n", 55 | "model.add(Flatten())\n", 56 | "model.add(Dense(512))\n", 57 | "model.add(Activation('relu'))\n", 58 | "model.add(Dropout(0.5))\n", 59 | "model.add(Dense(n_classes))\n", 60 | "model.add(Activation('softmax'))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "opt = Adam(lr=0.0001)\n", 70 | "model.compile(loss='categorical_crossentropy',\n", 71 | "optimizer=opt, metrics=['accuracy'])" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "datagen = ImageDataGenerator(\n", 81 | " rotation_range=15,\n", 82 | " width_shift_range=0.15,\n", 83 | " height_shift_range=0.15,\n", 84 | " horizontal_flip=True,\n", 85 | " vertical_flip=False)\n", 86 | "\n", 87 | "datagen.fit(X_train)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2), ModelCheckpoint('checkpoints/weights.{epoch:02d}-'+str(batch_size)+'.hdf5', save_best_only=True),\n", 97 | " TensorBoard('~/notebooks/logs-lrscheduler')\n", 98 | " ]" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "batch_size = 128\n", 108 | "n_epochs = 1000\n", 109 | "history = model.fit_generator(datagen.flow(X_train, y_train,\n", 110 | " batch_size=batch_size),\n", 111 | " steps_per_epoch=X_train.shape[0] // batch_size,\n", 112 | " epochs=epochs,\n", 113 | " validation_data=(X_val, y_val),\n", 114 | " callbacks=callbacks\n", 115 | " )" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.6.8" 136 | }, 137 | "widgets": { 138 | "state": {}, 139 | "version": "1.1.2" 140 | } 141 | }, 142 | "nbformat": 4, 143 | "nbformat_minor": 2 144 | } 145 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/比较不同的优化器.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import pandas as pd\n", 11 | "from matplotlib import pyplot as plt\n", 12 | "\n", 13 | "from sklearn.model_selection import train_test_split\n", 14 | "from keras.models import Sequential\n", 15 | "from keras.layers import Dense, Dropout\n", 16 | "from keras.wrappers.scikit_learn import KerasRegressor\n", 17 | "from keras.callbacks import EarlyStopping, ModelCheckpoint\n", 18 | "from keras.optimizers import SGD, Adadelta, Adam, RMSprop, Adagrad, Nadam, Adamax" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 28 | "validation_split = 0.1\n", 29 | "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=validation_split, random_state=SEED)\n", 30 | "\n", 31 | "X_train = X_train.astype('float32')\n", 32 | "X_train /=255.\n", 33 | "X_val = X_val.astype('float32')\n", 34 | "X_val /=255.\n", 35 | "X_test = X_test.astype('float32')\n", 36 | "X_test /=255.\n", 37 | "\n", 38 | "n_classes = 10\n", 39 | "y_train = to_categorical(y_train, n_classes)\n", 40 | "y_val = to_categorical(y_val, n_classes)\n", 41 | "y_test = to_categorical(y_test, n_classes)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "def create_model(opt): \n", 51 | " model = Sequential()\n", 52 | " model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]))\n", 53 | " model.add(Activation('relu'))\n", 54 | " model.add(Conv2D(32, (3, 3)))\n", 55 | " model.add(Activation('relu'))\n", 56 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 57 | " model.add(Dropout(0.25))\n", 58 | "\n", 59 | " model.add(Conv2D(64, (3, 3), padding='same'))\n", 60 | " model.add(Activation('relu'))\n", 61 | " model.add(Conv2D(64, (3, 3)))\n", 62 | " model.add(Activation('relu'))\n", 63 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 64 | " model.add(Dropout(0.25))\n", 65 | "\n", 66 | " model.add(Flatten())\n", 67 | " model.add(Dense(512))\n", 68 | " model.add(Activation('relu'))\n", 69 | " model.add(Dropout(0.5))\n", 70 | " model.add(Dense(num_classes))\n", 71 | " model.add(Activation('softmax'))\n", 72 | " return model" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "def create_callbacks(opt):\n", 82 | " callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=2),\n", 83 | " ModelCheckpoint('checkpoints/weights.{epoch:02d}-'+opt+'.h5', save_best_only=False, verbose=True),\n", 84 | " TensorBoard()]\n", 85 | " return callbacks" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "opts = dict({\n", 95 | " 'sgd': SGD(),\n", 96 | " 'sgd-0001': SGD(lr=0.0001, decay=0.00001),\n", 97 | " 'adam': Adam(),\n", 98 | " 'adam': Adam(lr=0.0001),\n", 99 | " 'adadelta': Adadelta(),\n", 100 | " 'rmsprop': RMSprop(),\n", 101 | " 'rmsprop-0001': RMSprop(lr=0.0001),\n", 102 | " 'nadam': Nadam(),\n", 103 | " 'adamax': Adamax()\n", 104 | " })" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "n_epochs = 1000\n", 114 | "batch_size = 128\n", 115 | "\n", 116 | "results = []\n", 117 | "for opt in opts:\n", 118 | " model = create_model(opt)\n", 119 | " callbacks = create_callbacks(opt)\n", 120 | " model.compile(loss='categorical_crossentropy', optimizer=opts[opt], metrics=['accuracy'])\n", 121 | " hist = model.fit(X_train, y_train, batch_size=batch_size, epochs=n_epochs,\n", 122 | " validation_data=(X_val, y_val),\n", 123 | " verbose=1,\n", 124 | " callbacks=callbacks)\n", 125 | " best_epoch = np.argmax(hist.history['val_acc'])\n", 126 | " best_acc = hist.history['val_acc'][best_epoch] \n", 127 | " best_model = create_model(opt)\n", 128 | " best_model.load_weights('checkpoints/weights.{:02d}-{}.h5'.format(best_epoch, opt))\n", 129 | " best_model.compile(loss='mse', optimizer=opts[opt], metrics=['accuracy'])\n", 130 | " score = best_model.evaluate(X_test, y_test, verbose=0)\n", 131 | " results.append([opt, best_epoch, best_acc, score[1]])" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "res = pd.DataFrame(results)\n", 141 | "res.columns = ['optimizer', 'epochs', 'val_accuracy', 'test_last', 'test_accuracy']\n", 142 | "res" 143 | ] 144 | } 145 | ], 146 | "metadata": { 147 | "kernelspec": { 148 | "display_name": "Python 3", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.6.8" 163 | }, 164 | "widgets": { 165 | "state": {}, 166 | "version": "1.1.2" 167 | } 168 | }, 169 | "nbformat": 4, 170 | "nbformat_minor": 2 171 | } 172 | -------------------------------------------------------------------------------- /超参数选择、调优和神经网络学习/网格搜索调参.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sys\n", 10 | "import numpy as np\n", 11 | "\n", 12 | "from hyperopt import fmin, tpe, hp, STATUS_OK, Trials\n", 13 | "\n", 14 | "from keras.models import Sequential\n", 15 | "from keras.layers import Dense, Dropout, Activation, Flatten\n", 16 | "from keras.layers import Conv2D, MaxPooling2D\n", 17 | "from keras.optimizers import Adam\n", 18 | "from sklearn.model_selection import train_test_split\n", 19 | "from keras.utils import to_categorical\n", 20 | "from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint\n", 21 | "\n", 22 | "from keras.datasets import cifar10" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n", 32 | "validation_split = 0.1\n", 33 | "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=validation_split, random_state=SEED)\n", 34 | "\n", 35 | "X_train = X_train.astype('float32')\n", 36 | "X_train /=255.\n", 37 | "X_val = X_val.astype('float32')\n", 38 | "X_val /=255.\n", 39 | "X_test = X_test.astype('float32')\n", 40 | "X_test /=255.\n", 41 | "\n", 42 | "n_classes = 10\n", 43 | "y_train = to_categorical(y_train, n_classes)\n", 44 | "y_val = to_categorical(y_val, n_classes)\n", 45 | "y_test = to_categorical(y_test, n_classes)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "space = {'batch_size' : hp.choice('batch_size', [32, 64, 128,256]), 'n_epochs' : 1000}" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "def get_callbacks(pars):\n", 64 | " callbacks =[EarlyStopping(monitor='val_acc', p atience=5, verbose=2),\n", 65 | " ModelCheckpoint('checkpoints/{}.h5'.format(pars['batch_size']), save_best_only=True),\n", 66 | " TensorBoard('~/notebooks/logs-gridsearch', write_graph=True, write_grads=True, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)]\n", 67 | " return callbacks" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "def f_nn(pars): \n", 77 | " print ('Parameters: ', pars)\n", 78 | " model = Sequential()\n", 79 | " model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:]))\n", 80 | " model.add(Activation('relu'))\n", 81 | " model.add(Conv2D(32, (3, 3)))\n", 82 | " model.add(Activation('relu'))\n", 83 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 84 | " model.add(Dropout(0.25))\n", 85 | "\n", 86 | " model.add(Conv2D(64, (3, 3), padding='same'))\n", 87 | " model.add(Activation('relu'))\n", 88 | " model.add(Conv2D(64, (3, 3)))\n", 89 | " model.add(Activation('relu'))\n", 90 | " model.add(MaxPooling2D(pool_size=(2, 2)))\n", 91 | " model.add(Dropout(0.25))\n", 92 | "\n", 93 | " model.add(Flatten())\n", 94 | " model.add(Dense(512))\n", 95 | " model.add(Activation('relu'))\n", 96 | " model.add(Dropout(0.5))\n", 97 | " model.add(Dense(n_classes))\n", 98 | " model.add(Activation('softmax'))\n", 99 | " optimizer = Adam()\n", 100 | " model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])\n", 101 | " \n", 102 | " history = model.fit(X_train, y_train, epochs=pars['n_epochs'], batch_size=pars['batch_size'], \n", 103 | " validation_data=[X_val, y_val],\n", 104 | " verbose = 1, callbacks=get_callbacks(pars)) \n", 105 | " best_epoch = np.argmax(history.history['val_acc'])\n", 106 | " best_val_acc = np.max(history.history['val_acc'])\n", 107 | " print('Epoch {} - val acc: {}'.format(best_epoch, best_val_acc))\n", 108 | " sys.stdout.flush() \n", 109 | " return {'val_acc': best_val_acc, 'best_epoch': best_epoch, 'eval_time': time.time(), 'status': STATUS_OK}" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "trials = Trials()\n", 119 | "best = fmin(f_nn, space, algo=tpe.suggest, max_evals=50, trials=trials)\n", 120 | "print(best)" 121 | ] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.6.8" 141 | }, 142 | "widgets": { 143 | "state": {}, 144 | "version": "1.1.2" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 2 149 | } 150 | -------------------------------------------------------------------------------- /预训练模型/.ipynb_checkpoints/使用GoogleLeNet Inception进行大规模视觉识别-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "\n", 11 | "from keras.applications.inception_v3 import InceptionV3\n", 12 | "from keras.applications import imagenet_utils\n", 13 | "from keras.preprocessing.image import load_img\n", 14 | "from keras.preprocessing.image import img_to_array" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "pretrained_model = InceptionV3\n", 24 | "model = pretrained_model(weights=\"imagenet\")" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "input_dim = (299, 299)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "preprocess = imagenet_utils.preprocess_input" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "image = load_img('../Data/dog_example_2.jpg', target_size=input_dim)\n", 52 | "image = img_to_array(image)\n", 53 | "image = image.reshape((1, *image.shape))\n", 54 | "image = preprocess(image)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "preds = model.predict(image)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "preds_decoded = imagenet_utils.decode_predictions(preds)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "preds_decoded" 82 | ] 83 | } 84 | ], 85 | "metadata": { 86 | "kernelspec": { 87 | "display_name": "Python 3", 88 | "language": "python", 89 | "name": "python3" 90 | }, 91 | "language_info": { 92 | "codemirror_mode": { 93 | "name": "ipython", 94 | "version": 3 95 | }, 96 | "file_extension": ".py", 97 | "mimetype": "text/x-python", 98 | "name": "python", 99 | "nbconvert_exporter": "python", 100 | "pygments_lexer": "ipython3", 101 | "version": "3.6.8" 102 | }, 103 | "widgets": { 104 | "state": {}, 105 | "version": "1.1.2" 106 | } 107 | }, 108 | "nbformat": 4, 109 | "nbformat_minor": 2 110 | } 111 | -------------------------------------------------------------------------------- /预训练模型/.ipynb_checkpoints/对新类别使用预训练的VGG模型-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import matplotlib.pyplot as plt\n", 11 | "\n", 12 | "from keras.models import Model\n", 13 | "from keras.applications.vgg16 import VGG16\n", 14 | "from keras.layers import Dense, GlobalAveragePooling2D\n", 15 | "from keras.optimizers import Adam\n", 16 | "\n", 17 | "from keras.applications import imagenet_utils\n", 18 | "from keras.utils import np_utils\n", 19 | "from keras.callbacks import EarlyStopping" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "from keras.datasets import cifar10\n", 29 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "n_classes = len(np.unique(y_train))\n", 39 | "y_train = np_utils.to_categorical(y_train, n_classes)\n", 40 | "y_test = np_utils.to_categorical(y_test, n_classes)\n", 41 | "\n", 42 | "X_train = X_train.astype('float32')/255.\n", 43 | "X_test = X_test.astype('float32')/255." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "vgg_model = VGG16(weights='imagenet', include_top=False)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "x = vgg_model.output\n", 62 | "x = GlobalAveragePooling2D()(x)\n", 63 | "x = Dense(512, activation='relu')(x)\n", 64 | "out = Dense(10, activation='softmax')(x)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "model = Model(inputs=vgg_model.input, outputs=out)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "for layer in vgg_model.layers:\n", 83 | " layer.trainable = False" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "opt = Adam()\n", 93 | "model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n", 94 | "model.summary()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=0)]" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "n_epochs = 50\n", 113 | "batch_size = 512\n", 114 | "history = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, validation_split=0.2, verbose=1, callbacks=callbacks)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "plt.plot(np.arange(len(history.history['acc'])), history.history['acc'], label='training')\n", 124 | "plt.plot(np.arange(len(history.history['val_acc'])), history.history['val_acc'], label='validation')\n", 125 | "plt.title('Accuracy')\n", 126 | "plt.xlabel('batches')\n", 127 | "plt.ylabel('accuracy ')\n", 128 | "plt.legend(loc=0)\n", 129 | "plt.show()" 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Python 3", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 3 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython3", 149 | "version": "3.6.8" 150 | }, 151 | "widgets": { 152 | "state": {}, 153 | "version": "1.1.2" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 2 158 | } 159 | -------------------------------------------------------------------------------- /预训练模型/.ipynb_checkpoints/用ResNet提取瓶颈特征-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.models import Model\n", 10 | "from keras.applications.resnet50 import ResNet50\n", 11 | "\n", 12 | "from keras.applications.resnet50 import preprocess_input\n", 13 | "from keras.preprocessing.image import load_img\n", 14 | "from keras.preprocessing.image import img_to_array\n", 15 | "from keras.applications import imagenet_utils" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "resnet_model = ResNet50(weights='imagenet')" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "model = Model(inputs=resnet_model.input, outputs=resnet_model.get_layer('avg_pool').output)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "input_dim = (224, 224)\n", 43 | "image = load_img('../Data/dog_example.jpg', target_size=input_dim)\n", 44 | "image = img_to_array(image)\n", 45 | "image = image.reshape((1, *image.shape))\n", 46 | "image = preprocess_input(image)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "avg_pool_features = model.predict(image)" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.6.8" 76 | }, 77 | "widgets": { 78 | "state": {}, 79 | "version": "1.1.2" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 2 84 | } 85 | -------------------------------------------------------------------------------- /预训练模型/.ipynb_checkpoints/用Xception细调-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "from keras.models import Model\n", 11 | "from keras.applications import Xception\n", 12 | "from keras.layers import Dense, GlobalAveragePooling2D\n", 13 | "from keras.optimizers import Adam\n", 14 | "\n", 15 | "from keras.applications import imagenet_utils\n", 16 | "from keras.utils import np_utils\n", 17 | "from keras.callbacks import EarlyStopping" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from keras.datasets import cifar10\n", 27 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "n_classes = len(np.unique(y_train))\n", 37 | "y_train = np_utils.to_categorical(y_train, n_classes)\n", 38 | "y_test = np_utils.to_categorical(y_test, n_classes)\n", 39 | "\n", 40 | "X_train = X_train.astype('float32')/255.\n", 41 | "X_test = X_test.astype('float32')/255." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "xception_model = Xception(weights='imagenet', include_top=False)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "x = xception_model.output\n", 60 | "x = GlobalAveragePooling2D()(x)\n", 61 | "x = Dense(512, activation='relu')(x)\n", 62 | "out = Dense(10, activation='softmax')(x)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "model = Model(inputs=xception_model.input, outputs=out)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "for layer in xception_model.layers:\n", 81 | " layer.trainable = False" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "opt = Adam()\n", 91 | "model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n", 92 | "model.summary()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=0)]\n" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "n_epochs = 100\n", 111 | "batch_size = 512\n", 112 | "history = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, validation_split=0.2, verbose=1, callbacks=callbacks)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "for i, layer in enumerate(model.layers):\n", 122 | " print(i, layer.name)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "for layer in model.layers[:115]:\n", 132 | " layer.trainable = False\n", 133 | "for layer in model.layers[115:]:\n", 134 | " layer.trainable = True" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "opt_finetune = Adam()\n", 144 | "model.compile(optimizer=opt_finetune, loss='categorical_crossentropy', metrics=['accuracy'])\n", 145 | "model.summary()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "history_finetune = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, validation_split=0.2, verbose=1, callbacks=callbacks)" 155 | ] 156 | } 157 | ], 158 | "metadata": { 159 | "kernelspec": { 160 | "display_name": "Python 3", 161 | "language": "python", 162 | "name": "python3" 163 | }, 164 | "language_info": { 165 | "codemirror_mode": { 166 | "name": "ipython", 167 | "version": 3 168 | }, 169 | "file_extension": ".py", 170 | "mimetype": "text/x-python", 171 | "name": "python", 172 | "nbconvert_exporter": "python", 173 | "pygments_lexer": "ipython3", 174 | "version": "3.6.8" 175 | }, 176 | "widgets": { 177 | "state": {}, 178 | "version": "1.1.2" 179 | } 180 | }, 181 | "nbformat": 4, 182 | "nbformat_minor": 2 183 | } 184 | -------------------------------------------------------------------------------- /预训练模型/README.md: -------------------------------------------------------------------------------- 1 | # 预训练模型 2 | - 使用GoogleLeNet/Inception进行大规模视觉识别 3 | - 用ResNet提取瓶颈特征 4 | - 对新类别使用预训练的VGG模型 5 | - 用Xception细调 -------------------------------------------------------------------------------- /预训练模型/使用GoogleLeNet Inception进行大规模视觉识别.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "\n", 11 | "from keras.applications.inception_v3 import InceptionV3\n", 12 | "from keras.applications import imagenet_utils\n", 13 | "from keras.preprocessing.image import load_img\n", 14 | "from keras.preprocessing.image import img_to_array" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "pretrained_model = InceptionV3\n", 24 | "model = pretrained_model(weights=\"imagenet\")" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "input_dim = (299, 299)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "preprocess = imagenet_utils.preprocess_input" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "image = load_img('../Data/dog_example_2.jpg', target_size=input_dim)\n", 52 | "image = img_to_array(image)\n", 53 | "image = image.reshape((1, *image.shape))\n", 54 | "image = preprocess(image)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "preds = model.predict(image)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "preds_decoded = imagenet_utils.decode_predictions(preds)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "preds_decoded" 82 | ] 83 | } 84 | ], 85 | "metadata": { 86 | "kernelspec": { 87 | "display_name": "Python 3", 88 | "language": "python", 89 | "name": "python3" 90 | }, 91 | "language_info": { 92 | "codemirror_mode": { 93 | "name": "ipython", 94 | "version": 3 95 | }, 96 | "file_extension": ".py", 97 | "mimetype": "text/x-python", 98 | "name": "python", 99 | "nbconvert_exporter": "python", 100 | "pygments_lexer": "ipython3", 101 | "version": "3.6.8" 102 | }, 103 | "widgets": { 104 | "state": {}, 105 | "version": "1.1.2" 106 | } 107 | }, 108 | "nbformat": 4, 109 | "nbformat_minor": 2 110 | } 111 | -------------------------------------------------------------------------------- /预训练模型/对新类别使用预训练的VGG模型.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import matplotlib.pyplot as plt\n", 11 | "\n", 12 | "from keras.models import Model\n", 13 | "from keras.applications.vgg16 import VGG16\n", 14 | "from keras.layers import Dense, GlobalAveragePooling2D\n", 15 | "from keras.optimizers import Adam\n", 16 | "\n", 17 | "from keras.applications import imagenet_utils\n", 18 | "from keras.utils import np_utils\n", 19 | "from keras.callbacks import EarlyStopping" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "from keras.datasets import cifar10\n", 29 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "n_classes = len(np.unique(y_train))\n", 39 | "y_train = np_utils.to_categorical(y_train, n_classes)\n", 40 | "y_test = np_utils.to_categorical(y_test, n_classes)\n", 41 | "\n", 42 | "X_train = X_train.astype('float32')/255.\n", 43 | "X_test = X_test.astype('float32')/255." 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "vgg_model = VGG16(weights='imagenet', include_top=False)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "x = vgg_model.output\n", 62 | "x = GlobalAveragePooling2D()(x)\n", 63 | "x = Dense(512, activation='relu')(x)\n", 64 | "out = Dense(10, activation='softmax')(x)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "model = Model(inputs=vgg_model.input, outputs=out)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "for layer in vgg_model.layers:\n", 83 | " layer.trainable = False" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "opt = Adam()\n", 93 | "model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n", 94 | "model.summary()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=0)]" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "n_epochs = 50\n", 113 | "batch_size = 512\n", 114 | "history = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, validation_split=0.2, verbose=1, callbacks=callbacks)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "plt.plot(np.arange(len(history.history['acc'])), history.history['acc'], label='training')\n", 124 | "plt.plot(np.arange(len(history.history['val_acc'])), history.history['val_acc'], label='validation')\n", 125 | "plt.title('Accuracy')\n", 126 | "plt.xlabel('batches')\n", 127 | "plt.ylabel('accuracy ')\n", 128 | "plt.legend(loc=0)\n", 129 | "plt.show()" 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Python 3", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 3 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython3", 149 | "version": "3.6.8" 150 | }, 151 | "widgets": { 152 | "state": {}, 153 | "version": "1.1.2" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 2 158 | } 159 | -------------------------------------------------------------------------------- /预训练模型/用ResNet提取瓶颈特征.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from keras.models import Model\n", 10 | "from keras.applications.resnet50 import ResNet50\n", 11 | "\n", 12 | "from keras.applications.resnet50 import preprocess_input\n", 13 | "from keras.preprocessing.image import load_img\n", 14 | "from keras.preprocessing.image import img_to_array\n", 15 | "from keras.applications import imagenet_utils" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "resnet_model = ResNet50(weights='imagenet')" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "model = Model(inputs=resnet_model.input, outputs=resnet_model.get_layer('avg_pool').output)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "input_dim = (224, 224)\n", 43 | "image = load_img('../Data/dog_example.jpg', target_size=input_dim)\n", 44 | "image = img_to_array(image)\n", 45 | "image = image.reshape((1, *image.shape))\n", 46 | "image = preprocess_input(image)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "avg_pool_features = model.predict(image)" 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.6.8" 76 | }, 77 | "widgets": { 78 | "state": {}, 79 | "version": "1.1.2" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 2 84 | } 85 | -------------------------------------------------------------------------------- /预训练模型/用Xception细调.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "from keras.models import Model\n", 11 | "from keras.applications import Xception\n", 12 | "from keras.layers import Dense, GlobalAveragePooling2D\n", 13 | "from keras.optimizers import Adam\n", 14 | "\n", 15 | "from keras.applications import imagenet_utils\n", 16 | "from keras.utils import np_utils\n", 17 | "from keras.callbacks import EarlyStopping" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from keras.datasets import cifar10\n", 27 | "(X_train, y_train), (X_test, y_test) = cifar10.load_data()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "n_classes = len(np.unique(y_train))\n", 37 | "y_train = np_utils.to_categorical(y_train, n_classes)\n", 38 | "y_test = np_utils.to_categorical(y_test, n_classes)\n", 39 | "\n", 40 | "X_train = X_train.astype('float32')/255.\n", 41 | "X_test = X_test.astype('float32')/255." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "xception_model = Xception(weights='imagenet', include_top=False)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "x = xception_model.output\n", 60 | "x = GlobalAveragePooling2D()(x)\n", 61 | "x = Dense(512, activation='relu')(x)\n", 62 | "out = Dense(10, activation='softmax')(x)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "model = Model(inputs=xception_model.input, outputs=out)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "for layer in xception_model.layers:\n", 81 | " layer.trainable = False" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "opt = Adam()\n", 91 | "model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n", 92 | "model.summary()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "callbacks = [EarlyStopping(monitor='val_acc', patience=5, verbose=0)]\n" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "n_epochs = 100\n", 111 | "batch_size = 512\n", 112 | "history = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, validation_split=0.2, verbose=1, callbacks=callbacks)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "for i, layer in enumerate(model.layers):\n", 122 | " print(i, layer.name)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "for layer in model.layers[:115]:\n", 132 | " layer.trainable = False\n", 133 | "for layer in model.layers[115:]:\n", 134 | " layer.trainable = True" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "opt_finetune = Adam()\n", 144 | "model.compile(optimizer=opt_finetune, loss='categorical_crossentropy', metrics=['accuracy'])\n", 145 | "model.summary()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "history_finetune = model.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size, validation_split=0.2, verbose=1, callbacks=callbacks)" 155 | ] 156 | } 157 | ], 158 | "metadata": { 159 | "kernelspec": { 160 | "display_name": "Python 3", 161 | "language": "python", 162 | "name": "python3" 163 | }, 164 | "language_info": { 165 | "codemirror_mode": { 166 | "name": "ipython", 167 | "version": 3 168 | }, 169 | "file_extension": ".py", 170 | "mimetype": "text/x-python", 171 | "name": "python", 172 | "nbconvert_exporter": "python", 173 | "pygments_lexer": "ipython3", 174 | "version": "3.6.8" 175 | }, 176 | "widgets": { 177 | "state": {}, 178 | "version": "1.1.2" 179 | } 180 | }, 181 | "nbformat": 4, 182 | "nbformat_minor": 2 183 | } 184 | --------------------------------------------------------------------------------