├── .gitignore ├── README.md ├── chap11_gaussian_mixture └── README.md ├── chap12_RBM ├── README.md └── rbm.py ├── chap14_reinforcement_learning ├── README.md ├── RL_QG_agent.py ├── __init__.py ├── reversi │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-35.pyc │ │ └── reversi.cpython-35.pyc │ └── reversi.py ├── reversi_main.py └── 期末作业说明.pdf ├── chap1_warmup ├── README.md └── numpy_ tutorial.ipynb ├── chap2_linear_regression ├── README.md ├── exercise-linear_regression.ipynb ├── linear_regression-tf2.0.ipynb ├── test.txt └── train.txt ├── chap3_SVM ├── README.md ├── data │ ├── test_kernel.txt │ ├── test_linear.txt │ ├── test_multi.txt │ ├── train_kernel.txt │ ├── train_linear.txt │ └── train_multi.txt └── svm.py ├── chap3_softmax_regression ├── README.md ├── logistic_regression-exercise.ipynb └── softmax_regression-exercise.ipynb ├── chap4_ simple neural network ├── README.md ├── tf2.0-exercise.ipynb ├── tf2.0-tutorial.pdf ├── tutorial_minst_fnn-numpy-exercise.ipynb └── tutorial_minst_fnn-tf2.0-exercise.ipynb ├── chap5_CNN ├── CNN_pytorch.ipynb ├── CNN_tensorflow.ipynb ├── README.md ├── corgi.jpg ├── tutorial_cifar10_conv-keras.ipynb ├── tutorial_mnist_conv-basic.ipynb ├── tutorial_mnist_conv-keras-sequential.ipynb ├── tutorial_mnist_conv-keras.ipynb └── tutorial_random_filter.ipynb ├── chap6_RNN ├── Learn2Carry-exercise.ipynb ├── README.md ├── poem_generation_with_RNN-exercise.ipynb ├── poems.txt ├── tangshi.txt ├── tangshi_for_pytorch │ ├── main.py │ ├── poems.txt │ ├── rnn.py │ └── tangshi.txt └── 题目要求 └── chap7-seq2seq-and-attention ├── seq2seq-attn.jpg ├── seq2seq.png ├── sequence_reversal-exercise.ipynb └── sequence_reversal_with_attention-exercise.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | .idea/ 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 《神经网络与深度学习》课程练习 2 | 3 | 书籍信息:[神经网络与深度学习](https://nndl.github.io/) 4 | 5 | 欢迎大家补充练习题目。 6 | 7 | ### 环境设定 8 | 本次作业需要首先安装 anaconda3 下载地址 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ , 9 | tensorflow 2.0 10 | pytorch >0.4 11 | 12 | ## Exercise 13 | 14 | ### 1. 热身练习 warmup 15 | numpy是Python中对于矩阵处理很实用的工具包,本小节作业主要是熟悉基本的numpy操作。 16 | 17 | ### 2. 线性回归模型 Linear Regression 18 | 19 | ### 3. 线性模型 20 | 21 | 1. 支持向量机 support vector machine 22 | 2. Softmax回归 Softmax Regression 23 | 24 | ### 4. 前馈神经网络 Simple Neural Network 25 | 26 | 利用numpy实现全连接神经网络 27 | 28 | ### 5. 卷积神经网络 Convolutional Neural Network (CNN) 29 | 利用卷积神经网络,处理MNIST 数据集分类问题。 30 | 31 | ### 6. 循环神经网络 Recurrent Neural Network (RNN) 32 | 基于循环神经网络的唐诗生成问题 33 | ### 7. 注意力机制 Attention Mechanism 34 | 35 | 1. 使用sequence to sequence 模型将一个字符串序列逆置。 36 | 2. 使用attentive sequence to sequence 模型将一个字符串序列逆置。 37 | 38 | ### 11. 高斯混合模型 Gaussian Mixture Model 39 | 40 | ### 12. 受限玻尔兹曼机 Restricted Boltzmann Machine (RBM) 41 | 42 | 使用受限玻尔兹曼机(Restricted Boltzmann Machine, RBM),对MNIST 数据集建模。 43 | 44 | ### 14. 深度强化学习 Deep Reinforcement Learning 45 | 强化学习: 黑白棋 46 | 47 | 48 | -------------------------------------------------------------------------------- /chap11_gaussian_mixture/README.md: -------------------------------------------------------------------------------- 1 | # 混合高斯模型 2 | 3 | ## 问题描述: 4 | 5 | ​ 生成一组不带标签的聚类数据,使用高斯混合模型(Gaussian Mixture Models)进行无监督聚类。 6 | 7 | 8 | 9 | 10 | 11 | 12 | ## 数据集: 13 | 14 | 本次作业不提供数据,请自己生成数据。 15 | 16 | 17 | 18 | 19 | 20 | ## 题目要求: 21 | 22 | ​ 推荐使用python 及numpy 编写代码,不建议使用框架。 -------------------------------------------------------------------------------- /chap12_RBM/README.md: -------------------------------------------------------------------------------- 1 | # 受限玻尔兹曼机 2 | 3 | 4 | 5 | ## 问题描述: 6 | 7 | ​ 使用受限玻尔兹曼机(Restricted Boltzmann Machine, RBM),对MNIST 数据集建模。其中,每个 8 | 像素点(值为0 或1)作为可观测变量,同时自定义一组隐变量(隐变量的个数为超参),完成以下任务: 9 | 10 | 1. 使用不带标签的MNIST 样本数据,训练RBM 参数。 11 | 2. 根据第1 步训练出的RBM,使用Gibbs 采样的方法,生成一组服从分布的样本。 12 | 13 | 14 | 15 | 16 | 17 | 18 | ## 数据集: 19 | 20 | MNIST数据集包括60000张训练图片和10000张测试图片。图片样本的数量已经足够训练一个很复杂的模型(例如 CNN的深层神经网络)。它经常被用来作为一个新 的模式识别模型的测试用例。而且它也是一个方便学生和研究者们执行用例的数据集。除此之外,MNIST数据集是一个相对较小的数据集,可以在你的笔记本CPUs上面直接执行 21 | 22 | 23 | 24 | 25 | 26 | ## 题目要求: 27 | 28 | - [ ] 请使用代码模板rbm.py,补全缺失部分,尽量不改动主体部分。 29 | - [ ] 推荐使用python 及numpy 编写主要逻辑代码,适度使用框架。 -------------------------------------------------------------------------------- /chap12_RBM/rbm.py: -------------------------------------------------------------------------------- 1 | # python: 2.7 2 | # encoding: utf-8 3 | 4 | import numpy as np 5 | 6 | 7 | class RBM: 8 | """Restricted Boltzmann Machine.""" 9 | 10 | def __init__(self, n_hidden=2, n_observe=784): 11 | """Initialize model.""" 12 | 13 | # 请补全此处代码 14 | pass 15 | 16 | def train(self, data): 17 | """Train model using data.""" 18 | 19 | # 请补全此处代码 20 | pass 21 | 22 | def sample(self): 23 | """Sample from trained model.""" 24 | 25 | # 请补全此处代码 26 | pass 27 | 28 | 29 | # train restricted boltzmann machine using mnist dataset 30 | if __name__ == '__main__': 31 | # load mnist dataset, no label 32 | mnist = np.load('mnist_bin.npy') # 60000x28x28 33 | n_imgs, n_rows, n_cols = mnist.shape 34 | img_size = n_rows * n_cols 35 | print mnist.shape 36 | 37 | # construct rbm model 38 | rbm = RBM(2, img_size) 39 | 40 | # train rbm model using mnist 41 | rbm.train(mnist) 42 | 43 | # sample from rbm model 44 | s = rbm.sample() 45 | -------------------------------------------------------------------------------- /chap14_reinforcement_learning/README.md: -------------------------------------------------------------------------------- 1 | # 黑白棋游戏 2 | 3 | 4 | 5 | ## 问题描述: 6 | 7 | ​ 本次作业要实现的是 利用强化学习等知识 玩atari 游戏中的 黑白棋游戏。具体任务是补全RL_QG_agent.py 文件. 8 | 9 | 10 | 11 | 12 | 13 | ## 环境配置: 14 | 15 | 配置环境安装办法: 16 | 17 | 1. pip install gym[all] # 安装 gym 18 | 2. 找到 安装的包的目录,然后复制 github 上面的reversi 文件夹, 到gym/envs/ 中 19 | (windows中的目录路径是 20 | C:\Program Files\Anaconda3\Lib\site-packages\gym\envs) 21 | 3. 在envs文件夹中 有__init__.py 文件,在文件 末尾,添加注册信息。 22 | (参考 github 上面 __init__.py 文件末尾的注册信息(即id='Reversi8x8-v0', 的注册信息)) 23 | 24 | 25 | 26 | 27 | 28 | ## 题目要求: 29 | 30 | ​ Github 中reversi_main.py 是一个demo程序,主要为了规范后期判作业时候的接口.本作业后面会运行大家的程序,因此需要统一接口,并且注意保证自己的代码没有错误,可以运行。训练程序的时候 黑白双方可以自己规定,环境中没有对弈对象。因此训练程序的时候时自己设置对弈对象,比如与随机进行对弈,其次可以和一些搜索算法对弈。 31 | 32 | 33 | 34 | 参考文献: 35 | 36 | 1. Learning to Play Othello with Deep Neural Networks 37 | 2. Reinforcement Learning in the Game of Othello: Learning Against a Fixed Opponent and Learning from Self-Play 38 | 39 | -------------------------------------------------------------------------------- /chap14_reinforcement_learning/RL_QG_agent.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import os 3 | 4 | class RL_QG_agent: 5 | def __init__(self): 6 | self.model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Reversi") 7 | pass # 删掉这句话,并填写相应代码 8 | 9 | def init_model(self): 10 | 11 | # 定义自己的 网络 12 | self.sess = tf.Session() 13 | self.saver = tf.train.Saver() 14 | # 补全代码 15 | 16 | 17 | def place(self,state,enables): 18 | # 这个函数 主要用于测试, 返回的 action是 0-63 之间的一个数值, 19 | # action 表示的是 要下的位置。 20 | action = 123456789 # 删掉这句话,并填写相应代码 21 | 22 | return action 23 | 24 | def save_model(self): # 保存 模型 25 | self.saver.save(self.sess, os.path.join(self.model_dir, 'parameter.ckpt')) 26 | 27 | def load_model(self):# 重新导入模型 28 | self.saver.restore(self.sess, os.path.join(self.model_dir, 'parameter.ckpt')) 29 | 30 | # 定义自己需要的函数 -------------------------------------------------------------------------------- /chap14_reinforcement_learning/__init__.py: -------------------------------------------------------------------------------- 1 | from gym.envs.registration import registry, register, make, spec 2 | 3 | # Algorithmic 4 | # ---------------------------------------- 5 | 6 | register( 7 | id='Copy-v0', 8 | entry_point='gym.envs.algorithmic:CopyEnv', 9 | max_episode_steps=200, 10 | reward_threshold=25.0, 11 | ) 12 | 13 | register( 14 | id='RepeatCopy-v0', 15 | entry_point='gym.envs.algorithmic:RepeatCopyEnv', 16 | max_episode_steps=200, 17 | reward_threshold=75.0, 18 | ) 19 | 20 | register( 21 | id='ReversedAddition-v0', 22 | entry_point='gym.envs.algorithmic:ReversedAdditionEnv', 23 | kwargs={'rows' : 2}, 24 | max_episode_steps=200, 25 | reward_threshold=25.0, 26 | ) 27 | 28 | register( 29 | id='ReversedAddition3-v0', 30 | entry_point='gym.envs.algorithmic:ReversedAdditionEnv', 31 | kwargs={'rows' : 3}, 32 | max_episode_steps=200, 33 | reward_threshold=25.0, 34 | ) 35 | 36 | register( 37 | id='DuplicatedInput-v0', 38 | entry_point='gym.envs.algorithmic:DuplicatedInputEnv', 39 | max_episode_steps=200, 40 | reward_threshold=9.0, 41 | ) 42 | 43 | register( 44 | id='Reverse-v0', 45 | entry_point='gym.envs.algorithmic:ReverseEnv', 46 | max_episode_steps=200, 47 | reward_threshold=25.0, 48 | ) 49 | 50 | # Classic 51 | # ---------------------------------------- 52 | 53 | register( 54 | id='CartPole-v0', 55 | entry_point='gym.envs.classic_control:CartPoleEnv', 56 | max_episode_steps=200, 57 | reward_threshold=195.0, 58 | ) 59 | 60 | register( 61 | id='CartPole-v1', 62 | entry_point='gym.envs.classic_control:CartPoleEnv', 63 | max_episode_steps=500, 64 | reward_threshold=475.0, 65 | ) 66 | 67 | register( 68 | id='MountainCar-v0', 69 | entry_point='gym.envs.classic_control:MountainCarEnv', 70 | max_episode_steps=200, 71 | reward_threshold=-110.0, 72 | ) 73 | 74 | register( 75 | id='MountainCarContinuous-v0', 76 | entry_point='gym.envs.classic_control:Continuous_MountainCarEnv', 77 | max_episode_steps=999, 78 | reward_threshold=90.0, 79 | ) 80 | 81 | register( 82 | id='Pendulum-v0', 83 | entry_point='gym.envs.classic_control:PendulumEnv', 84 | max_episode_steps=200, 85 | ) 86 | 87 | register( 88 | id='Acrobot-v1', 89 | entry_point='gym.envs.classic_control:AcrobotEnv', 90 | max_episode_steps=500, 91 | ) 92 | 93 | # Box2d 94 | # ---------------------------------------- 95 | 96 | register( 97 | id='LunarLander-v2', 98 | entry_point='gym.envs.box2d:LunarLander', 99 | max_episode_steps=1000, 100 | reward_threshold=200, 101 | ) 102 | 103 | register( 104 | id='LunarLanderContinuous-v2', 105 | entry_point='gym.envs.box2d:LunarLanderContinuous', 106 | max_episode_steps=1000, 107 | reward_threshold=200, 108 | ) 109 | 110 | register( 111 | id='BipedalWalker-v2', 112 | entry_point='gym.envs.box2d:BipedalWalker', 113 | max_episode_steps=1600, 114 | reward_threshold=300, 115 | ) 116 | 117 | register( 118 | id='BipedalWalkerHardcore-v2', 119 | entry_point='gym.envs.box2d:BipedalWalkerHardcore', 120 | max_episode_steps=2000, 121 | reward_threshold=300, 122 | ) 123 | 124 | register( 125 | id='CarRacing-v0', 126 | entry_point='gym.envs.box2d:CarRacing', 127 | max_episode_steps=1000, 128 | reward_threshold=900, 129 | ) 130 | 131 | # Toy Text 132 | # ---------------------------------------- 133 | 134 | register( 135 | id='Blackjack-v0', 136 | entry_point='gym.envs.toy_text:BlackjackEnv', 137 | ) 138 | 139 | register( 140 | id='KellyCoinflip-v0', 141 | entry_point='gym.envs.toy_text:KellyCoinflipEnv', 142 | reward_threshold=246.61, 143 | ) 144 | register( 145 | id='KellyCoinflipGeneralized-v0', 146 | entry_point='gym.envs.toy_text:KellyCoinflipGeneralizedEnv', 147 | ) 148 | 149 | register( 150 | id='FrozenLake-v0', 151 | entry_point='gym.envs.toy_text:FrozenLakeEnv', 152 | kwargs={'map_name' : '4x4'}, 153 | max_episode_steps=100, 154 | reward_threshold=0.78, # optimum = .8196 155 | ) 156 | 157 | register( 158 | id='FrozenLake8x8-v0', 159 | entry_point='gym.envs.toy_text:FrozenLakeEnv', 160 | kwargs={'map_name' : '8x8'}, 161 | max_episode_steps=200, 162 | reward_threshold=0.99, # optimum = 1 163 | ) 164 | 165 | register( 166 | id='CliffWalking-v0', 167 | entry_point='gym.envs.toy_text:CliffWalkingEnv', 168 | ) 169 | 170 | register( 171 | id='NChain-v0', 172 | entry_point='gym.envs.toy_text:NChainEnv', 173 | max_episode_steps=1000, 174 | ) 175 | 176 | register( 177 | id='Roulette-v0', 178 | entry_point='gym.envs.toy_text:RouletteEnv', 179 | max_episode_steps=100, 180 | ) 181 | 182 | register( 183 | id='Taxi-v2', 184 | entry_point='gym.envs.toy_text.taxi:TaxiEnv', 185 | reward_threshold=8, # optimum = 8.46 186 | max_episode_steps=200, 187 | ) 188 | 189 | register( 190 | id='GuessingGame-v0', 191 | entry_point='gym.envs.toy_text.guessing_game:GuessingGame', 192 | max_episode_steps=200, 193 | ) 194 | 195 | register( 196 | id='HotterColder-v0', 197 | entry_point='gym.envs.toy_text.hotter_colder:HotterColder', 198 | max_episode_steps=200, 199 | ) 200 | 201 | # Mujoco 202 | # ---------------------------------------- 203 | 204 | # 2D 205 | 206 | register( 207 | id='Reacher-v1', 208 | entry_point='gym.envs.mujoco:ReacherEnv', 209 | max_episode_steps=50, 210 | reward_threshold=-3.75, 211 | ) 212 | 213 | register( 214 | id='Pusher-v0', 215 | entry_point='gym.envs.mujoco:PusherEnv', 216 | max_episode_steps=100, 217 | reward_threshold=0.0, 218 | ) 219 | 220 | register( 221 | id='Thrower-v0', 222 | entry_point='gym.envs.mujoco:ThrowerEnv', 223 | max_episode_steps=100, 224 | reward_threshold=0.0, 225 | ) 226 | 227 | register( 228 | id='Striker-v0', 229 | entry_point='gym.envs.mujoco:StrikerEnv', 230 | max_episode_steps=100, 231 | reward_threshold=0.0, 232 | ) 233 | 234 | register( 235 | id='InvertedPendulum-v1', 236 | entry_point='gym.envs.mujoco:InvertedPendulumEnv', 237 | max_episode_steps=1000, 238 | reward_threshold=950.0, 239 | ) 240 | 241 | register( 242 | id='InvertedDoublePendulum-v1', 243 | entry_point='gym.envs.mujoco:InvertedDoublePendulumEnv', 244 | max_episode_steps=1000, 245 | reward_threshold=9100.0, 246 | ) 247 | 248 | register( 249 | id='HalfCheetah-v1', 250 | entry_point='gym.envs.mujoco:HalfCheetahEnv', 251 | max_episode_steps=1000, 252 | reward_threshold=4800.0, 253 | ) 254 | 255 | register( 256 | id='Hopper-v1', 257 | entry_point='gym.envs.mujoco:HopperEnv', 258 | max_episode_steps=1000, 259 | reward_threshold=3800.0, 260 | ) 261 | 262 | register( 263 | id='Swimmer-v1', 264 | entry_point='gym.envs.mujoco:SwimmerEnv', 265 | max_episode_steps=1000, 266 | reward_threshold=360.0, 267 | ) 268 | 269 | register( 270 | id='Walker2d-v1', 271 | max_episode_steps=1000, 272 | entry_point='gym.envs.mujoco:Walker2dEnv', 273 | ) 274 | 275 | register( 276 | id='Ant-v1', 277 | entry_point='gym.envs.mujoco:AntEnv', 278 | max_episode_steps=1000, 279 | reward_threshold=6000.0, 280 | ) 281 | 282 | register( 283 | id='Humanoid-v1', 284 | entry_point='gym.envs.mujoco:HumanoidEnv', 285 | max_episode_steps=1000, 286 | ) 287 | 288 | register( 289 | id='HumanoidStandup-v1', 290 | entry_point='gym.envs.mujoco:HumanoidStandupEnv', 291 | max_episode_steps=1000, 292 | ) 293 | 294 | # Atari 295 | # ---------------------------------------- 296 | 297 | # # print ', '.join(["'{}'".format(name.split('.')[0]) for name in atari_py.list_games()]) 298 | for game in ['air_raid', 'alien', 'amidar', 'assault', 'asterix', 'asteroids', 'atlantis', 299 | 'bank_heist', 'battle_zone', 'beam_rider', 'berzerk', 'bowling', 'boxing', 'breakout', 'carnival', 300 | 'centipede', 'chopper_command', 'crazy_climber', 'demon_attack', 'double_dunk', 301 | 'elevator_action', 'enduro', 'fishing_derby', 'freeway', 'frostbite', 'gopher', 'gravitar', 302 | 'hero', 'ice_hockey', 'jamesbond', 'journey_escape', 'kangaroo', 'krull', 'kung_fu_master', 303 | 'montezuma_revenge', 'ms_pacman', 'name_this_game', 'phoenix', 'pitfall', 'pong', 'pooyan', 304 | 'private_eye', 'qbert', 'riverraid', 'road_runner', 'robotank', 'seaquest', 'skiing', 305 | 'solaris', 'space_invaders', 'star_gunner', 'tennis', 'time_pilot', 'tutankham', 'up_n_down', 306 | 'venture', 'video_pinball', 'wizard_of_wor', 'yars_revenge', 'zaxxon']: 307 | for obs_type in ['image', 'ram']: 308 | # space_invaders should yield SpaceInvaders-v0 and SpaceInvaders-ram-v0 309 | name = ''.join([g.capitalize() for g in game.split('_')]) 310 | if obs_type == 'ram': 311 | name = '{}-ram'.format(name) 312 | 313 | nondeterministic = False 314 | if game == 'elevator_action' and obs_type == 'ram': 315 | # ElevatorAction-ram-v0 seems to yield slightly 316 | # non-deterministic observations about 10% of the time. We 317 | # should track this down eventually, but for now we just 318 | # mark it as nondeterministic. 319 | nondeterministic = True 320 | 321 | register( 322 | id='{}-v0'.format(name), 323 | entry_point='gym.envs.atari:AtariEnv', 324 | kwargs={'game': game, 'obs_type': obs_type, 'repeat_action_probability': 0.25}, 325 | max_episode_steps=10000, 326 | nondeterministic=nondeterministic, 327 | ) 328 | 329 | register( 330 | id='{}-v4'.format(name), 331 | entry_point='gym.envs.atari:AtariEnv', 332 | kwargs={'game': game, 'obs_type': obs_type}, 333 | max_episode_steps=100000, 334 | nondeterministic=nondeterministic, 335 | ) 336 | 337 | # Standard Deterministic (as in the original DeepMind paper) 338 | if game == 'space_invaders': 339 | frameskip = 3 340 | else: 341 | frameskip = 4 342 | 343 | # Use a deterministic frame skip. 344 | register( 345 | id='{}Deterministic-v0'.format(name), 346 | entry_point='gym.envs.atari:AtariEnv', 347 | kwargs={'game': game, 'obs_type': obs_type, 'frameskip': frameskip, 'repeat_action_probability': 0.25}, 348 | max_episode_steps=100000, 349 | nondeterministic=nondeterministic, 350 | ) 351 | 352 | register( 353 | id='{}Deterministic-v4'.format(name), 354 | entry_point='gym.envs.atari:AtariEnv', 355 | kwargs={'game': game, 'obs_type': obs_type, 'frameskip': frameskip}, 356 | max_episode_steps=100000, 357 | nondeterministic=nondeterministic, 358 | ) 359 | 360 | register( 361 | id='{}NoFrameskip-v0'.format(name), 362 | entry_point='gym.envs.atari:AtariEnv', 363 | kwargs={'game': game, 'obs_type': obs_type, 'frameskip': 1, 'repeat_action_probability': 0.25}, # A frameskip of 1 means we get every frame 364 | max_episode_steps=frameskip * 100000, 365 | nondeterministic=nondeterministic, 366 | ) 367 | 368 | # No frameskip. (Atari has no entropy source, so these are 369 | # deterministic environments.) 370 | register( 371 | id='{}NoFrameskip-v4'.format(name), 372 | entry_point='gym.envs.atari:AtariEnv', 373 | kwargs={'game': game, 'obs_type': obs_type, 'frameskip': 1}, # A frameskip of 1 means we get every frame 374 | max_episode_steps=frameskip * 100000, 375 | nondeterministic=nondeterministic, 376 | ) 377 | 378 | # Board games 379 | # ---------------------------------------- 380 | 381 | register( 382 | id='Go9x9-v0', 383 | entry_point='gym.envs.board_game:GoEnv', 384 | kwargs={ 385 | 'player_color': 'black', 386 | 'opponent': 'pachi:uct:_2400', 387 | 'observation_type': 'image3c', 388 | 'illegal_move_mode': 'lose', 389 | 'board_size': 9, 390 | }, 391 | # The pachi player seems not to be determistic given a fixed seed. 392 | # (Reproduce by running 'import gym; h = gym.make('Go9x9-v0'); h.seed(1); h.reset(); h.step(15); h.step(16); h.step(17)' a few times.) 393 | # 394 | # This is probably due to a computation time limit. 395 | nondeterministic=True, 396 | ) 397 | 398 | register( 399 | id='Go19x19-v0', 400 | entry_point='gym.envs.board_game:GoEnv', 401 | kwargs={ 402 | 'player_color': 'black', 403 | 'opponent': 'pachi:uct:_2400', 404 | 'observation_type': 'image3c', 405 | 'illegal_move_mode': 'lose', 406 | 'board_size': 19, 407 | }, 408 | nondeterministic=True, 409 | ) 410 | 411 | register( 412 | id='Hex9x9-v0', 413 | entry_point='gym.envs.board_game:HexEnv', 414 | kwargs={ 415 | 'player_color': 'black', 416 | 'opponent': 'random', 417 | 'observation_type': 'numpy3c', 418 | 'illegal_move_mode': 'lose', 419 | 'board_size': 9, 420 | }, 421 | ) 422 | 423 | # Debugging 424 | # ---------------------------------------- 425 | 426 | register( 427 | id='OneRoundDeterministicReward-v0', 428 | entry_point='gym.envs.debugging:OneRoundDeterministicRewardEnv', 429 | local_only=True 430 | ) 431 | 432 | register( 433 | id='TwoRoundDeterministicReward-v0', 434 | entry_point='gym.envs.debugging:TwoRoundDeterministicRewardEnv', 435 | local_only=True 436 | ) 437 | 438 | register( 439 | id='OneRoundNondeterministicReward-v0', 440 | entry_point='gym.envs.debugging:OneRoundNondeterministicRewardEnv', 441 | local_only=True 442 | ) 443 | 444 | register( 445 | id='TwoRoundNondeterministicReward-v0', 446 | entry_point='gym.envs.debugging:TwoRoundNondeterministicRewardEnv', 447 | local_only=True, 448 | ) 449 | 450 | # Parameter tuning 451 | # ---------------------------------------- 452 | register( 453 | id='ConvergenceControl-v0', 454 | entry_point='gym.envs.parameter_tuning:ConvergenceControl', 455 | ) 456 | 457 | register( 458 | id='CNNClassifierTraining-v0', 459 | entry_point='gym.envs.parameter_tuning:CNNClassifierTraining', 460 | ) 461 | 462 | # Safety 463 | # ---------------------------------------- 464 | 465 | # interpretability envs 466 | register( 467 | id='PredictActionsCartpole-v0', 468 | entry_point='gym.envs.safety:PredictActionsCartpoleEnv', 469 | max_episode_steps=200, 470 | ) 471 | 472 | register( 473 | id='PredictObsCartpole-v0', 474 | entry_point='gym.envs.safety:PredictObsCartpoleEnv', 475 | max_episode_steps=200, 476 | ) 477 | 478 | # semi_supervised envs 479 | # probably the easiest: 480 | register( 481 | id='SemisuperPendulumNoise-v0', 482 | entry_point='gym.envs.safety:SemisuperPendulumNoiseEnv', 483 | max_episode_steps=200, 484 | ) 485 | # somewhat harder because of higher variance: 486 | register( 487 | id='SemisuperPendulumRandom-v0', 488 | entry_point='gym.envs.safety:SemisuperPendulumRandomEnv', 489 | max_episode_steps=200, 490 | ) 491 | # probably the hardest because you only get a constant number of rewards in total: 492 | register( 493 | id='SemisuperPendulumDecay-v0', 494 | entry_point='gym.envs.safety:SemisuperPendulumDecayEnv', 495 | max_episode_steps=200, 496 | ) 497 | 498 | # off_switch envs 499 | register( 500 | id='OffSwitchCartpole-v0', 501 | entry_point='gym.envs.safety:OffSwitchCartpoleEnv', 502 | max_episode_steps=200, 503 | ) 504 | 505 | register( 506 | id='OffSwitchCartpoleProb-v0', 507 | entry_point='gym.envs.safety:OffSwitchCartpoleProbEnv', 508 | max_episode_steps=200, 509 | ) 510 | 511 | register( 512 | id='Reversi8x8-v0', 513 | entry_point='gym.envs.reversi:ReversiEnv', 514 | kwargs={ 515 | 'player_color': 'black', 516 | 'opponent': 'random', 517 | 'observation_type': 'numpy3c', 518 | 'illegal_place_mode': 'lose', 519 | 'board_size': 8, 520 | } 521 | ) -------------------------------------------------------------------------------- /chap14_reinforcement_learning/reversi/__init__.py: -------------------------------------------------------------------------------- 1 | from gym.envs.reversi.reversi import ReversiEnv 2 | -------------------------------------------------------------------------------- /chap14_reinforcement_learning/reversi/__pycache__/__init__.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap14_reinforcement_learning/reversi/__pycache__/__init__.cpython-35.pyc -------------------------------------------------------------------------------- /chap14_reinforcement_learning/reversi/__pycache__/reversi.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap14_reinforcement_learning/reversi/__pycache__/reversi.cpython-35.pyc -------------------------------------------------------------------------------- /chap14_reinforcement_learning/reversi/reversi.py: -------------------------------------------------------------------------------- 1 | """ 2 | Game of Reversi 3 | """ 4 | 5 | from six import StringIO 6 | import sys 7 | import gym 8 | from gym import spaces 9 | import numpy as np 10 | from gym import error 11 | from gym.utils import seeding 12 | 13 | def make_random_policy(np_random): 14 | def random_policy(state, player_color): 15 | possible_places = ReversiEnv.get_possible_actions(state, player_color) 16 | # No places left 17 | if len(possible_places) == 0: 18 | return d**2 + 1 19 | a = np_random.randint(len(possible_places)) 20 | return possible_places[a] 21 | return random_policy 22 | 23 | class ReversiEnv(gym.Env): 24 | """ 25 | Reversi environment. Play against a fixed opponent. 26 | """ 27 | BLACK = 0 28 | WHITE = 1 29 | metadata = {"render.modes": ["ansi","human"]} 30 | 31 | def __init__(self, player_color, opponent, observation_type, illegal_place_mode, board_size): 32 | """ 33 | Args: 34 | player_color: Stone color for the agent. Either 'black' or 'white' 35 | opponent: An opponent policy 36 | observation_type: State encoding 37 | illegal_place_mode: What to do when the agent makes an illegal place. Choices: 'raise' or 'lose' 38 | board_size: size of the Reversi board 39 | """ 40 | assert isinstance(board_size, int) and board_size >= 1, 'Invalid board size: {}'.format(board_size) 41 | self.board_size = board_size 42 | 43 | colormap = { 44 | 'black': ReversiEnv.BLACK, 45 | 'white': ReversiEnv.WHITE, 46 | } 47 | try: 48 | self.player_color = colormap[player_color] 49 | except KeyError: 50 | raise error.Error("player_color must be 'black' or 'white', not {}".format(player_color)) 51 | 52 | self.opponent = opponent 53 | 54 | assert observation_type in ['numpy3c'] 55 | self.observation_type = observation_type 56 | 57 | assert illegal_place_mode in ['lose', 'raise'] 58 | self.illegal_place_mode = illegal_place_mode 59 | 60 | if self.observation_type != 'numpy3c': 61 | raise error.Error('Unsupported observation type: {}'.format(self.observation_type)) 62 | 63 | # One action for each board position and resign and pass 64 | self.action_space = spaces.Discrete(self.board_size ** 2 + 2) 65 | observation = self.reset() 66 | self.observation_space = spaces.Box(np.zeros(observation.shape), np.ones(observation.shape)) 67 | 68 | self._seed() 69 | 70 | def _seed(self, seed=None): 71 | self.np_random, seed = seeding.np_random(seed) 72 | 73 | # Update the random policy if needed 74 | if isinstance(self.opponent, str): 75 | if self.opponent == 'random': 76 | self.opponent_policy = make_random_policy(self.np_random) 77 | print("################################################################") 78 | else: 79 | raise error.Error('Unrecognized opponent policy {}'.format(self.opponent)) 80 | else: 81 | self.opponent_policy = self.opponent 82 | 83 | return [seed] 84 | 85 | def _reset(self): 86 | # init board setting 87 | self.state = np.zeros((3, self.board_size, self.board_size)) 88 | self.state[2, :, :] = 1.0 89 | self.state[2, 3:5, 3:5] = 0 90 | self.state[0, 4, 3] = 1 91 | self.state[0, 3, 4] = 1 92 | self.state[1, 3, 3] = 1 93 | self.state[1, 4, 4] = 1 94 | self.to_play = ReversiEnv.BLACK 95 | self.possible_actions = ReversiEnv.get_possible_actions(self.state, self.to_play) 96 | self.done = False 97 | 98 | # Let the opponent play if it's not the agent's turn 99 | if self.player_color != self.to_play: 100 | a = self.opponent_policy(self.state) 101 | ReversiEnv.make_place(self.state, a, ReversiEnv.BLACK) 102 | self.to_play = ReversiEnv.WHITE 103 | return self.state 104 | 105 | def _step(self, action): 106 | color = action[1] 107 | action = action[0] 108 | 109 | assert self.to_play == self.player_color 110 | # If already terminal, then don't do anything 111 | if self.done: # 如果已经结束了 112 | return self.state, 0., True, {'state': self.state} 113 | if color == 0: # 黑色棋子是 0 114 | if ReversiEnv.pass_place(self.board_size, action): 115 | pass 116 | elif ReversiEnv.resign_place(self.board_size, action): 117 | return self.state, -1, True, {'state': self.state} 118 | elif not ReversiEnv.valid_place(self.state, action, self.player_color): 119 | if self.illegal_place_mode == 'raise': 120 | raise 121 | elif self.illegal_place_mode == 'lose': 122 | # Automatic loss on illegal place 123 | self.done = True 124 | return self.state, -1., True, {'state': self.state} 125 | else: 126 | raise error.Error('Unsupported illegal place action: {}'.format(self.illegal_place_mode)) 127 | else: 128 | ReversiEnv.make_place(self.state, action, self.player_color) 129 | self.possible_actions = ReversiEnv.get_possible_actions(self.state, 1) 130 | 131 | else: # # Opponent play 白色棋子 是 1 132 | if ReversiEnv.pass_place(self.board_size, action): 133 | pass 134 | elif ReversiEnv.resign_place(self.board_size, action): 135 | return self.state, 1, True, {'state': self.state} 136 | elif not ReversiEnv.valid_place(self.state, action, 1 - self.player_color): 137 | if self.illegal_place_mode == 'raise': 138 | raise 139 | elif self.illegal_place_mode == 'lose': 140 | # Automatic loss on illegal place 141 | self.done = True 142 | return self.state, 1., True, {'state': self.state} 143 | else: 144 | raise error.Error('Unsupported illegal place action: {}'.format(self.illegal_place_mode)) 145 | else: 146 | ReversiEnv.make_place(self.state, action , 1 - self.player_color) 147 | self.possible_actions = ReversiEnv.get_possible_actions(self.state, 0 ) 148 | 149 | 150 | reward = ReversiEnv.game_finished(self.state) 151 | if self.player_color == ReversiEnv.WHITE: 152 | reward = - reward 153 | self.done = reward != 0 154 | return self.state, reward, self.done, {'state': self.state} 155 | 156 | # def _reset_opponent(self): 157 | # if self.opponent == 'random': 158 | # self.opponent_policy = random_policy 159 | # else: 160 | # raise error.Error('Unrecognized opponent policy {}'.format(self.opponent)) 161 | 162 | def _render(self, mode='human', close=False): 163 | if close: 164 | return 165 | board = self.state 166 | outfile = StringIO() if mode == 'ansi' else sys.stdout 167 | 168 | outfile.write(' ' * 7) 169 | for j in range(board.shape[1]): 170 | outfile.write(' ' + str(j + 1) + ' | ') 171 | outfile.write('\n') 172 | outfile.write(' ' * 5) 173 | outfile.write('-' * (board.shape[1] * 6 - 1)) 174 | outfile.write('\n') 175 | for i in range(board.shape[1]): 176 | outfile.write(' ' + str(i + 1) + ' |') 177 | for j in range(board.shape[1]): 178 | if board[2, i, j] == 1: 179 | outfile.write(' O ') 180 | elif board[0, i, j] == 1: 181 | outfile.write(' B ') 182 | else: 183 | outfile.write(' W ') 184 | outfile.write('|') 185 | outfile.write('\n') 186 | outfile.write(' ' ) 187 | outfile.write('-' * (board.shape[1] * 7 - 1)) 188 | outfile.write('\n') 189 | 190 | if mode != 'human': 191 | return outfile 192 | 193 | # @staticmethod 194 | # def pass_place(board_size, action): 195 | # return action == board_size ** 2 196 | 197 | @staticmethod 198 | def resign_place(board_size, action): 199 | return action == board_size ** 2 200 | 201 | @staticmethod 202 | def pass_place(board_size, action): 203 | return action == board_size ** 2 + 1 204 | 205 | @staticmethod 206 | def get_possible_actions(board, player_color): 207 | actions=[] 208 | d = board.shape[-1] 209 | opponent_color = 1 - player_color 210 | for pos_x in range(d): 211 | for pos_y in range(d): 212 | if (board[2, pos_x, pos_y]==0): 213 | continue 214 | for dx in [-1, 0, 1]: 215 | for dy in [-1, 0, 1]: 216 | if(dx == 0 and dy == 0): 217 | continue 218 | nx = pos_x + dx 219 | ny = pos_y + dy 220 | n = 0 221 | if (nx not in range(d) or ny not in range(d)): 222 | continue 223 | while(board[opponent_color, nx, ny] == 1): 224 | tmp_nx = nx + dx 225 | tmp_ny = ny + dy 226 | if (tmp_nx not in range(d) or tmp_ny not in range(d)): 227 | break 228 | n += 1 229 | nx += dx 230 | ny += dy 231 | if(n > 0 and board[player_color, nx, ny] == 1): 232 | actions.append(pos_x*8+pos_y) 233 | if len(actions)==0: 234 | actions = [d**2 + 1] 235 | return actions 236 | 237 | @staticmethod 238 | def valid_reverse_opponent(board, coords, player_color): 239 | ''' 240 | check whether there is any reversible places 241 | 这里意思应该是 判断这里是否有 翻转的 棋子。 242 | ''' 243 | d = board.shape[-1] 244 | opponent_color = 1 - player_color 245 | pos_x = coords[0] 246 | pos_y = coords[1] 247 | for dx in [-1, 0, 1]: 248 | for dy in [-1, 0, 1]: 249 | if(dx == 0 and dy == 0): 250 | continue 251 | nx = pos_x + dx 252 | ny = pos_y + dy 253 | n = 0 254 | if (nx not in range(d) or ny not in range(d)): 255 | continue 256 | while(board[opponent_color, nx, ny] == 1): 257 | tmp_nx = nx + dx 258 | tmp_ny = ny + dy 259 | if (tmp_nx not in range(d) or tmp_ny not in range(d)): 260 | break 261 | n += 1 262 | nx += dx 263 | ny += dy 264 | if(n > 0 and board[player_color, nx, ny] == 1): 265 | return True 266 | return False 267 | 268 | @staticmethod 269 | def valid_place(board, action, player_color): 270 | coords = ReversiEnv.action_to_coordinate(board, action) 271 | # check whether there is any empty places 272 | if board[2, coords[0], coords[1]] == 1: 273 | # check whether there is any reversible places 274 | if ReversiEnv.valid_reverse_opponent(board, coords, player_color): 275 | return True 276 | else: 277 | return False 278 | else: 279 | return False 280 | 281 | @staticmethod 282 | def make_place(board, action, player_color): 283 | coords = ReversiEnv.action_to_coordinate(board, action) 284 | 285 | d = board.shape[-1] 286 | opponent_color = 1 - player_color 287 | pos_x = coords[0] 288 | pos_y = coords[1] 289 | 290 | for dx in [-1, 0, 1]: 291 | for dy in [-1, 0, 1]: 292 | if(dx == 0 and dy == 0): 293 | continue 294 | nx = pos_x + dx 295 | ny = pos_y + dy 296 | n = 0 297 | if (nx not in range(d) or ny not in range(d)): 298 | continue 299 | while(board[opponent_color, nx, ny] == 1): 300 | tmp_nx = nx + dx 301 | tmp_ny = ny + dy 302 | if (tmp_nx not in range(d) or tmp_ny not in range(d)): 303 | break 304 | n += 1 305 | nx += dx 306 | ny += dy 307 | if(n > 0 and board[player_color, nx, ny] == 1): 308 | nx = pos_x + dx 309 | ny = pos_y + dy 310 | while(board[opponent_color, nx, ny] == 1): 311 | board[2, nx, ny] = 0 312 | board[player_color, nx, ny] = 1 313 | board[opponent_color, nx, ny] = 0 314 | nx += dx 315 | ny += dy 316 | board[2, pos_x, pos_y] = 0 317 | board[player_color, pos_x, pos_y] = 1 318 | board[opponent_color, pos_x, pos_y] = 0 319 | return board 320 | 321 | @staticmethod 322 | def coordinate_to_action(board, coords): 323 | return coords[0] * board.shape[-1] + coords[1] 324 | 325 | @staticmethod 326 | def action_to_coordinate(board, action): 327 | return action // board.shape[-1], action % board.shape[-1] 328 | 329 | @staticmethod 330 | def game_finished(board): 331 | # Returns 1 if player 1 wins, -1 if player 2 wins and 0 otherwise 332 | d = board.shape[-1] 333 | 334 | player_score_x, player_score_y = np.where(board[0, :, :] == 1) 335 | player_score = len(player_score_x) 336 | opponent_score_x, opponent_score_y = np.where(board[1, :, :] == 1) 337 | opponent_score = len(opponent_score_x) 338 | if player_score == 0: 339 | return -1 340 | elif opponent_score == 0: 341 | return 1 342 | else: 343 | free_x, free_y = np.where(board[2, :, :] == 1) 344 | if free_x.size == 0: 345 | if player_score > (d**2)/2: 346 | return 1 347 | elif player_score == (d**2)/2: 348 | return 1 349 | else: 350 | return -1 351 | else: 352 | return 0 353 | return 0 354 | -------------------------------------------------------------------------------- /chap14_reinforcement_learning/reversi_main.py: -------------------------------------------------------------------------------- 1 | import gym 2 | import random 3 | import numpy as np 4 | 5 | from RL_QG_agent import RL_QG_agent 6 | 7 | env = gym.make('Reversi8x8-v0') 8 | env.reset() 9 | 10 | agent = RL_QG_agent() 11 | agent.load_model() 12 | 13 | max_epochs = 100 14 | 15 | for i_episode in range(max_epochs): 16 | observation = env.reset() 17 | # observation 是 3 x 8 x 8 的 list,表示当前的棋局,具体定义在 reversi.py 中的 state 18 | for t in range(100): 19 | action = [1,2] 20 | # action 包含 两个整型数字,action[0]表示下棋的位置,action[1] 表示下棋的颜色(黑棋0或者白棋1) 21 | 22 | ################### 黑棋 ############################### 0表示黑棋 23 | # 这部分 黑棋 是随机下棋 24 | env.render() # 打印当前棋局 25 | enables = env.possible_actions 26 | if len(enables) == 0: 27 | action_ = env.board_size**2 + 1 28 | else: 29 | action_ = random.choice(enables) 30 | action[0] = action_ 31 | action[1] = 0 # 黑棋 为 0 32 | observation, reward, done, info = env.step(action) 33 | ################### 白棋 ############################### 1表示白棋 34 | env.render() 35 | enables = env.possible_actions 36 | # if nothing to do ,select pass 37 | if len(enables) == 0: 38 | action_ = env.board_size ** 2 + 1 # pass 39 | else: 40 | action_ = agent.place(observation, enables) # 调用自己训练的模型 41 | action[0] = action_ 42 | action[1] = 1 # 白棋 为 1 43 | observation, reward, done, info = env.step(action) 44 | 45 | 46 | if done: # 游戏 结束 47 | print("Episode finished after {} timesteps".format(t+1)) 48 | black_score = len(np.where(env.state[0,:,:]==1)[0]) 49 | if black_score >32: 50 | print("黑棋赢了!") 51 | else: 52 | print("白棋赢了!") 53 | print(black_score) 54 | break -------------------------------------------------------------------------------- /chap14_reinforcement_learning/期末作业说明.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap14_reinforcement_learning/期末作业说明.pdf -------------------------------------------------------------------------------- /chap1_warmup/README.md: -------------------------------------------------------------------------------- 1 | # numpy exercise 2 | 3 | 4 | 5 | 6 | ## 数据集: 无 7 | 8 | 9 | 10 | ## 题目要求: 11 | 12 | ​ 按照ipython 文件中的 要求,利用numpy 实现对应的操作。 -------------------------------------------------------------------------------- /chap1_warmup/numpy_ tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# numpy 练习题" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": { 13 | "collapsed": true 14 | }, 15 | "source": [ 16 | " " 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "### numpy 的array操作" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "#### 1.导入numpy库" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": { 37 | "collapsed": true 38 | }, 39 | "outputs": [], 40 | "source": [] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "#### 2.建立一个一维数组 a 初始化为[4,5,6], (1)输出a 的类型(type)(2)输出a的各维度的大小(shape)(3)输出 a的第一个元素(值为4)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "#### 3.建立一个二维数组 b,初始化为 [ [4, 5, 6],[1, 2, 3]] (1)输出各维度的大小(shape)(2)输出 b(0,0),b(0,1),b(1,1) 这三个元素(对应值分别为4,5,2)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": { 69 | "collapsed": true 70 | }, 71 | "outputs": [], 72 | "source": [] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "#### 4. (1)建立一个全0矩阵 a, 大小为 3x3; 类型为整型(提示: dtype = int)(2)建立一个全1矩阵b,大小为4x5; (3)建立一个单位矩阵c ,大小为4x4; (4)生成一个随机数矩阵d,大小为 3x2." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "collapsed": true 86 | }, 87 | "outputs": [], 88 | "source": [] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "#### 5. 建立一个数组 a,(值为[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] ) ,(1)打印a; (2)输出 下标为(2,3),(0,0) 这两个数组元素的值" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": { 101 | "collapsed": true 102 | }, 103 | "outputs": [], 104 | "source": [] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "#### 6.把上一题的 a数组的 0到1行 2到3列,放到b里面去,(此处不需要从新建立a,直接调用即可)(1),输出b;(2) 输出b 的(0,0)这个元素的值" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": { 117 | "collapsed": true 118 | }, 119 | "outputs": [], 120 | "source": [] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | " #### 7. 把第5题中数组a的最后两行所有元素放到 c中,(提示: a[1:2, :])(1)输出 c ; (2) 输出 c 中第一行的最后一个元素(提示,使用 -1 表示最后一个元素)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": { 133 | "collapsed": true 134 | }, 135 | "outputs": [], 136 | "source": [] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "#### 8.建立数组a,初始化a为[[1, 2], [3, 4], [5, 6]],输出 (0,0)(1,1)(2,0)这三个元素(提示: 使用 print(a[[0, 1, 2], [0, 1, 0]]) )" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "#### 9.建立矩阵a ,初始化为[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],输出(0,0),(1,2),(2,0),(3,1) (提示使用 b = np.array([0, 2, 0, 1]) print(a[np.arange(4), b]))" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": { 165 | "collapsed": true 166 | }, 167 | "outputs": [], 168 | "source": [] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "#### 10.对9 中输出的那四个元素,每个都加上10,然后重新输出矩阵a.(提示: a[np.arange(4), b] += 10 )" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": { 181 | "collapsed": true 182 | }, 183 | "outputs": [], 184 | "source": [] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "### array 的数学运算" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "#### 11. 执行 x = np.array([1, 2]),然后输出 x 的数据类型" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": { 204 | "collapsed": true 205 | }, 206 | "outputs": [], 207 | "source": [] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "#### 12.执行 x = np.array([1.0, 2.0]) ,然后输出 x 的数据类类型" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": { 220 | "collapsed": true 221 | }, 222 | "outputs": [], 223 | "source": [] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "#### 13.执行 x = np.array([[1, 2], [3, 4]], dtype=np.float64) ,y = np.array([[5, 6], [7, 8]], dtype=np.float64),然后输出 x+y ,和 np.add(x,y)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": { 236 | "collapsed": true 237 | }, 238 | "outputs": [], 239 | "source": [] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "#### 14. 利用 13题目中的x,y 输出 x-y 和 np.subtract(x,y)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": null, 251 | "metadata": { 252 | "collapsed": true 253 | }, 254 | "outputs": [], 255 | "source": [] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "#### 15. 利用13题目中的x,y 输出 x*y ,和 np.multiply(x, y) 还有 np.dot(x,y),比较差异。然后自己换一个不是方阵的试试。" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": { 268 | "collapsed": true 269 | }, 270 | "outputs": [], 271 | "source": [] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "#### 16. 利用13题目中的x,y,输出 x / y .(提示 : 使用函数 np.divide())" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "#### 17. 利用13题目中的x,输出 x的 开方。(提示: 使用函数 np.sqrt() )" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": null, 297 | "metadata": { 298 | "collapsed": true 299 | }, 300 | "outputs": [], 301 | "source": [] 302 | }, 303 | { 304 | "cell_type": "markdown", 305 | "metadata": {}, 306 | "source": [ 307 | "#### 18.利用13题目中的x,y ,执行 print(x.dot(y)) 和 print(np.dot(x,y))" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": { 314 | "collapsed": true 315 | }, 316 | "outputs": [], 317 | "source": [] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "##### 19.利用13题目中的 x,进行求和。提示:输出三种求和 (1)print(np.sum(x)): (2)print(np.sum(x,axis =0 )); (3)print(np.sum(x,axis = 1))" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "metadata": { 330 | "collapsed": true 331 | }, 332 | "outputs": [], 333 | "source": [] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": {}, 338 | "source": [ 339 | "#### 20.利用13题目中的 x,进行求平均数(提示:输出三种平均数(1)print(np.mean(x)) (2)print(np.mean(x,axis = 0))(3) print(np.mean(x,axis =1)))" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": null, 345 | "metadata": { 346 | "collapsed": true 347 | }, 348 | "outputs": [], 349 | "source": [] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "#### 21.利用13题目中的x,对x 进行矩阵转置,然后输出转置后的结果,(提示: x.T 表示对 x 的转置)" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": { 362 | "collapsed": true 363 | }, 364 | "outputs": [], 365 | "source": [] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": { 370 | "collapsed": true 371 | }, 372 | "source": [ 373 | "#### 22.利用13题目中的x,求e的指数(提示: 函数 np.exp())" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": null, 379 | "metadata": { 380 | "collapsed": true 381 | }, 382 | "outputs": [], 383 | "source": [] 384 | }, 385 | { 386 | "cell_type": "markdown", 387 | "metadata": {}, 388 | "source": [ 389 | "#### 23.利用13题目中的 x,求值最大的下标(提示(1)print(np.argmax(x)) ,(2) print(np.argmax(x, axis =0))(3)print(np.argmax(x),axis =1))" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": null, 395 | "metadata": {}, 396 | "outputs": [], 397 | "source": [] 398 | }, 399 | { 400 | "cell_type": "markdown", 401 | "metadata": {}, 402 | "source": [ 403 | "#### 24,画图,y=x*x 其中 x = np.arange(0, 100, 0.1) (提示这里用到 matplotlib.pyplot 库)" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": null, 409 | "metadata": { 410 | "collapsed": true 411 | }, 412 | "outputs": [], 413 | "source": [] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "#### 25.画图。画正弦函数和余弦函数, x = np.arange(0, 3 * np.pi, 0.1)(提示:这里用到 np.sin() np.cos() 函数和 matplotlib.pyplot 库)" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": null, 425 | "metadata": { 426 | "collapsed": true 427 | }, 428 | "outputs": [], 429 | "source": [] 430 | } 431 | ], 432 | "metadata": { 433 | "anaconda-cloud": {}, 434 | "kernelspec": { 435 | "display_name": "Python 3", 436 | "language": "python", 437 | "name": "python3" 438 | }, 439 | "language_info": { 440 | "codemirror_mode": { 441 | "name": "ipython", 442 | "version": 3 443 | }, 444 | "file_extension": ".py", 445 | "mimetype": "text/x-python", 446 | "name": "python", 447 | "nbconvert_exporter": "python", 448 | "pygments_lexer": "ipython3", 449 | "version": "3.7.0" 450 | } 451 | }, 452 | "nbformat": 4, 453 | "nbformat_minor": 1 454 | } 455 | -------------------------------------------------------------------------------- /chap2_linear_regression/README.md: -------------------------------------------------------------------------------- 1 | # 线性回归 2 | 3 | 4 | 5 | ## 问题描述: 6 | 7 | 有一个函数![image](http://latex.codecogs.com/gif.latex?f%3A%20%5Cmathbb%7BR%7D%5Crightarrow%20%5Cmathbb%7BR%7D) ,使得。现 ![image](http://latex.codecogs.com/gif.latex?y%20%3D%20f%28x%29)在不知道函数 $f(\cdot)$的具体形式,给定满足函数关系的一组训练样本![image](http://latex.codecogs.com/gif.latex?%5Cleft%20%5C%7B%20%5Cleft%20%28%20x_%7B1%7D%2Cy_%7B1%7D%20%5Cright%20%29%2C...%2C%5Cleft%20%28%20x_%7BN%7D%2Cy_%7BN%7D%20%5Cright%20%29%20%5Cright%20%5C%7D%2CN%3D300),请使用线性回归模型拟合出函数$y=f(x)$。 8 | 9 | (可尝试一种或几种不同的基函数,如多项式、高斯或sigmoid基函数) 10 | 11 | 12 | 13 | 14 | ## 数据集: 15 | 16 | 根据某种函数关系生成的train 和test 数据。 17 | 18 | 19 | 20 | ## 题目要求: 21 | 22 | - [ ] 按顺序完成 `exercise-linear_regression.ipynb`中的填空 23 | 1. 先完成最小二乘法的优化 (参考书中第二章 2.3节中的公式) 24 | 1. 附加题:实现“多项式基函数”以及“高斯基函数”(可参考PRML) 25 | 1. 附加题:完成梯度下降的优化 (参考书中第二章 2.3节中的公式) 26 | 27 | - [ ] 参照`lienar_regression-tf2.0.ipnb`使用tensorflow2.0 使用梯度下降完成线性回归 28 | - [ ] 使用训练集train.txt 进行训练,使用测试集test.txt 进行评估(标准差),训练模型时请不要使用测试集。 29 | 30 | -------------------------------------------------------------------------------- /chap2_linear_regression/exercise-linear_regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 说明\n", 8 | "\n", 9 | "请按照填空顺序编号分别完成 参数优化,不同基函数的实现" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "import matplotlib.pyplot as plt\n", 20 | "\n", 21 | "def load_data(filename):\n", 22 | " \"\"\"载入数据。\"\"\"\n", 23 | " xys = []\n", 24 | " with open(filename, 'r') as f:\n", 25 | " for line in f:\n", 26 | " xys.append(map(float, line.strip().split()))\n", 27 | " xs, ys = zip(*xys)\n", 28 | " return np.asarray(xs), np.asarray(ys)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## 不同的基函数 (basis function)的实现 填空顺序 2\n", 36 | "\n", 37 | "请分别在这里实现“多项式基函数”以及“高斯基函数”\n", 38 | "\n", 39 | "其中以及训练集的x的范围在0-25之间" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 6, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "def identity_basis(x):\n", 49 | " ret = np.expand_dims(x, axis=1)\n", 50 | " return ret\n", 51 | "\n", 52 | "def multinomial_basis(x, feature_num=10):\n", 53 | " '''多项式基函数'''\n", 54 | " x = np.expand_dims(x, axis=1) # shape(N, 1)\n", 55 | " #==========\n", 56 | " #todo '''请实现多项式基函数'''\n", 57 | " #==========\n", 58 | " ret = None\n", 59 | " return ret\n", 60 | "\n", 61 | "def gaussian_basis(x, feature_num=10):\n", 62 | " '''高斯基函数'''\n", 63 | " #==========\n", 64 | " #todo '''请实现高斯基函数'''\n", 65 | " #==========\n", 66 | " ret = None\n", 67 | " return ret" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "## 返回一个训练好的模型 填空顺序 1 用最小二乘法进行模型优化 \n", 75 | "## 填空顺序 3 用梯度下降进行模型优化\n", 76 | "> 先完成最小二乘法的优化 (参考书中第二章 2.3中的公式)\n", 77 | "\n", 78 | "> 再完成梯度下降的优化 (参考书中第二章 2.3中的公式)\n", 79 | "\n", 80 | "在main中利用训练集训练好模型的参数,并且返回一个训练好的模型。\n", 81 | "\n", 82 | "计算出一个优化后的w,请分别使用最小二乘法以及梯度下降两种办法优化w" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 7, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "def main(x_train, y_train):\n", 92 | " \"\"\"\n", 93 | " 训练模型,并返回从x到y的映射。\n", 94 | " \n", 95 | " \"\"\"\n", 96 | " basis_func = identity_basis\n", 97 | " phi0 = np.expand_dims(np.ones_like(x_train), axis=1)\n", 98 | " phi1 = basis_func(x_train)\n", 99 | " phi = np.concatenate([phi0, phi1], axis=1)\n", 100 | " \n", 101 | " \n", 102 | " #==========\n", 103 | " #todo '''计算出一个优化后的w,请分别使用最小二乘法以及梯度下降两种办法优化w'''\n", 104 | " #==========\n", 105 | " \n", 106 | " def f(x):\n", 107 | " phi0 = np.expand_dims(np.ones_like(x), axis=1)\n", 108 | " phi1 = basis_func(x)\n", 109 | " phi = np.concatenate([phi0, phi1], axis=1)\n", 110 | " y = np.dot(phi, w)\n", 111 | " return y\n", 112 | "\n", 113 | " return f" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "## 评估结果 \n", 121 | "> 没有需要填写的代码,但是建议读懂" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "def evaluate(ys, ys_pred):\n", 131 | " \"\"\"评估模型。\"\"\"\n", 132 | " std = np.sqrt(np.mean(np.abs(ys - ys_pred) ** 2))\n", 133 | " return std\n", 134 | "\n", 135 | "# 程序主入口(建议不要改动以下函数的接口)\n", 136 | "if __name__ == '__main__':\n", 137 | " train_file = 'train.txt'\n", 138 | " test_file = 'test.txt'\n", 139 | " # 载入数据\n", 140 | " x_train, y_train = load_data(train_file)\n", 141 | " x_test, y_test = load_data(test_file)\n", 142 | " print(x_train.shape)\n", 143 | " print(x_test.shape)\n", 144 | "\n", 145 | " # 使用线性回归训练模型,返回一个函数f()使得y = f(x)\n", 146 | " f = main(x_train, y_train)\n", 147 | "\n", 148 | " y_train_pred = f(x_train)\n", 149 | " std = evaluate(y_train, y_train_pred)\n", 150 | " print('训练集预测值与真实值的标准差:{:.1f}'.format(std))\n", 151 | " \n", 152 | " # 计算预测的输出值\n", 153 | " y_test_pred = f(x_test)\n", 154 | " # 使用测试集评估模型\n", 155 | " std = evaluate(y_test, y_test_pred)\n", 156 | " print('预测值与真实值的标准差:{:.1f}'.format(std))\n", 157 | "\n", 158 | " #显示结果\n", 159 | " plt.plot(x_train, y_train, 'ro', markersize=3)\n", 160 | "# plt.plot(x_test, y_test, 'k')\n", 161 | " plt.plot(x_test, y_test_pred, 'k')\n", 162 | " plt.xlabel('x')\n", 163 | " plt.ylabel('y')\n", 164 | " plt.title('Linear Regression')\n", 165 | " plt.legend(['train', 'test', 'pred'])\n", 166 | " plt.show()" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [] 189 | } 190 | ], 191 | "metadata": { 192 | "kernelspec": { 193 | "display_name": "Python 3", 194 | "language": "python", 195 | "name": "python3" 196 | }, 197 | "language_info": { 198 | "codemirror_mode": { 199 | "name": "ipython", 200 | "version": 3 201 | }, 202 | "file_extension": ".py", 203 | "mimetype": "text/x-python", 204 | "name": "python", 205 | "nbconvert_exporter": "python", 206 | "pygments_lexer": "ipython3", 207 | "version": "3.7.0" 208 | }, 209 | "pycharm": { 210 | "stem_cell": { 211 | "cell_type": "raw", 212 | "source": [], 213 | "metadata": { 214 | "collapsed": false 215 | } 216 | } 217 | } 218 | }, 219 | "nbformat": 4, 220 | "nbformat_minor": 2 221 | } -------------------------------------------------------------------------------- /chap2_linear_regression/test.txt: -------------------------------------------------------------------------------- 1 | 0.10000 0.39950 2 | 0.20000 0.79601 3 | 0.45000 1.75490 4 | 0.60000 2.29393 5 | 0.85000 3.10384 6 | 0.95000 3.39025 7 | 1.15000 3.88829 8 | 1.20000 3.99612 9 | 1.30000 4.19067 10 | 1.45000 4.42814 11 | 1.50000 4.49248 12 | 1.60000 4.59872 13 | 1.65000 4.64060 14 | 1.80000 4.72154 15 | 1.90000 4.73890 16 | 2.00000 4.72789 17 | 2.05000 4.71209 18 | 2.10000 4.68963 19 | 2.15000 4.66070 20 | 2.20000 4.62549 21 | 2.25000 4.58422 22 | 2.95000 3.52127 23 | 3.00000 3.42336 24 | 3.15000 3.12478 25 | 3.25000 2.92541 26 | 3.30000 2.82676 27 | 3.35000 2.72929 28 | 3.60000 2.27244 29 | 3.75000 2.03532 30 | 3.85000 1.89812 31 | 3.90000 1.83670 32 | 4.25000 1.56503 33 | 4.30000 1.55150 34 | 4.50000 1.56741 35 | 4.65000 1.65584 36 | 5.00000 2.12323 37 | 5.10000 2.32256 38 | 5.15000 2.43270 39 | 5.30000 2.80320 40 | 5.35000 2.93944 41 | 5.60000 3.70620 42 | 5.65000 3.87485 43 | 6.25000 6.15046 44 | 6.30000 6.35044 45 | 6.35000 6.55029 46 | 6.40000 6.74965 47 | 6.45000 6.94813 48 | 6.70000 7.91455 49 | 6.85000 8.46084 50 | 7.10000 9.28691 51 | 7.20000 9.58100 52 | 7.25000 9.71924 53 | 7.40000 10.09612 54 | 7.50000 10.31400 55 | 7.55000 10.41246 56 | 7.60000 10.50376 57 | 7.95000 10.93618 58 | 8.20000 11.02219 59 | 8.25000 11.01781 60 | 8.45000 10.93273 61 | 8.50000 10.89546 62 | 8.55000 10.85220 63 | 8.70000 10.68891 64 | 9.00000 10.23636 65 | 9.05000 10.14820 66 | 9.25000 9.77167 67 | 9.35000 9.57412 68 | 9.40000 9.47433 69 | 9.45000 9.37434 70 | 9.65000 8.98003 71 | 9.95000 8.44578 72 | 10.05000 8.29417 73 | 10.35000 7.95374 74 | 10.40000 7.91652 75 | 10.45000 7.88551 76 | 10.50000 7.86091 77 | 10.65000 7.82736 78 | 10.70000 7.83009 79 | 10.75000 7.84001 80 | 10.85000 7.88173 81 | 10.90000 7.91369 82 | 11.00000 8.00003 83 | 11.05000 8.05444 84 | 11.10000 8.11634 85 | 11.15000 8.18570 86 | 11.20000 8.26247 87 | 11.25000 8.34658 88 | 11.45000 8.75446 89 | 11.65000 9.26981 90 | 11.80000 9.71942 91 | 11.90000 10.04559 92 | 12.15000 10.93667 93 | 12.30000 11.51030 94 | 12.35000 11.70594 95 | 12.40000 11.90319 96 | 12.95000 14.07287 97 | 13.05000 14.44499 98 | 13.10000 14.62598 99 | 13.25000 15.14483 100 | 13.40000 15.62113 101 | 13.45000 15.76914 102 | 13.55000 16.04754 103 | 13.65000 16.30099 104 | 13.70000 16.41786 105 | 13.75000 16.52795 106 | 13.95000 16.89761 107 | 14.05000 17.03861 108 | 14.10000 17.09793 109 | 14.15000 17.14975 110 | 14.30000 17.26032 111 | 14.35000 17.28231 112 | 14.45000 17.30440 113 | 14.55000 17.29796 114 | 14.80000 17.16476 115 | 14.85000 17.11954 116 | 14.95000 17.01233 117 | 15.05000 16.88452 118 | 15.50000 16.11940 119 | 16.00000 15.13629 120 | 16.10000 14.95379 121 | 16.25000 14.70235 122 | 16.45000 14.42263 123 | 16.55000 14.31200 124 | 16.60000 14.26494 125 | 16.65000 14.22373 126 | 16.75000 14.15970 127 | 16.80000 14.13730 128 | 16.95000 14.11067 129 | 17.00000 14.11581 130 | 17.10000 14.14780 131 | 17.15000 14.17483 132 | 17.35000 14.35761 133 | 17.50000 14.57312 134 | 17.75000 15.07698 135 | 18.00000 15.74704 136 | 18.05000 15.89886 137 | 18.15000 16.21837 138 | 18.20000 16.38550 139 | 18.25000 16.55717 140 | 18.55000 17.66471 141 | 18.65000 18.05530 142 | 18.70000 18.25300 143 | 18.75000 18.45183 144 | 18.80000 18.65139 145 | 18.95000 19.25083 146 | 19.05000 19.64731 147 | 19.10000 19.84350 148 | 19.15000 20.03783 149 | 19.35000 20.78945 150 | 19.55000 21.48367 151 | 19.75000 22.10081 152 | 19.80000 22.24102 153 | 19.85000 22.37513 154 | 19.90000 22.50293 155 | 19.95000 22.62423 156 | 20.00000 22.73884 157 | 20.20000 23.12746 158 | 20.25000 23.20658 159 | 20.30000 23.27830 160 | 20.35000 23.34258 161 | 20.50000 23.49049 162 | 20.60000 23.55172 163 | 20.75000 23.58847 164 | 21.10000 23.43338 165 | 21.30000 23.21227 166 | 21.45000 22.99536 167 | 21.50000 22.91492 168 | 21.60000 22.74375 169 | 21.65000 22.65371 170 | 21.75000 22.46645 171 | 21.85000 22.27204 172 | 21.90000 22.17307 173 | 21.95000 22.07341 174 | 22.00000 21.97345 175 | 22.05000 21.87355 176 | 22.25000 21.48209 177 | 22.40000 21.20733 178 | 22.60000 20.88422 179 | 22.65000 20.81337 180 | 22.75000 20.68573 181 | 22.85000 20.57872 182 | 22.90000 20.53361 183 | 22.95000 20.49440 184 | 23.15000 20.40097 185 | 23.20000 20.39437 186 | 23.35000 20.41713 187 | 23.45000 20.46878 188 | 23.50000 20.50575 189 | 23.55000 20.55021 190 | 23.90000 21.06980 191 | 24.10000 21.52388 192 | 24.15000 21.65394 193 | 24.20000 21.79023 194 | 24.25000 21.93255 195 | 24.50000 22.72593 196 | 24.70000 23.44192 197 | 24.75000 23.62961 198 | 24.80000 23.82009 199 | 24.85000 24.01303 200 | 24.90000 24.20806 201 | -------------------------------------------------------------------------------- /chap2_linear_regression/train.txt: -------------------------------------------------------------------------------- 1 | 2.65000 4.06609 2 | 4.70000 1.70023 3 | 14.25000 17.23092 4 | 22.10000 21.77409 5 | 10.55000 7.84291 6 | 6.50000 7.14536 7 | 4.10000 1.64517 8 | 24.65000 23.25737 9 | 24.40000 22.39327 10 | 22.80000 20.62952 11 | 0.90000 3.24998 12 | 11.30000 8.43794 13 | 21.80000 22.36996 14 | 22.55000 20.95936 15 | 19.20000 20.22994 16 | 21.40000 23.07195 17 | 11.70000 9.41405 18 | 5.75000 4.22516 19 | 15.15000 16.73838 20 | 7.65000 10.58780 21 | 7.45000 10.20851 22 | 1.95000 4.73688 23 | 18.35000 16.91289 24 | 19.30000 20.60610 25 | 2.80000 3.80496 26 | 11.35000 8.53646 27 | 23.00000 20.46134 28 | 0.25000 0.99221 29 | 5.70000 4.04794 30 | 8.30000 11.00652 31 | 0.40000 1.56826 32 | 0.15000 0.59831 33 | 24.60000 23.07631 34 | 0.75000 2.79492 35 | 9.10000 10.05730 36 | 20.65000 23.57124 37 | 12.85000 13.68953 38 | 19.50000 21.31662 39 | 15.40000 16.30936 40 | 20.45000 23.44868 41 | 21.05000 23.47470 42 | 19.00000 19.44963 43 | 12.65000 12.90060 44 | 18.50000 17.47256 45 | 15.10000 16.81359 46 | 5.25000 2.67320 47 | 17.30000 14.30068 48 | 17.85000 15.32631 49 | 3.50000 2.44765 50 | 1.05000 3.65227 51 | 19.25000 20.41948 52 | 9.90000 8.52739 53 | 21.15000 23.38623 54 | 0.80000 2.95207 55 | 15.95000 15.23096 56 | 3.10000 3.22474 57 | 12.75000 13.29780 58 | 15.45000 16.21534 59 | 20.15000 23.04103 60 | 6.80000 8.28234 61 | 5.95000 4.96884 62 | 5.45000 3.22977 63 | 9.60000 9.07702 64 | 8.65000 10.74867 65 | 9.80000 8.70056 66 | 15.70000 15.72389 67 | 21.00000 23.50997 68 | 1.75000 4.70196 69 | 24.35000 22.23432 70 | 6.75000 8.10013 71 | 15.55000 16.02192 72 | 17.45000 14.49388 73 | 6.05000 5.35677 74 | 18.40000 17.09630 75 | 16.50000 14.36464 76 | 4.60000 1.61893 77 | 6.20000 5.95073 78 | 17.25000 14.25124 79 | 3.40000 2.63338 80 | 9.85000 8.61243 81 | 12.80000 13.49453 82 | 7.85000 10.84998 83 | 20.55000 23.52482 84 | 1.35000 4.27717 85 | 22.70000 20.74711 86 | 1.00000 3.52441 87 | 4.20000 1.58527 88 | 15.85000 15.42532 89 | 4.80000 1.81151 90 | 17.90000 15.46053 91 | 13.15000 14.80317 92 | 20.40000 23.39938 93 | 21.70000 22.56116 94 | 13.60000 16.17749 95 | 6.60000 7.53462 96 | 19.65000 21.80300 97 | 13.35000 15.46756 98 | 8.85000 10.48095 99 | 10.80000 7.85719 100 | 0.55000 2.11806 101 | 4.90000 1.95264 102 | 17.70000 14.96225 103 | 15.20000 16.65920 104 | 8.90000 10.40306 105 | 22.35000 21.29640 106 | 2.35000 4.48442 107 | 11.50000 8.87364 108 | 7.05000 9.13153 109 | 16.35000 14.55352 110 | 22.30000 21.38811 111 | 17.65000 14.85437 112 | 13.20000 14.97622 113 | 21.35000 23.14435 114 | 3.55000 2.35856 115 | 3.65000 2.18964 116 | 5.55000 3.54228 117 | 11.95000 10.21577 118 | 23.25000 20.39478 119 | 2.90000 3.61775 120 | 2.30000 4.53712 121 | 3.70000 2.11049 122 | 10.95000 7.95311 123 | 5.50000 3.38338 124 | 12.55000 12.50089 125 | 10.15000 8.16010 126 | 6.90000 8.63532 127 | 1.40000 4.35635 128 | 24.05000 21.40026 129 | 15.25000 16.57637 130 | 0.00000 0.00000 131 | 20.95000 23.53896 132 | 3.05000 3.32439 133 | 16.90000 14.11263 134 | 8.75000 10.62417 135 | 17.40000 14.42202 136 | 15.75000 15.62393 137 | 15.30000 16.49022 138 | 4.40000 1.54519 139 | 6.65000 7.72593 140 | 13.90000 16.81602 141 | 17.95000 15.60085 142 | 7.00000 8.97096 143 | 9.20000 9.86867 144 | 0.70000 2.63265 145 | 4.95000 2.03429 146 | 16.40000 14.48568 147 | 0.65000 2.46556 148 | 5.80000 4.40619 149 | 8.40000 10.96380 150 | 9.70000 8.88472 151 | 11.85000 9.88004 152 | 16.30000 14.62584 153 | 2.60000 4.14650 154 | 15.65000 15.82379 155 | 23.10000 20.41444 156 | 8.35000 10.98845 157 | 20.85000 23.57734 158 | 8.10000 11.00967 159 | 14.50000 17.30469 160 | 18.85000 18.85133 161 | 23.80000 20.88460 162 | 4.55000 1.58947 163 | 14.00000 16.97182 164 | 11.60000 9.13151 165 | 5.85000 4.59071 166 | 2.55000 4.22305 167 | 3.95000 1.78044 168 | 18.30000 16.73307 169 | 1.25000 4.09695 170 | 10.25000 8.04590 171 | 2.85000 3.71243 172 | 12.60000 12.70087 173 | 19.45000 21.14503 174 | 20.05000 22.84660 175 | 20.90000 23.56147 176 | 7.30000 9.85131 177 | 4.05000 1.68442 178 | 14.60000 17.28437 179 | 7.75000 10.73380 180 | 11.55000 8.99939 181 | 17.60000 14.75347 182 | 8.95000 10.32142 183 | 19.60000 21.64589 184 | 12.50000 12.30103 185 | 14.40000 17.29697 186 | 8.15000 11.01952 187 | 17.80000 15.19839 188 | 4.75000 1.75212 189 | 7.80000 10.79563 190 | 9.50000 9.27455 191 | 4.45000 1.55268 192 | 13.80000 16.63109 193 | 2.50000 4.29542 194 | 11.75000 9.56401 195 | 18.45000 17.28297 196 | 6.10000 5.55351 197 | 6.55000 7.34098 198 | 20.70000 23.58346 199 | 22.20000 21.57799 200 | 23.30000 20.40234 201 | 20.80000 23.58639 202 | 8.60000 10.80319 203 | 12.10000 10.75106 204 | 5.05000 2.21935 205 | 16.20000 14.78273 206 | 13.00000 14.26050 207 | 15.60000 15.92326 208 | 9.55000 9.17531 209 | 14.20000 17.19408 210 | 7.90000 10.89682 211 | 6.95000 8.80546 212 | 14.65000 17.26407 213 | 10.00000 8.36794 214 | 3.45000 2.53938 215 | 1.10000 3.77362 216 | 23.70000 20.72854 217 | 3.20000 3.02488 218 | 12.05000 10.56882 219 | 15.35000 16.40110 220 | 12.90000 13.88242 221 | 10.30000 7.99694 222 | 23.05000 20.43462 223 | 20.10000 22.94737 224 | 1.70000 4.67499 225 | 16.70000 14.18857 226 | 4.35000 1.54484 227 | 16.15000 14.86666 228 | 4.00000 1.72959 229 | 12.25000 11.31664 230 | 18.90000 19.05127 231 | 2.40000 4.42639 232 | 7.15000 9.43681 233 | 24.95000 24.40482 234 | 23.85000 20.97361 235 | 4.15000 1.61205 236 | 19.70000 21.95472 237 | 4.85000 1.87836 238 | 10.20000 8.10038 239 | 2.70000 3.98214 240 | 12.20000 11.12531 241 | 12.00000 10.39028 242 | 9.15000 9.96400 243 | 14.75000 17.20407 244 | 10.60000 7.83167 245 | 23.40000 20.43925 246 | 6.00000 5.16175 247 | 23.75000 20.80289 248 | 21.55000 22.83094 249 | 23.95000 21.17306 250 | 12.70000 13.09970 251 | 9.75000 8.79144 252 | 22.50000 21.03848 253 | 8.00000 10.96807 254 | 2.75000 3.89498 255 | 16.05000 15.04378 256 | 13.85000 16.72715 257 | 21.25000 23.27541 258 | 0.50000 1.93828 259 | 3.80000 1.96443 260 | 17.05000 14.12815 261 | 1.85000 4.73383 262 | 24.55000 22.89906 263 | 9.30000 9.67336 264 | 5.40000 3.08171 265 | 7.70000 10.66450 266 | 15.00000 16.95086 267 | 12.45000 12.10168 268 | 11.40000 8.64201 269 | 13.30000 15.30871 270 | 2.45000 4.36329 271 | 8.05000 10.99255 272 | 15.90000 15.32742 273 | 17.20000 14.20930 274 | 21.20000 23.33348 275 | 22.15000 21.67545 276 | 19.40000 20.96920 277 | 24.45000 22.55723 278 | 13.50000 15.91135 279 | 23.65000 20.66162 280 | 5.90000 4.77837 281 | 0.05000 0.19994 282 | 0.35000 1.37869 283 | 17.55000 14.65968 284 | 18.60000 17.85908 285 | 8.80000 10.55475 286 | 22.45000 21.12124 287 | 6.15000 5.75162 288 | 24.00000 21.28326 289 | 15.80000 15.52428 290 | 14.70000 17.23724 291 | 18.10000 16.05606 292 | 23.60000 20.60217 293 | 16.85000 14.12155 294 | 0.30000 1.18656 295 | 14.90000 17.06864 296 | 24.30000 22.08066 297 | 7.35000 9.97700 298 | 5.20000 2.54964 299 | 10.10000 8.22479 300 | 1.55000 4.54935 301 | -------------------------------------------------------------------------------- /chap3_SVM/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 支持向量机(SVM) 4 | 5 | 6 | 7 | ## 问题描述: 8 | 9 | 本次作业分为三个部分: 10 | 11 | 1. 使用基于某种核函数(线性,多项式或高斯核函数)的SVM 解决非线性可分的二分类问题,数 12 | 据集为train_kernel.txt 及test_kernel.txt。 13 | 14 | 2. 分别使用线性分类器(squared error)、logistic 回归(cross entropy error)以及SVM(hinge error) 解 15 | 决线性二分类问题,并比较三种模型的效果。数据集为train_linear.txt 及test_linear.txt。 16 | 三种误差函数定义如下(Bishop P327): 17 | ![image](http://latex.codecogs.com/gif.latex?E_%7Blinear%7D%3D%5Csum_%7Bn%3D1%7D%5E%7BN%7D%28y_%7Bn%7D%20-t_%7Bn%7D%29%5E%7B2%7D+%5Clambda%20%5Cleft%20%5C%7C%20%5Cmathbf%7Bw%7D%20%5Cright%20%5C%7C%5E%7B2%7D) 18 | 19 | ![image](http://latex.codecogs.com/gif.latex?E_%7Blogistic%7D%3D%5Csum_%7Bn%3D1%7D%5E%7BN%7Dlog%281+exp%28-y_%7Bn%7Dt_%7Bn%7D%29%29%20+%20%5Clambda%5Cleft%20%5C%7C%20%5Cmathbf%7Bw%7D%20%5Cright%20%5C%7C%5E%7B2%7D) 20 | 21 | ![image](http://latex.codecogs.com/gif.latex?E_%7BSVM%7D%3D%5Csum_%7Bn%3D1%7D%5E%7BN%7D%5B1-y_%7Bn%7Dt_%7Bn%7D%5D+%5Clambda%20%5Cleft%20%5C%7C%20%5Cmathbf%7Bw%7D%20%5Cright%20%5C%7C%5E%7B2%7D) 22 | 23 | 24 | ​ 25 | 其中![image](http://latex.codecogs.com/gif.latex?y_%7Bn%7D%3D%5Cmathbf%7Bw%7D%5E%7BT%7Dx_%7Bn%7D+b),![image](http://latex.codecogs.com/gif.latex?t_%7Bn%7D) 为类别标签。 26 | 27 | 3. 使用多分类SVM 解决三分类问题。数据集为train_multi.txt 及test_multi.txt。(5%) 28 | 29 | 30 | 31 | 32 | 33 | ## 数据集: 34 | 35 | MNIST数据集包括60000张训练图片和10000张测试图片。图片样本的数量已经足够训练一个很复杂的模型(例如 CNN的深层神经网络)。它经常被用来作为一个新 的模式识别模型的测试用例。而且它也是一个方便学生和研究者们执行用例的数据集。除此之外,MNIST数据集是一个相对较小的数据集,可以在你的笔记本CPUs上面直接执行 36 | 37 | 38 | 39 | 40 | 41 | ## 题目要求: 42 | 43 | - [ ] 请使用代码模板rbm.py,补全缺失部分,尽量不改动主体部分。 44 | - [ ] 推荐使用python 及numpy 编写主要逻辑代码,适度使用框架。 45 | -------------------------------------------------------------------------------- /chap3_SVM/data/test_kernel.txt: -------------------------------------------------------------------------------- 1 | x1 x2 t 2 | 12.04 4.55 1 3 | 9.58 7.76 1 4 | 8.28 14.45 1 5 | 2.24 3.66 1 6 | 13.79 7.51 1 7 | 2.47 2.25 1 8 | 10.14 14.23 1 9 | 6.86 0.81 1 10 | 8.11 11.16 1 11 | 7.00 15.14 1 12 | 0.44 7.47 1 13 | 4.76 9.59 1 14 | 6.44 5.45 1 15 | 8.20 10.23 1 16 | 8.69 3.76 1 17 | 9.30 11.59 1 18 | 5.03 0.07 1 19 | 6.07 2.63 1 20 | 15.94 9.05 1 21 | 11.03 3.03 1 22 | 5.71 0.48 1 23 | 13.75 7.77 1 24 | 11.97 9.11 1 25 | 6.78 6.57 1 26 | 11.56 13.02 1 27 | 9.68 2.93 1 28 | 4.57 6.92 1 29 | 4.45 3.26 1 30 | 2.65 5.51 1 31 | 15.62 6.95 1 32 | 0.24 2.48 1 33 | 12.44 3.18 1 34 | 2.96 0.88 1 35 | 10.38 2.51 1 36 | 0.58 8.23 1 37 | 3.92 13.08 1 38 | 10.84 0.78 1 39 | 9.53 1.26 1 40 | 7.56 7.58 1 41 | 1.65 11.78 1 42 | 6.96 0.47 1 43 | 14.44 7.88 1 44 | 13.82 3.94 1 45 | 0.20 0.23 1 46 | 5.95 10.28 1 47 | 3.63 13.24 1 48 | 2.48 11.45 1 49 | 1.88 8.86 1 50 | 7.53 5.39 1 51 | 5.97 9.17 1 52 | 11.53 13.03 1 53 | 12.36 9.92 1 54 | 6.30 5.72 1 55 | 7.82 14.23 1 56 | 0.91 2.68 1 57 | 3.84 10.11 1 58 | 11.69 1.08 1 59 | 14.41 7.65 1 60 | 7.09 0.50 1 61 | 10.59 6.26 1 62 | 1.36 4.77 1 63 | 4.85 13.58 1 64 | 5.26 0.15 1 65 | 7.28 10.90 1 66 | 7.45 1.54 1 67 | 7.97 2.54 1 68 | 10.51 9.87 1 69 | 2.53 9.81 1 70 | 2.88 13.63 1 71 | 6.32 8.90 1 72 | 6.19 0.63 1 73 | 2.15 12.64 1 74 | 3.60 7.35 1 75 | 12.34 4.68 1 76 | 9.05 4.30 1 77 | 0.20 6.32 1 78 | 6.60 9.19 1 79 | 0.10 9.78 1 80 | 1.64 4.67 1 81 | 5.17 10.36 1 82 | 14.85 4.88 1 83 | 14.25 11.83 1 84 | 15.36 5.55 1 85 | 5.09 0.67 1 86 | 14.26 3.67 1 87 | 12.63 11.35 1 88 | 7.89 9.46 1 89 | 11.98 3.63 1 90 | 6.43 0.32 1 91 | 4.44 0.51 1 92 | 10.39 1.99 1 93 | 12.80 8.45 1 94 | 9.74 0.16 1 95 | 4.79 4.47 1 96 | 9.15 1.93 1 97 | 1.60 0.29 1 98 | 0.64 8.16 1 99 | 3.66 9.11 1 100 | 7.68 5.61 1 101 | 9.72 13.88 1 102 | 13.42 2.52 -1 103 | 13.83 21.37 -1 104 | 6.24 15.74 -1 105 | 6.79 19.05 -1 106 | 17.87 3.86 -1 107 | 23.66 10.33 -1 108 | 18.89 20.20 -1 109 | 15.15 15.16 -1 110 | 10.11 17.02 -1 111 | 22.27 7.46 -1 112 | 0.15 16.73 -1 113 | 7.12 20.94 -1 114 | 5.42 21.19 -1 115 | 15.23 4.52 -1 116 | 0.80 11.87 -1 117 | 21.70 5.36 -1 118 | 8.60 19.94 -1 119 | 2.98 19.75 -1 120 | 12.61 19.24 -1 121 | 12.86 23.67 -1 122 | 21.25 3.82 -1 123 | 9.42 24.58 -1 124 | 17.29 11.79 -1 125 | 21.10 5.90 -1 126 | 13.20 24.62 -1 127 | 5.65 19.64 -1 128 | 18.51 15.09 -1 129 | 24.76 11.58 -1 130 | 21.18 20.88 -1 131 | 4.90 18.33 -1 132 | 12.05 17.97 -1 133 | 5.02 18.11 -1 134 | 23.98 10.90 -1 135 | 21.22 12.82 -1 136 | 18.84 19.96 -1 137 | 15.72 16.89 -1 138 | 5.83 18.70 -1 139 | 2.53 14.27 -1 140 | 11.01 19.55 -1 141 | 15.33 18.11 -1 142 | 7.50 16.35 -1 143 | 1.69 19.84 -1 144 | 24.37 9.84 -1 145 | 16.50 18.94 -1 146 | 3.16 14.47 -1 147 | 16.34 11.87 -1 148 | 13.30 21.73 -1 149 | 12.76 16.06 -1 150 | 11.91 18.58 -1 151 | 11.15 16.70 -1 152 | 7.50 23.63 -1 153 | 11.38 17.97 -1 154 | 16.70 12.51 -1 155 | 19.94 5.26 -1 156 | 14.49 1.16 -1 157 | 3.62 18.59 -1 158 | 11.60 0.71 -1 159 | 11.52 19.80 -1 160 | 15.80 5.30 -1 161 | 20.50 20.68 -1 162 | 12.11 17.20 -1 163 | 3.85 17.91 -1 164 | 16.71 13.57 -1 165 | 18.18 15.00 -1 166 | 2.79 21.56 -1 167 | 9.90 19.13 -1 168 | 21.24 10.07 -1 169 | 24.49 11.94 -1 170 | 11.45 19.00 -1 171 | 16.86 7.58 -1 172 | 0.39 20.07 -1 173 | 5.49 20.36 -1 174 | 2.34 20.44 -1 175 | 21.98 19.87 -1 176 | 21.57 16.17 -1 177 | 9.08 19.74 -1 178 | 8.10 19.34 -1 179 | 0.72 14.82 -1 180 | 22.63 4.52 -1 181 | 20.43 12.12 -1 182 | 2.80 19.75 -1 183 | 2.94 20.64 -1 184 | 19.81 1.28 -1 185 | 16.31 12.98 -1 186 | 21.99 9.41 -1 187 | 6.62 18.74 -1 188 | 1.15 17.44 -1 189 | 18.75 2.83 -1 190 | 19.34 9.23 -1 191 | 14.38 24.01 -1 192 | 10.58 16.19 -1 193 | 21.43 19.18 -1 194 | 21.50 5.62 -1 195 | 7.56 21.67 -1 196 | 11.34 18.96 -1 197 | 22.62 8.54 -1 198 | 22.03 10.32 -1 199 | 7.64 22.48 -1 200 | 10.85 16.26 -1 201 | 19.39 8.54 -1 202 | -------------------------------------------------------------------------------- /chap3_SVM/data/test_linear.txt: -------------------------------------------------------------------------------- 1 | x1 x2 t 2 | 63.95 46.59 1 3 | 71.60 40.39 1 4 | 71.19 51.97 1 5 | 88.55 38.07 1 6 | 74.40 61.21 1 7 | 91.67 63.24 1 8 | 64.82 80.18 1 9 | 85.32 9.09 1 10 | 97.35 27.48 1 11 | 75.01 47.23 1 12 | 66.25 51.10 1 13 | 51.30 51.60 1 14 | 75.93 81.80 1 15 | 71.24 83.88 1 16 | 80.64 68.65 1 17 | 92.53 56.25 1 18 | 63.74 86.96 1 19 | 97.41 61.65 1 20 | 82.40 69.15 1 21 | 103.36 47.12 1 22 | 72.52 54.71 1 23 | 81.83 62.67 1 24 | 59.26 74.06 1 25 | 87.02 55.23 1 26 | 137.35 74.36 1 27 | 66.87 86.16 1 28 | 82.94 44.08 1 29 | 111.60 70.06 1 30 | 69.87 43.43 1 31 | 78.15 56.27 1 32 | 76.65 10.51 1 33 | 91.70 37.64 1 34 | 85.51 47.36 1 35 | 78.17 52.41 1 36 | 80.14 39.24 1 37 | 74.92 76.94 1 38 | 100.34 69.15 1 39 | 70.41 56.56 1 40 | 87.88 49.28 1 41 | 86.68 72.18 1 42 | 53.94 61.45 1 43 | 89.34 17.73 1 44 | 83.89 78.73 1 45 | 67.57 88.44 1 46 | 68.22 32.66 1 47 | 79.34 61.73 1 48 | 76.00 53.14 1 49 | 118.74 45.98 1 50 | 103.26 30.36 1 51 | 77.33 38.94 1 52 | 88.60 45.08 1 53 | 99.77 57.66 1 54 | 44.03 58.45 1 55 | 104.70 42.76 1 56 | 106.48 100.65 1 57 | 110.37 60.67 1 58 | 118.08 22.11 1 59 | 97.55 64.50 1 60 | 58.15 54.51 1 61 | 53.62 35.79 1 62 | 86.90 20.26 1 63 | 67.68 45.83 1 64 | 79.52 70.70 1 65 | 108.14 31.57 1 66 | 61.76 50.12 1 67 | 98.59 70.20 1 68 | 84.97 52.05 1 69 | 82.65 49.28 1 70 | 56.90 48.18 1 71 | 65.24 47.06 1 72 | 39.80 36.51 1 73 | 115.31 51.39 1 74 | 104.73 58.92 1 75 | 71.71 47.16 1 76 | 77.78 84.07 1 77 | 34.09 47.52 1 78 | 62.52 53.14 1 79 | 104.77 60.94 1 80 | 92.27 79.34 1 81 | 59.44 60.55 1 82 | 70.40 46.60 1 83 | 58.77 86.04 1 84 | 41.12 40.89 1 85 | 91.21 56.32 1 86 | 73.98 55.70 1 87 | 87.52 55.29 1 88 | 105.87 72.15 1 89 | 76.30 92.91 1 90 | 83.04 74.54 1 91 | 91.79 70.81 1 92 | 48.11 67.80 1 93 | 75.81 43.44 1 94 | 83.81 25.37 1 95 | 45.25 16.09 1 96 | 121.50 52.45 1 97 | 125.04 33.28 1 98 | 94.40 38.01 1 99 | 71.94 74.99 1 100 | 81.49 95.31 1 101 | 105.38 34.00 1 102 | 21.69 95.85 -1 103 | 6.94 152.80 -1 104 | 61.24 119.62 -1 105 | 26.36 122.12 -1 106 | 67.80 103.88 -1 107 | 24.99 116.61 -1 108 | -1.90 131.16 -1 109 | 21.75 93.76 -1 110 | 22.64 103.30 -1 111 | 5.63 105.74 -1 112 | -0.72 103.18 -1 113 | 53.51 107.77 -1 114 | 49.76 67.33 -1 115 | 17.13 99.77 -1 116 | 21.82 100.04 -1 117 | -2.74 80.31 -1 118 | 34.33 106.95 -1 119 | 22.81 109.02 -1 120 | 34.11 122.74 -1 121 | 15.47 106.14 -1 122 | 9.78 75.80 -1 123 | 1.43 112.85 -1 124 | 39.46 135.59 -1 125 | -0.50 122.58 -1 126 | 46.07 109.52 -1 127 | 15.32 106.58 -1 128 | 10.56 67.28 -1 129 | -3.81 98.70 -1 130 | 6.08 100.63 -1 131 | 22.44 118.73 -1 132 | -7.31 128.30 -1 133 | 4.08 92.56 -1 134 | 39.75 96.93 -1 135 | 31.85 108.05 -1 136 | 12.66 80.28 -1 137 | 45.26 92.02 -1 138 | 44.32 79.03 -1 139 | 44.30 110.41 -1 140 | -5.99 94.98 -1 141 | 59.68 110.49 -1 142 | -18.88 85.99 -1 143 | 6.70 73.36 -1 144 | 40.33 86.23 -1 145 | 30.80 102.29 -1 146 | 15.20 93.75 -1 147 | -26.56 84.93 -1 148 | 30.37 91.10 -1 149 | 27.63 86.81 -1 150 | 26.77 52.72 -1 151 | 12.03 90.12 -1 152 | 5.56 99.16 -1 153 | 71.18 106.02 -1 154 | 34.27 110.60 -1 155 | -24.35 112.24 -1 156 | 24.04 136.80 -1 157 | 35.23 68.84 -1 158 | 26.46 104.69 -1 159 | 32.79 104.69 -1 160 | 30.44 62.70 -1 161 | 34.17 94.73 -1 162 | 4.54 73.35 -1 163 | 26.54 85.82 -1 164 | 12.39 89.80 -1 165 | 4.74 93.10 -1 166 | 68.91 124.30 -1 167 | 6.75 124.48 -1 168 | 20.31 78.53 -1 169 | 31.44 85.43 -1 170 | 33.20 98.51 -1 171 | 21.19 56.46 -1 172 | -22.87 109.96 -1 173 | 115.07 127.90 -1 174 | 32.60 96.47 -1 175 | 26.99 75.92 -1 176 | 48.07 75.42 -1 177 | 10.09 90.32 -1 178 | 1.46 88.52 -1 179 | 4.33 84.59 -1 180 | 3.55 113.31 -1 181 | -32.13 93.51 -1 182 | 3.75 69.06 -1 183 | 3.59 118.65 -1 184 | 20.45 93.28 -1 185 | 28.08 97.89 -1 186 | -16.78 113.94 -1 187 | 22.39 83.01 -1 188 | 18.24 134.53 -1 189 | 68.29 64.58 -1 190 | 11.52 83.17 -1 191 | 50.41 86.30 -1 192 | 15.22 91.08 -1 193 | 49.92 65.78 -1 194 | 19.80 93.39 -1 195 | 45.80 71.26 -1 196 | 26.38 123.67 -1 197 | 12.81 58.48 -1 198 | 31.77 97.00 -1 199 | 46.95 113.10 -1 200 | 16.57 123.29 -1 201 | 20.61 101.49 -1 202 | -------------------------------------------------------------------------------- /chap3_SVM/data/test_multi.txt: -------------------------------------------------------------------------------- 1 | x1 x2 t 2 | 6.15 15.18 -1 3 | 7.26 11.61 -1 4 | 13.47 8.87 -1 5 | 5.98 16.75 -1 6 | 12.72 3.39 -1 7 | 10.59 9.54 -1 8 | 14.21 17.79 -1 9 | 8.60 11.94 -1 10 | 4.78 17.45 -1 11 | 5.68 10.42 -1 12 | 11.91 3.37 -1 13 | 3.57 8.77 -1 14 | 9.67 3.25 -1 15 | 3.41 12.01 -1 16 | 13.05 8.87 -1 17 | 9.72 14.41 -1 18 | 13.84 12.27 -1 19 | 9.82 11.04 -1 20 | 16.22 1.62 -1 21 | 12.79 8.93 -1 22 | 7.51 13.05 -1 23 | 13.98 6.42 -1 24 | 6.27 8.11 -1 25 | 7.77 12.21 -1 26 | 5.60 9.08 -1 27 | 12.15 5.89 -1 28 | 6.63 7.72 -1 29 | 12.75 6.14 -1 30 | 10.59 15.75 -1 31 | 12.15 13.62 -1 32 | 8.49 7.00 -1 33 | 9.99 9.95 -1 34 | 7.51 9.51 -1 35 | 8.50 2.54 -1 36 | 7.22 11.22 -1 37 | 6.14 11.79 -1 38 | 12.69 8.95 -1 39 | 12.46 10.53 -1 40 | 6.18 12.87 -1 41 | 3.67 11.54 -1 42 | 14.33 11.55 -1 43 | 3.93 6.23 -1 44 | 3.65 11.62 -1 45 | 13.76 13.56 -1 46 | 4.38 17.14 -1 47 | 12.57 12.07 -1 48 | 7.76 15.06 -1 49 | 10.30 15.02 -1 50 | 10.80 19.90 -1 51 | 5.42 10.24 -1 52 | 15.05 9.75 -1 53 | 4.11 8.59 -1 54 | 8.46 11.40 -1 55 | 3.31 10.19 -1 56 | 6.96 13.53 -1 57 | 8.91 19.74 -1 58 | 9.83 3.62 -1 59 | 11.19 13.83 -1 60 | 7.45 8.45 -1 61 | 11.87 7.46 -1 62 | 4.90 18.12 -1 63 | 7.89 7.97 -1 64 | 8.76 12.34 -1 65 | 13.67 11.72 -1 66 | 8.28 11.91 -1 67 | 6.28 3.97 -1 68 | 8.52 8.66 -1 69 | 6.89 17.03 -1 70 | 6.23 10.42 -1 71 | 9.99 9.70 -1 72 | 1.74 10.44 -1 73 | 17.19 6.82 -1 74 | 3.10 16.44 -1 75 | 15.89 1.73 -1 76 | 11.82 6.92 -1 77 | 4.78 12.22 -1 78 | 6.90 6.31 -1 79 | 12.86 6.92 -1 80 | 8.56 8.32 -1 81 | 7.59 5.50 -1 82 | 1.63 7.04 -1 83 | 6.60 10.42 -1 84 | 5.77 5.66 -1 85 | 19.36 11.77 -1 86 | 7.45 9.40 -1 87 | 11.75 7.68 -1 88 | 8.59 11.48 -1 89 | 15.44 14.09 -1 90 | 6.71 6.28 -1 91 | 11.60 12.25 -1 92 | 5.94 2.52 -1 93 | 12.93 12.05 -1 94 | 12.08 10.35 -1 95 | 9.39 12.02 -1 96 | 4.85 11.17 -1 97 | 4.56 9.21 -1 98 | 18.88 8.99 -1 99 | 9.11 5.05 -1 100 | 7.61 9.29 -1 101 | 8.54 3.59 -1 102 | 28.99 11.27 0 103 | 29.39 3.94 0 104 | 27.29 7.46 0 105 | 27.00 14.47 0 106 | 30.97 8.25 0 107 | 34.20 14.63 0 108 | 30.79 13.33 0 109 | 28.26 11.21 0 110 | 30.02 15.30 0 111 | 27.93 9.81 0 112 | 33.07 9.31 0 113 | 34.10 12.62 0 114 | 27.65 12.54 0 115 | 23.70 9.26 0 116 | 32.27 5.84 0 117 | 32.90 -0.08 0 118 | 34.84 9.98 0 119 | 32.29 9.39 0 120 | 29.76 9.42 0 121 | 31.34 4.17 0 122 | 30.26 8.74 0 123 | 33.95 16.09 0 124 | 30.05 8.09 0 125 | 29.01 10.93 0 126 | 27.45 6.79 0 127 | 35.24 8.86 0 128 | 24.87 11.59 0 129 | 28.08 17.77 0 130 | 28.46 8.14 0 131 | 37.26 4.90 0 132 | 27.08 14.06 0 133 | 23.34 14.15 0 134 | 33.00 10.46 0 135 | 43.66 17.22 0 136 | 29.28 11.75 0 137 | 35.29 7.77 0 138 | 29.44 9.56 0 139 | 22.82 11.14 0 140 | 32.27 6.17 0 141 | 25.97 8.71 0 142 | 21.08 4.72 0 143 | 26.24 12.78 0 144 | 31.88 14.32 0 145 | 33.68 10.02 0 146 | 29.83 12.67 0 147 | 27.66 12.24 0 148 | 28.54 14.06 0 149 | 31.41 9.45 0 150 | 29.50 11.55 0 151 | 37.08 6.90 0 152 | 26.67 14.10 0 153 | 29.91 11.45 0 154 | 28.86 6.09 0 155 | 28.01 12.12 0 156 | 29.20 9.41 0 157 | 31.54 11.74 0 158 | 23.40 10.46 0 159 | 28.23 19.04 0 160 | 25.36 16.51 0 161 | 28.76 11.30 0 162 | 16.55 3.52 0 163 | 32.25 4.59 0 164 | 26.71 10.35 0 165 | 35.36 13.59 0 166 | 30.59 7.54 0 167 | 33.71 19.01 0 168 | 24.94 17.62 0 169 | 34.45 14.11 0 170 | 31.79 13.69 0 171 | 19.83 11.37 0 172 | 29.58 10.89 0 173 | 29.86 10.31 0 174 | 29.63 6.56 0 175 | 27.47 9.83 0 176 | 35.57 2.33 0 177 | 25.49 8.67 0 178 | 35.10 10.86 0 179 | 29.87 5.29 0 180 | 36.73 11.27 0 181 | 34.89 10.47 0 182 | 34.60 7.88 0 183 | 34.80 8.68 0 184 | 34.49 10.95 0 185 | 29.94 7.80 0 186 | 33.32 10.30 0 187 | 32.83 13.78 0 188 | 28.36 8.01 0 189 | 26.50 10.83 0 190 | 28.74 8.76 0 191 | 25.01 11.19 0 192 | 30.63 12.28 0 193 | 27.49 12.03 0 194 | 30.17 10.67 0 195 | 32.70 12.05 0 196 | 32.11 7.80 0 197 | 31.89 7.65 0 198 | 29.62 6.56 0 199 | 30.44 14.94 0 200 | 27.00 10.55 0 201 | 40.55 8.37 0 202 | 17.55 20.19 1 203 | 15.24 33.06 1 204 | 22.90 33.30 1 205 | 18.28 25.12 1 206 | 17.30 25.03 1 207 | 20.27 21.95 1 208 | 21.34 30.93 1 209 | 24.69 33.69 1 210 | 23.53 29.09 1 211 | 27.02 26.39 1 212 | 16.09 31.42 1 213 | 23.43 36.27 1 214 | 18.92 30.39 1 215 | 30.74 18.55 1 216 | 19.91 27.90 1 217 | 23.66 31.34 1 218 | 15.07 24.68 1 219 | 20.16 20.95 1 220 | 14.41 23.57 1 221 | 21.41 26.37 1 222 | 18.19 29.45 1 223 | 18.38 26.36 1 224 | 24.84 24.86 1 225 | 12.12 31.57 1 226 | 21.42 27.49 1 227 | 20.05 28.94 1 228 | 22.30 28.60 1 229 | 22.81 22.72 1 230 | 23.73 27.85 1 231 | 14.52 26.23 1 232 | 21.38 29.61 1 233 | 18.52 23.91 1 234 | 22.69 27.01 1 235 | 19.89 32.43 1 236 | 15.71 26.32 1 237 | 17.10 23.48 1 238 | 19.42 20.77 1 239 | 23.01 27.75 1 240 | 22.27 29.52 1 241 | 19.11 23.60 1 242 | 19.07 26.66 1 243 | 22.80 28.65 1 244 | 20.48 24.18 1 245 | 20.95 19.54 1 246 | 15.39 25.98 1 247 | 15.51 23.84 1 248 | 15.60 26.59 1 249 | 20.20 23.73 1 250 | 17.82 26.78 1 251 | 22.95 29.60 1 252 | 23.68 30.82 1 253 | 20.89 20.87 1 254 | 26.91 16.55 1 255 | 16.11 23.17 1 256 | 17.69 27.27 1 257 | 19.60 27.73 1 258 | 21.56 22.42 1 259 | 21.32 27.36 1 260 | 15.47 21.30 1 261 | 17.51 27.06 1 262 | 11.75 30.49 1 263 | 18.31 29.05 1 264 | 23.31 32.43 1 265 | 22.66 29.65 1 266 | 18.67 29.35 1 267 | 22.26 25.57 1 268 | 17.89 25.47 1 269 | 18.17 28.95 1 270 | 21.64 21.47 1 271 | 31.21 26.77 1 272 | 19.79 30.75 1 273 | 13.90 33.68 1 274 | 22.72 24.93 1 275 | 27.26 21.83 1 276 | 20.84 24.33 1 277 | 13.18 22.72 1 278 | 24.14 32.89 1 279 | 13.29 31.61 1 280 | 16.73 34.55 1 281 | 17.60 26.98 1 282 | 25.99 26.53 1 283 | 16.97 30.09 1 284 | 18.55 24.56 1 285 | 16.52 30.95 1 286 | 26.18 30.43 1 287 | 12.22 24.05 1 288 | 20.63 35.00 1 289 | 20.58 23.11 1 290 | 22.68 32.53 1 291 | 25.98 33.11 1 292 | 22.40 25.88 1 293 | 20.92 24.25 1 294 | 20.67 27.33 1 295 | 21.45 30.50 1 296 | 17.47 27.17 1 297 | 16.06 17.98 1 298 | 20.73 31.23 1 299 | 24.94 33.35 1 300 | 26.47 27.69 1 301 | 22.62 24.81 1 302 | -------------------------------------------------------------------------------- /chap3_SVM/data/train_kernel.txt: -------------------------------------------------------------------------------- 1 | x1 x2 t 2 | 6.26 13.76 1 3 | 14.09 9.79 1 4 | 4.64 4.93 1 5 | 3.38 13.36 1 6 | 12.23 3.58 1 7 | 5.14 9.25 1 8 | 1.64 10.05 1 9 | 1.92 5.28 1 10 | 2.16 6.67 1 11 | 8.33 4.72 1 12 | 6.61 5.32 1 13 | 11.86 3.12 1 14 | 2.69 2.29 1 15 | 1.63 7.15 1 16 | 2.79 11.29 1 17 | 0.28 8.78 1 18 | 1.78 6.21 1 19 | 9.39 4.13 1 20 | 10.18 0.81 1 21 | 7.53 0.51 1 22 | 0.21 2.32 1 23 | 11.32 4.15 1 24 | 6.32 10.96 1 25 | 10.02 10.95 1 26 | 9.52 5.46 1 27 | 15.42 6.78 1 28 | 15.51 6.95 1 29 | 0.27 6.28 1 30 | 8.28 3.87 1 31 | 6.68 3.00 1 32 | 10.96 0.96 1 33 | 2.73 11.71 1 34 | 9.97 11.51 1 35 | 7.38 4.20 1 36 | 2.75 5.38 1 37 | 2.05 8.25 1 38 | 9.41 0.41 1 39 | 3.90 3.15 1 40 | 3.08 9.60 1 41 | 8.89 12.86 1 42 | 12.72 5.71 1 43 | 14.93 8.08 1 44 | 5.02 8.43 1 45 | 5.11 9.08 1 46 | 2.31 7.48 1 47 | 0.99 9.21 1 48 | 8.94 10.04 1 49 | 7.57 10.27 1 50 | 1.57 2.46 1 51 | 12.57 10.42 1 52 | 15.93 7.64 1 53 | 12.49 14.72 1 54 | 14.63 11.80 1 55 | 4.49 2.22 1 56 | 1.33 3.86 1 57 | 2.50 12.25 1 58 | 10.19 2.21 1 59 | 7.50 0.59 1 60 | 9.42 5.84 1 61 | 8.08 0.41 1 62 | 6.14 11.40 1 63 | 5.15 12.18 1 64 | 1.59 2.15 1 65 | 11.20 1.75 1 66 | 10.16 1.11 1 67 | 9.35 10.57 1 68 | 6.85 14.54 1 69 | 1.28 10.04 1 70 | 0.36 6.00 1 71 | 7.30 15.96 1 72 | 11.57 3.90 1 73 | 5.21 13.25 1 74 | 11.04 13.68 1 75 | 8.90 7.11 1 76 | 13.93 12.15 1 77 | 7.30 8.82 1 78 | 13.52 11.91 1 79 | 4.94 0.26 1 80 | 2.06 2.17 1 81 | 10.25 0.70 1 82 | 0.06 3.80 1 83 | 4.01 0.16 1 84 | 5.92 0.70 1 85 | 6.09 1.70 1 86 | 6.10 3.68 1 87 | 6.61 0.50 1 88 | 12.12 10.73 1 89 | 0.96 7.27 1 90 | 3.63 12.95 1 91 | 12.17 4.18 1 92 | 3.62 14.14 1 93 | 1.23 0.33 1 94 | 5.68 11.53 1 95 | 3.06 0.01 1 96 | 10.25 2.11 1 97 | 7.03 5.37 1 98 | 2.13 6.68 1 99 | 10.79 0.89 1 100 | 9.09 13.59 1 101 | 8.94 3.58 1 102 | 15.22 14.62 -1 103 | 21.07 19.64 -1 104 | 15.22 13.36 -1 105 | 2.84 20.49 -1 106 | 15.30 1.54 -1 107 | 16.73 6.01 -1 108 | 21.10 16.55 -1 109 | 21.42 4.10 -1 110 | 20.85 5.27 -1 111 | 0.82 16.02 -1 112 | 1.88 16.74 -1 113 | 21.09 17.50 -1 114 | 0.68 17.22 -1 115 | 16.89 4.21 -1 116 | 0.83 19.51 -1 117 | 22.74 9.13 -1 118 | 3.40 18.96 -1 119 | 12.72 1.36 -1 120 | 6.57 21.29 -1 121 | 16.42 6.87 -1 122 | 15.97 10.88 -1 123 | 21.45 5.91 -1 124 | 16.11 15.69 -1 125 | 13.87 2.38 -1 126 | 12.01 24.47 -1 127 | 16.56 22.66 -1 128 | 14.89 2.70 -1 129 | 14.84 13.61 -1 130 | 16.28 9.45 -1 131 | 9.33 23.48 -1 132 | 12.90 24.61 -1 133 | 6.14 20.41 -1 134 | 23.26 8.79 -1 135 | 16.47 7.69 -1 136 | 23.60 13.37 -1 137 | 22.65 16.46 -1 138 | 18.22 3.64 -1 139 | 4.59 19.66 -1 140 | 18.97 7.25 -1 141 | 18.53 21.13 -1 142 | 17.22 16.16 -1 143 | 20.06 16.10 -1 144 | 3.58 14.95 -1 145 | 22.67 9.54 -1 146 | 4.55 22.34 -1 147 | 8.34 18.39 -1 148 | 4.63 17.59 -1 149 | 16.86 7.29 -1 150 | 18.15 8.69 -1 151 | 15.62 15.84 -1 152 | 20.77 12.91 -1 153 | 16.90 12.45 -1 154 | 15.49 1.30 -1 155 | 20.53 2.46 -1 156 | 7.89 23.00 -1 157 | 12.98 2.00 -1 158 | 4.83 17.63 -1 159 | 16.54 5.50 -1 160 | 14.97 1.91 -1 161 | 0.87 14.73 -1 162 | 15.08 15.57 -1 163 | 17.25 1.21 -1 164 | 10.71 20.19 -1 165 | 18.55 13.09 -1 166 | 18.99 4.81 -1 167 | 23.77 8.09 -1 168 | 20.75 11.91 -1 169 | 18.58 8.48 -1 170 | 14.63 0.91 -1 171 | 16.43 12.90 -1 172 | 16.01 15.55 -1 173 | 12.80 24.79 -1 174 | 11.68 19.84 -1 175 | 9.94 16.27 -1 176 | 17.55 19.79 -1 177 | 18.87 2.37 -1 178 | 16.05 3.27 -1 179 | 18.16 3.01 -1 180 | 19.78 8.67 -1 181 | 17.21 20.90 -1 182 | 18.40 12.85 -1 183 | 15.29 18.25 -1 184 | 11.54 19.31 -1 185 | 21.38 7.89 -1 186 | 17.81 0.42 -1 187 | 10.76 23.45 -1 188 | 0.05 14.96 -1 189 | 10.92 19.18 -1 190 | 13.57 2.23 -1 191 | 0.55 11.99 -1 192 | 17.55 15.23 -1 193 | 11.17 20.14 -1 194 | 11.53 19.45 -1 195 | 2.40 20.29 -1 196 | 19.30 22.55 -1 197 | 18.54 12.28 -1 198 | 22.42 8.06 -1 199 | 16.58 4.49 -1 200 | 16.33 6.96 -1 201 | 19.59 7.35 -1 202 | -------------------------------------------------------------------------------- /chap3_SVM/data/train_linear.txt: -------------------------------------------------------------------------------- 1 | x1 x2 t 2 | 63.26 76.47 1 3 | 48.69 44.47 1 4 | 80.61 57.08 1 5 | 110.75 25.65 1 6 | 115.78 24.79 1 7 | 54.23 47.30 1 8 | 105.55 74.07 1 9 | 70.81 41.98 1 10 | 85.19 87.45 1 11 | 56.34 75.63 1 12 | 76.94 59.28 1 13 | 71.23 56.01 1 14 | 85.74 36.29 1 15 | 70.33 42.80 1 16 | 77.68 21.23 1 17 | 86.88 79.43 1 18 | 83.84 63.85 1 19 | 101.73 43.63 1 20 | 88.52 59.87 1 21 | 94.20 66.04 1 22 | 92.71 32.93 1 23 | 92.51 87.86 1 24 | 85.84 28.87 1 25 | 47.29 69.45 1 26 | 71.40 40.74 1 27 | 87.52 50.14 1 28 | 107.47 81.45 1 29 | 73.71 69.01 1 30 | 101.78 53.36 1 31 | 73.85 42.84 1 32 | 92.61 72.31 1 33 | 82.26 59.89 1 34 | 71.36 46.16 1 35 | 98.29 54.66 1 36 | 54.59 65.21 1 37 | 99.94 62.36 1 38 | 71.10 79.36 1 39 | 103.38 69.11 1 40 | 107.18 74.49 1 41 | 88.30 58.65 1 42 | 67.35 67.98 1 43 | 34.97 36.05 1 44 | 82.63 14.19 1 45 | 58.51 111.26 1 46 | 92.53 88.67 1 47 | 82.71 67.98 1 48 | 55.67 55.61 1 49 | 59.10 27.35 1 50 | 75.84 54.06 1 51 | 102.22 56.32 1 52 | 86.78 87.52 1 53 | 82.98 23.03 1 54 | 71.10 57.43 1 55 | 83.62 16.72 1 56 | 69.66 53.04 1 57 | 102.94 66.19 1 58 | 77.34 56.53 1 59 | 87.70 53.20 1 60 | 100.19 37.81 1 61 | 76.64 81.82 1 62 | 112.35 77.35 1 63 | 63.18 54.28 1 64 | 74.95 55.99 1 65 | 44.15 60.53 1 66 | 98.38 52.80 1 67 | 89.93 71.40 1 68 | 82.28 41.08 1 69 | 45.89 51.96 1 70 | 81.21 95.40 1 71 | 52.61 22.25 1 72 | 91.54 69.92 1 73 | 63.45 23.08 1 74 | 81.89 47.88 1 75 | 98.73 53.63 1 76 | 98.08 64.97 1 77 | 33.41 40.02 1 78 | 59.20 43.31 1 79 | 41.37 72.30 1 80 | 70.22 79.76 1 81 | 84.60 107.49 1 82 | 100.63 60.36 1 83 | 74.35 72.45 1 84 | 99.98 65.74 1 85 | 70.00 35.01 1 86 | 83.68 41.37 1 87 | 77.48 42.68 1 88 | 95.58 57.15 1 89 | 94.32 51.48 1 90 | 80.90 86.11 1 91 | 116.61 55.62 1 92 | 74.40 51.76 1 93 | 80.06 43.78 1 94 | 77.55 38.65 1 95 | 75.94 57.67 1 96 | 66.47 37.92 1 97 | 81.63 31.26 1 98 | 81.21 44.07 1 99 | 76.55 63.18 1 100 | 120.21 47.46 1 101 | 72.38 28.44 1 102 | 27.74 145.17 -1 103 | 1.31 105.65 -1 104 | 31.74 106.87 -1 105 | 26.34 87.51 -1 106 | -8.06 83.86 -1 107 | 3.64 69.17 -1 108 | -0.18 84.87 -1 109 | 11.41 63.02 -1 110 | 16.76 75.80 -1 111 | -3.10 106.89 -1 112 | 55.31 118.74 -1 113 | 14.46 119.89 -1 114 | 35.78 69.87 -1 115 | 35.75 99.52 -1 116 | 21.38 94.45 -1 117 | 21.50 68.18 -1 118 | -21.25 105.27 -1 119 | 23.15 118.98 -1 120 | -25.74 88.05 -1 121 | 24.16 104.63 -1 122 | 14.99 73.31 -1 123 | 34.98 84.88 -1 124 | 18.87 114.37 -1 125 | -2.49 54.83 -1 126 | 26.44 63.16 -1 127 | -2.47 111.08 -1 128 | 49.18 86.79 -1 129 | 27.58 107.92 -1 130 | 29.29 36.31 -1 131 | 22.22 101.80 -1 132 | 14.80 95.11 -1 133 | 60.43 91.86 -1 134 | 50.77 86.94 -1 135 | 35.27 98.61 -1 136 | 24.87 103.35 -1 137 | 11.73 92.24 -1 138 | 29.32 102.93 -1 139 | 14.29 87.13 -1 140 | 13.54 74.51 -1 141 | 28.51 98.58 -1 142 | 5.85 87.35 -1 143 | -23.29 100.18 -1 144 | 59.00 112.92 -1 145 | 12.63 84.87 -1 146 | -6.43 83.48 -1 147 | 5.13 93.81 -1 148 | 15.41 108.70 -1 149 | -9.21 133.93 -1 150 | 11.79 85.95 -1 151 | 25.34 55.38 -1 152 | 6.85 96.49 -1 153 | 15.34 63.22 -1 154 | 8.68 92.07 -1 155 | 22.37 106.55 -1 156 | 34.77 103.60 -1 157 | -30.95 50.28 -1 158 | 21.28 74.93 -1 159 | 55.28 83.12 -1 160 | 70.66 82.61 -1 161 | 25.94 103.86 -1 162 | 0.69 91.59 -1 163 | -1.66 94.55 -1 164 | 33.60 115.65 -1 165 | -0.62 107.46 -1 166 | 15.23 125.38 -1 167 | 7.24 55.98 -1 168 | 80.98 105.75 -1 169 | 51.23 57.60 -1 170 | 15.88 72.80 -1 171 | 23.95 113.38 -1 172 | 47.90 126.74 -1 173 | 33.17 86.18 -1 174 | 44.77 76.05 -1 175 | 24.55 92.57 -1 176 | 3.80 114.25 -1 177 | 22.61 62.73 -1 178 | 40.43 91.45 -1 179 | -9.28 100.53 -1 180 | 11.45 104.74 -1 181 | -14.71 107.47 -1 182 | 50.26 69.36 -1 183 | 5.09 98.34 -1 184 | 62.62 76.62 -1 185 | 37.17 89.35 -1 186 | 50.17 107.28 -1 187 | 44.24 100.28 -1 188 | 0.62 122.83 -1 189 | 18.72 99.69 -1 190 | 14.91 106.94 -1 191 | 23.51 103.50 -1 192 | 44.56 96.28 -1 193 | 28.01 101.95 -1 194 | 36.61 74.96 -1 195 | 10.66 77.52 -1 196 | 1.87 114.79 -1 197 | -4.40 132.30 -1 198 | 64.26 123.22 -1 199 | 37.31 108.32 -1 200 | 6.83 71.53 -1 201 | 38.88 106.11 -1 202 | -------------------------------------------------------------------------------- /chap3_SVM/data/train_multi.txt: -------------------------------------------------------------------------------- 1 | x1 x2 t 2 | 9.02 7.51 -1 3 | 19.11 14.19 -1 4 | 16.24 6.59 -1 5 | 13.07 6.84 -1 6 | 14.20 13.88 -1 7 | 11.29 10.41 -1 8 | 10.62 9.34 -1 9 | 11.56 9.12 -1 10 | 13.97 14.32 -1 11 | 16.59 3.57 -1 12 | 10.79 8.24 -1 13 | 11.21 5.45 -1 14 | 9.90 6.75 -1 15 | 12.21 6.36 -1 16 | 12.16 8.60 -1 17 | 8.87 15.14 -1 18 | 4.35 9.19 -1 19 | 9.08 9.32 -1 20 | 6.01 14.53 -1 21 | 15.02 4.41 -1 22 | 5.89 7.71 -1 23 | 19.65 11.49 -1 24 | 14.03 13.31 -1 25 | 15.11 17.24 -1 26 | 2.25 10.06 -1 27 | 12.15 5.00 -1 28 | 11.00 10.69 -1 29 | 6.43 15.29 -1 30 | 4.81 11.90 -1 31 | 7.10 14.73 -1 32 | 12.67 5.27 -1 33 | 20.45 5.71 -1 34 | 10.55 5.54 -1 35 | 11.47 8.06 -1 36 | 8.14 10.26 -1 37 | 6.70 9.68 -1 38 | 9.35 9.00 -1 39 | 13.49 8.51 -1 40 | -1.28 6.54 -1 41 | 10.30 9.14 -1 42 | 16.38 7.53 -1 43 | 10.30 7.90 -1 44 | 10.51 10.16 -1 45 | 2.73 12.39 -1 46 | 11.66 10.85 -1 47 | 7.54 11.90 -1 48 | 5.46 12.61 -1 49 | 12.51 7.17 -1 50 | 6.66 3.35 -1 51 | 9.59 17.76 -1 52 | 16.16 11.92 -1 53 | 12.09 8.98 -1 54 | 9.99 7.56 -1 55 | 9.59 9.50 -1 56 | 13.72 7.01 -1 57 | 7.48 10.37 -1 58 | 8.87 12.02 -1 59 | 7.06 12.48 -1 60 | 11.28 7.36 -1 61 | 11.96 4.09 -1 62 | 13.41 9.14 -1 63 | 12.47 6.53 -1 64 | 9.30 11.48 -1 65 | 6.36 13.90 -1 66 | 10.91 13.92 -1 67 | 9.99 15.38 -1 68 | 4.92 11.66 -1 69 | 7.37 4.08 -1 70 | 8.08 9.84 -1 71 | 6.48 9.31 -1 72 | 5.24 2.66 -1 73 | 12.23 7.51 -1 74 | 12.40 13.37 -1 75 | 8.13 12.32 -1 76 | 12.22 13.67 -1 77 | 20.38 8.17 -1 78 | 2.57 8.09 -1 79 | 11.86 17.88 -1 80 | 7.68 13.93 -1 81 | 9.45 9.54 -1 82 | 7.10 11.32 -1 83 | 15.17 16.20 -1 84 | 10.61 5.96 -1 85 | 18.17 14.82 -1 86 | 11.62 1.32 -1 87 | 6.07 7.61 -1 88 | 6.88 14.35 -1 89 | 10.02 8.70 -1 90 | 8.32 5.69 -1 91 | 8.58 9.85 -1 92 | 9.11 6.65 -1 93 | 7.17 2.81 -1 94 | 21.71 6.09 -1 95 | 8.64 11.72 -1 96 | 7.00 4.26 -1 97 | 14.99 9.54 -1 98 | 8.27 3.24 -1 99 | 14.58 12.03 -1 100 | 3.96 14.35 -1 101 | 10.71 9.75 -1 102 | 27.25 -2.15 0 103 | 24.27 11.91 0 104 | 28.16 9.70 0 105 | 32.68 7.77 0 106 | 29.85 5.86 0 107 | 20.90 10.41 0 108 | 26.74 9.04 0 109 | 27.88 17.77 0 110 | 32.72 10.63 0 111 | 27.45 7.33 0 112 | 31.18 4.79 0 113 | 33.92 14.23 0 114 | 24.95 4.74 0 115 | 31.33 4.85 0 116 | 32.27 13.31 0 117 | 27.94 12.66 0 118 | 31.21 17.44 0 119 | 36.39 10.00 0 120 | 35.99 13.89 0 121 | 35.52 14.75 0 122 | 32.74 13.36 0 123 | 29.67 6.01 0 124 | 26.66 7.34 0 125 | 25.68 15.83 0 126 | 30.17 14.29 0 127 | 28.32 9.34 0 128 | 27.66 5.88 0 129 | 40.03 11.86 0 130 | 30.72 9.40 0 131 | 33.33 6.58 0 132 | 32.21 8.25 0 133 | 35.22 12.88 0 134 | 34.85 10.26 0 135 | 33.41 13.42 0 136 | 34.10 5.48 0 137 | 26.70 9.16 0 138 | 33.02 12.62 0 139 | 27.67 16.08 0 140 | 21.86 10.24 0 141 | 25.92 18.18 0 142 | 30.30 7.81 0 143 | 31.77 9.88 0 144 | 25.25 10.04 0 145 | 35.60 9.05 0 146 | 23.46 11.47 0 147 | 28.89 13.40 0 148 | 34.95 7.95 0 149 | 32.57 5.86 0 150 | 20.62 18.31 0 151 | 29.78 15.37 0 152 | 25.81 11.68 0 153 | 29.30 6.00 0 154 | 33.08 10.36 0 155 | 31.44 14.79 0 156 | 23.57 4.68 0 157 | 27.56 8.84 0 158 | 24.76 9.80 0 159 | 31.08 5.74 0 160 | 26.01 4.52 0 161 | 29.26 8.00 0 162 | 31.33 7.00 0 163 | 37.10 17.89 0 164 | 30.47 8.08 0 165 | 28.78 15.70 0 166 | 31.61 15.39 0 167 | 30.49 11.25 0 168 | 30.59 17.11 0 169 | 37.36 12.82 0 170 | 41.15 9.30 0 171 | 26.88 15.33 0 172 | 42.84 3.01 0 173 | 20.42 7.72 0 174 | 29.72 12.18 0 175 | 29.24 7.07 0 176 | 27.69 10.03 0 177 | 26.93 7.98 0 178 | 19.67 19.81 0 179 | 30.36 10.41 0 180 | 22.27 6.04 0 181 | 28.93 6.13 0 182 | 26.02 10.05 0 183 | 30.11 4.49 0 184 | 28.99 11.93 0 185 | 31.95 9.01 0 186 | 30.09 3.55 0 187 | 32.38 14.15 0 188 | 31.21 11.50 0 189 | 35.73 8.74 0 190 | 27.98 5.56 0 191 | 24.10 12.07 0 192 | 29.56 6.90 0 193 | 27.81 7.30 0 194 | 32.01 18.43 0 195 | 30.52 6.68 0 196 | 31.36 9.59 0 197 | 35.97 4.32 0 198 | 26.67 14.23 0 199 | 29.77 11.34 0 200 | 25.87 10.83 0 201 | 39.11 6.27 0 202 | 24.58 22.49 1 203 | 19.49 29.39 1 204 | 27.70 27.91 1 205 | 21.63 19.27 1 206 | 19.79 28.51 1 207 | 20.28 25.83 1 208 | 21.75 31.93 1 209 | 18.87 22.87 1 210 | 19.15 34.06 1 211 | 21.15 29.38 1 212 | 18.90 22.92 1 213 | 13.64 28.24 1 214 | 17.78 31.31 1 215 | 25.90 32.87 1 216 | 25.99 24.96 1 217 | 21.75 28.31 1 218 | 22.32 27.67 1 219 | 17.52 33.44 1 220 | 14.60 28.70 1 221 | 15.59 27.51 1 222 | 16.49 31.20 1 223 | 16.97 32.25 1 224 | 11.59 28.37 1 225 | 23.38 27.20 1 226 | 27.44 31.83 1 227 | 14.87 29.37 1 228 | 20.77 30.32 1 229 | 22.46 32.35 1 230 | 18.62 29.83 1 231 | 18.98 17.20 1 232 | 18.17 26.92 1 233 | 21.59 23.05 1 234 | 24.75 24.89 1 235 | 14.74 24.71 1 236 | 22.41 24.58 1 237 | 16.01 29.48 1 238 | 15.96 21.45 1 239 | 12.29 29.39 1 240 | 19.00 17.41 1 241 | 19.74 26.14 1 242 | 19.54 26.38 1 243 | 21.55 26.76 1 244 | 27.37 24.96 1 245 | 17.11 26.64 1 246 | 13.56 29.86 1 247 | 26.65 28.60 1 248 | 20.13 25.99 1 249 | 21.82 33.33 1 250 | 18.60 25.97 1 251 | 20.37 21.32 1 252 | 17.54 23.04 1 253 | 16.23 15.81 1 254 | 17.27 21.84 1 255 | 20.33 27.82 1 256 | 20.48 27.95 1 257 | 15.24 28.81 1 258 | 17.27 27.88 1 259 | 19.25 25.09 1 260 | 16.19 32.26 1 261 | 13.48 25.49 1 262 | 19.06 29.00 1 263 | 22.05 26.64 1 264 | 21.68 20.09 1 265 | 26.11 29.98 1 266 | 24.28 24.73 1 267 | 13.17 29.63 1 268 | 14.85 22.89 1 269 | 21.77 31.23 1 270 | 29.67 22.99 1 271 | 19.79 27.92 1 272 | 18.31 24.78 1 273 | 31.47 28.44 1 274 | 19.13 29.47 1 275 | 19.19 22.78 1 276 | 29.46 36.58 1 277 | 19.48 23.47 1 278 | 19.32 26.04 1 279 | 20.47 32.13 1 280 | 23.99 26.98 1 281 | 16.85 32.26 1 282 | 18.96 23.59 1 283 | 26.30 25.27 1 284 | 20.42 27.31 1 285 | 9.17 26.45 1 286 | 20.42 31.76 1 287 | 14.36 30.36 1 288 | 13.95 34.03 1 289 | 27.59 26.67 1 290 | 20.19 20.28 1 291 | 22.28 32.06 1 292 | 21.82 25.63 1 293 | 19.16 27.01 1 294 | 26.30 33.07 1 295 | 15.74 26.06 1 296 | 20.48 18.70 1 297 | 13.24 27.27 1 298 | 21.97 30.68 1 299 | 27.02 26.63 1 300 | 16.38 21.88 1 301 | 25.43 29.82 1 302 | -------------------------------------------------------------------------------- /chap3_SVM/svm.py: -------------------------------------------------------------------------------- 1 | # python: 3.5.2 2 | # encoding: utf-8 3 | 4 | import numpy as np 5 | 6 | 7 | def load_data(fname): 8 | """ 9 | 载入数据。 10 | """ 11 | with open(fname, 'r') as f: 12 | data = [] 13 | line = f.readline() 14 | for line in f: 15 | line = line.strip().split() 16 | x1 = float(line[0]) 17 | x2 = float(line[1]) 18 | t = int(line[2]) 19 | data.append([x1, x2, t]) 20 | return np.array(data) 21 | 22 | 23 | def eval_acc(label, pred): 24 | """ 25 | 计算准确率。 26 | """ 27 | return np.sum(label == pred) / len(pred) 28 | 29 | 30 | class SVM(): 31 | """ 32 | SVM模型。 33 | """ 34 | 35 | def __init__(self): 36 | # 请补全此处代码 37 | pass 38 | 39 | def train(self, data_train): 40 | """ 41 | 训练模型。 42 | """ 43 | 44 | # 请补全此处代码 45 | 46 | def predict(self, x): 47 | """ 48 | 预测标签。 49 | """ 50 | 51 | # 请补全此处代码 52 | 53 | 54 | if __name__ == '__main__': 55 | # 载入数据,实际实用时将x替换为具体名称 56 | train_file = 'data/train_linear.txt' 57 | test_file = 'data/test_linear.txt' 58 | data_train = load_data(train_file) # 数据格式[x1, x2, t] 59 | data_test = load_data(test_file) 60 | 61 | # 使用训练集训练SVM模型 62 | svm = SVM() # 初始化模型 63 | svm.train(data_train) # 训练模型 64 | 65 | # 使用SVM模型预测标签 66 | x_train = data_train[:, :2] # feature [x1, x2] 67 | t_train = data_train[:, 2] # 真实标签 68 | t_train_pred = svm.predict(x_train) # 预测标签 69 | x_test = data_test[:, :2] 70 | t_test = data_test[:, 2] 71 | t_test_pred = svm.predict(x_test) 72 | 73 | # 评估结果,计算准确率 74 | acc_train = eval_acc(t_train, t_train_pred) 75 | acc_test = eval_acc(t_test, t_test_pred) 76 | print("train accuracy: {:.1f}%".format(acc_train * 100)) 77 | print("test accuracy: {:.1f}%".format(acc_test * 100)) 78 | -------------------------------------------------------------------------------- /chap3_softmax_regression/README.md: -------------------------------------------------------------------------------- 1 | # Logistic回归以及softmax回归 2 | 3 | 4 | ## 问题描述 5 | 1. 完成 `logistic_regression-exercise.ipnb`中的填空 6 | * 填空一:实现sigmoid的交叉熵损失函数(不使用tf内置的loss 函数) 7 | 8 | 1. 完成 `softmax_regression-exercise.ipnb`中的填空 9 | * 填空一:在__init__构造函数中建立模型所需的参数 10 | * 填空二:实现softmax的交叉熵损失函数(不使用tf内置的loss 函数) 11 | 12 | 13 | -------------------------------------------------------------------------------- /chap4_ simple neural network/README.md: -------------------------------------------------------------------------------- 1 | # 1. 全连接神经网络 2 | 3 | ## 问题描述: 4 | 5 | ​ 利用numpy 和tensorflow 、pytorch 搭建全连接神经网络。使用numpy 实现此练习需要自己手动求导,而tensorflow 和pytorch 具有自动求导机制。 6 | 7 | 8 | 9 | 10 | 11 | ## 数据集: 12 | 13 | MNIST数据集包括60000张训练图片和10000张测试图片。图片样本的数量已经足够训练一个很复杂的模型(例如 CNN的深层神经网络)。它经常被用来作为一个新 的模式识别模型的测试用例。而且它也是一个方便学生和研究者们执行用例的数据集。除此之外,MNIST数据集是一个相对较小的数据集,可以在你的笔记本CPUs上面直接执行 14 | 15 | 16 | 17 | 18 | 19 | ## 题目要求: 20 | 21 | ​ 补全本章节中所有*.ipynb文件中提示补全的部分。 22 | 23 | 24 | 25 | 26 | 27 | # 2. 函数拟合 28 | 29 | ## 问题描述: 30 | 31 | ​ 理论和实验证明,一个两层的ReLU网络可以模拟任何函数[1~5]。请自行定义一个函数, 并使用基于ReLU的神经网络来拟合此函数。 32 | 33 | 34 | 35 | 36 | ## 要求: 37 | 38 | 39 | 40 | - 请自行在函数上采样生成训练集和测试集,使用训练集来训练神经网络,使用测试集来验证拟合效果。 41 | - 可以使用深度学习框架来编写模型,如tensorflow、pytorch、keras等。 42 | - 如果不使用上述框架,直接用NumPy实现可以最高加5分的附加分。 43 | - 提交时请一并提交代码和报告。 44 | - 代码建议注释清楚(5分) 45 | - 报告至少应包含以下部分:(5分) 46 | - 函数定义、数据采集、模型描述、拟合效果。 47 | 48 | 49 | ## 示例: 50 | 51 | ![](fitting.jpg) 52 | 53 | ## 参考文献: 54 | 55 | [1] G. Cybenko. 1989. Approximation by superpositions of a sigmoidal function. 56 | 57 | [2] K. Hornik, M. Stinchcombe, and H. White. 1989. Multilayer feedforward networks are universal approximators. 58 | 59 | [3] Moshe Leshno, et al. 1993. Multilayer feedforward networks with a nonpolynomial activation function can approximate any function 60 | 61 | [4] Vinod Nair and Geoffrey E. Hinton. 2010. Rectified linear units improve restricted boltzmann machines. 62 | 63 | [5] Xavier Glorot, Antoine Bordes, Yoshua Bengio. 2011. Deep Sparse Rectifier Neural Networks. PMLR 15:315-323. 64 | 65 | -------------------------------------------------------------------------------- /chap4_ simple neural network/tf2.0-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tensorflow2.0 小练习" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import tensorflow as tf\n", 17 | "import numpy as np" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "## 实现softmax函数" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 6, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "def softmax(x):\n", 34 | " ##########\n", 35 | " '''实现softmax函数,只要求对最后一维归一化,\n", 36 | " 不允许用tf自带的softmax函数'''\n", 37 | " ##########\n", 38 | " return prob_x\n", 39 | "\n", 40 | "test_data = np.random.normal(size=[10, 5])\n", 41 | "(softmax(test_data).numpy() - tf.nn.softmax(test_data, axis=-1).numpy())**2 <0.0001" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "## 实现sigmoid函数" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 9, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "def sigmoid(x):\n", 58 | " ##########\n", 59 | " '''实现sigmoid函数, 不允许用tf自带的sigmoid函数'''\n", 60 | " ##########\n", 61 | " return prob_x\n", 62 | "\n", 63 | "test_data = np.random.normal(size=[10, 5])\n", 64 | "(sigmoid(test_data).numpy() - tf.nn.sigmoid(test_data).numpy())**2 < 0.0001" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "## 实现 softmax 交叉熵loss函数" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 32, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "def softmax_ce(x, label):\n", 81 | " ##########\n", 82 | " '''实现 softmax 交叉熵loss函数, 不允许用tf自带的softmax_cross_entropy函数'''\n", 83 | " ##########\n", 84 | " return loss\n", 85 | "\n", 86 | "test_data = np.random.normal(size=[10, 5])\n", 87 | "prob = tf.nn.softmax(test_data)\n", 88 | "label = np.zeros_like(test_data)\n", 89 | "label[np.arange(10), np.random.randint(0, 5, size=10)]=1.\n", 90 | "\n", 91 | "((tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(label, test_data))\n", 92 | " - softmax_ce(prob, label))**2 < 0.0001).numpy()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "## 实现 sigmoid 交叉熵loss函数" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 46, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "[0. 0. 0. 1. 1. 0. 1. 1. 1. 0.]\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "def sigmoid_ce(x, label):\n", 117 | " ##########\n", 118 | " '''实现 softmax 交叉熵loss函数, 不允许用tf自带的softmax_cross_entropy函数'''\n", 119 | " ##########\n", 120 | " return loss\n", 121 | "\n", 122 | "test_data = np.random.normal(size=[10])\n", 123 | "prob = tf.nn.sigmoid(test_data)\n", 124 | "label = np.random.randint(0, 2, 10).astype(test_data.dtype)\n", 125 | "print (label)\n", 126 | "\n", 127 | "((tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(label, test_data))- sigmoid_ce(prob, label))**2 < 0.0001).numpy()\n" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 3", 141 | "language": "python", 142 | "name": "python3" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 3 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython3", 154 | "version": "3.7.0" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 2 159 | } 160 | -------------------------------------------------------------------------------- /chap4_ simple neural network/tf2.0-tutorial.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap4_ simple neural network/tf2.0-tutorial.pdf -------------------------------------------------------------------------------- /chap4_ simple neural network/tutorial_minst_fnn-numpy-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 准备数据" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import os\n", 17 | "import numpy as np\n", 18 | "import tensorflow as tf\n", 19 | "from tensorflow import keras\n", 20 | "from tensorflow.keras import layers, optimizers, datasets\n", 21 | "\n", 22 | "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # or any {'0', '1', '2'}\n", 23 | "\n", 24 | "def mnist_dataset():\n", 25 | " (x, y), (x_test, y_test) = datasets.mnist.load_data()\n", 26 | " #normalize\n", 27 | " x = x/255.0\n", 28 | " x_test = x_test/255.0\n", 29 | " \n", 30 | " return (x, y), (x_test, y_test)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "## Demo numpy based auto differentiation" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 3, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "import numpy as np\n", 47 | "\n", 48 | "class Matmul:\n", 49 | " def __init__(self):\n", 50 | " self.mem = {}\n", 51 | " \n", 52 | " def forward(self, x, W):\n", 53 | " h = np.matmul(x, W)\n", 54 | " self.mem={'x': x, 'W':W}\n", 55 | " return h\n", 56 | " \n", 57 | " def backward(self, grad_y):\n", 58 | " '''\n", 59 | " x: shape(N, d)\n", 60 | " w: shape(d, d')\n", 61 | " grad_y: shape(N, d')\n", 62 | " '''\n", 63 | " x = self.mem['x']\n", 64 | " W = self.mem['W']\n", 65 | " \n", 66 | " ####################\n", 67 | " '''计算矩阵乘法的对应的梯度'''\n", 68 | " ####################\n", 69 | " return grad_x, grad_W\n", 70 | "\n", 71 | "\n", 72 | "class Relu:\n", 73 | " def __init__(self):\n", 74 | " self.mem = {}\n", 75 | " \n", 76 | " def forward(self, x):\n", 77 | " self.mem['x']=x\n", 78 | " return np.where(x > 0, x, np.zeros_like(x))\n", 79 | " \n", 80 | " def backward(self, grad_y):\n", 81 | " '''\n", 82 | " grad_y: same shape as x\n", 83 | " '''\n", 84 | " ####################\n", 85 | " '''计算relu 激活函数对应的梯度'''\n", 86 | " ####################\n", 87 | " return grad_x\n", 88 | " \n", 89 | "\n", 90 | "\n", 91 | "class Softmax:\n", 92 | " '''\n", 93 | " softmax over last dimention\n", 94 | " '''\n", 95 | " def __init__(self):\n", 96 | " self.epsilon = 1e-12\n", 97 | " self.mem = {}\n", 98 | " \n", 99 | " def forward(self, x):\n", 100 | " '''\n", 101 | " x: shape(N, c)\n", 102 | " '''\n", 103 | " x_exp = np.exp(x)\n", 104 | " partition = np.sum(x_exp, axis=1, keepdims=True)\n", 105 | " out = x_exp/(partition+self.epsilon)\n", 106 | " \n", 107 | " self.mem['out'] = out\n", 108 | " self.mem['x_exp'] = x_exp\n", 109 | " return out\n", 110 | " \n", 111 | " def backward(self, grad_y):\n", 112 | " '''\n", 113 | " grad_y: same shape as x\n", 114 | " '''\n", 115 | " s = self.mem['out']\n", 116 | " sisj = np.matmul(np.expand_dims(s,axis=2), np.expand_dims(s, axis=1)) # (N, c, c)\n", 117 | " g_y_exp = np.expand_dims(grad_y, axis=1)\n", 118 | " tmp = np.matmul(g_y_exp, sisj) #(N, 1, c)\n", 119 | " tmp = np.squeeze(tmp, axis=1)\n", 120 | " tmp = -tmp+grad_y*s \n", 121 | " return tmp\n", 122 | " \n", 123 | "class Log:\n", 124 | " '''\n", 125 | " softmax over last dimention\n", 126 | " '''\n", 127 | " def __init__(self):\n", 128 | " self.epsilon = 1e-12\n", 129 | " self.mem = {}\n", 130 | " \n", 131 | " def forward(self, x):\n", 132 | " '''\n", 133 | " x: shape(N, c)\n", 134 | " '''\n", 135 | " out = np.log(x+self.epsilon)\n", 136 | " \n", 137 | " self.mem['x'] = x\n", 138 | " return out\n", 139 | " \n", 140 | " def backward(self, grad_y):\n", 141 | " '''\n", 142 | " grad_y: same shape as x\n", 143 | " '''\n", 144 | " x = self.mem['x']\n", 145 | " \n", 146 | " return 1./(x+1e-12) * grad_y\n", 147 | " \n" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "## Gradient check" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 5, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "# import tensorflow as tf\n", 164 | "\n", 165 | "# x = np.random.normal(size=[5, 6])\n", 166 | "# W = np.random.normal(size=[6, 4])\n", 167 | "# aa = Matmul()\n", 168 | "# out = aa.forward(x, W) # shape(5, 4)\n", 169 | "# grad = aa.backward(np.ones_like(out))\n", 170 | "# print (grad)\n", 171 | "\n", 172 | "# with tf.GradientTape() as tape:\n", 173 | "# x, W = tf.constant(x), tf.constant(W)\n", 174 | "# tape.watch(x)\n", 175 | "# y = tf.matmul(x, W)\n", 176 | "# loss = tf.reduce_sum(y)\n", 177 | "# grads = tape.gradient(loss, x)\n", 178 | "# print (grads)\n", 179 | "\n", 180 | "# import tensorflow as tf\n", 181 | "\n", 182 | "# x = np.random.normal(size=[5, 6])\n", 183 | "# aa = Relu()\n", 184 | "# out = aa.forward(x) # shape(5, 4)\n", 185 | "# grad = aa.backward(np.ones_like(out))\n", 186 | "# print (grad)\n", 187 | "\n", 188 | "# with tf.GradientTape() as tape:\n", 189 | "# x= tf.constant(x)\n", 190 | "# tape.watch(x)\n", 191 | "# y = tf.nn.relu(x)\n", 192 | "# loss = tf.reduce_sum(y)\n", 193 | "# grads = tape.gradient(loss, x)\n", 194 | "# print (grads)\n", 195 | "\n", 196 | "# import tensorflow as tf\n", 197 | "# x = np.random.normal(size=[5, 6], scale=5.0, loc=1)\n", 198 | "# label = np.zeros_like(x)\n", 199 | "# label[0, 1]=1.\n", 200 | "# label[1, 0]=1\n", 201 | "# label[1, 1]=1\n", 202 | "# label[2, 3]=1\n", 203 | "# label[3, 5]=1\n", 204 | "# label[4, 0]=1\n", 205 | "# print(label)\n", 206 | "# aa = Softmax()\n", 207 | "# out = aa.forward(x) # shape(5, 6)\n", 208 | "# grad = aa.backward(label)\n", 209 | "# print (grad)\n", 210 | "\n", 211 | "# with tf.GradientTape() as tape:\n", 212 | "# x= tf.constant(x)\n", 213 | "# tape.watch(x)\n", 214 | "# y = tf.nn.softmax(x)\n", 215 | "# loss = tf.reduce_sum(y*label)\n", 216 | "# grads = tape.gradient(loss, x)\n", 217 | "# print (grads)\n", 218 | "\n", 219 | "# import tensorflow as tf\n", 220 | "\n", 221 | "# x = np.random.normal(size=[5, 6])\n", 222 | "# aa = Log()\n", 223 | "# out = aa.forward(x) # shape(5, 4)\n", 224 | "# grad = aa.backward(label)\n", 225 | "# print (grad)\n", 226 | "\n", 227 | "# with tf.GradientTape() as tape:\n", 228 | "# x= tf.constant(x)\n", 229 | "# tape.watch(x)\n", 230 | "# y = tf.math.log(x)\n", 231 | "# loss = tf.reduce_sum(y*label)\n", 232 | "# grads = tape.gradient(loss, x)\n", 233 | "# print (grads)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "# Final Gradient Check" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 6, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "name": "stdout", 250 | "output_type": "stream", 251 | "text": [ 252 | "[[ 0. 29.88862875 0. 0. 0.\n", 253 | " 0. ]\n", 254 | " [363.02160168 0. 0. 0. 0.\n", 255 | " 0. ]\n", 256 | " [ 0. 0. 0. 12.25561872 0.\n", 257 | " 0. ]\n", 258 | " [ 0. 0. 0. 0. 0.\n", 259 | " 53.54971193]\n", 260 | " [689.96870449 0. 0. 0. 0.\n", 261 | " 0. ]]\n", 262 | "----------------------------------------\n", 263 | "[[ 0. 29.88862875 0. 0. 0.\n", 264 | " 0. ]\n", 265 | " [363.02160181 0. 0. 0. 0.\n", 266 | " 0. ]\n", 267 | " [ 0. 0. 0. 12.25561872 0.\n", 268 | " 0. ]\n", 269 | " [ 0. 0. 0. 0. 0.\n", 270 | " 53.54971193]\n", 271 | " [689.96870497 0. 0. 0. 0.\n", 272 | " 0. ]]\n" 273 | ] 274 | } 275 | ], 276 | "source": [ 277 | "import tensorflow as tf\n", 278 | "\n", 279 | "label = np.zeros_like(x)\n", 280 | "label[0, 1]=1.\n", 281 | "label[1, 0]=1\n", 282 | "label[2, 3]=1\n", 283 | "label[3, 5]=1\n", 284 | "label[4, 0]=1\n", 285 | "\n", 286 | "x = np.random.normal(size=[5, 6])\n", 287 | "W1 = np.random.normal(size=[6, 5])\n", 288 | "W2 = np.random.normal(size=[5, 6])\n", 289 | "\n", 290 | "mul_h1 = Matmul()\n", 291 | "mul_h2 = Matmul()\n", 292 | "relu = Relu()\n", 293 | "softmax = Softmax()\n", 294 | "log = Log()\n", 295 | "\n", 296 | "h1 = mul_h1.forward(x, W1) # shape(5, 4)\n", 297 | "h1_relu = relu.forward(h1)\n", 298 | "h2 = mul_h2.forward(h1_relu, W2)\n", 299 | "h2_soft = softmax.forward(h2)\n", 300 | "h2_log = log.forward(h2_soft)\n", 301 | "\n", 302 | "\n", 303 | "h2_log_grad = log.backward(label)\n", 304 | "h2_soft_grad = softmax.backward(h2_log_grad)\n", 305 | "h2_grad, W2_grad = mul_h2.backward(h2_soft_grad)\n", 306 | "h1_relu_grad = relu.backward(h2_grad)\n", 307 | "h1_grad, W1_grad = mul_h1.backward(h1_relu_grad)\n", 308 | "\n", 309 | "print(h2_log_grad)\n", 310 | "print('--'*20)\n", 311 | "# print(W2_grad)\n", 312 | "\n", 313 | "with tf.GradientTape() as tape:\n", 314 | " x, W1, W2, label = tf.constant(x), tf.constant(W1), tf.constant(W2), tf.constant(label)\n", 315 | " tape.watch(W1)\n", 316 | " tape.watch(W2)\n", 317 | " h1 = tf.matmul(x, W1)\n", 318 | " h1_relu = tf.nn.relu(h1)\n", 319 | " h2 = tf.matmul(h1_relu, W2)\n", 320 | " prob = tf.nn.softmax(h2)\n", 321 | " log_prob = tf.math.log(prob)\n", 322 | " loss = tf.reduce_sum(label * log_prob)\n", 323 | " grads = tape.gradient(loss, [prob])\n", 324 | " print (grads[0].numpy())" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "metadata": {}, 330 | "source": [ 331 | "## 建立模型" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 10, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [ 340 | "class myModel:\n", 341 | " def __init__(self):\n", 342 | " \n", 343 | " self.W1 = np.random.normal(size=[28*28+1, 100])\n", 344 | " self.W2 = np.random.normal(size=[100, 10])\n", 345 | " \n", 346 | " self.mul_h1 = Matmul()\n", 347 | " self.mul_h2 = Matmul()\n", 348 | " self.relu = Relu()\n", 349 | " self.softmax = Softmax()\n", 350 | " self.log = Log()\n", 351 | " \n", 352 | " \n", 353 | " def forward(self, x):\n", 354 | " x = x.reshape(-1, 28*28)\n", 355 | " bias = np.ones(shape=[x.shape[0], 1])\n", 356 | " x = np.concatenate([x, bias], axis=1)\n", 357 | " \n", 358 | " self.h1 = self.mul_h1.forward(x, self.W1) # shape(5, 4)\n", 359 | " self.h1_relu = self.relu.forward(self.h1)\n", 360 | " self.h2 = self.mul_h2.forward(self.h1_relu, self.W2)\n", 361 | " self.h2_soft = self.softmax.forward(self.h2)\n", 362 | " self.h2_log = self.log.forward(self.h2_soft)\n", 363 | " \n", 364 | " def backward(self, label):\n", 365 | " self.h2_log_grad = self.log.backward(-label)\n", 366 | " self.h2_soft_grad = self.softmax.backward(self.h2_log_grad)\n", 367 | " self.h2_grad, self.W2_grad = self.mul_h2.backward(self.h2_soft_grad)\n", 368 | " self.h1_relu_grad = self.relu.backward(self.h2_grad)\n", 369 | " self.h1_grad, self.W1_grad = self.mul_h1.backward(self.h1_relu_grad)\n", 370 | " \n", 371 | "model = myModel()\n" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "## 计算 loss" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 11, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [ 387 | "def compute_loss(log_prob, labels):\n", 388 | " return np.mean(np.sum(-log_prob*labels, axis=1))\n", 389 | " \n", 390 | "\n", 391 | "def compute_accuracy(log_prob, labels):\n", 392 | " predictions = np.argmax(log_prob, axis=1)\n", 393 | " truth = np.argmax(labels, axis=1)\n", 394 | " return np.mean(predictions==truth)\n", 395 | "\n", 396 | "def train_one_step(model, x, y):\n", 397 | " model.forward(x)\n", 398 | " model.backward(y)\n", 399 | " model.W1 -= 1e-5* model.W1_grad\n", 400 | " model.W2 -= 1e-5* model.W2_grad\n", 401 | " loss = compute_loss(model.h2_log, y)\n", 402 | " accuracy = compute_accuracy(model.h2_log, y)\n", 403 | " return loss, accuracy\n", 404 | "\n", 405 | "def test(model, x, y):\n", 406 | " model.forward(x)\n", 407 | " loss = compute_loss(model.h2_log, y)\n", 408 | " accuracy = compute_accuracy(model.h2_log, y)\n", 409 | " return loss, accuracy" 410 | ] 411 | }, 412 | { 413 | "cell_type": "markdown", 414 | "metadata": {}, 415 | "source": [ 416 | "## 实际训练" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 12, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "name": "stdout", 426 | "output_type": "stream", 427 | "text": [ 428 | "epoch 0 : loss 24.72223286454846 ; accuracy 0.07413333333333333\n", 429 | "epoch 1 : loss 23.924144333646588 ; accuracy 0.1002\n", 430 | "epoch 2 : loss 23.024883096483002 ; accuracy 0.1252\n", 431 | "epoch 3 : loss 21.789798024024133 ; accuracy 0.16403333333333334\n", 432 | "epoch 4 : loss 20.651041530819974 ; accuracy 0.209\n", 433 | "epoch 5 : loss 19.77011018744175 ; accuracy 0.23966666666666667\n", 434 | "epoch 6 : loss 18.820253731189734 ; accuracy 0.26993333333333336\n", 435 | "epoch 7 : loss 17.89411614473916 ; accuracy 0.3019\n", 436 | "epoch 8 : loss 17.241523788018537 ; accuracy 0.32666666666666666\n", 437 | "epoch 9 : loss 16.750174736771154 ; accuracy 0.34513333333333335\n", 438 | "epoch 10 : loss 16.34424433437691 ; accuracy 0.36118333333333336\n", 439 | "epoch 11 : loss 15.995649918471548 ; accuracy 0.37455\n", 440 | "epoch 12 : loss 15.686255253464532 ; accuracy 0.38605\n", 441 | "epoch 13 : loss 15.398560292015457 ; accuracy 0.39716666666666667\n", 442 | "epoch 14 : loss 15.108667168656357 ; accuracy 0.40665\n", 443 | "epoch 15 : loss 14.768259637386473 ; accuracy 0.41781666666666667\n", 444 | "epoch 16 : loss 14.336060964222801 ; accuracy 0.42795\n", 445 | "epoch 17 : loss 13.800404674290487 ; accuracy 0.4438166666666667\n", 446 | "epoch 18 : loss 13.238676768175882 ; accuracy 0.45813333333333334\n", 447 | "epoch 19 : loss 12.275000859496435 ; accuracy 0.49001666666666666\n", 448 | "epoch 20 : loss 11.789863414935745 ; accuracy 0.5096833333333334\n", 449 | "epoch 21 : loss 11.099507177500909 ; accuracy 0.5359833333333334\n", 450 | "epoch 22 : loss 10.947845925466538 ; accuracy 0.5452\n", 451 | "epoch 23 : loss 10.37985058714904 ; accuracy 0.5686333333333333\n", 452 | "epoch 24 : loss 10.194234392408456 ; accuracy 0.5764666666666667\n", 453 | "epoch 25 : loss 9.9158980895203 ; accuracy 0.5883666666666667\n", 454 | "epoch 26 : loss 9.73326221836977 ; accuracy 0.59515\n", 455 | "epoch 27 : loss 9.547157941634678 ; accuracy 0.6050833333333333\n", 456 | "epoch 28 : loss 9.364149820809562 ; accuracy 0.61045\n", 457 | "epoch 29 : loss 9.241444692225024 ; accuracy 0.6171333333333333\n", 458 | "epoch 30 : loss 9.033834821879237 ; accuracy 0.6230166666666667\n", 459 | "epoch 31 : loss 8.909350423734871 ; accuracy 0.62905\n", 460 | "epoch 32 : loss 8.686975386903871 ; accuracy 0.6366\n", 461 | "epoch 33 : loss 8.52761694967565 ; accuracy 0.6425166666666666\n", 462 | "epoch 34 : loss 8.280276344997858 ; accuracy 0.6510666666666667\n", 463 | "epoch 35 : loss 8.017204559951352 ; accuracy 0.6591833333333333\n", 464 | "epoch 36 : loss 7.6927551641724925 ; accuracy 0.6705333333333333\n", 465 | "epoch 37 : loss 7.289147376861199 ; accuracy 0.6818166666666666\n", 466 | "epoch 38 : loss 6.890080011599251 ; accuracy 0.69715\n", 467 | "epoch 39 : loss 6.621282824275839 ; accuracy 0.70885\n", 468 | "epoch 40 : loss 6.432028811711102 ; accuracy 0.71685\n", 469 | "epoch 41 : loss 6.297568845936132 ; accuracy 0.7227666666666667\n", 470 | "epoch 42 : loss 6.199755928239367 ; accuracy 0.72755\n", 471 | "epoch 43 : loss 6.1290342585715685 ; accuracy 0.73055\n", 472 | "epoch 44 : loss 6.063146731968448 ; accuracy 0.7336166666666667\n", 473 | "epoch 45 : loss 6.032675366835999 ; accuracy 0.7359\n", 474 | "epoch 46 : loss 5.960594129095456 ; accuracy 0.73785\n", 475 | "epoch 47 : loss 5.943588768247914 ; accuracy 0.73995\n", 476 | "epoch 48 : loss 5.8297749382365 ; accuracy 0.7436666666666667\n", 477 | "epoch 49 : loss 5.800353627628091 ; accuracy 0.7465833333333334\n", 478 | "test loss 5.590955869790617 ; accuracy 0.7588\n" 479 | ] 480 | } 481 | ], 482 | "source": [ 483 | "train_data, test_data = mnist_dataset()\n", 484 | "train_label = np.zeros(shape=[train_data[0].shape[0], 10])\n", 485 | "test_label = np.zeros(shape=[test_data[0].shape[0], 10])\n", 486 | "train_label[np.arange(train_data[0].shape[0]), np.array(train_data[1])] = 1.\n", 487 | "test_label[np.arange(test_data[0].shape[0]), np.array(test_data[1])] = 1.\n", 488 | "\n", 489 | "for epoch in range(50):\n", 490 | " loss, accuracy = train_one_step(model, train_data[0], train_label)\n", 491 | " print('epoch', epoch, ': loss', loss, '; accuracy', accuracy)\n", 492 | "loss, accuracy = test(model, test_data[0], test_label)\n", 493 | "\n", 494 | "print('test loss', loss, '; accuracy', accuracy)" 495 | ] 496 | } 497 | ], 498 | "metadata": { 499 | "kernelspec": { 500 | "display_name": "Python 3", 501 | "language": "python", 502 | "name": "python3" 503 | }, 504 | "language_info": { 505 | "codemirror_mode": { 506 | "name": "ipython", 507 | "version": 3 508 | }, 509 | "file_extension": ".py", 510 | "mimetype": "text/x-python", 511 | "name": "python", 512 | "nbconvert_exporter": "python", 513 | "pygments_lexer": "ipython3", 514 | "version": "3.7.0" 515 | } 516 | }, 517 | "nbformat": 4, 518 | "nbformat_minor": 2 519 | } 520 | -------------------------------------------------------------------------------- /chap4_ simple neural network/tutorial_minst_fnn-tf2.0-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 准备数据" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 7, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import os\n", 17 | "import numpy as np\n", 18 | "import tensorflow as tf\n", 19 | "from tensorflow import keras\n", 20 | "from tensorflow.keras import layers, optimizers, datasets\n", 21 | "\n", 22 | "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # or any {'0', '1', '2'}\n", 23 | "\n", 24 | "def mnist_dataset():\n", 25 | " (x, y), (x_test, y_test) = datasets.mnist.load_data()\n", 26 | " #normalize\n", 27 | " x = x/255.0\n", 28 | " x_test = x_test/255.0\n", 29 | " \n", 30 | " return (x, y), (x_test, y_test)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 8, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "print(list(zip([1, 2, 3, 4], ['a', 'b', 'c', 'd'])))" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## 建立模型" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "class myModel:\n", 64 | " def __init__(self):\n", 65 | " ####################\n", 66 | " '''声明模型对应的参数'''\n", 67 | " ####################\n", 68 | " def __call__(self, x):\n", 69 | " ####################\n", 70 | " '''实现模型函数体,返回未归一化的logits'''\n", 71 | " ####################\n", 72 | " return logits\n", 73 | " \n", 74 | "model = myModel()\n", 75 | "\n", 76 | "optimizer = optimizers.Adam()" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "## 计算 loss" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 13, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "@tf.function\n", 93 | "def compute_loss(logits, labels):\n", 94 | " return tf.reduce_mean(\n", 95 | " tf.nn.sparse_softmax_cross_entropy_with_logits(\n", 96 | " logits=logits, labels=labels))\n", 97 | "\n", 98 | "@tf.function\n", 99 | "def compute_accuracy(logits, labels):\n", 100 | " predictions = tf.argmax(logits, axis=1)\n", 101 | " return tf.reduce_mean(tf.cast(tf.equal(predictions, labels), tf.float32))\n", 102 | "\n", 103 | "@tf.function\n", 104 | "def train_one_step(model, optimizer, x, y):\n", 105 | " with tf.GradientTape() as tape:\n", 106 | " logits = model(x)\n", 107 | " loss = compute_loss(logits, y)\n", 108 | "\n", 109 | " # compute gradient\n", 110 | " trainable_vars = [model.W1, model.W2, model.b1, model.b2]\n", 111 | " grads = tape.gradient(loss, trainable_vars)\n", 112 | " for g, v in zip(grads, trainable_vars):\n", 113 | " v.assign_sub(0.01*g)\n", 114 | "\n", 115 | " accuracy = compute_accuracy(logits, y)\n", 116 | "\n", 117 | " # loss and accuracy is scalar tensor\n", 118 | " return loss, accuracy\n", 119 | "\n", 120 | "@tf.function\n", 121 | "def test(model, x, y):\n", 122 | " logits = model(x)\n", 123 | " loss = compute_loss(logits, y)\n", 124 | " accuracy = compute_accuracy(logits, y)\n", 125 | " return loss, accuracy" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "## 实际训练" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 14, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "epoch 0 : loss 0.7526799 ; accuracy 0.8300833\n", 145 | "epoch 1 : loss 0.7520259 ; accuracy 0.83023334\n", 146 | "epoch 2 : loss 0.7513737 ; accuracy 0.83031666\n", 147 | "epoch 3 : loss 0.75072306 ; accuracy 0.8305\n", 148 | "epoch 4 : loss 0.7500742 ; accuracy 0.8305167\n", 149 | "epoch 5 : loss 0.74942684 ; accuracy 0.83063334\n", 150 | "epoch 6 : loss 0.748781 ; accuracy 0.83071667\n", 151 | "epoch 7 : loss 0.74813706 ; accuracy 0.8308\n", 152 | "epoch 8 : loss 0.74749464 ; accuracy 0.8308667\n", 153 | "epoch 9 : loss 0.74685377 ; accuracy 0.83095\n", 154 | "epoch 10 : loss 0.74621457 ; accuracy 0.8311167\n", 155 | "epoch 11 : loss 0.74557704 ; accuracy 0.8312167\n", 156 | "epoch 12 : loss 0.7449409 ; accuracy 0.83133334\n", 157 | "epoch 13 : loss 0.7443065 ; accuracy 0.8314667\n", 158 | "epoch 14 : loss 0.7436737 ; accuracy 0.8315833\n", 159 | "epoch 15 : loss 0.74304247 ; accuracy 0.83165\n", 160 | "epoch 16 : loss 0.74241275 ; accuracy 0.8318167\n", 161 | "epoch 17 : loss 0.74178463 ; accuracy 0.8319333\n", 162 | "epoch 18 : loss 0.74115807 ; accuracy 0.83208334\n", 163 | "epoch 19 : loss 0.7405333 ; accuracy 0.83218336\n", 164 | "epoch 20 : loss 0.73990965 ; accuracy 0.8323333\n", 165 | "epoch 21 : loss 0.7392879 ; accuracy 0.83241665\n", 166 | "epoch 22 : loss 0.7386676 ; accuracy 0.8325833\n", 167 | "epoch 23 : loss 0.7380488 ; accuracy 0.8327\n", 168 | "epoch 24 : loss 0.73743147 ; accuracy 0.83278334\n", 169 | "epoch 25 : loss 0.7368157 ; accuracy 0.8329167\n", 170 | "epoch 26 : loss 0.7362015 ; accuracy 0.8330167\n", 171 | "epoch 27 : loss 0.7355887 ; accuracy 0.83318335\n", 172 | "epoch 28 : loss 0.7349776 ; accuracy 0.8332\n", 173 | "epoch 29 : loss 0.73436785 ; accuracy 0.8332833\n", 174 | "epoch 30 : loss 0.73375976 ; accuracy 0.8333333\n", 175 | "epoch 31 : loss 0.733153 ; accuracy 0.83343333\n", 176 | "epoch 32 : loss 0.73254776 ; accuracy 0.8335\n", 177 | "epoch 33 : loss 0.731944 ; accuracy 0.83351666\n", 178 | "epoch 34 : loss 0.7313418 ; accuracy 0.83365\n", 179 | "epoch 35 : loss 0.73074096 ; accuracy 0.83376664\n", 180 | "epoch 36 : loss 0.7301418 ; accuracy 0.83391666\n", 181 | "epoch 37 : loss 0.72954386 ; accuracy 0.83411664\n", 182 | "epoch 38 : loss 0.7289475 ; accuracy 0.83416665\n", 183 | "epoch 39 : loss 0.7283526 ; accuracy 0.83421665\n", 184 | "epoch 40 : loss 0.727759 ; accuracy 0.83421665\n", 185 | "epoch 41 : loss 0.72716707 ; accuracy 0.8343833\n", 186 | "epoch 42 : loss 0.7265763 ; accuracy 0.8344833\n", 187 | "epoch 43 : loss 0.72598726 ; accuracy 0.83461666\n", 188 | "epoch 44 : loss 0.7253995 ; accuracy 0.83468336\n", 189 | "epoch 45 : loss 0.7248133 ; accuracy 0.83488333\n", 190 | "epoch 46 : loss 0.72422826 ; accuracy 0.83493334\n", 191 | "epoch 47 : loss 0.7236447 ; accuracy 0.83501667\n", 192 | "epoch 48 : loss 0.72306263 ; accuracy 0.8351167\n", 193 | "epoch 49 : loss 0.7224819 ; accuracy 0.83515\n", 194 | "test loss 0.69837373 ; accuracy 0.846\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "train_data, test_data = mnist_dataset()\n", 200 | "for epoch in range(50):\n", 201 | " loss, accuracy = train_one_step(model, optimizer, \n", 202 | " tf.constant(train_data[0], dtype=tf.float32), \n", 203 | " tf.constant(train_data[1], dtype=tf.int64))\n", 204 | " print('epoch', epoch, ': loss', loss.numpy(), '; accuracy', accuracy.numpy())\n", 205 | "loss, accuracy = test(model, \n", 206 | " tf.constant(test_data[0], dtype=tf.float32), \n", 207 | " tf.constant(test_data[1], dtype=tf.int64))\n", 208 | "\n", 209 | "print('test loss', loss.numpy(), '; accuracy', accuracy.numpy())" 210 | ] 211 | } 212 | ], 213 | "metadata": { 214 | "kernelspec": { 215 | "display_name": "Python 3", 216 | "language": "python", 217 | "name": "python3" 218 | }, 219 | "language_info": { 220 | "codemirror_mode": { 221 | "name": "ipython", 222 | "version": 3 223 | }, 224 | "file_extension": ".py", 225 | "mimetype": "text/x-python", 226 | "name": "python", 227 | "nbconvert_exporter": "python", 228 | "pygments_lexer": "ipython3", 229 | "version": "3.7.0" 230 | } 231 | }, 232 | "nbformat": 4, 233 | "nbformat_minor": 2 234 | } 235 | -------------------------------------------------------------------------------- /chap5_CNN/CNN_pytorch.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 os\n", 12 | "import torch\n", 13 | "import torch.nn as nn\n", 14 | "from torch.autograd import Variable\n", 15 | "import torch.utils.data as Data\n", 16 | "import torchvision\n", 17 | "import torch.nn.functional as F\n", 18 | "import numpy as np\n", 19 | "learning_rate = 1e-4\n", 20 | "keep_prob_rate = 0.7 #\n", 21 | "max_epoch = 3\n", 22 | "BATCH_SIZE = 50\n", 23 | "\n", 24 | "DOWNLOAD_MNIST = False\n", 25 | "if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):\n", 26 | " # not mnist dir or mnist is empyt dir\n", 27 | " DOWNLOAD_MNIST = True\n", 28 | "\n", 29 | "\n", 30 | "train_data = torchvision.datasets.MNIST(root='./mnist/',train=True, transform=torchvision.transforms.ToTensor(), download=DOWNLOAD_MNIST,)\n", 31 | "train_loader = Data.DataLoader(dataset = train_data ,batch_size= BATCH_SIZE ,shuffle= True)\n", 32 | "\n", 33 | "test_data = torchvision.datasets.MNIST(root = './mnist/',train = False)\n", 34 | "test_x = Variable(torch.unsqueeze(test_data.test_data,dim = 1),volatile = True).type(torch.FloatTensor)[:500]/255.\n", 35 | "test_y = test_data.test_labels[:500].numpy()\n", 36 | "\n", 37 | "class CNN(nn.Module):\n", 38 | " def __init__(self):\n", 39 | " super(CNN, self).__init__()\n", 40 | " self.conv1 = nn.Sequential(\n", 41 | " nn.Conv2d( # ???\n", 42 | " # patch 7 * 7 ; 1 in channels ; 32 out channels ; ; stride is 1\n", 43 | " # padding style is same(that means the convolution opration's input and output have the same size)\n", 44 | " in_channels= , \n", 45 | " out_channels= ,\n", 46 | " kernel_size= ,\n", 47 | " stride= ,\n", 48 | " padding= ,\n", 49 | " ),\n", 50 | " nn.ReLU(), # activation function\n", 51 | " nn.MaxPool2d(2), # pooling operation\n", 52 | " )\n", 53 | " self.conv2 = nn.Sequential( # ???\n", 54 | " # line 1 : convolution function, patch 5*5 , 32 in channels ;64 out channels; padding style is same; stride is 1\n", 55 | " # line 2 : choosing your activation funciont\n", 56 | " # line 3 : pooling operation function.\n", 57 | " \n", 58 | " \n", 59 | "\n", 60 | " )\n", 61 | " self.out1 = nn.Linear( 7*7*64 , 1024 , bias= True) # full connection layer one\n", 62 | "\n", 63 | " self.dropout = nn.Dropout(keep_prob_rate)\n", 64 | " self.out2 = nn.Linear(1024,10,bias=True)\n", 65 | "\n", 66 | "\n", 67 | "\n", 68 | " def forward(self, x):\n", 69 | " x = self.conv1(x)\n", 70 | " x = self.conv2(x)\n", 71 | " x = x.view( ) # flatten the output of coonv2 to (batch_size ,32 * 7 * 7) # ???\n", 72 | " out1 = self.out1(x)\n", 73 | " out1 = F.relu(out1)\n", 74 | " out1 = self.dropout(out1)\n", 75 | " out2 = self.out2(out1)\n", 76 | " output = F.softmax(out2)\n", 77 | " return output\n", 78 | "\n", 79 | "\n", 80 | "def test(cnn):\n", 81 | " global prediction\n", 82 | " y_pre = cnn(test_x)\n", 83 | " _,pre_index= torch.max(y_pre,1)\n", 84 | " pre_index= pre_index.view(-1)\n", 85 | " prediction = pre_index.data.numpy()\n", 86 | " correct = np.sum(prediction == test_y)\n", 87 | " return correct / 500.0\n", 88 | "\n", 89 | "\n", 90 | "def train(cnn):\n", 91 | " optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate )\n", 92 | " loss_func = nn.CrossEntropyLoss()\n", 93 | " for epoch in range(max_epoch):\n", 94 | " for step, (x_, y_) in enumerate(train_loader):\n", 95 | " x ,y= Variable(x_),Variable(y_)\n", 96 | " output = cnn(x) \n", 97 | " loss = loss_func(output,y)\n", 98 | " optimizer.zero_grad()\n", 99 | " loss.backward()\n", 100 | " optimizer.step()\n", 101 | " \n", 102 | " if step != 0 and step % 20 ==0:\n", 103 | " print(\"=\" * 10,step,\"=\"*5,\"=\"*5, \"test accuracy is \",test(cnn) ,\"=\" * 10 )\n", 104 | "\n", 105 | "if __name__ == '__main__':\n", 106 | " cnn = CNN()\n", 107 | " train(cnn)\n", 108 | "\n", 109 | "\n" 110 | ] 111 | } 112 | ], 113 | "metadata": { 114 | "anaconda-cloud": {}, 115 | "kernelspec": { 116 | "display_name": "Python [default]", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.5.2" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 1 135 | } 136 | -------------------------------------------------------------------------------- /chap5_CNN/CNN_tensorflow.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 tensorflow as tf\n", 12 | "from tensorflow.examples.tutorials.mnist import input_data\n", 13 | "mnist = input_data.read_data_sets('MNIST_data', one_hot=True)\n", 14 | "\n", 15 | "learning_rate = 1e-4\n", 16 | "keep_prob_rate = 0.7 # \n", 17 | "max_epoch = 2000\n", 18 | "def compute_accuracy(v_xs, v_ys):\n", 19 | " global prediction\n", 20 | " y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})\n", 21 | " correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))\n", 22 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 23 | " result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})\n", 24 | " return result\n", 25 | "\n", 26 | "def weight_variable(shape):\n", 27 | " initial = tf.truncated_normal(shape, stddev=0.1)\n", 28 | " return tf.Variable(initial)\n", 29 | "\n", 30 | "def bias_variable(shape):\n", 31 | " initial = tf.constant(0.1, shape=shape)\n", 32 | " return tf.Variable(initial)\n", 33 | "\n", 34 | "def conv2d(x, W):\n", 35 | " # 每一维度 滑动步长全部是 1, padding 方式 选择 same\n", 36 | " # 提示 使用函数 tf.nn.conv2d\n", 37 | " \n", 38 | " return \n", 39 | "\n", 40 | "def max_pool_2x2(x):\n", 41 | " # 滑动步长 是 2步; 池化窗口的尺度 高和宽度都是2; padding 方式 请选择 same\n", 42 | " # 提示 使用函数 tf.nn.max_pool\n", 43 | " \n", 44 | " return \n", 45 | "\n", 46 | "# define placeholder for inputs to network\n", 47 | "xs = tf.placeholder(tf.float32, [None, 784])/255.\n", 48 | "ys = tf.placeholder(tf.float32, [None, 10])\n", 49 | "keep_prob = tf.placeholder(tf.float32)\n", 50 | "x_image = tf.reshape(xs, [-1, 28, 28, 1])\n", 51 | "\n", 52 | "# 卷积层 1\n", 53 | "## conv1 layer ##\n", 54 | "\n", 55 | "W_conv1 = # patch 7x7, in size 1, out size 32\n", 56 | "b_conv1 = \n", 57 | "h_conv1 = # 卷积 自己选择 选择激活函数\n", 58 | "h_pool1 = # 池化 \n", 59 | "\n", 60 | "# 卷积层 2\n", 61 | "W_conv2 = # patch 5x5, in size 32, out size 64\n", 62 | "b_conv2 = \n", 63 | "h_conv2 = # 卷积 自己选择 选择激活函数\n", 64 | "h_pool2 = # 池化\n", 65 | "\n", 66 | "# 全连接层 1\n", 67 | "## fc1 layer ##\n", 68 | "W_fc1 = weight_variable([7*7*64, 1024])\n", 69 | "b_fc1 = bias_variable([1024])\n", 70 | "\n", 71 | "h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])\n", 72 | "h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)\n", 73 | "h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)\n", 74 | "\n", 75 | "# 全连接层 2\n", 76 | "## fc2 layer ##\n", 77 | "W_fc2 = weight_variable([1024, 10])\n", 78 | "b_fc2 = bias_variable([10])\n", 79 | "prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)\n", 80 | "\n", 81 | "\n", 82 | "# 交叉熵函数\n", 83 | "cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),\n", 84 | " reduction_indices=[1]))\n", 85 | "train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)\n", 86 | "\n", 87 | "with tf.Session() as sess:\n", 88 | " init = tf.global_variables_initializer()\n", 89 | " sess.run(init)\n", 90 | " \n", 91 | " for i in range(max_epoch):\n", 92 | " batch_xs, batch_ys = mnist.train.next_batch(100)\n", 93 | " sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob:keep_prob_rate})\n", 94 | " if i % 100 == 0:\n", 95 | " print(compute_accuracy(\n", 96 | " mnist.test.images[:1000], mnist.test.labels[:1000]))\n" 97 | ] 98 | } 99 | ], 100 | "metadata": { 101 | "anaconda-cloud": {}, 102 | "kernelspec": { 103 | "display_name": "Python [conda root]", 104 | "language": "python", 105 | "name": "conda-root-py" 106 | }, 107 | "language_info": { 108 | "codemirror_mode": { 109 | "name": "ipython", 110 | "version": 3 111 | }, 112 | "file_extension": ".py", 113 | "mimetype": "text/x-python", 114 | "name": "python", 115 | "nbconvert_exporter": "python", 116 | "pygments_lexer": "ipython3", 117 | "version": "3.5.2" 118 | } 119 | }, 120 | "nbformat": 4, 121 | "nbformat_minor": 1 122 | } 123 | -------------------------------------------------------------------------------- /chap5_CNN/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 卷积神经网络(CNN) 4 | 5 | 6 | 7 | ## 问题描述: 8 | 9 | 利用卷积神经网络,实现对MNIST 数据集的分类问题。 10 | 11 | 12 | 13 | 14 | ## 数据集: 15 | 16 | MNIST数据集包括60000张训练图片和10000张测试图片。图片样本的数量已经足够训练一个很复杂的模型(例如 CNN的深层神经网络)。它经常被用来作为一个新 的模式识别模型的测试用例。而且它也是一个方便学生和研究者们执行用例的数据集。除此之外,MNIST数据集是一个相对较小的数据集,可以在你的笔记本CPUs上面直接执行 17 | 18 | 19 | 20 | 21 | 22 | ## 题目要求: 23 | 24 | tensorflow版的卷积神经网路 conv2d() 和max_pool_2x2()函数,然后补全两层卷积的 8个空; 25 | 26 | pytorch版本的卷积神经网络 需要补齐 self.conv1 中 nn.Conv2d( ) 和 self.conv2( ) 的参数,还需要填写 x = x.view( )中的内容。 27 | 28 | 两个版本的训练精度都应该在 96% 以上。 -------------------------------------------------------------------------------- /chap5_CNN/corgi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap5_CNN/corgi.jpg -------------------------------------------------------------------------------- /chap5_CNN/tutorial_mnist_conv-basic.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 准备数据" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 29, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import os\n", 17 | "import tensorflow as tf\n", 18 | "from tensorflow import keras\n", 19 | "from tensorflow.keras import layers, optimizers, datasets\n", 20 | "from tensorflow.keras.layers import Dense, Dropout, Flatten\n", 21 | "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n", 22 | "\n", 23 | "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # or any {'0', '1', '2'}\n", 24 | "\n", 25 | "def mnist_dataset():\n", 26 | " (x, y), (x_test, y_test) = datasets.mnist.load_data()\n", 27 | " x = x.reshape(x.shape[0], 28, 28,1)\n", 28 | " x_test = x_test.reshape(x_test.shape[0], 28, 28,1)\n", 29 | " \n", 30 | " ds = tf.data.Dataset.from_tensor_slices((x, y))\n", 31 | " ds = ds.map(prepare_mnist_features_and_labels)\n", 32 | " ds = ds.take(20000).shuffle(20000).batch(32)\n", 33 | " \n", 34 | " test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))\n", 35 | " test_ds = test_ds.map(prepare_mnist_features_and_labels)\n", 36 | " test_ds = test_ds.take(20000).shuffle(20000).batch(20000)\n", 37 | " return ds, test_ds\n", 38 | "\n", 39 | "def prepare_mnist_features_and_labels(x, y):\n", 40 | " x = tf.cast(x, tf.float32) / 255.0\n", 41 | " y = tf.cast(y, tf.int64)\n", 42 | " return x, y" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "## 建立模型" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 24, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "class myConvModel(keras.Model):\n", 66 | " def __init__(self):\n", 67 | " super(myConvModel, self).__init__()\n", 68 | " self.l1_conv = Conv2D(32, (5, 5), activation='relu', padding='same')\n", 69 | " self.l2_conv = Conv2D(64, (5, 5), activation='relu', padding='same')\n", 70 | " self.pool = MaxPooling2D(pool_size=(2, 2), strides=2)\n", 71 | " self.flat = Flatten()\n", 72 | " self.dense1 = layers.Dense(100, activation='tanh')\n", 73 | " self.dense2 = layers.Dense(10)\n", 74 | " @tf.function\n", 75 | " def call(self, x):\n", 76 | " h1 = self.l1_conv(x)\n", 77 | " h1_pool = self.pool(h1)\n", 78 | " h2 = self.l2_conv(h1_pool)\n", 79 | " h2_pool = self.pool(h2)\n", 80 | " flat_h = self.flat(h2_pool)\n", 81 | " dense1 = self.dense1(flat_h)\n", 82 | " logits = self.dense2(dense1)\n", 83 | " return logits\n", 84 | "\n", 85 | "model = myConvModel()\n", 86 | "optimizer = optimizers.Adam()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "## 定义loss以及train loop" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 25, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "@tf.function\n", 103 | "def compute_loss(logits, labels):\n", 104 | " return tf.reduce_mean(\n", 105 | " tf.nn.sparse_softmax_cross_entropy_with_logits(\n", 106 | " logits=logits, labels=labels))\n", 107 | "\n", 108 | "@tf.function\n", 109 | "def compute_accuracy(logits, labels):\n", 110 | " predictions = tf.argmax(logits, axis=1)\n", 111 | " return tf.reduce_mean(tf.cast(tf.equal(predictions, labels), tf.float32))\n", 112 | "\n", 113 | "@tf.function\n", 114 | "def train_one_step(model, optimizer, x, y):\n", 115 | "\n", 116 | " with tf.GradientTape() as tape:\n", 117 | " logits = model(x)\n", 118 | " loss = compute_loss(logits, y)\n", 119 | "\n", 120 | " # compute gradient\n", 121 | " grads = tape.gradient(loss, model.trainable_variables)\n", 122 | " # update to weights\n", 123 | " optimizer.apply_gradients(zip(grads, model.trainable_variables))\n", 124 | "\n", 125 | " accuracy = compute_accuracy(logits, y)\n", 126 | "\n", 127 | " # loss and accuracy is scalar tensor\n", 128 | " return loss, accuracy\n", 129 | "\n", 130 | "@tf.function\n", 131 | "def test_step(model, x, y):\n", 132 | " logits = model(x)\n", 133 | " loss = compute_loss(logits, y)\n", 134 | " accuracy = compute_accuracy(logits, y)\n", 135 | " return loss, accuracy\n", 136 | "\n", 137 | "def train(epoch, model, optimizer, ds):\n", 138 | " loss = 0.0\n", 139 | " accuracy = 0.0\n", 140 | " for step, (x, y) in enumerate(ds):\n", 141 | " loss, accuracy = train_one_step(model, optimizer, x, y)\n", 142 | "\n", 143 | " if step % 500 == 0:\n", 144 | " print('epoch', epoch, ': loss', loss.numpy(), '; accuracy', accuracy.numpy())\n", 145 | "\n", 146 | " return loss, accuracy\n", 147 | "def test(model, ds):\n", 148 | " loss = 0.0\n", 149 | " accuracy = 0.0\n", 150 | " for step, (x, y) in enumerate(ds):\n", 151 | " loss, accuracy = test_step(model, x, y)\n", 152 | "\n", 153 | " \n", 154 | " print('test loss', loss.numpy(), '; accuracy', accuracy.numpy())\n", 155 | "\n", 156 | " return loss, accuracy" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "# 训练" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 26, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "epoch 0 : loss 2.307023 ; accuracy 0.09375\n", 176 | "epoch 0 : loss 0.16172078 ; accuracy 0.96875\n", 177 | "epoch 1 : loss 0.15242997 ; accuracy 0.9375\n", 178 | "epoch 1 : loss 0.24359244 ; accuracy 0.9375\n", 179 | "test loss 0.057900324 ; accuracy 0.9812\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "train_ds, test_ds = mnist_dataset()\n", 185 | "for epoch in range(2):\n", 186 | " loss, accuracy = train(epoch, model, optimizer, train_ds)\n", 187 | "loss, accuracy = test(model, test_ds)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": null, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [] 203 | } 204 | ], 205 | "metadata": { 206 | "kernelspec": { 207 | "display_name": "Python 3", 208 | "language": "python", 209 | "name": "python3" 210 | }, 211 | "language_info": { 212 | "codemirror_mode": { 213 | "name": "ipython", 214 | "version": 3 215 | }, 216 | "file_extension": ".py", 217 | "mimetype": "text/x-python", 218 | "name": "python", 219 | "nbconvert_exporter": "python", 220 | "pygments_lexer": "ipython3", 221 | "version": "3.7.0" 222 | } 223 | }, 224 | "nbformat": 4, 225 | "nbformat_minor": 2 226 | } 227 | -------------------------------------------------------------------------------- /chap5_CNN/tutorial_mnist_conv-keras-sequential.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 准备数据" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import os\n", 17 | "import tensorflow as tf\n", 18 | "from tensorflow import keras\n", 19 | "from tensorflow.keras import layers, optimizers, datasets\n", 20 | "from tensorflow.keras.layers import Dense, Dropout, Flatten\n", 21 | "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n", 22 | "\n", 23 | "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # or any {'0', '1', '2'}\n", 24 | "\n", 25 | "def mnist_dataset():\n", 26 | " (x, y), (x_test, y_test) = datasets.mnist.load_data()\n", 27 | " x = x.reshape(x.shape[0], 28, 28,1)\n", 28 | " x_test = x_test.reshape(x_test.shape[0], 28, 28,1)\n", 29 | " ds = tf.data.Dataset.from_tensor_slices((x, y))\n", 30 | " ds = ds.map(prepare_mnist_features_and_labels)\n", 31 | " ds = ds.take(20000).shuffle(20000).batch(100)\n", 32 | " \n", 33 | " test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))\n", 34 | " test_ds = test_ds.map(prepare_mnist_features_and_labels)\n", 35 | " test_ds = test_ds.take(20000).shuffle(20000).batch(20000)\n", 36 | " return ds, test_ds\n", 37 | "\n", 38 | "def prepare_mnist_features_and_labels(x, y):\n", 39 | " x = tf.cast(x, tf.float32) / 255.0\n", 40 | " y = tf.cast(y, tf.int64)\n", 41 | " return x, y" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "3136" 53 | ] 54 | }, 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "7*7*64" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "## 建立模型" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "model = keras.Sequential([\n", 78 | " Conv2D(32, (5, 5), activation='relu', padding='same'),\n", 79 | " MaxPooling2D(pool_size=2, strides=2),\n", 80 | " Conv2D(64, (5, 5), activation='relu', padding='same'),\n", 81 | " MaxPooling2D(pool_size=2, strides=2),\n", 82 | " Flatten(), #N*7*7*64 =>N*3136\n", 83 | " layers.Dense(128, activation='tanh'), #N*128\n", 84 | " layers.Dense(10, activation='softmax')]) #N*10\n", 85 | "optimizer = optimizers.Adam(0.0001)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## 编译, fit以及evaluate" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 4, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "Epoch 1/5\n" 105 | ] 106 | }, 107 | { 108 | "name": "stderr", 109 | "output_type": "stream", 110 | "text": [ 111 | "WARNING: Logging before flag parsing goes to stderr.\n", 112 | "W0920 16:06:11.339046 4318938560 deprecation.py:323] From /Users/jerrik/a3/envs/tf2.0/lib/python3.7/site-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support..wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n", 113 | "Instructions for updating:\n", 114 | "Use tf.where in 2.0, which has the same broadcast rule as np.where\n" 115 | ] 116 | }, 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "200/200 [==============================] - 31s 154ms/step - loss: 0.9428 - accuracy: 0.6292\n", 122 | "Epoch 2/5\n", 123 | "200/200 [==============================] - 28s 139ms/step - loss: 0.2700 - accuracy: 0.9200\n", 124 | "Epoch 3/5\n", 125 | "200/200 [==============================] - 34s 169ms/step - loss: 0.1815 - accuracy: 0.9445\n", 126 | "Epoch 4/5\n", 127 | "200/200 [==============================] - 31s 154ms/step - loss: 0.1368 - accuracy: 0.9585\n", 128 | "Epoch 5/5\n", 129 | "200/200 [==============================] - 32s 159ms/step - loss: 0.1099 - accuracy: 0.9680\n", 130 | "1/1 [==============================] - 7s 7s/step - loss: 0.0984 - accuracy: 0.9710\n" 131 | ] 132 | }, 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "[0.09840553253889084, 0.971]" 137 | ] 138 | }, 139 | "execution_count": 4, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "model.compile(optimizer=optimizer,\n", 146 | " loss='sparse_categorical_crossentropy',\n", 147 | " metrics=['accuracy'])\n", 148 | "train_ds, test_ds = mnist_dataset()\n", 149 | "model.fit(train_ds, epochs=5)\n", 150 | "model.evaluate(test_ds)" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "metadata": {}, 164 | "outputs": [], 165 | "source": [] 166 | } 167 | ], 168 | "metadata": { 169 | "kernelspec": { 170 | "display_name": "Python 3", 171 | "language": "python", 172 | "name": "python3" 173 | }, 174 | "language_info": { 175 | "codemirror_mode": { 176 | "name": "ipython", 177 | "version": 3 178 | }, 179 | "file_extension": ".py", 180 | "mimetype": "text/x-python", 181 | "name": "python", 182 | "nbconvert_exporter": "python", 183 | "pygments_lexer": "ipython3", 184 | "version": "3.7.0" 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 2 189 | } 190 | -------------------------------------------------------------------------------- /chap5_CNN/tutorial_mnist_conv-keras.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## 准备数据" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import os\n", 17 | "import tensorflow as tf\n", 18 | "from tensorflow import keras\n", 19 | "from tensorflow.keras import layers, optimizers, datasets\n", 20 | "from tensorflow.keras.layers import Dense, Dropout, Flatten\n", 21 | "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n", 22 | "\n", 23 | "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # or any {'0', '1', '2'}\n", 24 | "\n", 25 | "def mnist_dataset():\n", 26 | " (x, y), (x_test, y_test) = datasets.mnist.load_data()\n", 27 | " x = x.reshape(x.shape[0], 28, 28,1)\n", 28 | " x_test = x_test.reshape(x_test.shape[0], 28, 28,1)\n", 29 | " ds = tf.data.Dataset.from_tensor_slices((x, y))\n", 30 | " ds = ds.map(prepare_mnist_features_and_labels)\n", 31 | " ds = ds.take(20000).shuffle(20000).batch(100)\n", 32 | " \n", 33 | " test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))\n", 34 | " test_ds = test_ds.map(prepare_mnist_features_and_labels)\n", 35 | " test_ds = test_ds.take(20000).shuffle(20000).batch(20000)\n", 36 | " return ds, test_ds\n", 37 | "\n", 38 | "def prepare_mnist_features_and_labels(x, y):\n", 39 | " x = tf.cast(x, tf.float32) / 255.0\n", 40 | " y = tf.cast(y, tf.int64)\n", 41 | " return x, y" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "## 建立模型" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 5, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "class myConvModel(keras.Model):\n", 58 | " def __init__(self):\n", 59 | " super(myConvModel, self).__init__()\n", 60 | " self.l1_conv = Conv2D(32, (5, 5), activation='relu', padding='same')\n", 61 | " self.l2_conv = Conv2D(64, (5, 5), activation='relu', padding='same')\n", 62 | " self.pool = MaxPooling2D(pool_size=(2, 2), strides=2)\n", 63 | " self.flat = Flatten()\n", 64 | " self.dense1 = layers.Dense(100, activation='tanh')\n", 65 | " self.dense2 = layers.Dense(10)\n", 66 | " @tf.function\n", 67 | " def call(self, x):\n", 68 | " h1 = self.l1_conv(x)\n", 69 | " h1_pool = self.pool(h1)\n", 70 | " h2 = self.l2_conv(h1_pool)\n", 71 | " h2_pool = self.pool(h2)\n", 72 | " flat_h = self.flat(h2_pool)\n", 73 | " dense1 = self.dense1(flat_h)\n", 74 | " logits = self.dense2(dense1)\n", 75 | " probs = tf.nn.softmax(logits, axis=-1)\n", 76 | " return probs\n", 77 | "\n", 78 | "model = myConvModel()\n", 79 | "optimizer = optimizers.Adam()" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "## 编译, fit以及evaluate" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "Epoch 1/5\n", 99 | "200/200 [==============================] - 27s 134ms/step - loss: 0.2757 - accuracy: 0.8197\n", 100 | "Epoch 2/5\n", 101 | "200/200 [==============================] - 26s 132ms/step - loss: 0.0678 - accuracy: 0.9782\n", 102 | "Epoch 3/5\n", 103 | "200/200 [==============================] - 24s 122ms/step - loss: 0.0405 - accuracy: 0.9880\n", 104 | "Epoch 4/5\n", 105 | "200/200 [==============================] - 27s 133ms/step - loss: 0.0252 - accuracy: 0.9939\n", 106 | "Epoch 5/5\n", 107 | "200/200 [==============================] - 25s 127ms/step - loss: 0.0181 - accuracy: 0.9954\n", 108 | "1/1 [==============================] - 5s 5s/step - loss: 0.0372 - accuracy: 0.9865\n" 109 | ] 110 | }, 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "[0.03720134124159813, 0.9865]" 115 | ] 116 | }, 117 | "execution_count": 6, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "model.compile(optimizer=optimizer,\n", 124 | " loss='sparse_categorical_crossentropy',\n", 125 | " metrics=['accuracy'])\n", 126 | "train_ds, test_ds = mnist_dataset()\n", 127 | "model.fit(train_ds, epochs=5)\n", 128 | "model.evaluate(test_ds)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | } 138 | ], 139 | "metadata": { 140 | "kernelspec": { 141 | "display_name": "Python 3", 142 | "language": "python", 143 | "name": "python3" 144 | }, 145 | "language_info": { 146 | "codemirror_mode": { 147 | "name": "ipython", 148 | "version": 3 149 | }, 150 | "file_extension": ".py", 151 | "mimetype": "text/x-python", 152 | "name": "python", 153 | "nbconvert_exporter": "python", 154 | "pygments_lexer": "ipython3", 155 | "version": "3.7.0" 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 2 160 | } 161 | -------------------------------------------------------------------------------- /chap6_RNN/Learn2Carry-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 加法进位实验\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "scrolled": true 22 | }, 23 | "outputs": [ 24 | { 25 | "name": "stderr", 26 | "output_type": "stream", 27 | "text": [ 28 | "/remote-home/jjgong01/a3/envs/tf2.0/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", 29 | " from ._conv import register_converters as _register_converters\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "import numpy as np\n", 35 | "import tensorflow as tf\n", 36 | "import collections\n", 37 | "from tensorflow import keras\n", 38 | "from tensorflow.keras import layers\n", 39 | "from tensorflow.keras import layers, optimizers, datasets\n", 40 | "import os,sys,tqdm\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## 数据生成\n", 48 | "我们随机在 `start->end`之间采样除整数对`(num1, num2)`,计算结果`num1+num2`作为监督信号。\n", 49 | "\n", 50 | "* 首先将数字转换成数字位列表 `convertNum2Digits`\n", 51 | "* 将数字位列表反向\n", 52 | "* 将数字位列表填充到同样的长度 `pad2len`\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "def gen_data_batch(batch_size, start, end):\n", 62 | " '''在(start, end)区间采样生成一个batch的整型的数据\n", 63 | " Args :\n", 64 | " batch_size: batch_size\n", 65 | " start: 开始数值\n", 66 | " end: 结束数值\n", 67 | " '''\n", 68 | " numbers_1 = np.random.randint(start, end, batch_size)\n", 69 | " numbers_2 = np.random.randint(start, end, batch_size)\n", 70 | " results = numbers_1 + numbers_2\n", 71 | " return numbers_1, numbers_2, results\n", 72 | "\n", 73 | "def convertNum2Digits(Num):\n", 74 | " '''将一个整数转换成一个数字位的列表,例如 133412 ==> [1, 3, 3, 4, 1, 2]\n", 75 | " '''\n", 76 | " strNum = str(Num)\n", 77 | " chNums = list(strNum)\n", 78 | " digitNums = [int(o) for o in strNum]\n", 79 | " return digitNums\n", 80 | "\n", 81 | "def convertDigits2Num(Digits):\n", 82 | " '''将数字位列表反向, 例如 [1, 3, 3, 4, 1, 2] ==> [2, 1, 4, 3, 3, 1]\n", 83 | " '''\n", 84 | " digitStrs = [str(o) for o in Digits]\n", 85 | " numStr = ''.join(digitStrs)\n", 86 | " Num = int(numStr)\n", 87 | " return Num\n", 88 | "\n", 89 | "def pad2len(lst, length, pad=0):\n", 90 | " '''将一个列表用`pad`填充到`length`的长度 例如 pad2len([1, 3, 2, 3], 6, pad=0) ==> [1, 3, 2, 3, 0, 0]\n", 91 | " '''\n", 92 | " lst+=[pad]*(length - len(lst))\n", 93 | " return lst\n", 94 | "\n", 95 | "def results_converter(res_lst):\n", 96 | " '''将预测好的数字位列表批量转换成为原始整数\n", 97 | " Args:\n", 98 | " res_lst: shape(b_sz, len(digits))\n", 99 | " '''\n", 100 | " res = [reversed(digits) for digits in res_lst]\n", 101 | " return [convertDigits2Num(digits) for digits in res]\n", 102 | "\n", 103 | "def prepare_batch(Nums1, Nums2, results, maxlen):\n", 104 | " '''准备一个batch的数据,将数值转换成反转的数位列表并且填充到固定长度\n", 105 | " Args:\n", 106 | " Nums1: shape(batch_size,)\n", 107 | " Nums2: shape(batch_size,)\n", 108 | " results: shape(batch_size,)\n", 109 | " maxlen: type(int)\n", 110 | " Returns:\n", 111 | " Nums1: shape(batch_size, maxlen)\n", 112 | " Nums2: shape(batch_size, maxlen)\n", 113 | " results: shape(batch_size, maxlen)\n", 114 | " '''\n", 115 | " Nums1 = [convertNum2Digits(o) for o in Nums1]\n", 116 | " Nums2 = [convertNum2Digits(o) for o in Nums2]\n", 117 | " results = [convertNum2Digits(o) for o in results]\n", 118 | " \n", 119 | " Nums1 = [list(reversed(o)) for o in Nums1]\n", 120 | " Nums2 = [list(reversed(o)) for o in Nums2]\n", 121 | " results = [list(reversed(o)) for o in results]\n", 122 | " \n", 123 | " Nums1 = [pad2len(o, maxlen) for o in Nums1]\n", 124 | " Nums2 = [pad2len(o, maxlen) for o in Nums2]\n", 125 | " results = [pad2len(o, maxlen) for o in results]\n", 126 | " \n", 127 | " return Nums1, Nums2, results" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "# 建模过程, 按照图示完成建模" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 3, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "class myRNNModel(keras.Model):\n", 144 | " def __init__(self):\n", 145 | " super(myRNNModel, self).__init__()\n", 146 | " self.embed_layer = tf.keras.layers.Embedding(10, 32, \n", 147 | " batch_input_shape=[None, None])\n", 148 | " \n", 149 | " self.rnncell = tf.keras.layers.SimpleRNNCell(64)\n", 150 | " self.rnn_layer = tf.keras.layers.RNN(self.rnncell, return_sequences=True)\n", 151 | " self.dense = tf.keras.layers.Dense(10)\n", 152 | " \n", 153 | " @tf.function\n", 154 | " def call(self, num1, num2):\n", 155 | " '''\n", 156 | " 此处完成上述图中模型\n", 157 | " '''\n", 158 | " return logits" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 4, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "@tf.function\n", 168 | "def compute_loss(logits, labels):\n", 169 | " losses = tf.nn.sparse_softmax_cross_entropy_with_logits(\n", 170 | " logits=logits, labels=labels)\n", 171 | " return tf.reduce_mean(losses)\n", 172 | "\n", 173 | "@tf.function\n", 174 | "def train_one_step(model, optimizer, x, y, label):\n", 175 | " with tf.GradientTape() as tape:\n", 176 | " logits = model(x, y)\n", 177 | " loss = compute_loss(logits, label)\n", 178 | "\n", 179 | " # compute gradient\n", 180 | " grads = tape.gradient(loss, model.trainable_variables)\n", 181 | " optimizer.apply_gradients(zip(grads, model.trainable_variables))\n", 182 | " return loss\n", 183 | "\n", 184 | "def train(steps, model, optimizer):\n", 185 | " loss = 0.0\n", 186 | " accuracy = 0.0\n", 187 | " for step in range(steps):\n", 188 | " datas = gen_data_batch(batch_size=200, start=0, end=555555555)\n", 189 | " Nums1, Nums2, results = prepare_batch(*datas, maxlen=11)\n", 190 | " loss = train_one_step(model, optimizer, tf.constant(Nums1, dtype=tf.int32), \n", 191 | " tf.constant(Nums2, dtype=tf.int32),\n", 192 | " tf.constant(results, dtype=tf.int32))\n", 193 | " if step%50 == 0:\n", 194 | " print('step', step, ': loss', loss.numpy())\n", 195 | "\n", 196 | " return loss\n", 197 | "\n", 198 | "def evaluate(model):\n", 199 | " datas = gen_data_batch(batch_size=2000, start=555555555, end=999999999)\n", 200 | " Nums1, Nums2, results = prepare_batch(*datas, maxlen=11)\n", 201 | " logits = model(tf.constant(Nums1, dtype=tf.int32), tf.constant(Nums2, dtype=tf.int32))\n", 202 | " logits = logits.numpy()\n", 203 | " pred = np.argmax(logits, axis=-1)\n", 204 | " res = results_converter(pred)\n", 205 | " for o in list(zip(datas[2], res))[:20]:\n", 206 | " print(o[0], o[1], o[0]==o[1])\n", 207 | "\n", 208 | " print('accuracy is: %g' % np.mean([o[0]==o[1] for o in zip(datas[2], res)]))\n" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 5, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "optimizer = optimizers.Adam(0.001)\n", 218 | "model = myRNNModel()" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 6, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "step 0 : loss 2.338743\n", 231 | "step 50 : loss 1.9090827\n", 232 | "step 100 : loss 1.8924072\n", 233 | "step 150 : loss 1.8782815\n", 234 | "step 200 : loss 1.8933363\n", 235 | "step 250 : loss 1.8919109\n", 236 | "step 300 : loss 1.8841321\n", 237 | "step 350 : loss 1.8789333\n", 238 | "step 400 : loss 1.8834051\n", 239 | "step 450 : loss 1.8841839\n", 240 | "step 500 : loss 1.8757857\n", 241 | "step 550 : loss 1.883435\n", 242 | "step 600 : loss 1.8695543\n", 243 | "step 650 : loss 1.8730036\n", 244 | "step 700 : loss 1.8712887\n", 245 | "step 750 : loss 1.8748087\n", 246 | "step 800 : loss 1.867813\n", 247 | "step 850 : loss 1.8665794\n", 248 | "step 900 : loss 1.8346387\n", 249 | "step 950 : loss 1.771845\n", 250 | "step 1000 : loss 1.66644\n", 251 | "step 1050 : loss 1.5232488\n", 252 | "step 1100 : loss 1.216567\n", 253 | "step 1150 : loss 0.98256004\n", 254 | "step 1200 : loss 0.8200839\n", 255 | "step 1250 : loss 0.69820565\n", 256 | "step 1300 : loss 0.5886905\n", 257 | "step 1350 : loss 0.50323135\n", 258 | "step 1400 : loss 0.42252934\n", 259 | "step 1450 : loss 0.36340263\n", 260 | "step 1500 : loss 0.2949791\n", 261 | "step 1550 : loss 0.24327374\n", 262 | "step 1600 : loss 0.20031683\n", 263 | "step 1650 : loss 0.16559783\n", 264 | "step 1700 : loss 0.13266504\n", 265 | "step 1750 : loss 0.11583002\n", 266 | "step 1800 : loss 0.09522024\n", 267 | "step 1850 : loss 0.08221528\n", 268 | "step 1900 : loss 0.07254601\n", 269 | "step 1950 : loss 0.06226328\n", 270 | "step 2000 : loss 0.054419417\n", 271 | "step 2050 : loss 0.049692407\n", 272 | "step 2100 : loss 0.043883115\n", 273 | "step 2150 : loss 0.038623303\n", 274 | "step 2200 : loss 0.03507858\n", 275 | "step 2250 : loss 0.031759493\n", 276 | "step 2300 : loss 0.028898347\n", 277 | "step 2350 : loss 0.026097616\n", 278 | "step 2400 : loss 0.024030704\n", 279 | "step 2450 : loss 0.022617567\n", 280 | "step 2500 : loss 0.020465054\n", 281 | "step 2550 : loss 0.018972643\n", 282 | "step 2600 : loss 0.017769149\n", 283 | "step 2650 : loss 0.016359653\n", 284 | "step 2700 : loss 0.015172582\n", 285 | "step 2750 : loss 0.014478247\n", 286 | "step 2800 : loss 0.013233788\n", 287 | "step 2850 : loss 0.012570047\n", 288 | "step 2900 : loss 0.011696805\n", 289 | "step 2950 : loss 0.01100141\n", 290 | "1314970279 1314970279 True\n", 291 | "1646630267 1646630267 True\n", 292 | "1765093362 1765093362 True\n", 293 | "1671475338 1671475338 True\n", 294 | "1467649664 1467649664 True\n", 295 | "1247588085 1247588085 True\n", 296 | "1396644726 1396644726 True\n", 297 | "1363359355 1363359355 True\n", 298 | "1617949833 1617949833 True\n", 299 | "1629931811 1629931811 True\n", 300 | "1452165053 1452165053 True\n", 301 | "1589216399 1589216399 True\n", 302 | "1696585095 1696585095 True\n", 303 | "1492344012 1492344012 True\n", 304 | "1398874117 1398874117 True\n", 305 | "1659389908 1659389908 True\n", 306 | "1808769780 1808769780 True\n", 307 | "1767070925 1767070925 True\n", 308 | "1252130370 1252130370 True\n", 309 | "1345605622 1345605622 True\n", 310 | "accuracy is: 1\n" 311 | ] 312 | } 313 | ], 314 | "source": [ 315 | "train(3000, model, optimizer)\n", 316 | "evaluate(model)" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 11, 322 | "metadata": { 323 | "collapsed": true 324 | }, 325 | "outputs": [], 326 | "source": [] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": null, 331 | "metadata": {}, 332 | "outputs": [], 333 | "source": [] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": null, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": null, 352 | "metadata": { 353 | "collapsed": true 354 | }, 355 | "outputs": [], 356 | "source": [] 357 | } 358 | ], 359 | "metadata": { 360 | "kernelspec": { 361 | "display_name": "Python 3", 362 | "language": "python", 363 | "name": "python3" 364 | }, 365 | "language_info": { 366 | "codemirror_mode": { 367 | "name": "ipython", 368 | "version": 3 369 | }, 370 | "file_extension": ".py", 371 | "mimetype": "text/x-python", 372 | "name": "python", 373 | "nbconvert_exporter": "python", 374 | "pygments_lexer": "ipython3", 375 | "version": "3.7.0" 376 | } 377 | }, 378 | "nbformat": 4, 379 | "nbformat_minor": 2 380 | } 381 | -------------------------------------------------------------------------------- /chap6_RNN/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 循环神经网络(RNN) 4 | 5 | 6 | 7 | ## 问题描述: 8 | 9 | 利用循环神经网络,实现唐诗生成任务。 10 | 11 | 12 | 13 | 14 | ## 数据集: 15 | 16 | 唐诗 17 | 18 | 19 | 20 | 21 | 22 | ## 题目要求: 23 | 24 | 补全程序,主要是前面的3个空和 生成诗歌的一段代码。(tensorflow) [pytorch 需要补全 对应的 rnn.py 文件中的两处代码] 25 | 26 | 生成诗歌 开头词汇是 “ 日 、 红 、 山 、 夜 、 湖、 海 、 月 。 27 | 28 | 参考文献: 29 | 30 | ​ Xingxing Zhang and Mirella Lapata. 2014. Chinese poetry generation with recurrent neural networks. In Proceedings of the 2014 Conference on EMNLP. Association for Computational Linguistics, October 31 | 32 | 并且参考了这篇博客 https://blog.csdn.net/Irving_zhang/article/details/76664998 33 | 34 | -------------------------------------------------------------------------------- /chap6_RNN/poem_generation_with_RNN-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 诗歌生成" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# 数据处理" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stderr", 24 | "output_type": "stream", 25 | "text": [ 26 | "/remote-home/jjgong01/a3/envs/tf2.0/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", 27 | " from ._conv import register_converters as _register_converters\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "import numpy as np\n", 33 | "import tensorflow as tf\n", 34 | "import collections\n", 35 | "from tensorflow import keras\n", 36 | "from tensorflow.keras import layers\n", 37 | "from tensorflow.keras import layers, optimizers, datasets\n", 38 | "\n", 39 | "start_token = 'bos'\n", 40 | "end_token = 'eos'\n", 41 | "\n", 42 | "def process_dataset(fileName):\n", 43 | " examples = []\n", 44 | " with open(fileName, 'r') as fd:\n", 45 | " for line in fd:\n", 46 | " outs = line.strip().split(':')\n", 47 | " content = ''.join(outs[1:])\n", 48 | " ins = [start_token] + list(content) + [end_token] \n", 49 | " if len(ins) > 200:\n", 50 | " continue\n", 51 | " examples.append(ins)\n", 52 | " \n", 53 | " counter = collections.Counter()\n", 54 | " for e in examples:\n", 55 | " for w in e:\n", 56 | " counter[w]+=1\n", 57 | " \n", 58 | " sorted_counter = sorted(counter.items(), key=lambda x: -x[1]) # 排序\n", 59 | " words, _ = zip(*sorted_counter)\n", 60 | " words = ('PAD', 'UNK') + words[:len(words)]\n", 61 | " word2id = dict(zip(words, range(len(words))))\n", 62 | " id2word = {word2id[k]:k for k in word2id}\n", 63 | " \n", 64 | " indexed_examples = [[word2id[w] for w in poem]\n", 65 | " for poem in examples]\n", 66 | " seqlen = [len(e) for e in indexed_examples]\n", 67 | " \n", 68 | " instances = list(zip(indexed_examples, seqlen))\n", 69 | " \n", 70 | " return instances, word2id, id2word\n", 71 | "\n", 72 | "def poem_dataset():\n", 73 | " instances, word2id, id2word = process_dataset('../poems.txt')\n", 74 | " ds = tf.data.Dataset.from_generator(lambda: [ins for ins in instances], \n", 75 | " (tf.int64, tf.int64), \n", 76 | " (tf.TensorShape([None]),tf.TensorShape([])))\n", 77 | " ds = ds.shuffle(buffer_size=10240)\n", 78 | " ds = ds.padded_batch(100, padded_shapes=(tf.TensorShape([None]),tf.TensorShape([])))\n", 79 | " ds = ds.map(lambda x, seqlen: (x[:, :-1], x[:, 1:], seqlen-1))\n", 80 | " return ds, word2id, id2word" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "# 模型代码, 完成建模代码" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 2, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "class myRNNModel(keras.Model):\n", 97 | " def __init__(self, w2id):\n", 98 | " super(myRNNModel, self).__init__()\n", 99 | " self.v_sz = len(w2id)\n", 100 | " self.embed_layer = tf.keras.layers.Embedding(self.v_sz, 64, \n", 101 | " batch_input_shape=[None, None])\n", 102 | " \n", 103 | " self.rnncell = tf.keras.layers.SimpleRNNCell(128)\n", 104 | " self.rnn_layer = tf.keras.layers.RNN(self.rnncell, return_sequences=True)\n", 105 | " self.dense = tf.keras.layers.Dense(self.v_sz)\n", 106 | " \n", 107 | " @tf.function\n", 108 | " def call(self, inp_ids):\n", 109 | " '''\n", 110 | " 此处完成建模过程,可以参考Learn2Carry\n", 111 | " '''\n", 112 | " return logits\n", 113 | " \n", 114 | " @tf.function\n", 115 | " def get_next_token(self, x, state):\n", 116 | " '''\n", 117 | " shape(x) = [b_sz,] \n", 118 | " '''\n", 119 | " \n", 120 | " inp_emb = self.embed_layer(x) #shape(b_sz, emb_sz)\n", 121 | " h, state = self.rnncell.call(inp_emb, state) # shape(b_sz, h_sz)\n", 122 | " logits = self.dense(h) # shape(b_sz, v_sz)\n", 123 | " out = tf.argmax(logits, axis=-1)\n", 124 | " return out, state" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "## 一个计算sequence loss的辅助函数,只需了解用途。" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 3, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "def mkMask(input_tensor, maxLen):\n", 141 | " shape_of_input = tf.shape(input_tensor)\n", 142 | " shape_of_output = tf.concat(axis=0, values=[shape_of_input, [maxLen]])\n", 143 | "\n", 144 | " oneDtensor = tf.reshape(input_tensor, shape=(-1,))\n", 145 | " flat_mask = tf.sequence_mask(oneDtensor, maxlen=maxLen)\n", 146 | " return tf.reshape(flat_mask, shape_of_output)\n", 147 | "\n", 148 | "\n", 149 | "def reduce_avg(reduce_target, lengths, dim):\n", 150 | " \"\"\"\n", 151 | " Args:\n", 152 | " reduce_target : shape(d_0, d_1,..,d_dim, .., d_k)\n", 153 | " lengths : shape(d0, .., d_(dim-1))\n", 154 | " dim : which dimension to average, should be a python number\n", 155 | " \"\"\"\n", 156 | " shape_of_lengths = lengths.get_shape()\n", 157 | " shape_of_target = reduce_target.get_shape()\n", 158 | " if len(shape_of_lengths) != dim:\n", 159 | " raise ValueError(('Second input tensor should be rank %d, ' +\n", 160 | " 'while it got rank %d') % (dim, len(shape_of_lengths)))\n", 161 | " if len(shape_of_target) < dim+1 :\n", 162 | " raise ValueError(('First input tensor should be at least rank %d, ' +\n", 163 | " 'while it got rank %d') % (dim+1, len(shape_of_target)))\n", 164 | "\n", 165 | " rank_diff = len(shape_of_target) - len(shape_of_lengths) - 1\n", 166 | " mxlen = tf.shape(reduce_target)[dim]\n", 167 | " mask = mkMask(lengths, mxlen)\n", 168 | " if rank_diff!=0:\n", 169 | " len_shape = tf.concat(axis=0, values=[tf.shape(lengths), [1]*rank_diff])\n", 170 | " mask_shape = tf.concat(axis=0, values=[tf.shape(mask), [1]*rank_diff])\n", 171 | " else:\n", 172 | " len_shape = tf.shape(lengths)\n", 173 | " mask_shape = tf.shape(mask)\n", 174 | " lengths_reshape = tf.reshape(lengths, shape=len_shape)\n", 175 | " mask = tf.reshape(mask, shape=mask_shape)\n", 176 | "\n", 177 | " mask_target = reduce_target * tf.cast(mask, dtype=reduce_target.dtype)\n", 178 | "\n", 179 | " red_sum = tf.reduce_sum(mask_target, axis=[dim], keepdims=False)\n", 180 | " red_avg = red_sum / (tf.cast(lengths_reshape, dtype=tf.float32) + 1e-30)\n", 181 | " return red_avg" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "# 定义loss函数,定义训练函数" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 4, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "@tf.function\n", 198 | "def compute_loss(logits, labels, seqlen):\n", 199 | " losses = tf.nn.sparse_softmax_cross_entropy_with_logits(\n", 200 | " logits=logits, labels=labels)\n", 201 | " losses = reduce_avg(losses, seqlen, dim=1)\n", 202 | " return tf.reduce_mean(losses)\n", 203 | "\n", 204 | "@tf.function\n", 205 | "def train_one_step(model, optimizer, x, y, seqlen):\n", 206 | " '''\n", 207 | " 完成一步优化过程,可以参考之前做过的模型\n", 208 | " '''\n", 209 | " return loss\n", 210 | "\n", 211 | "def train(epoch, model, optimizer, ds):\n", 212 | " loss = 0.0\n", 213 | " accuracy = 0.0\n", 214 | " for step, (x, y, seqlen) in enumerate(ds):\n", 215 | " loss = train_one_step(model, optimizer, x, y, seqlen)\n", 216 | "\n", 217 | " if step % 500 == 0:\n", 218 | " print('epoch', epoch, ': loss', loss.numpy())\n", 219 | "\n", 220 | " return loss" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "# 训练优化过程" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 5, 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "name": "stderr", 237 | "output_type": "stream", 238 | "text": [ 239 | "WARNING: Logging before flag parsing goes to stderr.\n", 240 | "W0921 02:36:07.816946 140256959960832 deprecation.py:323] From /remote-home/jjgong01/a3/envs/tf2.0/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py:410: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.\n", 241 | "Instructions for updating:\n", 242 | "tf.py_func is deprecated in TF V2. Instead, there are two\n", 243 | " options available in V2.\n", 244 | " - tf.py_function takes a python function which manipulates tf eager\n", 245 | " tensors instead of numpy arrays. It's easy to convert a tf eager tensor to\n", 246 | " an ndarray (just call tensor.numpy()) but having access to eager tensors\n", 247 | " means `tf.py_function`s can use accelerators such as GPUs as well as\n", 248 | " being differentiable using a gradient tape.\n", 249 | " - tf.numpy_function maintains the semantics of the deprecated tf.py_func\n", 250 | " (it is not differentiable, and manipulates numpy arrays). It drops the\n", 251 | " stateful argument making all functions stateful.\n", 252 | " \n" 253 | ] 254 | }, 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "epoch 0 : loss 8.8213\n", 260 | "epoch 1 : loss 6.508053\n", 261 | "epoch 2 : loss 5.8712797\n", 262 | "epoch 3 : loss 5.679646\n", 263 | "epoch 4 : loss 5.510748\n", 264 | "epoch 5 : loss 5.367529\n", 265 | "epoch 6 : loss 5.2667193\n", 266 | "epoch 7 : loss 5.172158\n", 267 | "epoch 8 : loss 5.096925\n", 268 | "epoch 9 : loss 5.0376143\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "optimizer = optimizers.Adam(0.0005)\n", 274 | "train_ds, word2id, id2word = poem_dataset()\n", 275 | "model = myRNNModel(word2id)\n", 276 | "\n", 277 | "for epoch in range(10):\n", 278 | " loss = train(epoch, model, optimizer, train_ds)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "# 生成过程" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 74, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "一年一里,花落花声。eosPAD山风雨,风吹落月。eosPAD山风雨,风吹落月。eosPAD山风雨,风吹落月。eosPAD山风雨,风\n" 298 | ] 299 | } 300 | ], 301 | "source": [ 302 | "def gen_sentence():\n", 303 | " state = [tf.random.normal(shape=(1, 128), stddev=0.5), tf.random.normal(shape=(1, 128), stddev=0.5)]\n", 304 | " cur_token = tf.constant([word2id['bos']], dtype=tf.int32)\n", 305 | " collect = []\n", 306 | " for _ in range(50):\n", 307 | " cur_token, state = model.get_next_token(cur_token, state)\n", 308 | " collect.append(cur_token.numpy()[0])\n", 309 | " return [id2word[t] for t in collect]\n", 310 | "print(''.join(gen_sentence()))" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": null, 316 | "metadata": {}, 317 | "outputs": [], 318 | "source": [] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [] 326 | } 327 | ], 328 | "metadata": { 329 | "anaconda-cloud": {}, 330 | "kernelspec": { 331 | "display_name": "Python 3", 332 | "language": "python", 333 | "name": "python3" 334 | }, 335 | "language_info": { 336 | "codemirror_mode": { 337 | "name": "ipython", 338 | "version": 3 339 | }, 340 | "file_extension": ".py", 341 | "mimetype": "text/x-python", 342 | "name": "python", 343 | "nbconvert_exporter": "python", 344 | "pygments_lexer": "ipython3", 345 | "version": "3.7.0" 346 | } 347 | }, 348 | "nbformat": 4, 349 | "nbformat_minor": 1 350 | } 351 | -------------------------------------------------------------------------------- /chap6_RNN/tangshi_for_pytorch/main.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import collections 3 | import torch 4 | from torch.autograd import Variable 5 | import torch.optim as optim 6 | 7 | import rnn 8 | 9 | start_token = 'G' 10 | end_token = 'E' 11 | batch_size = 64 12 | 13 | 14 | def process_poems1(file_name): 15 | """ 16 | 17 | :param file_name: 18 | :return: poems_vector have tow dimmention ,first is the poem, the second is the word_index 19 | e.g. [[1,2,3,4,5,6,7,8,9,10],[9,6,3,8,5,2,7,4,1]] 20 | 21 | """ 22 | poems = [] 23 | with open(file_name, "r", encoding='utf-8', ) as f: 24 | for line in f.readlines(): 25 | try: 26 | title, content = line.strip().split(':') 27 | # content = content.replace(' ', '').replace(',','').replace('。','') 28 | content = content.replace(' ', '') 29 | if '_' in content or '(' in content or '(' in content or '《' in content or '[' in content or \ 30 | start_token in content or end_token in content: 31 | continue 32 | if len(content) < 5 or len(content) > 80: 33 | continue 34 | content = start_token + content + end_token 35 | poems.append(content) 36 | except ValueError as e: 37 | print("error") 38 | pass 39 | # 按诗的字数排序 40 | poems = sorted(poems, key=lambda line: len(line)) 41 | # print(poems) 42 | # 统计每个字出现次数 43 | all_words = [] 44 | for poem in poems: 45 | all_words += [word for word in poem] 46 | counter = collections.Counter(all_words) # 统计词和词频。 47 | count_pairs = sorted(counter.items(), key=lambda x: -x[1]) # 排序 48 | words, _ = zip(*count_pairs) 49 | words = words[:len(words)] + (' ',) 50 | word_int_map = dict(zip(words, range(len(words)))) 51 | poems_vector = [list(map(word_int_map.get, poem)) for poem in poems] 52 | return poems_vector, word_int_map, words 53 | 54 | def process_poems2(file_name): 55 | """ 56 | :param file_name: 57 | :return: poems_vector have tow dimmention ,first is the poem, the second is the word_index 58 | e.g. [[1,2,3,4,5,6,7,8,9,10],[9,6,3,8,5,2,7,4,1]] 59 | 60 | """ 61 | poems = [] 62 | with open(file_name, "r", encoding='utf-8', ) as f: 63 | # content = '' 64 | for line in f.readlines(): 65 | try: 66 | line = line.strip() 67 | if line: 68 | content = line.replace(' '' ', '').replace(',','').replace('。','') 69 | if '_' in content or '(' in content or '(' in content or '《' in content or '[' in content or \ 70 | start_token in content or end_token in content: 71 | continue 72 | if len(content) < 5 or len(content) > 80: 73 | continue 74 | # print(content) 75 | content = start_token + content + end_token 76 | poems.append(content) 77 | # content = '' 78 | except ValueError as e: 79 | # print("error") 80 | pass 81 | # 按诗的字数排序 82 | poems = sorted(poems, key=lambda line: len(line)) 83 | # print(poems) 84 | # 统计每个字出现次数 85 | all_words = [] 86 | for poem in poems: 87 | all_words += [word for word in poem] 88 | counter = collections.Counter(all_words) # 统计词和词频。 89 | count_pairs = sorted(counter.items(), key=lambda x: -x[1]) # 排序 90 | words, _ = zip(*count_pairs) 91 | words = words[:len(words)] + (' ',) 92 | word_int_map = dict(zip(words, range(len(words)))) 93 | poems_vector = [list(map(word_int_map.get, poem)) for poem in poems] 94 | return poems_vector, word_int_map, words 95 | 96 | def generate_batch(batch_size, poems_vec, word_to_int): 97 | n_chunk = len(poems_vec) // batch_size 98 | x_batches = [] 99 | y_batches = [] 100 | for i in range(n_chunk): 101 | start_index = i * batch_size 102 | end_index = start_index + batch_size 103 | x_data = poems_vec[start_index:end_index] 104 | y_data = [] 105 | for row in x_data: 106 | y = row[1:] 107 | y.append(row[-1]) 108 | y_data.append(y) 109 | """ 110 | x_data y_data 111 | [6,2,4,6,9] [2,4,6,9,9] 112 | [1,4,2,8,5] [4,2,8,5,5] 113 | """ 114 | # print(x_data[0]) 115 | # print(y_data[0]) 116 | # exit(0) 117 | x_batches.append(x_data) 118 | y_batches.append(y_data) 119 | return x_batches, y_batches 120 | 121 | 122 | def run_training(): 123 | # 处理数据集 124 | # poems_vector, word_to_int, vocabularies = process_poems2('./tangshi.txt') 125 | poems_vector, word_to_int, vocabularies = process_poems1('./poems.txt') 126 | # 生成batch 127 | print("finish loadding data") 128 | BATCH_SIZE = 100 129 | 130 | torch.manual_seed(5) 131 | word_embedding = rnn_lstm.word_embedding( vocab_length= len(word_to_int) + 1 , embedding_dim= 100) 132 | rnn_model = rnn_lstm.RNN_model(batch_sz = BATCH_SIZE,vocab_len = len(word_to_int) + 1 ,word_embedding = word_embedding ,embedding_dim= 100, lstm_hidden_dim=128) 133 | 134 | # optimizer = optim.Adam(rnn_model.parameters(), lr= 0.001) 135 | optimizer=optim.RMSprop(rnn_model.parameters(), lr=0.01) 136 | 137 | loss_fun = torch.nn.NLLLoss() 138 | # rnn_model.load_state_dict(torch.load('./poem_generator_rnn')) # if you have already trained your model you can load it by this line. 139 | 140 | for epoch in range(30): 141 | batches_inputs, batches_outputs = generate_batch(BATCH_SIZE, poems_vector, word_to_int) 142 | n_chunk = len(batches_inputs) 143 | for batch in range(n_chunk): 144 | batch_x = batches_inputs[batch] 145 | batch_y = batches_outputs[batch] # (batch , time_step) 146 | 147 | loss = 0 148 | for index in range(BATCH_SIZE): 149 | x = np.array(batch_x[index], dtype = np.int64) 150 | y = np.array(batch_y[index], dtype = np.int64) 151 | x = Variable(torch.from_numpy(np.expand_dims(x,axis=1))) 152 | y = Variable(torch.from_numpy(y )) 153 | pre = rnn_model(x) 154 | loss += loss_fun(pre , y) 155 | if index == 0: 156 | _, pre = torch.max(pre, dim=1) 157 | print('prediction', pre.data.tolist()) # the following three line can print the output and the prediction 158 | print('b_y ', y.data.tolist()) # And you need to take a screenshot and then past is to your homework paper. 159 | print('*' * 30) 160 | loss = loss / BATCH_SIZE 161 | print("epoch ",epoch,'batch number',batch,"loss is: ", loss.data.tolist()) 162 | optimizer.zero_grad() 163 | loss.backward() 164 | torch.nn.utils.clip_grad_norm(rnn_model.parameters(), 1) 165 | optimizer.step() 166 | 167 | if batch % 20 ==0: 168 | torch.save(rnn_model.state_dict(), './poem_generator_rnn') 169 | print("finish save model") 170 | 171 | 172 | 173 | def to_word(predict, vocabs): # 预测的结果转化成汉字 174 | sample = np.argmax(predict) 175 | 176 | if sample >= len(vocabs): 177 | sample = len(vocabs) - 1 178 | 179 | return vocabs[sample] 180 | 181 | 182 | def pretty_print_poem(poem): # 令打印的结果更工整 183 | shige=[] 184 | for w in poem: 185 | if w == start_token or w == end_token: 186 | break 187 | shige.append(w) 188 | poem_sentences = poem.split('。') 189 | for s in poem_sentences: 190 | if s != '' and len(s) > 10: 191 | print(s + '。') 192 | 193 | 194 | def gen_poem(begin_word): 195 | # poems_vector, word_int_map, vocabularies = process_poems2('./tangshi.txt') # use the other dataset to train the network 196 | poems_vector, word_int_map, vocabularies = process_poems1('./poems.txt') 197 | word_embedding = rnn_lstm.word_embedding(vocab_length=len(word_int_map) + 1, embedding_dim=100) 198 | rnn_model = rnn_lstm.RNN_model(batch_sz=64, vocab_len=len(word_int_map) + 1, word_embedding=word_embedding, 199 | embedding_dim=100, lstm_hidden_dim=128) 200 | 201 | rnn_model.load_state_dict(torch.load('./poem_generator_rnn')) 202 | 203 | # 指定开始的字 204 | 205 | poem = begin_word 206 | word = begin_word 207 | while word != end_token: 208 | input = np.array([word_int_map[w] for w in poem],dtype= np.int64) 209 | input = Variable(torch.from_numpy(input)) 210 | output = rnn_model(input, is_test=True) 211 | word = to_word(output.data.tolist()[-1], vocabularies) 212 | poem += word 213 | # print(word) 214 | # print(poem) 215 | if len(poem) > 30: 216 | break 217 | return poem 218 | 219 | 220 | 221 | run_training() # 如果不是训练阶段 ,请注销这一行 。 网络训练时间很长。 222 | 223 | 224 | pretty_print_poem(gen_poem("日")) 225 | pretty_print_poem(gen_poem("红")) 226 | pretty_print_poem(gen_poem("山")) 227 | pretty_print_poem(gen_poem("夜")) 228 | pretty_print_poem(gen_poem("湖")) 229 | pretty_print_poem(gen_poem("湖")) 230 | pretty_print_poem(gen_poem("湖")) 231 | pretty_print_poem(gen_poem("君")) 232 | 233 | 234 | -------------------------------------------------------------------------------- /chap6_RNN/tangshi_for_pytorch/rnn.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | from torch.autograd import Variable 4 | import torch.nn.functional as F 5 | 6 | import numpy as np 7 | 8 | def weights_init(m): 9 | classname = m.__class__.__name__ # obtain the class name 10 | if classname.find('Linear') != -1: 11 | weight_shape = list(m.weight.data.size()) 12 | fan_in = weight_shape[1] 13 | fan_out = weight_shape[0] 14 | w_bound = np.sqrt(6. / (fan_in + fan_out)) 15 | m.weight.data.uniform_(-w_bound, w_bound) 16 | m.bias.data.fill_(0) 17 | print("inital linear weight ") 18 | 19 | 20 | class word_embedding(nn.Module): 21 | def __init__(self,vocab_length , embedding_dim): 22 | super(word_embedding, self).__init__() 23 | w_embeding_random_intial = np.random.uniform(-1,1,size=(vocab_length ,embedding_dim)) 24 | self.word_embedding = nn.Embedding(vocab_length,embedding_dim) 25 | self.word_embedding.weight.data.copy_(torch.from_numpy(w_embeding_random_intial)) 26 | def forward(self,input_sentence): 27 | """ 28 | :param input_sentence: a tensor ,contain several word index. 29 | :return: a tensor ,contain word embedding tensor 30 | """ 31 | sen_embed = self.word_embedding(input_sentence) 32 | return sen_embed 33 | 34 | 35 | class RNN_model(nn.Module): 36 | def __init__(self, batch_sz ,vocab_len ,word_embedding,embedding_dim, lstm_hidden_dim): 37 | super(RNN_model,self).__init__() 38 | 39 | self.word_embedding_lookup = word_embedding 40 | self.batch_size = batch_sz 41 | self.vocab_length = vocab_len 42 | self.word_embedding_dim = embedding_dim 43 | self.lstm_dim = lstm_hidden_dim 44 | ######################################### 45 | # here you need to define the "self.rnn_lstm" the input size is "embedding_dim" and the output size is "lstm_hidden_dim" 46 | # the lstm should have two layers, and the input and output tensors are provided as (batch, seq, feature) 47 | # ??? 48 | 49 | 50 | 51 | ########################################## 52 | self.fc = nn.Linear(lstm_hidden_dim, vocab_len ) 53 | self.apply(weights_init) # call the weights initial function. 54 | 55 | self.softmax = nn.LogSoftmax() # the activation function. 56 | # self.tanh = nn.Tanh() 57 | def forward(self,sentence,is_test = False): 58 | batch_input = self.word_embedding_lookup(sentence).view(1,-1,self.word_embedding_dim) 59 | # print(batch_input.size()) # print the size of the input 60 | ################################################ 61 | # here you need to put the "batch_input" input the self.lstm which is defined before. 62 | # the hidden output should be named as output, the initial hidden state and cell state set to zero. 63 | # ??? 64 | 65 | 66 | 67 | 68 | ################################################ 69 | out = output.contiguous().view(-1,self.lstm_dim) 70 | 71 | out = F.relu(self.fc(out)) 72 | 73 | out = self.softmax(out) 74 | 75 | if is_test: 76 | prediction = out[ -1, : ].view(1,-1) 77 | output = prediction 78 | else: 79 | output = out 80 | # print(out) 81 | return output 82 | 83 | -------------------------------------------------------------------------------- /chap6_RNN/题目要求: -------------------------------------------------------------------------------- 1 | 2 | 作业主要内容: 3 | 1. 补全程序,主要是前面的3个空和 生成诗歌的一段代码。(tensorflow) [pytorCH 需要补全 对应的 rnn.py 文件中的两处代码](pytorch 和 tensorflow 4 | 两个版本中任意选择一个即可。) 5 | 2. 解释一下 RNN ,LSTM,GRU模型, 6 | 3. 叙述一下 这个诗歌生成的过程。 7 | 4. 生成诗歌 开头词汇是 “ 日 、 红 、 山 、 夜 、 湖、 海 、 月 ” ,等词汇作为begin word,把生成的截图放到报告里面。[pytorch 版本 需要放一张 训练时候的截图。] 8 | 9 | 报告需要包含作业内容的 2、3、4部分和 试验的总结。 10 | 11 | 参考文献: 12 | Xingxing Zhang and Mirella Lapata. 2014. Chinese poetry generation with recurrent neural networks. In Proceedings of the 2014 Conference on EMNLP. Association for Computational Linguistics, October 13 | -------------------------------------------------------------------------------- /chap7-seq2seq-and-attention/seq2seq-attn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap7-seq2seq-and-attention/seq2seq-attn.jpg -------------------------------------------------------------------------------- /chap7-seq2seq-and-attention/seq2seq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nndl/exercise/6fe84b5490f6dce789eba2c1b9b7128fe2f5bda4/chap7-seq2seq-and-attention/seq2seq.png -------------------------------------------------------------------------------- /chap7-seq2seq-and-attention/sequence_reversal-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 序列逆置\n", 8 | "使用sequence to sequence 模型将一个字符串序列逆置。\n", 9 | "例如 `OIMESIQFIQ` 逆置成 `QIFQISEMIO`(下图来自网络,是一个sequence to sequence 模型示意图 )\n", 10 | "![seq2seq](./seq2seq.png)" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "import numpy as np\n", 20 | "import tensorflow as tf\n", 21 | "import collections\n", 22 | "from tensorflow import keras\n", 23 | "from tensorflow.keras import layers\n", 24 | "from tensorflow.keras import layers, optimizers, datasets\n", 25 | "import os,sys,tqdm" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## 玩具序列数据生成\n", 33 | "生成只包含[A-Z]的字符串,并且将encoder输入以及decoder输入以及decoder输出准备好(转成index)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "(['IREBUBLOTS', 'IWEPUFQWJD'], , , )\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "import random\n", 57 | "import string\n", 58 | "\n", 59 | "def randomString(stringLength):\n", 60 | " \"\"\"Generate a random string with the combination of lowercase and uppercase letters \"\"\"\n", 61 | "\n", 62 | " letters = string.ascii_uppercase\n", 63 | " return ''.join(random.choice(letters) for i in range(stringLength))\n", 64 | "\n", 65 | "def get_batch(batch_size, length):\n", 66 | " batched_examples = [randomString(length) for i in range(batch_size)]\n", 67 | " enc_x = [[ord(ch)-ord('A')+1 for ch in list(exp)] for exp in batched_examples]\n", 68 | " y = [[o for o in reversed(e_idx)] for e_idx in enc_x]\n", 69 | " dec_x = [[0]+e_idx[:-1] for e_idx in y]\n", 70 | " return (batched_examples, tf.constant(enc_x, dtype=tf.int32), \n", 71 | " tf.constant(dec_x, dtype=tf.int32), tf.constant(y, dtype=tf.int32))\n", 72 | "print(get_batch(2, 10))" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "# 建立sequence to sequence 模型" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "class mySeq2SeqModel(keras.Model):\n", 89 | " def __init__(self):\n", 90 | " super(mySeq2SeqModel, self).__init__()\n", 91 | " self.v_sz=27\n", 92 | " self.embed_layer = tf.keras.layers.Embedding(self.v_sz, 64, \n", 93 | " batch_input_shape=[None, None])\n", 94 | " \n", 95 | " self.encoder_cell = tf.keras.layers.SimpleRNNCell(128)\n", 96 | " self.decoder_cell = tf.keras.layers.SimpleRNNCell(128)\n", 97 | " \n", 98 | " self.encoder = tf.keras.layers.RNN(self.encoder_cell, \n", 99 | " return_sequences=True, return_state=True)\n", 100 | " self.decoder = tf.keras.layers.RNN(self.decoder_cell, \n", 101 | " return_sequences=True, return_state=True)\n", 102 | " self.dense = tf.keras.layers.Dense(self.v_sz)\n", 103 | " \n", 104 | " @tf.function\n", 105 | " def call(self, enc_ids, dec_ids):\n", 106 | " '''\n", 107 | " 完成sequence2sequence 模型的搭建,模块已经在`__init__`函数中定义好\n", 108 | " '''\n", 109 | " return logits\n", 110 | " \n", 111 | " \n", 112 | "# @tf.function\n", 113 | " def encode(self, enc_ids):\n", 114 | " enc_emb = self.embed_layer(enc_ids) # shape(b_sz, len, emb_sz)\n", 115 | " enc_out, enc_state = self.encoder(enc_emb)\n", 116 | " \n", 117 | " return [enc_out[:, -1, :], enc_state]\n", 118 | " \n", 119 | " def get_next_token(self, x, state):\n", 120 | " '''\n", 121 | " shape(x) = [b_sz,] \n", 122 | " '''\n", 123 | " inp_emb = self.embed_layer(x) #shape(b_sz, emb_sz)\n", 124 | " h, state = self.decoder_cell.call(inp_emb, state) # shape(b_sz, h_sz)\n", 125 | " logits = self.dense(h) # shape(b_sz, v_sz)\n", 126 | " out = tf.argmax(logits, axis=-1)\n", 127 | " return out, state" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "# Loss函数以及训练逻辑" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 4, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "@tf.function\n", 144 | "def compute_loss(logits, labels):\n", 145 | " losses = tf.nn.sparse_softmax_cross_entropy_with_logits(\n", 146 | " logits=logits, labels=labels)\n", 147 | " losses = tf.reduce_mean(losses)\n", 148 | " return losses\n", 149 | "\n", 150 | "@tf.function\n", 151 | "def train_one_step(model, optimizer, enc_x, dec_x, y):\n", 152 | " with tf.GradientTape() as tape:\n", 153 | " logits = model(enc_x, dec_x)\n", 154 | " loss = compute_loss(logits, y)\n", 155 | "\n", 156 | " # compute gradient\n", 157 | " grads = tape.gradient(loss, model.trainable_variables)\n", 158 | " optimizer.apply_gradients(zip(grads, model.trainable_variables))\n", 159 | " return loss\n", 160 | "\n", 161 | "def train(model, optimizer, seqlen):\n", 162 | " loss = 0.0\n", 163 | " accuracy = 0.0\n", 164 | " for step in range(3000):\n", 165 | " batched_examples, enc_x, dec_x, y = get_batch(32, seqlen)\n", 166 | " loss = train_one_step(model, optimizer, enc_x, dec_x, y)\n", 167 | " if step % 500 == 0:\n", 168 | " print('step', step, ': loss', loss.numpy())\n", 169 | " return loss" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "# 训练迭代" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 5, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "step 0 : loss 3.3078957\n", 189 | "step 500 : loss 1.6194503\n", 190 | "step 1000 : loss 1.1144965\n", 191 | "step 1500 : loss 0.8585207\n", 192 | "step 2000 : loss 0.69941413\n", 193 | "step 2500 : loss 0.6125215\n" 194 | ] 195 | }, 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "" 200 | ] 201 | }, 202 | "execution_count": 5, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "optimizer = optimizers.Adam(0.0005)\n", 209 | "model = mySeq2SeqModel()\n", 210 | "train(model, optimizer, seqlen=20)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "# 测试模型逆置能力\n", 218 | "首先要先对输入的一个字符串进行encode,然后在用decoder解码出逆置的字符串\n", 219 | "\n", 220 | "测试阶段跟训练阶段的区别在于,在训练的时候decoder的输入是给定的,而在预测的时候我们需要一步步生成下一步的decoder的输入" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 6, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "name": "stdout", 230 | "output_type": "stream", 231 | "text": [ 232 | "[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]\n", 233 | "[('OIMESIQFIQ', 'QIFQISEMIO'), ('MCFHOXNWHS', 'SHWNXOHFCM'), ('BYGBKVOAQT', 'TQAOVKBGYB'), ('RHSOVKGUWI', 'IWUGKVOSHR'), ('LKHWBGZXBO', 'OBXZGBWHKL'), ('ERYQCZPWKB', 'BKWPZCQYRE'), ('ZIQQHPBJTR', 'RTJBPHQQIZ'), ('AQFDDTPZQH', 'HQZPTDDFQA'), ('PRWJBEGJEK', 'QMJQEEJWRP'), ('MRAWVSFNZL', 'LZNFSVWARM'), ('CLFVWJRCJV', 'HQBIJWZFLC'), ('WARGSOPMKZ', 'ZKMPOSGRAW'), ('OLWBOACXQY', 'YQXCAOBWLO'), ('VBKDBZCRIP', 'PIRCZBDKBV'), ('BRWCJPWTWQ', 'QWTWPJCWRB'), ('ZARPMFTSES', 'SESTFMPRAZ'), ('VXGVVJFUBR', 'RBUFJVVGXV'), ('ETAQRUYCCQ', 'QCCYURQATE'), ('BIOVWZODCI', 'ICDOZWVOIB'), ('RLOAZAKWEP', 'PEWKAZAOLR'), ('YRMRDECTVZ', 'ZVTCEDRMRY'), ('WMPHBAHFGJ', 'JGFHABHPMW'), ('LEAHJTFHEG', 'GEHFTJHAEL'), ('WIHALQECAX', 'XACEQLAHIW'), ('XYDOZHQOTE', 'ETOQHZODYX'), ('XEVMHNNBRS', 'SRBNNHMVEX'), ('JCKCQRHZYP', 'PYZHRQCKCJ'), ('FZZROYFMUR', 'RUMFYORZZF'), ('IMDNMAIJEH', 'HEJIAMNDMI'), ('LHNWNFQNQH', 'ZQNQJNONHL'), ('MPLVUUCUUG', 'GUUCUUVLPM'), ('AAFWQGSHWO', 'OWHSGQWFAA')]\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "def sequence_reversal():\n", 239 | " def decode(init_state, steps=10):\n", 240 | " b_sz = tf.shape(init_state[0])[0]\n", 241 | " cur_token = tf.zeros(shape=[b_sz], dtype=tf.int32)\n", 242 | " state = init_state\n", 243 | " collect = []\n", 244 | " for i in range(steps):\n", 245 | " cur_token, state = model.get_next_token(cur_token, state)\n", 246 | " collect.append(tf.expand_dims(cur_token, axis=-1))\n", 247 | " out = tf.concat(collect, axis=-1).numpy()\n", 248 | " out = [''.join([chr(idx+ord('A')-1) for idx in exp]) for exp in out]\n", 249 | " return out\n", 250 | " \n", 251 | " batched_examples, enc_x, _, _ = get_batch(32, 10)\n", 252 | " state = model.encode(enc_x)\n", 253 | " return decode(state, enc_x.get_shape()[-1]), batched_examples\n", 254 | "\n", 255 | "def is_reverse(seq, rev_seq):\n", 256 | " rev_seq_rev = ''.join([i for i in reversed(list(rev_seq))])\n", 257 | " if seq == rev_seq_rev:\n", 258 | " return True\n", 259 | " else:\n", 260 | " return False\n", 261 | "print([is_reverse(*item) for item in list(zip(*sequence_reversal()))])\n", 262 | "print(list(zip(*sequence_reversal())))" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [] 271 | } 272 | ], 273 | "metadata": { 274 | "kernelspec": { 275 | "display_name": "Python 3", 276 | "language": "python", 277 | "name": "python3" 278 | }, 279 | "language_info": { 280 | "codemirror_mode": { 281 | "name": "ipython", 282 | "version": 3 283 | }, 284 | "file_extension": ".py", 285 | "mimetype": "text/x-python", 286 | "name": "python", 287 | "nbconvert_exporter": "python", 288 | "pygments_lexer": "ipython3", 289 | "version": "3.7.0" 290 | } 291 | }, 292 | "nbformat": 4, 293 | "nbformat_minor": 2 294 | } 295 | -------------------------------------------------------------------------------- /chap7-seq2seq-and-attention/sequence_reversal_with_attention-exercise.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 序列逆置 (加注意力的seq2seq)\n", 8 | "使用attentive sequence to sequence 模型将一个字符串序列逆置。例如 `OIMESIQFIQ` 逆置成 `QIFQISEMIO`(下图来自网络,是一个加attentino的sequence to sequence 模型示意图)\n", 9 | "![attentive seq2seq](./seq2seq-attn.jpg)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 19, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "import tensorflow as tf\n", 20 | "import collections\n", 21 | "from tensorflow import keras\n", 22 | "from tensorflow.keras import layers\n", 23 | "from tensorflow.keras import layers, optimizers, datasets\n", 24 | "import os,sys,tqdm" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## 玩具序列数据生成\n", 32 | "生成只包含[A-Z]的字符串,并且将encoder输入以及decoder输入以及decoder输出准备好(转成index)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 20, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "(['GLSUIOUHPB', 'QLXIKLZQBX'], , , )\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "import random\n", 56 | "import string\n", 57 | "\n", 58 | "def randomString(stringLength):\n", 59 | " \"\"\"Generate a random string with the combination of lowercase and uppercase letters \"\"\"\n", 60 | "\n", 61 | " letters = string.ascii_uppercase\n", 62 | " return ''.join(random.choice(letters) for i in range(stringLength))\n", 63 | "\n", 64 | "def get_batch(batch_size, length):\n", 65 | " batched_examples = [randomString(length) for i in range(batch_size)]\n", 66 | " enc_x = [[ord(ch)-ord('A')+1 for ch in list(exp)] for exp in batched_examples]\n", 67 | " y = [[o for o in reversed(e_idx)] for e_idx in enc_x]\n", 68 | " dec_x = [[0]+e_idx[:-1] for e_idx in y]\n", 69 | " return (batched_examples, tf.constant(enc_x, dtype=tf.int32), \n", 70 | " tf.constant(dec_x, dtype=tf.int32), tf.constant(y, dtype=tf.int32))\n", 71 | "print(get_batch(2, 10))" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "# 建立sequence to sequence 模型\n", 79 | "\n", 80 | "完成两空,模型搭建以及单步解码逻辑" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 26, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "class mySeq2SeqModel(keras.Model):\n", 90 | " def __init__(self):\n", 91 | " super(mySeq2SeqModel, self).__init__()\n", 92 | " self.v_sz=27\n", 93 | " self.hidden = 128\n", 94 | " self.embed_layer = tf.keras.layers.Embedding(self.v_sz, 64, \n", 95 | " batch_input_shape=[None, None])\n", 96 | " \n", 97 | " self.encoder_cell = tf.keras.layers.SimpleRNNCell(self.hidden)\n", 98 | " self.decoder_cell = tf.keras.layers.SimpleRNNCell(self.hidden)\n", 99 | " \n", 100 | " self.encoder = tf.keras.layers.RNN(self.encoder_cell, \n", 101 | " return_sequences=True, return_state=True)\n", 102 | " self.decoder = tf.keras.layers.RNN(self.decoder_cell, \n", 103 | " return_sequences=True, return_state=True)\n", 104 | " self.dense_attn = tf.keras.layers.Dense(self.hidden)\n", 105 | " self.dense = tf.keras.layers.Dense(self.v_sz)\n", 106 | " \n", 107 | " \n", 108 | " @tf.function\n", 109 | " def call(self, enc_ids, dec_ids):\n", 110 | " '''\n", 111 | " todo\n", 112 | " \n", 113 | " 完成带attention机制的 sequence2sequence 模型的搭建,模块已经在`__init__`函数中定义好,\n", 114 | " 用双线性attention,或者自己改一下`__init__`函数做加性attention\n", 115 | " '''\n", 116 | " return logits\n", 117 | " \n", 118 | " \n", 119 | " @tf.function\n", 120 | " def encode(self, enc_ids):\n", 121 | " enc_emb = self.embed_layer(enc_ids) # shape(b_sz, len, emb_sz)\n", 122 | " enc_out, enc_state = self.encoder(enc_emb)\n", 123 | " return enc_out, [enc_out[:, -1, :], enc_state]\n", 124 | " \n", 125 | " def get_next_token(self, x, state, enc_out):\n", 126 | " '''\n", 127 | " shape(x) = [b_sz,] \n", 128 | " '''\n", 129 | " \n", 130 | " '''\n", 131 | " todo\n", 132 | " 参考sequence_reversal-exercise, 自己构建单步解码逻辑'''\n", 133 | " return out, state" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "# Loss函数以及训练逻辑" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 27, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "@tf.function\n", 150 | "def compute_loss(logits, labels):\n", 151 | " losses = tf.nn.sparse_softmax_cross_entropy_with_logits(\n", 152 | " logits=logits, labels=labels)\n", 153 | " losses = tf.reduce_mean(losses)\n", 154 | " return losses\n", 155 | "\n", 156 | "@tf.function\n", 157 | "def train_one_step(model, optimizer, enc_x, dec_x, y):\n", 158 | " with tf.GradientTape() as tape:\n", 159 | " logits = model(enc_x, dec_x)\n", 160 | " loss = compute_loss(logits, y)\n", 161 | "\n", 162 | " # compute gradient\n", 163 | " grads = tape.gradient(loss, model.trainable_variables)\n", 164 | " optimizer.apply_gradients(zip(grads, model.trainable_variables))\n", 165 | " return loss\n", 166 | "\n", 167 | "def train(model, optimizer, seqlen):\n", 168 | " loss = 0.0\n", 169 | " accuracy = 0.0\n", 170 | " for step in range(2000):\n", 171 | " batched_examples, enc_x, dec_x, y = get_batch(32, seqlen)\n", 172 | " loss = train_one_step(model, optimizer, enc_x, dec_x, y)\n", 173 | " if step % 500 == 0:\n", 174 | " print('step', step, ': loss', loss.numpy())\n", 175 | " return loss" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "# 训练迭代" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 28, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "step 0 : loss 3.3069763\n", 195 | "step 500 : loss 1.3605067\n", 196 | "step 1000 : loss 0.2817158\n", 197 | "step 1500 : loss 0.105303034\n" 198 | ] 199 | }, 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "" 204 | ] 205 | }, 206 | "execution_count": 28, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "optimizer = optimizers.Adam(0.0005)\n", 213 | "model = mySeq2SeqModel()\n", 214 | "train(model, optimizer, seqlen=20)" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "# 测试模型逆置能力\n", 222 | "首先要先对输入的一个字符串进行encode,然后在用decoder解码出逆置的字符串\n", 223 | "\n", 224 | "测试阶段跟训练阶段的区别在于,在训练的时候decoder的输入是给定的,而在预测的时候我们需要一步步生成下一步的decoder的输入" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 30, 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "name": "stdout", 234 | "output_type": "stream", 235 | "text": [ 236 | "[False, True, True, True, True, True, True, True, True, True, True, True, True, True, False, True, True, True, True, True, True, True, True, False, False, True, True, True, True, True, True, True]\n", 237 | "[('LGNVPEUAIFNLDIRQPMEY', 'YEMPQRIDLNFIAUEPVNGL'), ('ZFKNSOQGHINBCDJMOQXO', 'OXQOMJDCBNIHGQOSNKFZ'), ('ZXGDWLBRJMDSNYXIEMKJ', 'JKMEIXYNSDMJRBLWDGXZ'), ('KSEOFXPRMGUBHSJWIGYC', 'CYGIWJSHBUGMRPXFOESK'), ('HHMACLLSVMESLETLYJLZ', 'ZLJYLTELSEMVSLLCAMHH'), ('PVIABJTRCEQZFSJZLKQP', 'PQKLZJSFZQECRTJBAIVP'), ('PTUYDMQCQKZWBNCJRZBG', 'GBZRJCNBWZKQCQMDYUTP'), ('ZFZEWFPTWQFFSNNQLOFW', 'WFOLQNNSFFQWTPFWEZFZ'), ('NAHVTGYNHHEXOKMOEDRN', 'NRDEOMKOXEHHNYGTVHAN'), ('CTDDCDXRMIQDKCHODXVU', 'UVXDOHCKDQIMRXDCDDTL'), ('QWMRORZQQQTSJEHNVSZN', 'NZSVNHEJSTQQQZRORMWQ'), ('HTLTKWDCPXYCOREQGOUA', 'AUOGQEROCYXPCDWKTLTH'), ('DIQEGEZTNCFGNHHCXEDE', 'EDEXCHHNGFCNTZEGEQIJ'), ('HLGMERZQBOYYDQWMKKVE', 'EVKKMWQDYYOBQZREMGLH'), ('MCUCLWJZZVBMJFTNMAMU', 'UMAMNTFJMBVZZJWLCUCY'), ('UJIKEJYXYBBUSNIDOMBL', 'LBMODINSUBBYXYJEKIJU'), ('XPBYWUZGZJMRVMCVONLM', 'MLNOVCMVRMJZGZUWYBPX'), ('GDEKKHYSSGHPDRIZSZLY', 'YLZSZIRDPHGSSYHKKEDG'), ('RVPPCAFTWYOLOFDBDRGR', 'RGRDBDFOLOYWTFACPPVR'), ('SHZHNIHVWGCNCFQJQGQZ', 'ZQGQJQFCNCGWVHINHBHS'), ('NAQPBIBCNFGVUDJKPMGZ', 'ZGMPKJDUVGFNCBIBPQAN'), ('INGPKHNHAVQCGQZHPHCG', 'GCHPHZQGCQVAHNHKPGNI'), ('KWWCWEGNMIMJUMYJETJQ', 'QJTEJYMUJMIMNGEWCWWK'), ('TETRFQIESYAPMFRUYSZE', 'EZSYURFMPAYSEIQFRTET'), ('QSRNBFLVLALCFPSZQNMN', 'NMNQZSPFCLALVLFBNRSQ'), ('SSZQYNNEMFKXJGTELUTF', 'FTULETGJXKFMENNYQZSS'), ('MBWHGDXBIOYHLEULXADG', 'GDAXLUELHYOIBXDGHWBM'), ('TTTFGSHTCLNAAMUMYNFU', 'UFNYMUMAANLCTHSGFTTT'), ('IEGUFJYJAIOGLNZYTKZC', 'CZKTYZNLGOIAJYJFUGEI'), ('MHBPGXFEZNNZHMXMQYTM', 'MTYQMXMHZNNZEFXGPBHM'), ('LUMXHUIUXCCBRUXDVOOC', 'COOVDXURBCCXUIUHXMJL'), ('QJTLJGVXGRYIYXWTGTQS', 'SQTGTWXYIYRGXVGJLTJQ')]\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "def sequence_reversal():\n", 243 | " def decode(init_state, steps, enc_out):\n", 244 | " b_sz = tf.shape(init_state[0])[0]\n", 245 | " cur_token = tf.zeros(shape=[b_sz], dtype=tf.int32)\n", 246 | " state = init_state\n", 247 | " collect = []\n", 248 | " for i in range(steps):\n", 249 | " cur_token, state = model.get_next_token(cur_token, state, enc_out)\n", 250 | " collect.append(tf.expand_dims(cur_token, axis=-1))\n", 251 | " out = tf.concat(collect, axis=-1).numpy()\n", 252 | " out = [''.join([chr(idx+ord('A')-1) for idx in exp]) for exp in out]\n", 253 | " return out\n", 254 | " \n", 255 | " batched_examples, enc_x, _, _ = get_batch(32, 20)\n", 256 | " enc_out, state = model.encode(enc_x)\n", 257 | " return decode(state, enc_x.get_shape()[-1], enc_out), batched_examples\n", 258 | "\n", 259 | "def is_reverse(seq, rev_seq):\n", 260 | " rev_seq_rev = ''.join([i for i in reversed(list(rev_seq))])\n", 261 | " if seq == rev_seq_rev:\n", 262 | " return True\n", 263 | " else:\n", 264 | " return False\n", 265 | "print([is_reverse(*item) for item in list(zip(*sequence_reversal()))])\n", 266 | "print(list(zip(*sequence_reversal())))" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [] 275 | } 276 | ], 277 | "metadata": { 278 | "kernelspec": { 279 | "display_name": "Python 3", 280 | "language": "python", 281 | "name": "python3" 282 | }, 283 | "language_info": { 284 | "codemirror_mode": { 285 | "name": "ipython", 286 | "version": 3 287 | }, 288 | "file_extension": ".py", 289 | "mimetype": "text/x-python", 290 | "name": "python", 291 | "nbconvert_exporter": "python", 292 | "pygments_lexer": "ipython3", 293 | "version": "3.7.0" 294 | } 295 | }, 296 | "nbformat": 4, 297 | "nbformat_minor": 2 298 | } 299 | --------------------------------------------------------------------------------