├── .gitignore ├── README.md ├── ai-notes.html ├── assets ├── Stack-I.png ├── Stack-II.png ├── eq3,4.png └── eq5,6.png ├── cifar.py ├── cnn.png ├── cnn0.81.h5 ├── cnn_evaluation.png ├── nn.png ├── nn0.52.h5 ├── nn_evaluation.png └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.pdf 2 | *.docx 3 | batches.meta 4 | data_batch_1 5 | data_batch_2 6 | data_batch_3 7 | data_batch_4 8 | data_batch_5 9 | test_batch 10 | image-classification-rnn/* 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #
. 639 | 640 | #### 机器学习算法分类 641 | 642 | - 有监督学习 Supervised (inductive) learning: **training data $\And$ desired outputs (labels)** 643 | 644 | - 回归 Regression 645 | - 分类 Classification 646 | - 支持向量机 Support Vector Machines & Kernel Methods 647 | - 决策树 Decision Tree Induction 648 | - 贝叶斯学习 Bayesian Learning 649 | - 神经网络&深度学习 Neural Networks & Deep Learning 650 | - 学习理论 Learning Theory 651 | 652 | - 无监督学习 Unsupervised learning: **training data** 653 | 654 | - 聚类 Clustering 655 | - 维度约减 Dimensionality reduction 656 | 657 | - 半监督学习 Semi-supervised learning: **training data $\And$ a few desired outputs** 658 | - 强化学习 Reinforcement learning 659 | - 给定一系列具有(延迟)奖励的状态和动作,输出策略;策略是状态到动作的映射 660 | - 时间差异学习 Temporal Difference Learning 661 | - Q 学习 Q Learning 662 | 663 | #### 机器学习算法组成 664 | 665 | - 表示 Representation 666 | - 数值函数 Numerical functions 667 | - 线性回归 Linear regression 668 | - 神经网络 Neural networks 669 | - 支持向量机 Support vector machines 670 | - 符号函数 Symbolic functions 671 | - 决策树 Decision trees 672 | - 命题逻辑规则 Rules in propositional logic 673 | - 一阶谓词逻辑的规则 Rules in first-order predicate logic 674 | - 基于实例的函数 Instance-based functions 675 | - 最近邻 Nearest-neighbor 676 | - 基于案例 Case-based 677 | - 概率图模型 Probabilistic Graphical Models 678 | - 朴素贝叶斯 Naive Bayes 679 | - 贝叶斯网络 Bayesian networks 680 | - 隐马尔可夫链 Hidden-Markov Models (HMMs) 681 | - 概率上下文无关语法 Probabilistic Context Free Grammars (PCFGs) 682 | - 马尔可夫网络 Markov networks 683 | - 优化 Optimization 684 | - 梯度下降 Gradient descent 685 | - 感知器 Perceptron 686 | - 反向传播 Backpropagation 687 | - 动态变成 Dynamic Programming 688 | - 隐马尔可夫学习 HMM Learning 689 | - 概率上下文无关语法学习 PCFG Learning 690 | - 分而治之 Divide and Conquer 691 | - 决策树归纳 Decision tree induction 692 | - 规则学习 Rule learning 693 | - 进化计算 Evolutionary Computation 694 | - 遗传算法 Genetic Algorithms (GAs) 695 | - 遗传编程 Genetic Programming (GP) 696 | - 神经进化 Neuro-evolution 697 | - 评价 Evaluation 698 | - 精度 Accuracy 699 | - 精确度和召回率 Precision and recall 700 | - 平方误差 Squared error 701 | - 相似性 Likelihood 702 | - 后验概率 Posterior probability 703 | - 成本/效用 Cost/Utility 704 | - 边距 Margin 705 | - 熵 Entropy 706 | - K-L 散度 K-L divergence 707 | 708 | #### 机器学习系统的设计 709 | 710 | - 选择训练经验 711 | - 选择学习对象,如目标函数 712 | - 选择如何表示目标函数 713 | - 选择学习算法从经验推断目标函数 714 | 715 | #### 机器学习实践 716 | 717 | - 了解领域,先验知识和目标 718 | - **数据集成,选择,清洁,预处理** 719 | - 学习模型 720 | - 解释结果 721 | - 巩固和部署发现的知识 722 | 723 | --- 724 | 725 | ## 深度学习 726 | 727 | #### Three Steps for Deep Learning 728 | 729 | 1. Define a set of function 730 | - Network Structure 731 | 2. Goodness of function 732 | - Training Data 733 | - Learning Target 734 | - Total Loss 735 | 3. Pick the best function 736 | - Gradient Descent 737 | 738 | #### Supervised Neural Network 739 | 740 | > _e.g._ Fully Connect Feedforward Network 741 | 742 | - Neuron, Weights, Bias, Activation Function (Sigmoid Function $sigma(z) = \frac{1}{1+e^{-z}}$), Output Layer (Softmax Layer) 743 | 744 | - Convolutional Neural Network (CNN) 745 | 746 | - Convolution 747 | - Stride 748 | - Zero Padding 749 | - MaxPooling 750 | - Flatten 751 | 752 | ```flow 753 | st=>start: Image 754 | e=>end: Output 755 | op2=>operation: MaxPooling: The same patterns appearing different regions 756 | op3=>operation: Flatten 757 | op5=>operation: Fully Connected Feedforward Network 758 | op4=>operation: New Image 759 | sub1=>subroutine: Repeat 760 | cond=>condition: Iterated N times 761 | io=>inputoutput: catch something 762 | op1=>operation: Convolution: Some patterns are much smaller than the whole image 763 | 764 | st->op1->op2->op4->cond 765 | cond(yes)->op3->op5->e 766 | cond(no)->sub1->op1 767 | ``` 768 | 769 | - Recurrent Neural Network (RNN): Neural Network with Memory 770 | 771 | #### Unsupervised Neural Networks 772 | 773 | - Embedding (word2vec) 774 | 775 | --- 776 | 777 | ## 生成模型 Generative Models 778 | 779 | - 给定训练数据,从同一分布生成新样本 780 | - 解决密度估计问题,这是无监督学习中的核心问题 781 | 782 | #### 主要方向 783 | 784 | - 显式密度估计:显式定义并求解$p_{model}(x)$ 785 | - 隐式密度估计:非显示地定义模型,而是学习可从$p_{model}(x)$采样的模型 786 | 787 | #### 应用 Application 788 | 789 | - 用于艺术品,超分辨率,着色的**真实采样** 790 | - **时序数据**的生成模型可用于**仿真和计划**(强化学习应用程序) 791 | - 推断潜在的一般**特征表示** 792 | 793 | #### 变分自动编码器 Variational Autoencoders (VAE) 794 | 795 | - 无监督,用于学习未标注数据的低维特征表示 796 | 797 | #### 生成对抗网络 Generative Adversarial Networks (GAN) 798 | 799 | - 不使用任何显式的密度函数 800 | - 采取博弈论的方法:通过双人游戏,从训练分布中学习并生成 801 | - 组成 802 | - 生成器网络 Generator Network:尝试通过生成逼真的图像来欺骗鉴别器 803 | - 鉴别器网络 Discriminator Network:尝试区分真实图像和伪造图像 804 | 805 | --- 806 | 807 | ## Deep Learning on Graphs 808 | 809 | #### Graph Applications 810 | 811 | - Link Prediction 812 | - Node Classification 813 | - Node Importance 814 | - Graph Classification 815 | 816 | #### 网络嵌入 Network Embedding 817 | 818 | > De-coupling the links 819 | 820 | - **Goal 1⃣️**: Reconstruct the original network 821 | - **Goal 2⃣️**: Support network inference (Community detection, Network distance, Network evolution) 822 | 823 | #### 图神经网络 Graph Neural Networks 824 | 825 | > Design new algorithms that can incorporate links 826 | 827 | - Graph Recurrent Neural Networks: Recursive definition of states, e.g. `PageRank` 828 | 829 | - Most primitive methods 830 | - Unified frameworks with GCN 831 | 832 | - Graph Convolutional Networks: Common local and global patterns 833 | - Convolutions: spectral, spatial 834 | - Readout 835 | - Improvements 836 | -------------------------------------------------------------------------------- /assets/Stack-I.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/assets/Stack-I.png -------------------------------------------------------------------------------- /assets/Stack-II.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/assets/Stack-II.png -------------------------------------------------------------------------------- /assets/eq3,4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/assets/eq3,4.png -------------------------------------------------------------------------------- /assets/eq5,6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/assets/eq5,6.png -------------------------------------------------------------------------------- /cifar.py: -------------------------------------------------------------------------------- 1 | from __future__ import (absolute_import, division, print_function, unicode_literals) 2 | 3 | import pickle 4 | 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | import pydot 8 | import tensorflow as tf 9 | import tqdm 10 | from keras.utils import vis_utils 11 | from tensorflow.keras import datasets, layers, models 12 | 13 | vis_utils.pydot = pydot 14 | 15 | 16 | def unpickle(file): 17 | with open(file, 'rb') as fo: 18 | dict = pickle.load(fo, encoding='bytes') 19 | return dict 20 | 21 | 22 | test, cross = unpickle('./test_batch'), unpickle(f'./data_batch_5') 23 | data, labels = [], [] 24 | for i in range(5): 25 | cifar = unpickle(f'./data_batch_{i + 1}') 26 | if i == 0: 27 | data = cifar[b'data'] / 255 28 | labels = np.array(cifar[b'labels']) 29 | else: 30 | data = np.append(data, cifar[b'data'] / 255, axis=0) 31 | labels = np.append(labels, np.array(cifar[b'labels']), axis=0) 32 | 33 | 34 | def network(data, labels, test, cross): 35 | data.resize((data.shape[0], 1, data.shape[-1])) 36 | cross[b'data'].resize((cross[b'data'].shape[0], 1, cross[b'data'].shape[-1])) 37 | test[b'data'].resize((test[b'data'].shape[0], 1, test[b'data'].shape[-1])) 38 | model = models.Sequential([ 39 | layers.Dense(512, activation='relu'), 40 | layers.Dense(256, activation='relu'), 41 | layers.Dense(128, activation='relu'), 42 | layers.Dense(10, activation='softmax') 43 | ]) 44 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 45 | history = model.fit(data, labels, epochs=10, validation_data=(cross[b'data'] / 255, np.array(cross[b'labels']))) 46 | plt.plot(history.history['accuracy'], label='accuracy') 47 | plt.plot(history.history['val_accuracy'], label='val_accuracy') 48 | plt.xlabel('Epoch') 49 | plt.ylabel('Accuracy') 50 | plt.legend(loc='lower right') 51 | plt.savefig('nn_evaluation.png', dpi=600) 52 | cross_loss, cross_acc = model.evaluate(cross[b'data'] / 255, np.array(cross[b'labels']), verbose=2) 53 | model.save(f'nn{cross_acc:.2}.h5') 54 | vis_utils.plot_model(model, to_file='nn.png', show_shapes=True, show_layer_names=True, expand_nested=True, dpi=600) 55 | print(f'Cross Validation Accuracy: {cross_acc}, Cross Validation lost: {cross_loss}') 56 | test_loss, test_acc = model.evaluate(test[b'data'] / 255, np.array(test[b'labels']), verbose=2) 57 | print(f'Test accuracy: {test_acc}, Test lost: {test_loss}') 58 | print(model.summary()) 59 | 60 | 61 | def CNN(data, labels, test, cross): 62 | data = np.array([i.reshape((3, 1024)).T.reshape(32, 32, 3) for i in data]) 63 | cross[b'data'] = np.array([i.reshape((3, 1024)).T.reshape(32, 32, 3) for i in cross[b'data']]) 64 | test[b'data'] = np.array([i.reshape((3, 1024)).T.reshape(32, 32, 3) for i in test[b'data']]) 65 | model = models.Sequential([ 66 | layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), 67 | layers.MaxPooling2D((2, 2)), 68 | layers.Conv2D(64, (3, 3), activation='relu'), 69 | layers.MaxPooling2D((2, 2)), 70 | layers.Conv2D(64, (3, 3), activation='relu'), 71 | layers.Flatten(), 72 | layers.Dense(64, activation='relu'), 73 | layers.Dense(10, activation='softmax') 74 | ]) 75 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 76 | history = model.fit(data, labels, epochs=10, validation_data=(cross[b'data'] / 255, np.array(cross[b'labels']))) 77 | plt.plot(history.history['accuracy'], label='accuracy') 78 | plt.plot(history.history['val_accuracy'], label='val_accuracy') 79 | plt.xlabel('Epoch') 80 | plt.ylabel('Accuracy') 81 | plt.ylim([0.5, 1]) 82 | plt.legend(loc='lower right') 83 | plt.savefig('cnn_evaluation.png', dpi=600) 84 | cross_loss, cross_acc = model.evaluate(cross[b'data'] / 255, np.array(cross[b'labels']), verbose=2) 85 | model.save(f'cnn{cross_acc:.2}.h5') 86 | from tensorflow.keras.utils import plot_model 87 | plot_model(model, to_file='cnn.png', show_shapes=True, show_layer_names=True, expand_nested=True, dpi=600) 88 | print(f'Cross Validation Accuracy: {cross_acc}, Cross Validation lost: {cross_loss}') 89 | test_loss, test_acc = model.evaluate(test[b'data'] / 255, np.array(test[b'labels']), verbose=2) 90 | print(f'Test accuracy: {test_acc}, Test lost: {test_loss}') 91 | print(model.summary()) 92 | 93 | 94 | class NearestNeighbor: 95 | def __init__(self, data, labels, test, cross): 96 | self.test = test 97 | self.data = data 98 | self.labels = labels 99 | self.test[b'data'] = test[b'data'] 100 | self.test[b'labels'] = test[b'labels'] 101 | self.cross = cross 102 | self.train() 103 | 104 | def train(self): 105 | predict = self.predict() 106 | accuracy = np.mean(predict == self.test[b'labels']) 107 | print(f'Accuracy:\t{accuracy}') 108 | 109 | def predict(self, k=7): 110 | predict = np.zeros(self.test[b'data'].shape[0], dtype=self.labels.dtype) 111 | for i in tqdm.tqdm(range(self.test[b'data'].shape[0])): 112 | L1 = np.sum(np.abs(self.data - self.test[b'data'][i, :]), axis=1) 113 | closest = self.labels[np.argsort(L1)[:k]] 114 | unique, indices = np.unique(closest, return_inverse=True) 115 | predict[i] = unique[np.argmax(np.bincount(indices))] 116 | return predict 117 | 118 | 119 | def rnn(data, labels, test, cross, first_exec=True): 120 | import tensorflow.compat.v1 as tf 121 | tf.disable_v2_behavior() 122 | size = 32 # 32 * 32 123 | timesteps = 32 124 | hidden_layer = 256 125 | classes = 10 126 | params = {"learning_rate": 0.001, "training_iters": 10000, "batch_size": 64} 127 | test_data, test_labels = test[b'data'] / 255, test[b'labels'] 128 | # 将RGB值转为灰度值 129 | print('Converting data......') 130 | data_array = np.array([[[item[index], item[index + 1024], item[index + 1024 * 2]] for index in range(1024)] 131 | for item in tqdm.tqdm(data)]) 132 | test_array = np.array([[[item[index], item[index + 1024], item[index + 1024 * 2]] for index in range(1024)] 133 | for item in tqdm.tqdm(test_data)]) 134 | data = np.array([[data_array[i, j].dot([0.299, 0.587, 0.114]) for j in range(data_array.shape[1])] 135 | for i in tqdm.tqdm(range(data_array.shape[0]))]) 136 | test = np.array([[test_array[i, j].dot([0.299, 0.587, 0.114]) for j in range(test_array.shape[1])] 137 | for i in tqdm.tqdm(range(test_array.shape[0]))]) 138 | labels = np.array([[1 if i == row else 0 for i in range(10)] for row in tqdm.tqdm(labels)]) 139 | test_labels = np.array([[1 if i == row else 0 for i in range(10)] for row in tqdm.tqdm(test_labels)]) 140 | # 按照 tutorial 定义 RNN 模型 141 | x = tf.placeholder("float", [None, timesteps, size]) 142 | y = tf.placeholder("float", [None, classes]) 143 | weights = tf.Variable(tf.random_normal([hidden_layer, classes]), name='weights') 144 | biases = tf.Variable(tf.random_normal([classes]), name='biases') 145 | 146 | def rnn_model(x, weights, biases): 147 | x = tf.transpose(x, [1, 0, 2]) 148 | x = tf.reshape(x, [-1, size]) 149 | x = tf.split(x, timesteps, axis=0) 150 | lstm_cell = tf.nn.rnn_cell.LSTMCell(hidden_layer, forget_bias=1.0) 151 | outputs, states = tf.nn.static_rnn(lstm_cell, x, dtype=tf.float32) 152 | return tf.matmul(outputs[-1], weights) + biases 153 | 154 | pred = rnn_model(x, weights, biases) 155 | cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y)) 156 | optimizer = tf.train.AdamOptimizer(learning_rate=params['learning_rate']).minimize(cost) 157 | correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) 158 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 159 | # 训练模型 160 | print('Training......') 161 | with tf.Session() as sess: 162 | sess.run(tf.global_variables_initializer()) 163 | # 一轮一轮地训练模型 164 | for step in tqdm.tqdm(range(1, int(params['training_iters'] / params['batch_size']) + 1)): 165 | batch_x = data[(step - 1) * params['batch_size']:step * params['batch_size']].reshape( 166 | (params['batch_size'], timesteps, size)) 167 | batch_y = labels[(step - 1) * params['batch_size']:step * params['batch_size']] 168 | sess.run(optimizer, feed_dict={x: batch_x, y: batch_y}) 169 | # 测试评估模型 170 | print("Accuracy:", 171 | sess.run(accuracy, feed_dict={ 172 | x: test[:128].reshape((-1, timesteps, size)), 173 | y: test_labels[:128] 174 | })) 175 | 176 | 177 | network(data, labels, test, cross) 178 | CNN(data, labels, test, cross) 179 | NearestNeighbor(data, labels, test, cross) 180 | rnn(data, labels, test, cross, 0) 181 | -------------------------------------------------------------------------------- /cnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/cnn.png -------------------------------------------------------------------------------- /cnn0.81.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/cnn0.81.h5 -------------------------------------------------------------------------------- /cnn_evaluation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/cnn_evaluation.png -------------------------------------------------------------------------------- /nn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/nn.png -------------------------------------------------------------------------------- /nn0.52.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/nn0.52.h5 -------------------------------------------------------------------------------- /nn_evaluation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cnarutox/AI/dba49839cddb9033ad68a2a8ff120f3f36325c0b/nn_evaluation.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | matplotlib==3.1.1 2 | tensorflow==2.5.2 3 | tqdm==4.36.1 4 | Keras==2.3.1 5 | numpy==1.17.3 6 | pydot==1.4.1 7 | --------------------------------------------------------------------------------