├── .gitignore ├── Basic-Concept ├── Deep Learning Practice Manual.md ├── Transfer Learning Tutorial.md └── readme.md ├── Basic-Tutorial ├── Classic-CNN-Structure │ ├── MXNet │ │ ├── models │ │ │ ├── MXNet_AlexNet.py │ │ │ ├── MXNet_DenseNet.py │ │ │ ├── MXNet_GoogLeNet.py │ │ │ ├── MXNet_ResNet.py │ │ │ ├── MXNet_VGG.py │ │ │ └── __init__.py │ │ ├── readme.ipynb │ │ └── utils.py │ ├── PyTorch │ │ ├── models │ │ │ ├── PyTorch_AlexNet.py │ │ │ ├── PyTorch_GoogLeNet.py │ │ │ ├── PyTorch_ResNet.py │ │ │ └── __init__.py │ │ └── readme.ipynb │ └── TensorFlow │ │ ├── AlexNet.py │ │ ├── CIFAR10_mytest.py │ │ ├── CNN_MNIST_test.py │ │ ├── VGGNet.py │ │ ├── test.py │ │ └── test2.py ├── MXNet │ ├── gluon-basics │ │ ├── block.md │ │ ├── custom-layer.md │ │ ├── index.md │ │ ├── parameters.md │ │ ├── serialization.md │ │ └── use-gpu.md │ └── readme.md ├── PaddlePaddle │ └── readme.md ├── PyTorch │ ├── feedforward_neural_network │ │ ├── main-gpu.py │ │ └── main.py │ ├── linear_regression │ │ └── main.py │ ├── logistic_regression │ │ └── main.py │ ├── pytorch_basics │ │ └── main.py │ └── readme.md ├── TensorFlow │ ├── lab-01-basics.ipynb │ ├── lab-02-1-linear_regression.py │ ├── lab-02-2-linear_regression_feed.py │ ├── lab-02-3-linear_regression_tensorflow.org.py │ ├── lab-03-1-minimizing_cost_show_graph.py │ ├── lab-03-2-minimizing_cost_gradient_update.py │ ├── lab-03-3-minimizing_cost_tf_optimizer.py │ ├── lab-03-X-minimizing_cost_tf_gradient.py │ ├── lab-04-1-multi_variable_linear_regression.py │ ├── lab-04-2-multi_variable_matmul_linear_regression.py │ ├── lab-04-3-file_input_linear_regression.py │ ├── lab-04-4-tf_reader_linear_regression.py │ ├── lab-05-1-logistic_regression.py │ ├── lab-05-2-logistic_regression_diabetes.py │ ├── lab-06-1-softmax_classifier.py │ ├── lab-06-2-softmax_zoo_classifier.py │ ├── lab-07-1-learning_rate_and_evaluation.py │ ├── lab-07-2-linear_regression_without_min_max.py │ ├── lab-07-3-linear_regression_min_max.py │ ├── lab-07-4-mnist_introduction.py │ ├── lab-08-tensor_manipulation.ipynb │ ├── lab-09-1-xor.py │ ├── lab-09-2-xor-nn.py │ ├── lab-09-3-xor-nn-wide-deep.py │ ├── lab-09-4-xor_tensorboard.py │ ├── lab-09-5-linear_back_prop.py │ ├── lab-09-6-multi-linear_back_prop.py │ ├── lab-09-7-sigmoid_back_prop.py │ ├── lab-09-x-xor-nn-back_prop.py │ ├── lab-10-1-mnist_softmax.py │ ├── lab-10-2-mnist_nn.py │ ├── lab-10-3-mnist_nn_xavier.py │ ├── lab-10-4-mnist_nn_deep.py │ ├── lab-10-5-mnist_nn_dropout.py │ ├── lab-10-6-mnist_nn_batchnorm.ipynb │ ├── lab-10-7-mnist_nn_higher_level_API.py │ ├── lab-10-8-mnist_nn_selu(wip).py │ ├── lab-10-X1-mnist_back_prop.py │ ├── lab-11-0-cnn_basics.ipynb │ ├── lab-11-1-mnist_cnn.py │ ├── lab-11-2-mnist_deep_cnn.py │ ├── lab-11-3-mnist_cnn_class.py │ ├── lab-11-4-mnist_cnn_layers.py │ ├── lab-11-5-mnist_cnn_ensemble_layers.py │ ├── lab-11-X-mnist_cnn_low_memory.py │ ├── lab-12-0-rnn_basics.ipynb │ ├── lab-12-1-hello-rnn.py │ ├── lab-12-2-char-seq-rnn.py │ ├── lab-12-3-char-seq-softmax-only.py │ ├── lab-12-4-rnn_long_char.py │ ├── lab-12-5-rnn_stock_prediction.py │ ├── lab-13-1-mnist_using_scope.py │ ├── lab-13-2-mnist_tensorboard.py │ ├── lab-13-3-mnist_save_restore.py │ ├── readme.md │ └── requirements.txt └── readme.md ├── Computer-vision ├── @PeterChenYijie AIChallenger-ScenesClassfication-pytorch │ ├── README.MD │ ├── dataset.py │ ├── main.py │ ├── models │ │ ├── __init__.py │ │ ├── basic_module.py │ │ ├── loss.py │ │ └── resnet.py │ ├── nohup.out │ ├── requirements.txt │ ├── scene.pth │ ├── scripts │ │ └── data_process.py │ └── utils.py ├── @PeterChenYijie Object_Detection_Tensorflow_API │ ├── Object_Detection_Tensorflow_API.ipynb │ ├── README.md │ ├── object_detection │ │ ├── data │ │ │ └── mscoco_label_map.pbtxt │ │ ├── ssd_mobilenet_v1_coco_2017_11_17 │ │ │ └── frozen_inference_graph.pb │ │ └── test_images │ │ │ ├── image1.jpg │ │ │ ├── image2.jpg │ │ │ └── image_info.txt │ └── object_detection_tutorial.ipynb ├── @PeterChenYijie fine-tuning-gluon │ ├── fine-tuning.py │ ├── img │ │ ├── dog_hotdog.jpg │ │ ├── leg_hotdog.jpg │ │ └── real_hotdog.jpg │ ├── readme.md │ └── utils.py ├── @PeterChenYijie kaggle-DogBreed-gluon │ ├── DogBreed.py │ ├── baseline.ipynb │ ├── fine_tuning.ipynb │ ├── model.py │ ├── pre_deal.py │ ├── readme.md │ ├── utils.py │ └── visual_visdom.py ├── @PeterChenYijie kaggle-IsIceberg │ ├── gluon │ │ ├── IceBerg_ImageProcessing_method.ipynb │ │ ├── Iceberg_classifier.ipynb │ │ ├── Loss22.png │ │ ├── dataset_aug.py │ │ ├── despeckling_SAR.ipynb │ │ ├── iceberg_classifier_origin.ipynb │ │ ├── mynet.py │ │ ├── pytorch_Iceberg_classifier.ipynb │ │ ├── stacking.ipynb │ │ ├── utils.py │ │ └── vggnet.py │ ├── pytorch │ │ ├── Iceberg_dataset.py │ │ ├── essemble.ipynb │ │ ├── gene_csv.py │ │ ├── iceberg_classfier.py │ │ ├── main.py │ │ ├── models │ │ │ ├── __init__.py │ │ │ ├── convert.py │ │ │ └── mynet.py │ │ ├── pytorch_Iceberg_classifier.ipynb │ │ ├── readme.md │ │ └── utils.py │ └── readme.md ├── @PeterChenYijie kaggle-cifar10-gluon │ ├── Loss.png │ ├── cifar10.py │ ├── model.py │ ├── pre_deal.py │ ├── readme.md │ └── utils.py ├── @PeterChenYijie yolo-tensorflow │ ├── .gitignore │ ├── README.md │ ├── download_data.sh │ ├── test.py │ ├── test │ │ ├── cat.jpg │ │ └── person.jpg │ ├── train.py │ ├── utils │ │ ├── __init__.py │ │ ├── pascal_voc.py │ │ └── timer.py │ └── yolo │ │ ├── __init__.py │ │ ├── config.py │ │ └── yolo_net.py ├── @chenyuntc GAN-PyTorch │ ├── DCGAN.ipynb │ ├── README.md │ └── WGAN.ipynb ├── @ypwhs kaggle-DogBreed-gluon │ ├── README.md │ ├── get_features.ipynb │ ├── get_features_v3.ipynb │ ├── preprocessing.ipynb │ ├── stanford.ipynb │ └── transfer_learning_all_pretrained_models.ipynb ├── @zhreshold SSD-mxnet │ └── README.md └── readme.md ├── Copywriting Guidelines.en.md ├── Copywriting Guidelines.zh.md ├── Extra ├── @PeterChenyijie paddlepaddle │ ├── analysis_info.ipynb │ ├── data_one.ipynb │ ├── generate_json.ipynb │ ├── multi_layer.ipynb │ ├── readme.md │ ├── v1.0.py │ └── visual_data.ipynb ├── TensorBoard-Official │ ├── LICENSE │ ├── README.md │ ├── labels_1024.tsv │ ├── mnist.py │ └── sprite_1024.png ├── TensorBoard-simply-usage │ ├── Deep_Layer_Visualization_Tutorial.ipynb │ ├── readme.md │ └── vis_tensorboard.py └── readme.md ├── LICENSE ├── Natural-language-processing ├── @PeterChenYijie rnn-scratch gluon │ ├── data │ │ └── jaychou_lyrics.txt.zip │ ├── rnn-scrath.py │ └── utils.py └── readme.md ├── Note ├── AI_hardware_advanced_technology.md ├── DeepLearning.md ├── Deep_Learning_Papers_Reading_Roadmap.md ├── Driverless.md ├── Mechine_Learning.md ├── Niubility │ ├── Computer-Version-Paper(updating).md │ ├── GitHub_Niubility.md │ ├── paper2017.md │ └── recommend_paper.md ├── image_processing.md ├── paper_read.md ├── readme.md ├── review.md └── terminology.md ├── README.md ├── img ├── 20180306151440.png ├── Adagrad formula.png ├── Adam formula.png ├── AlexNet.png ├── MegDet_Large Learning Policy.png ├── MegDet_multiDevice_BN.png ├── MegDet_sublinear_memory.png ├── Momentum formula.png ├── NAG formula.png ├── RMSprop formula.png ├── VGG16.png ├── book_1.jpg ├── book_2.jpg ├── book_Deep Learning.jpg ├── book_Machine Learning in Action.jpg ├── comp.tif ├── kaggle_IsIceberg.PNG └── paper_one.png └── mxnet_environment.yml /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ IDEA Project files 2 | .idea 3 | *.iml 4 | *.ipr 5 | *.iws 6 | *.xml 7 | out 8 | 9 | 10 | *.tar.gz 11 | 12 | */**/__pycache__ 13 | */**/**/__pycache__ 14 | 15 | *.pyc 16 | */*.so* 17 | */**/*.so* 18 | */**/*.dylib* 19 | 20 | */.ipynb_checkpoints 21 | */**/.ipynb_checkpoints 22 | */**/**/.ipynb_checkpoints 23 | 24 | 25 | *.csv 26 | *.Untitled.ipynb 27 | 28 | */**/**/cifar-10-batches-py 29 | *.pth 30 | *.html 31 | 32 | -------------------------------------------------------------------------------- /Basic-Concept/Transfer Learning Tutorial.md: -------------------------------------------------------------------------------- 1 | 更新:2018-04-14 2 | > 说明:这里所列举的学习手册主要是迁移学习在深度学习上的应用。对于迁移学习大局了解 3 | 可参考[这里](https://github.com/jindongwang/transferlearning-tutorial) 4 | 5 | 6 | {Todo}学者部分: [链接](https://github.com/jindongwang/transferlearning/blob/master/doc/scholar_TL.md) 7 | 8 | 9 | 参考文献: 10 | [1] 王晋东.迁移学习简明手册[[pdf-v1.0]](http://jd92.wang/assets/files/transfer_learning_tutorial_wjd.pdf), 11 | [Github](https://github.com/jindongwang/transferlearning-tutorial/tree/master) 12 | 13 | 一些推荐阅读文章: 14 | 15 | 16 | #### 1. 迁移学习概念 17 | 迁移学习的核心问题是,找到新问题和原问题之间的相似性,才可以顺利实现知识的迁移。也即生活中“举一反三”。 18 | > 定义:迁移学习,是指利用数据、任务、或模型之间的相似性,将在旧领域学习过的模型,应用于新领域的一种学习过程。 19 | `为什么需要迁移学习呢?` 20 | 可以归结为以下四点,即`迁移学习`为主要解决以下四个问题: 21 | 22 | * `大数据与少标注之间的矛盾`。虽然目前都提倡通过 AI 去解决问题,而且目前处在大数据时代,数据都比较容易获取到。然而,对于这种获取到数据是没有标注。然而,机器学习模型的训练和更新均起来数据标注。 23 | >利用迁移学习的思想,我们可以寻找一些与数据来构建模型,增加我们目标数据的标注。 24 | 25 | * `大数据与弱计算之间的矛盾`。即便对于拥有大数据,但对于目前而言,大数据下的计算力也是不可或缺的。然而,对于一个好的 Net 都需要经过漫长时间训练才能获得较好的模型参数。 26 | > 利用迁移学习的思想,我们可以将那些大公司在大数据上训练好的模型,迁移到我们的任务中。针对于我们的任务进行微调,从而我们也可以拥有在大数据上训练好的模型。更进一步,我们可以将这些模型针对我们的任务进行自适应更新,从而取得更好的效果。 27 | 28 | * `普适化模型与个性化需求之间的矛盾`。虽说机器学习的目标是用来构建尽可能通用的模型,提供整体的泛化能力,然而现在却是始终不能彻底解决的。与之同时,个性化的需求也使得同一个模型不能专门性针对个性用户需求。 29 | > 我们利用迁移学习的思想,进行自适应的学习。考虑到不同用户之间的相似性和差异性,我们对普适化模型进行灵活的调整,以便完成我们的任务。 30 | 31 | * `特定应用的需求`。在这些应用中,也存在着一些特定的应用,它们面临着一些现实存在的问题。 32 | > 我们可以利用上述介绍过的手段,从数据和模型方法上进行迁移学习 33 | 34 | **负迁移** 35 | 虽然我们都希望迁移学习能够顺利记性,但是事情本身可能并不会是那么顺利。这里有的时候,使用源领域和目标领域不存在关联,这里将导致预测结果变得十分差。>迁移学习指的是,利用数据和领域之间存在的相似性关系,把之前学习到的知识,应用于新的未知领域。迁移学习的核心问题是,找到两个领域的相似性。找到了这个相似性,就可以合理地利用,从而很好地完成迁移学习任务。 36 | 产生负迁移的原因主要有: 37 | * 数据问题:源域和目标域完全不相似 38 | * 方法问题:源域和目标域存在相似,但是迁移方法不够好,或者说没有找到迁移的成分。 39 | 负迁移给迁移学习的研究和应用带来了负面影响。在实际应用中,找到合理的相似性,并且选择或开发合理的迁移学习方法,能够避免负迁移现象。 40 | 41 | #### 2.迁移学习的研究领域 42 | 按照目标领域 43 | 1. 监督迁移学习(Supervised Transfer Learning) 44 | 2. 半监督迁移学习(Semi-Supervised Transfer Learning) 45 | 3. 无监督迁移学习(Unsupervised Transfer Learning) 46 | 47 | 按照学习方法 48 | 1. 基于样本的迁移学习方法(Instance based Transfer Learning) 49 | 2. 基于特征的迁移学习方法(Feature based Transfer Learning) 50 | 3. 基于模型的迁移学习方法(Model based Transfer Learning) 51 | 4. 基于关系的迁移学习方法(Relation based Transfer Learning) 52 | 53 | 按照特征分类 54 | 1. 同构迁移学习(Homogeneous Transfer Learning) 55 | 2. 异构迁移学习(Heterogeneous Transfer Learning) 56 | 57 | 按照离线和在线形式分 58 | 1. 离线迁移学习(Offline Transfer Learning) 59 | 2. 在线迁移学习(Online Transfer Learning) 60 | 61 | #### 3.迁移学习的应用 62 | 1. 计算机视觉 63 | 计算机这边的迁移就相对比较多,可以做微小变化去自适应不同环境下需求。当然,对于迁移学习十分重要的是学习的鲁棒性。 64 | 2. 文本分类 65 | 同理,对于不同文本之间迁移学习。 66 | 3. 时间序列 67 | 行为识别? 68 | 4. 医疗健康 69 | 个人觉得,在医疗健康,迁移学习将发挥其很大作用,但也应当注意到医疗方面目标域的数据分布问题。 70 | 71 | 72 | #### 4.深度迁移学习 73 | > 这里只做深度学习相关的学习内容。当然这里也直接跳过了一些理论基本,这是不应该的。{Todo} 希望今后加上。 74 | 75 | 为什么深度网络可以迁移? 76 | * 神经网络实际一种特殊的特征提取器 77 | 78 | 1. `深度网络自适应` 79 | 相信很多都知道`finetune`可以帮助我们节省很多时间,并提高精度,然而,我们忽略一个事实:`finetune`基本假设是训练数据和测试数据服从同一分布。对于不同分布的数据,实际 finetune 往往效果是不好的。因此,更好的方式是做到深度网络自适应。该方式主要完成: 80 | * 哪些层可以自适应,这决定网络的学习程度; 81 | * 采用什么样的自适应方法(度量原则),这决定网络的泛化能力 82 | 核心方法:`DDC`、`DAN`、`同时迁移领域和任务`、`深度联合分布自适应`、`AdaBN` 83 | 84 | 2. `深度对抗网络迁移` 85 | 核心方法:`DANN`、`DSN`、`SAN` 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | #### 前沿 94 | * 机器智能与人类经验结合迁移 95 | * 传递迁移学习 `Transitive transfer learning ` 96 | * 远领域迁移学习 `Distant domain transfer learning ` 97 | * 终身迁移学习 98 | * 在线迁移学习 99 | * 迁移强化学习 100 | * 迁移学习的可解释性 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Basic-Concept/readme.md: -------------------------------------------------------------------------------- 1 | 这里用来对深度学习一些内容进行`通俗`解释。 2 | (o(╯□╰)o 其实应当多一些理论上的支持的) 3 | 4 | 5 | ### 目录 6 | * [解析卷积神经网络](Deep%20Learning%20Practice%20Manual.md) 7 | * [迁移学习简明手册](Transfer%20Learning%20Tutorial.md) 8 | 9 | 10 | 感谢: 11 | [@weixs](http://lamda.nju.edu.cn/weixs/) 魏秀参学长,旷视科技南京研究院负责人 12 | [@jindongwang](https://github.com/jindongwang) 王晋东学长,中国科学院计算技术研究所 -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/models/MXNet_AlexNet.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | from mxnet.gluon import nn 3 | 4 | class MXNet_AlexNet(nn.HybridBlock): 5 | def __init__(self,num_classes,verbose=False,**kwargs): 6 | super(MXNet_AlexNet,self).__init__(**kwargs) 7 | self.verbose = verbose 8 | with self.name_scope(): 9 | net = self.net = nn.HybridSequential() 10 | # 第一阶段 11 | net.add(nn.Conv2D(channels=96,kernel_size=11,strides=4,activation="relu")) 12 | net.add(nn.MaxPool2D(pool_size=3,strides=2)) 13 | # 第二阶段 14 | net.add(nn.Conv2D(channels=256,kernel_size=5,padding=2,activation="relu")) 15 | net.add(nn.MaxPool2D(pool_size=3,strides=2)) 16 | # 第三阶段 17 | net.add(nn.Conv2D(channels=384,kernel_size=3,padding=1,activation="relu")) 18 | net.add(nn.Conv2D(channels=384,kernel_size=3,padding=1,activation="relu")) 19 | net.add(nn.Conv2D(channels=256,kernel_size=3,padding=1,activation="relu")) 20 | net.add(nn.MaxPool2D(pool_size=3,strides=2)) 21 | # 第四阶段 22 | net.add(nn.Flatten()) 23 | net.add(nn.Dense(4096,activation="relu")) 24 | net.add(nn.Dropout(.5)) 25 | # 第五阶段 26 | net.add(nn.Dense(4096,activation="relu")) 27 | net.add(nn.Dropout(.5)) 28 | # 第六阶段 29 | net.add(nn.Dense(num_classes)) 30 | 31 | def hybrid_forward(self, F, x, ): 32 | out = x 33 | for i, b in enumerate(self.net): 34 | out = b(out) 35 | if self.verbose: 36 | print('Block %d output: %s' % (i + 1, out.shape)) 37 | return out 38 | 39 | 40 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/models/MXNet_DenseNet.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | 3 | from mxnet.gluon import nn 4 | from mxnet import nd 5 | 6 | class DenseBlock(nn.HybridBlock): 7 | # layer 表示层级,growth_rate 表示每个卷积输出的通道数 8 | def __init__(self,layers,growth_rate,**kwargs): 9 | super(DenseBlock,self).__init__(**kwargs) 10 | self.net = net = nn.HybridSequential() 11 | for i in range(layers): 12 | net.add(self.conv_block(channels=growth_rate)) 13 | 14 | def hybrid_forward(self, F, x, *args, **kwargs): 15 | for layer in self.net: 16 | out = layer(x) 17 | x = nd.concat(x,out,dim=1) 18 | return x 19 | 20 | def conv_block(self,channels): 21 | out = nn.HybridSequential() 22 | out.add( 23 | nn.BatchNorm(), 24 | nn.Conv2D(channels=channels,kernel_size=3,padding=1,activation="relu") 25 | ) 26 | return out 27 | 28 | 29 | class MXNet_DenseNet(nn.HybridBlock): 30 | def __init__(self,num_class,verbose=False,init_channels=64,**kwargs): 31 | super(MXNet_DenseNet,self).__init__(**kwargs) 32 | self.verbose = verbose 33 | block_layers = [6,12,24,16] 34 | growth_rate = 64 35 | with self.name_scope(): 36 | # block 1 37 | b1 = nn.HybridSequential() 38 | b1.add( 39 | nn.Conv2D(init_channels,kernel_size=7,strides=2,padding=3), 40 | nn.BatchNorm(), 41 | nn.Activation("relu"), 42 | nn.MaxPool2D(pool_size=3,strides=2,padding=1) 43 | ) 44 | # block 2 :DenseBlock 45 | b2 = nn.HybridSequential() 46 | channels = init_channels 47 | for i,layers in enumerate(block_layers): 48 | b2.add(DenseBlock(layers=layers,growth_rate=growth_rate)) 49 | channels += layers*growth_rate 50 | if i != (len(block_layers)-1): 51 | b2.add(self.transition_block(channels//2)) 52 | # block 3 53 | b3 = nn.HybridSequential() 54 | b3.add( 55 | nn.BatchNorm(), 56 | nn.Activation("relu"), 57 | nn.AvgPool2D(pool_size=1), 58 | nn.Flatten(), 59 | nn.Dense(num_class) 60 | ) 61 | self.net = net = nn.HybridSequential() 62 | net.add(b1,b2,b3) 63 | 64 | def hybrid_forward(self, F, x, *args, **kwargs): 65 | out = x 66 | for i,b in enumerate(self.net): 67 | out = b(x) 68 | if self.verbose: 69 | print("Block %d output %s" % (i+1,out.shape)) 70 | return out 71 | 72 | # 过渡块,主要是用来 对通道数激增进行过渡,将长宽减半,同时改变通道数目 73 | def transition_block(self,channels): 74 | out = nn.HybridSequential() 75 | out.add( 76 | nn.BatchNorm(), 77 | nn.Activation("relu"), 78 | nn.Conv2D(channels=channels,kernel_size=1), 79 | nn.AvgPool2D(pool_size=2,strides=2) 80 | ) 81 | return out 82 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/models/MXNet_GoogLeNet.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | from mxnet.gluon import nn 3 | from mxnet import nd 4 | 5 | class Inception(nn.HybridBlock): 6 | def __init__(self,n1_1,n2_1,n2_3,n3_1,n3_5,n4_1,**kwargs): 7 | super(Inception,self).__init__(**kwargs) 8 | #path 1 9 | self.p1_conv_1 = nn.Conv2D(channels=n1_1,kernel_size=1,activation="relu") 10 | 11 | #path 2 12 | self.p2_conv_1 = nn.Conv2D(channels=n2_1,kernel_size=1,activation="relu") 13 | self.p2_conv_3 = nn.Conv2D(channels=n2_3,kernel_size=3,padding=1,activation="relu") 14 | 15 | #path 3 16 | self.p3_conv_1 = nn.Conv2D(channels=n3_1,kernel_size=1,activation="relu") 17 | self.p3_conv_5 = nn.Conv2D(channels=n3_5,kernel_size=5,padding=2,activation="relu") 18 | 19 | #path 4 20 | self.p4_pool_3 = nn.MaxPool2D(pool_size=3,padding=1,strides=1) 21 | self.p4_conv_1 = nn.Conv2D(channels=n4_1,kernel_size=1,activation="relu") 22 | 23 | def hybrid_forward(self, F, x): 24 | p1 = self.p1_conv_1(x) 25 | p2 = self.p2_conv_3(self.p2_conv_1(x)) 26 | p3 = self.p3_conv_5(self.p3_conv_1(x)) 27 | p4 = self.p4_conv_1(self.p4_pool_3(x)) 28 | 29 | return nd.concat(p1,p2,p3,p4,dim=1) 30 | 31 | 32 | class MXNet_GoogLeNet(nn.HybridBlock): 33 | def __init__(self,num_class,verbose=False,**kwargs): 34 | super(MXNet_GoogLeNet,self).__init__(**kwargs) 35 | self.verbose = verbose 36 | with self.name_scope(): 37 | # block 1 38 | b1 = nn.HybridSequential() 39 | b1.add( 40 | nn.Conv2D(64,kernel_size=7,strides=2,padding=3,activation="relu"), 41 | nn.MaxPool2D(pool_size=3,strides=2) 42 | ) 43 | #block 2 44 | b2 = nn.HybridSequential() 45 | b2.add( 46 | nn.Conv2D(64,kernel_size=1), 47 | nn.Conv2D(192,kernel_size=3,padding=1), 48 | nn.MaxPool2D(pool_size=3,strides=2) 49 | ) 50 | #block 3 51 | b3 = nn.HybridSequential() 52 | b3.add( 53 | Inception(64,96,128,16,32,32), 54 | Inception(128,128,192,32,96,64), 55 | nn.MaxPool2D(pool_size=3,strides=2) 56 | ) 57 | 58 | # block 4 59 | b4 = nn.HybridSequential() 60 | b4.add( 61 | Inception(192, 96, 208, 16, 48, 64), 62 | Inception(160, 112, 224, 24, 64, 64), 63 | Inception(128, 128, 256, 24, 64, 64), 64 | Inception(112, 144, 288, 32, 64, 64), 65 | Inception(256, 160, 320, 32, 128, 128), 66 | nn.MaxPool2D(pool_size=3, strides=2) 67 | ) 68 | 69 | # block 5 70 | b5 = nn.HybridSequential() 71 | b5.add( 72 | Inception(256, 160, 320, 32, 128, 128), 73 | Inception(384, 192, 384, 48, 128, 128), 74 | nn.AvgPool2D(pool_size=2) 75 | ) 76 | # block 6 77 | b6 = nn.HybridSequential() 78 | b6.add( 79 | nn.Flatten(), 80 | nn.Dense(num_class) 81 | ) 82 | 83 | net = self.net = nn.HybridSequential() 84 | net.add(b1,b2,b3,b4,b5,b6) 85 | def hybrid_forward(self, F, x): 86 | out = x 87 | for i, b in enumerate(self.net): 88 | out = b(out) 89 | if self.verbose: 90 | print('Block %d output: %s' % (i + 1, out.shape)) 91 | return out 92 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/models/MXNet_ResNet.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | # ResNet-18 3 | 4 | from mxnet.gluon import nn 5 | from mxnet import nd 6 | 7 | class Residual(nn.HybridBlock): 8 | def __init__(self,channels,same_shape=True,**kwargs): 9 | super(Residual,self).__init__(**kwargs) 10 | self.same_shape = same_shape 11 | strides = 1 if same_shape else 2 12 | self.conv1 = nn.Conv2D(channels=channels,kernel_size=3,padding=1,strides=strides) 13 | self.bn1 = nn.BatchNorm() 14 | 15 | self.conv2 = nn.Conv2D(channels=channels,kernel_size=3,padding=1) 16 | self.bn2 = nn.BatchNorm() 17 | 18 | if not same_shape: 19 | self.conv3 = nn.Conv2D(channels,kernel_size=1,strides=strides) 20 | 21 | def hybrid_forward(self, F, x, *args, **kwargs): 22 | out = nd.relu(self.bn1(self.conv1(x))) 23 | out = self.bn2(self.conv2(out)) 24 | if not self.same_shape: 25 | x = self.conv3(x) 26 | return nd.relu(x+out) 27 | 28 | class MXNet_ResNet(nn.HybridBlock): 29 | def __init__(self,num_class,verbose=False,**kwargs): 30 | super(MXNet_ResNet,self).__init__(**kwargs) 31 | self.verbose = verbose 32 | with self.name_scope(): 33 | # block 1 34 | b1 = nn.Conv2D(channels=64,kernel_size=7,strides=2) 35 | # block 2 36 | b2 = nn.HybridSequential() 37 | b2.add( 38 | nn.MaxPool2D(pool_size=3,strides=2), 39 | Residual(64), 40 | Residual(64) 41 | ) 42 | #block 3 43 | b3 = nn.HybridSequential() 44 | b3.add( 45 | Residual(128,same_shape=False), 46 | Residual(128) 47 | ) 48 | # block 4 49 | b4 = nn.HybridSequential() 50 | b4.add( 51 | Residual(256,same_shape=False), 52 | Residual(256) 53 | ) 54 | # block 5 55 | b5 = nn.HybridSequential() 56 | b5.add( 57 | Residual(512,same_shape=False), 58 | Residual(512) 59 | ) 60 | # block 6 61 | b6 = nn.HybridSequential() 62 | b6.add( 63 | nn.AvgPool2D(pool_size=3), 64 | nn.Dense(num_class) 65 | ) 66 | 67 | # all block together 68 | self.net = net = nn.HybridSequential() 69 | net.add(b1,b2,b3,b4,b5,b6) 70 | 71 | def hybrid_forward(self, F, x, *args, **kwargs): 72 | out = x 73 | for i,b in enumerate(self.net): 74 | out = b(out) 75 | if self.verbose: 76 | print("Block %d output %s" % (i+1,out.shape)) 77 | return out 78 | 79 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/models/MXNet_VGG.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | from mxnet.gluon import nn 3 | 4 | class MXNet_VGG(nn.HybridBlock): 5 | def __init__(self, num_classes, verbose=False, **kwargs): 6 | super(MXNet_VGG, self).__init__(**kwargs) 7 | self.verbose = verbose 8 | architecture = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512)) 9 | with self.name_scope(): 10 | net = self.net = nn.HybridSequential() 11 | net.add(self.vgg_stack(architecture)) 12 | net.add(nn.Flatten()) 13 | net.add(nn.Dense(4096,activation="relu")) 14 | net.add(nn.Dropout(0.5)) 15 | net.add(nn.Dense(4096,activation="relu")) 16 | net.add(nn.Dropout(0.5)) 17 | net.add(nn.Dense(num_classes)) 18 | 19 | @staticmethod 20 | def vgg_block(num_convs,channels): 21 | out = nn.HybridSequential() 22 | for _ in range(num_convs): 23 | out.add(nn.Conv2D(channels=channels,kernel_size=3,padding=1,activation="relu")) 24 | out.add(nn.MaxPool2D(pool_size=2,strides=2)) 25 | return out 26 | 27 | def vgg_stack(self,architecture): 28 | out = nn.HybridSequential() 29 | for (num_convs,channels) in architecture: 30 | out.add(self.vgg_block(num_convs,channels)) 31 | return out 32 | 33 | def hybrid_forward(self, F, x,): 34 | out = x 35 | for i, b in enumerate(self.net): 36 | out = b(out) 37 | if self.verbose: 38 | print('Block %d output: %s' % (i+1, out.shape)) 39 | return out 40 | 41 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/models/__init__.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | from .MXNet_AlexNet import * 3 | from .MXNet_ResNet import * 4 | from .MXNet_VGG import * 5 | from .MXNet_GoogLeNet import * 6 | from .MXNet_DenseNet import * -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/MXNet/utils.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | import mxnet as mx 3 | from mxnet import nd 4 | 5 | def try_gpu(): 6 | """If GPU is available, return mx.gpu(0); else return mx.cpu()""" 7 | try: 8 | ctx = mx.gpu() 9 | _ = nd.array([0], ctx=ctx) 10 | except: 11 | ctx = mx.cpu() 12 | return ctx 13 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/PyTorch/models/PyTorch_AlexNet.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | 3 | from torch import nn 4 | 5 | class PyTorch_AlexNet(nn.Module): 6 | def __init__(self,num_classes,verbose=False): 7 | super(PyTorch_AlexNet, self).__init__() 8 | self.verbose = verbose 9 | 10 | self.features = nn.Sequential( 11 | # 第一层 12 | nn.Conv2d(3,96,kernel_size=11,stride=4,padding=2), 13 | nn.ReLU(), 14 | nn.MaxPool2d(kernel_size=3,stride=2), 15 | # 第二层 16 | nn.Conv2d(96,256,kernel_size=5,padding=2), 17 | nn.ReLU(), 18 | nn.MaxPool2d(kernel_size=3,stride=2), 19 | # 第三层 20 | nn.Conv2d(256,384,kernel_size=3,padding=1), 21 | nn.ReLU(), 22 | # 第四层 23 | nn.Conv2d(384,384,kernel_size=3,padding=1), 24 | nn.ReLU(), 25 | # 第五层 26 | nn.Conv2d(384,256,kernel_size=3,padding=1), 27 | nn.ReLU(), 28 | nn.MaxPool2d(kernel_size=3,stride=2), 29 | ) 30 | self.classifier = nn.Sequential( 31 | nn.Linear(6*6*256,4096), 32 | nn.ReLU(), 33 | nn.Dropout(), 34 | nn.Linear(4096,4096), 35 | nn.ReLU(), 36 | nn.Dropout(), 37 | nn.Linear(4096,num_classes), 38 | ) 39 | 40 | def forward(self, x): 41 | x = self.features(x) 42 | if self.verbose: 43 | print(x.shape) 44 | x = x.view(x.size(0),-1) 45 | x = self.classifier(x) 46 | return x 47 | 48 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/PyTorch/models/PyTorch_GoogLeNet.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | # Todo: 3 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/PyTorch/models/PyTorch_ResNet.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | from torch.nn import functional as F 3 | 4 | class ResidualBlock(nn.Module): 5 | def __init__(self,inchannel,outchannel,stride=1,shortcut=None): 6 | super(ResidualBlock,self).__init__() 7 | self.left = nn.Sequential( 8 | nn.Conv2d(inchannel,outchannel,3,stride,1,bias=False), 9 | nn.BatchNorm2d(outchannel), 10 | nn.ReLU(), 11 | nn.Conv2d(outchannel,outchannel,3,1,1,bias=False), 12 | nn.BatchNorm2d(outchannel) 13 | ) 14 | self.right = shortcut 15 | 16 | def forward(self, x): 17 | out = self.left(x) 18 | residual = x if self.right is None else self.right(x) 19 | out += residual 20 | return F.relu(out) 21 | 22 | class Pytorch_ResNet(nn.Module): 23 | def __init__(self,num_classes,verbose=True): 24 | super(Pytorch_ResNet,self).__init__() 25 | self.verbose = verbose 26 | self.first = nn.Sequential( 27 | nn.Conv2d(3,64,7,2,3,bias=False), 28 | nn.BatchNorm2d(64), 29 | nn.ReLU(inplace=True), 30 | nn.MaxPool2d(3,2,1) 31 | ) 32 | self.layer1 = self._make_layer(64,128,3) 33 | self.layer2 = self._make_layer(128,256,4,stride=2) 34 | self.layer3 = self._make_layer(256,512,6,stride=2) 35 | self.layer4 = self._make_layer(512,512,3,stride=2) 36 | 37 | self.fc = nn.Linear(512,num_classes) 38 | 39 | def _make_layer(self,inchannel,outchannel,block_num,stride=1): 40 | shortcut = nn.Sequential( 41 | nn.Conv2d(inchannel,outchannel,1,stride,bias=False), 42 | nn.BatchNorm2d(outchannel) 43 | ) 44 | layers = [] 45 | layers.append(ResidualBlock(inchannel,outchannel,stride,shortcut)) 46 | for i in range(1,block_num): 47 | layers.append(ResidualBlock(outchannel,outchannel)) 48 | return nn.Sequential(*layers) 49 | def forward(self, x): 50 | x = self.first(x) 51 | 52 | x = self.layer1(x) 53 | x = self.layer2(x) 54 | x = self.layer3(x) 55 | x = self.layer4(x) 56 | 57 | x = F.avg_pool2d(x,7) 58 | if self.verbose: 59 | print(x.shape) 60 | x = x.view(x.size(0),-1) 61 | return self.fc(x) 62 | -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/PyTorch/models/__init__.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | from .PyTorch_AlexNet import * 3 | from .PyTorch_ResNet import * -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/TensorFlow/CNN_MNIST_test.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | # python 3 | from tensorflow.examples.tutorials.mnist import input_data 4 | import tensorflow as tf 5 | mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) 6 | sess = tf.InteractiveSession() 7 | 8 | def weight_variable(shape): 9 | initial = tf.truncated_normal(shape,stddev=0.1) 10 | return tf.Variable(initial) 11 | 12 | def bias_variable(shape): 13 | initial = tf.constant(0.1,shape=shape) 14 | return tf.Variable(initial) 15 | 16 | def conv2d(x,W): 17 | return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME') 18 | 19 | def max_pool_2x2(x): 20 | return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') 21 | 22 | x = tf.placeholder(tf.float32,[None,784]) 23 | y_ = tf.placeholder(tf.float32,[None,10]) 24 | x_image = tf.reshape(x,[-1,28,28,1]) 25 | 26 | w_conv1 = weight_variable([5,5,1,32]) 27 | b_conv1 = bias_variable([32]) 28 | h_conv1 = tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1) 29 | h_pool1 = max_pool_2x2(h_conv1) 30 | 31 | w_conv2 = weight_variable([5,5,32,64]) 32 | b_conv2 = bias_variable([64]) 33 | h_conv2 = tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2) 34 | h_pool2 = max_pool_2x2(h_conv2) 35 | 36 | W_fc1 = weight_variable([7*7*64,1024]) 37 | b_fc1 = bias_variable([1024]) 38 | h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64]) 39 | h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1) 40 | 41 | keep_prob = tf.placeholder(tf.float32) 42 | h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob) 43 | 44 | W_fc2 = weight_variable([1024,10]) 45 | b_fc2 = bias_variable([10]) 46 | y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2) 47 | 48 | cross_entroy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1])) 49 | train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entroy) 50 | 51 | correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1)) 52 | accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) 53 | 54 | tf.global_variables_initializer().run() 55 | for i in range(20000): 56 | batch = mnist.train.next_batch(50) 57 | if i%100 == 0: 58 | train_accuracy = accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0}) 59 | print ("Step %d,training accuracy %g"%(i,train_accuracy)) 60 | train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5}) 61 | 62 | print ("test accuracy %g"%accuracy.eval(feed_dict={ 63 | x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})) -------------------------------------------------------------------------------- /Basic-Tutorial/Classic-CNN-Structure/TensorFlow/test2.py: -------------------------------------------------------------------------------- 1 | #-*-coding:utf-8-*- 2 | import tensorflow as tf 3 | import numpy as np 4 | from tensorflow.examples.tutorials.mnist import input_data 5 | 6 | print('Download and Extract MNIST dataset') 7 | mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) # one_hot=True意思是编码格式为01编码 8 | print("tpye of 'mnist' is %s" % (type(mnist))) 9 | print("number of train data is %d" % (mnist.train.num_examples)) 10 | print("number of test data is %d" % (mnist.test.num_examples)) 11 | trainimg = mnist.train.images 12 | trainlabel = mnist.train.labels 13 | testimg = mnist.test.images 14 | testlabel = mnist.test.labels 15 | print("MNIST loaded") 16 | 17 | """ 18 | print("type of 'trainimg' is %s" % (type(trainimg))) 19 | print("type of 'trainlabel' is %s" % (type(trainlabel))) 20 | print("type of 'testimg' is %s" % (type(testimg))) 21 | print("type of 'testlabel' is %s" % (type(testlabel))) 22 | print("------------------------------------------------") 23 | print("shape of 'trainimg' is %s" % (trainimg.shape,)) 24 | print("shape of 'trainlabel' is %s" % (trainlabel.shape,)) 25 | print("shape of 'testimg' is %s" % (testimg.shape,)) 26 | print("shape of 'testlabel' is %s" % (testlabel.shape,)) 27 | 28 | """ 29 | 30 | x = tf.placeholder(tf.float32, [None, 784]) 31 | y = tf.placeholder(tf.float32, [None, 10]) # None is for infinite 32 | w = tf.Variable(tf.zeros([784, 10])) # 为了方便直接用0初始化,可以高斯初始化 33 | b = tf.Variable(tf.zeros([10])) # 10分类的任务,10种label,所以只需要初始化10个b 34 | 35 | pred = tf.nn.softmax(tf.matmul(x, w) + b) # 前向传播的预测值 36 | cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=[1])) # 交叉熵损失函数 37 | optm = tf.train.GradientDescentOptimizer(0.01).minimize(cost) 38 | corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.equal()对比预测值的索引和真实label的索引是否一样,一样返回True,不一样返回False 39 | accr = tf.reduce_mean(tf.cast(corr, tf.float32)) 40 | 41 | init = tf.global_variables_initializer() # 全局参数初始化器 42 | 43 | training_epochs = 100 # 所有样本迭代100次 44 | batch_size = 100 # 每进行一次迭代选择100个样本 45 | display_step = 5 46 | # SESSION 47 | sess = tf.Session() # 定义一个Session 48 | sess.run(init) # 在sess里run一下初始化操作 49 | # MINI-BATCH LEARNING 50 | for epoch in range(training_epochs): # 每一个epoch进行循环 51 | avg_cost = 0. # 刚开始损失值定义为0 52 | num_batch = int(mnist.train.num_examples/batch_size) 53 | for i in range(num_batch): # 每一个batch进行选择 54 | batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 通过next_batch()就可以一个一个batch的拿数据, 55 | sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) # run一下用梯度下降进行求解,通过placeholder把x,y传进来 56 | avg_cost += sess.run(cost, feed_dict={x: batch_xs, y:batch_ys})/num_batch 57 | # DISPLAY 58 | if epoch % display_step == 0: # display_step之前定义为5,这里每5个epoch打印一下 59 | train_acc = sess.run(accr, feed_dict={x: batch_xs, y:batch_ys}) 60 | test_acc = sess.run(accr, feed_dict={x: mnist.test.images, y: mnist.test.labels}) 61 | print("Epoch: %03d/%03d cost: %.9f TRAIN ACCURACY: %.3f TEST ACCURACY: %.3f" 62 | % (epoch, training_epochs, avg_cost, train_acc, test_acc)) 63 | print("DONE") -------------------------------------------------------------------------------- /Basic-Tutorial/MXNet/gluon-basics/custom-layer.md: -------------------------------------------------------------------------------- 1 | # 设计自定义层 2 | 3 | 神经网络的一个魅力是它有大量的层,例如全连接、卷积、循环、激活,和各式花样的连接方式。我们之前学到了如何使用Gluon提供的层来构建新的层(`nn.Block`)继而得到神经网络。虽然Gluon提供了大量的[层的定义](https://mxnet.incubator.apache.org/versions/master/api/python/gluon/gluon.html#neural-network-layers),但我们仍然会遇到现有层不够用的情况。 4 | 5 | 这时候的一个自然的想法是,我们不是学习了如何只使用基础数值运算包`NDArray`来实现各种的模型吗?它提供了大量的[底层计算函数](https://mxnet.incubator.apache.org/versions/master/api/python/ndarray/ndarray.html)足以实现即使不是100%那也是95%的神经网络吧。 6 | 7 | 但每次都从头写容易写到怀疑人生。实际上,即使在纯研究的领域里,我们也很少发现纯新的东西,大部分时候是在现有模型的基础上做一些改进。所以很可能大部分是可以沿用前面的而只有一部分是需要自己来实现。 8 | 9 | 这个教程我们将介绍如何使用底层的`NDArray`接口来实现一个`Gluon`的层,从而可以以后被重复调用。 10 | 11 | ## 定义一个简单的层 12 | 13 | 我们先来看如何定义一个简单层,它不需要维护模型参数。事实上这个跟前面介绍的如何使用nn.Block没什么区别。下面代码定义一个层将输入减掉均值。 14 | 15 | ```{.python .input n=1} 16 | from mxnet import nd 17 | from mxnet.gluon import nn 18 | 19 | class CenteredLayer(nn.Block): 20 | def __init__(self, **kwargs): 21 | super(CenteredLayer, self).__init__(**kwargs) 22 | 23 | def forward(self, x): 24 | return x - x.mean() 25 | ``` 26 | 27 | 我们可以马上实例化这个层用起来。 28 | 29 | ```{.python .input n=2} 30 | layer = CenteredLayer() 31 | layer(nd.array([1,2,3,4,5])) 32 | ``` 33 | 34 | 我们也可以用它来构造更复杂的神经网络: 35 | 36 | ```{.python .input n=3} 37 | net = nn.Sequential() 38 | with net.name_scope(): 39 | net.add(nn.Dense(128)) 40 | net.add(nn.Dense(10)) 41 | net.add(CenteredLayer()) 42 | ``` 43 | 44 | 确认下输出的均值确实是0: 45 | 46 | ```{.python .input n=4} 47 | net.initialize() 48 | y = net(nd.random.uniform(shape=(4, 8))) 49 | y.mean() 50 | ``` 51 | 52 | 当然大部分情况你可以看不到一个实实在在的0,而是一个很小的数。例如`5.82076609e-11`。这是因为MXNet默认使用32位float,会带来一定的浮点精度误差。 53 | 54 | ## 带模型参数的自定义层 55 | 56 | 虽然`CenteredLayer`可能会告诉实现自定义层大概是什么样子,但它缺少了重要的一块,就是它没有可以学习的模型参数。 57 | 58 | 记得我们之前访问`Dense`的权重的时候是通过`dense.weight.data()`,这里`weight`是一个`Parameter`的类型。我们可以显示的构建这样的一个参数。 59 | 60 | ```{.python .input n=5} 61 | from mxnet import gluon 62 | my_param = gluon.Parameter("exciting_parameter_yay", shape=(3,3)) 63 | ``` 64 | 65 | 这里我们创建一个$3\times3$大小的参数并取名为"exciting_parameter_yay"。然后用默认方法初始化打印结果。 66 | 67 | ```{.python .input n=6} 68 | my_param.initialize() 69 | (my_param.data(), my_param.grad()) 70 | ``` 71 | 72 | 通常自定义层的时候我们不会直接创建Parameter,而是用过Block自带的一个ParamterDict类型的成员变量`params`,顾名思义,这是一个由字符串名字映射到Parameter的字典。 73 | 74 | ```{.python .input n=7} 75 | pd = gluon.ParameterDict(prefix="block1_") 76 | pd.get("exciting_parameter_yay", shape=(3,3)) 77 | pd 78 | ``` 79 | 80 | 现在我们看下如果如果实现一个跟`Dense`一样功能的层,它概念跟前面的`CenteredLayer`的主要区别是我们在初始函数里通过`params`创建了参数: 81 | 82 | ```{.python .input n=19} 83 | class MyDense(nn.Block): 84 | def __init__(self, units, in_units, **kwargs): 85 | super(MyDense, self).__init__(**kwargs) 86 | with self.name_scope(): 87 | self.weight = self.params.get( 88 | 'weight', shape=(in_units, units)) 89 | self.bias = self.params.get('bias', shape=(units,)) 90 | 91 | def forward(self, x): 92 | linear = nd.dot(x, self.weight.data()) + self.bias.data() 93 | return nd.relu(linear) 94 | ``` 95 | 96 | 我们创建实例化一个对象来看下它的参数,这里我们特意加了前缀`prefix`,这是`nn.Block`初始化函数自带的参数。 97 | 98 | ```{.python .input} 99 | dense = MyDense(5, in_units=10, prefix='o_my_dense_') 100 | dense.params 101 | ``` 102 | 103 | 它的使用跟前面没有什么不一致: 104 | 105 | ```{.python .input n=20} 106 | dense.initialize() 107 | dense(nd.random.uniform(shape=(2,10))) 108 | ``` 109 | 110 | 我们构造的层跟Gluon提供的层用起来没太多区别: 111 | 112 | ```{.python .input n=19} 113 | net = nn.Sequential() 114 | with net.name_scope(): 115 | net.add(MyDense(32, in_units=64)) 116 | net.add(MyDense(2, in_units=32)) 117 | net.initialize() 118 | net(nd.random.uniform(shape=(2,64))) 119 | ``` 120 | 121 | 仔细的你可能还是注意到了,我们这里指定了输入的大小,而Gluon自带的`Dense`则无需如此。我们已经在前面节介绍过了这个延迟初始化如何使用。但如果实现一个这样的层我们将留到后面介绍了hybridize后。 122 | 123 | ## 总结 124 | 125 | 现在我们知道了如何把前面手写过的层全部包装了Gluon能用的Block,之后再用到的时候就可以飞起来了! 126 | 127 | ## 练习 128 | 129 | 1. 怎么修改自定义层里参数的默认初始化函数。 130 | 1. (这个比较难),在一个代码Cell里面输入`nn.Dense??`,看看它是怎么实现的。为什么它就可以支持延迟初始化了。 131 | 132 | **吐槽和讨论欢迎点**[这里](https://discuss.gluon.ai/t/topic/1256) 133 | -------------------------------------------------------------------------------- /Basic-Tutorial/MXNet/gluon-basics/index.md: -------------------------------------------------------------------------------- 1 | # Gluon基础 2 | 3 | ```eval_rst 4 | 5 | .. toctree:: 6 | :maxdepth: 2 7 | 8 | block 9 | parameters 10 | serialization 11 | custom-layer 12 | use-gpu 13 | 14 | ``` 15 | -------------------------------------------------------------------------------- /Basic-Tutorial/MXNet/gluon-basics/serialization.md: -------------------------------------------------------------------------------- 1 | # 序列化 --- 读写模型 2 | 3 | 我们现在已经讲了很多,包括 4 | 5 | - 如何处理数据 6 | - 如何构建模型 7 | - 如何在数据上训练模型 8 | - 如何使用不同的损失函数来做分类和回归 9 | 10 | 但即使知道了所有这些,我们还没有完全准备好来构建一个真正的机器学习系统。这是因为我们还没有讲如何读和写模型。因为现实中,我们通常在一个地方训练好模型,然后部署到很多不同的地方。我们需要把内存中的训练好的模型存在硬盘上好下次使用。 11 | 12 | ## 读写NDArrays 13 | 14 | 作为开始,我们先看看如何读写NDArray。虽然我们可以使用Python的序列化包例如`Pickle`,不过我们更倾向直接`save`和`load`,通常这样更快,而且别的语言,例如R和Scala也能用到。 15 | 16 | ```{.python .input n=2} 17 | from mxnet import nd 18 | 19 | x = nd.ones(3) 20 | y = nd.zeros(4) 21 | filename = "../data/test1.params" 22 | nd.save(filename, [x, y]) 23 | ``` 24 | 25 | 读回来 26 | 27 | ```{.python .input n=3} 28 | a, b = nd.load(filename) 29 | print(a, b) 30 | ``` 31 | 32 | 不仅可以读写单个NDArray,NDArray list,dict也是可以的: 33 | 34 | ```{.python .input n=4} 35 | mydict = {"x": x, "y": y} 36 | filename = "../data/test2.params" 37 | nd.save(filename, mydict) 38 | ``` 39 | 40 | ```{.python .input n=5} 41 | c = nd.load(filename) 42 | print(c) 43 | ``` 44 | 45 | ## 读写Gluon模型的参数 46 | 47 | 跟NDArray类似,Gluon的模型(就是`nn.Block`)提供便利的`save_params`和`load_params`函数来读写数据。我们同前一样创建一个简单的多层感知机 48 | 49 | ```{.python .input n=6} 50 | from mxnet.gluon import nn 51 | 52 | def get_net(): 53 | net = nn.Sequential() 54 | with net.name_scope(): 55 | net.add(nn.Dense(10, activation="relu")) 56 | net.add(nn.Dense(2)) 57 | return net 58 | 59 | net = get_net() 60 | net.initialize() 61 | x = nd.random.uniform(shape=(2,10)) 62 | print(net(x)) 63 | ``` 64 | 65 | 下面我们把模型参数存起来 66 | 67 | ```{.python .input} 68 | filename = "../data/mlp.params" 69 | net.save_params(filename) 70 | ``` 71 | 72 | 之后我们构建一个一样的多层感知机,但不像前面那样随机初始化,我们直接读取前面的模型参数。这样给定同样的输入,新的模型应该会输出同样的结果。 73 | 74 | ```{.python .input n=8} 75 | import mxnet as mx 76 | net2 = get_net() 77 | net2.load_params(filename, mx.cpu()) # FIXME, gluon will support default ctx later 78 | print(net2(x)) 79 | ``` 80 | 81 | ## 总结 82 | 83 | 通过`load_params`和`save_params`可以很方便的读写模型参数。 84 | 85 | **吐槽和讨论欢迎点**[这里](https://discuss.gluon.ai/t/topic/1255) 86 | -------------------------------------------------------------------------------- /Basic-Tutorial/MXNet/gluon-basics/use-gpu.md: -------------------------------------------------------------------------------- 1 | # 使用GPU来计算 2 | 3 | 【注意】运行本教程需要GPU。没有GPU的同学可以大致理解下内容,至少是`context`这个概念,因为之后我们也会用到。但没有GPU不会影响运行之后的大部分教程(好吧,还是有点点,可能运行会稍微慢点)。 4 | 5 | 前面的教程里我们一直在使用CPU来计算,因为绝大部分的计算设备都有CPU。但CPU的设计目的是处理通用的计算,例如打开浏览器和运行Jupyter,它一般只有少数的一块区域复杂数值计算,例如`nd.dot(A, B)`。对于复杂的神经网络和大规模的数据来说,单块CPU可能不够给力。 6 | 7 | 常用的解决办法是要么使用多台机器来协同计算,要么使用数值计算更加强劲的硬件,或者两者一起使用。本教程关注使用单块Nvidia GPU来加速计算,更多的选项例如多GPU和多机器计算则留到后面。 8 | 9 | 首先需要确保至少有一块Nvidia显卡已经安装好了,然后下载安装显卡驱动和[CUDA](https://developer.nvidia.com/cuda-downloads)(推荐下载8.0,CUDA自带了驱动)。完成后应该可以通过`nvidia-smi`查看显卡信息了。(Windows用户需要设一下PATH:`set PATH=C:\Program Files\NVIDIA Corporation\NVSMI;%PATH%`)。 10 | 11 | ```{.python .input n=1} 12 | !nvidia-smi 13 | ``` 14 | 15 | 接下来要要确认正确安装了的`mxnet`的GPU版本。具体来说是卸载了`mxnet`(`pip uninstall mxnet`),然后根据CUDA版本安装`mxnet-cu75`或者`mxnet-cu80`(例如`pip install --pre mxnet-cu80`)。 16 | 17 | 使用pip来确认下: 18 | 19 | ```{.python .input n=2} 20 | import pip 21 | for pkg in ['mxnet', 'mxnet-cu75', 'mxnet-cu80']: 22 | pip.main(['show', pkg]) 23 | ``` 24 | 25 | ## Context 26 | 27 | MXNet使用Context来指定使用哪个设备来存储和计算。默认会将数据开在主内存,然后利用CPU来计算,这个由`mx.cpu()`来表示。GPU则由`mx.gpu()`来表示。注意`mx.cpu()`表示所有的物理CPU和内存,意味着计算上会尽量使用多有的CPU核。但`mx.gpu()`只代表一块显卡和其对应的显卡内存。如果有多块GPU,我们用`mx.gpu(i)`来表示第*i*块GPU(*i*从0开始)。 28 | 29 | ```{.python .input n=3} 30 | import mxnet as mx 31 | [mx.cpu(), mx.gpu(), mx.gpu(1)] 32 | ``` 33 | 34 | ## NDArray的GPU计算 35 | 36 | 每个NDArray都有一个`context`属性来表示它存在哪个设备上,默认会是`cpu`。这是为什么前面每次我们打印NDArray的时候都会看到`@cpu(0)`这个标识。 37 | 38 | ```{.python .input n=4} 39 | from mxnet import nd 40 | x = nd.array([1,2,3]) 41 | x.context 42 | ``` 43 | 44 | ### GPU上创建内存 45 | 46 | 我们可以在创建的时候指定创建在哪个设备上(如果GPU不能用或者没有装MXNet GPU版本,这里会有error): 47 | 48 | ```{.python .input n=5} 49 | a = nd.array([1,2,3], ctx=mx.gpu()) 50 | b = nd.zeros((3,2), ctx=mx.gpu()) 51 | c = nd.random.uniform(shape=(2,3), ctx=mx.gpu()) 52 | (a,b,c) 53 | ``` 54 | 55 | 尝试将内存开到另外一块GPU上。如果不存在会报错。当然,如果你有大于10块GPU,那么下面代码会顺利执行。 56 | 57 | ```{.python .input n=6} 58 | import sys 59 | 60 | try: 61 | nd.array([1,2,3], ctx=mx.gpu(10)) 62 | except mx.MXNetError as err: 63 | sys.stderr.write(str(err)) 64 | ``` 65 | 66 | 我们可以通过`copyto`和`as_in_context`来在设备直接传输数据。 67 | 68 | ```{.python .input n=7} 69 | y = x.copyto(mx.gpu()) 70 | z = x.as_in_context(mx.gpu()) 71 | (y, z) 72 | ``` 73 | 74 | 这两个函数的主要区别是,如果源和目标的context一致,`as_in_context`不复制,而`copyto`总是会新建内存: 75 | 76 | ```{.python .input n=8} 77 | yy = y.as_in_context(mx.gpu()) 78 | zz = z.copyto(mx.gpu()) 79 | (yy is y, zz is z) 80 | ``` 81 | 82 | ### GPU上的计算 83 | 84 | 计算会在数据的`context`上执行。所以为了使用GPU,我们只需要事先将数据放在上面就行了。结果会自动保存在对应的设备上: 85 | 86 | ```{.python .input n=9} 87 | nd.exp(z + 2) * y 88 | ``` 89 | 90 | 注意所有计算要求输入数据在同一个设备上。不一致的时候系统不进行自动复制。这个设计的目的是因为设备之间的数据交互通常比较昂贵,我们希望用户确切的知道数据放在哪里,而不是隐藏这个细节。下面代码尝试将CPU上`x`和GPU上的`y`做运算。 91 | 92 | ```{.python .input n=10} 93 | try: 94 | x + y 95 | except mx.MXNetError as err: 96 | sys.stderr.write(str(err)) 97 | ``` 98 | 99 | ### 默认会复制回CPU的操作 100 | 101 | 如果某个操作需要将NDArray里面的内容转出来,例如打印或变成numpy格式,如果需要的话系统都会自动将数据copy到主内存。 102 | 103 | ```{.python .input n=11} 104 | print(y) 105 | print(y.asnumpy()) 106 | print(y.sum().asscalar()) 107 | ``` 108 | 109 | ## Gluon的GPU计算 110 | 111 | 同NDArray类似,Gluon的大部分函数可以通过`ctx`指定设备。下面代码将模型参数初始化在GPU上: 112 | 113 | ```{.python .input n=12} 114 | from mxnet import gluon 115 | net = gluon.nn.Sequential() 116 | net.add(gluon.nn.Dense(1)) 117 | 118 | net.initialize(ctx=mx.gpu()) 119 | ``` 120 | 121 | 输入GPU上的数据,会在GPU上计算结果 122 | 123 | ```{.python .input n=13} 124 | data = nd.random.uniform(shape=[3,2], ctx=mx.gpu()) 125 | net(data) 126 | ``` 127 | 128 | 确认下权重: 129 | 130 | ```{.python .input n=14} 131 | net[0].weight.data() 132 | ``` 133 | 134 | ## 总结 135 | 136 | 通过`context`我们可以很容易在不同的设备上计算。 137 | 138 | ## 练习 139 | 140 | - 试试大一点的计算任务,例如大矩阵的乘法,看看CPU和GPU的速度区别。如果是计算量很小的任务呢? 141 | - 试试CPU和GPU之间传递数据的速度 142 | - GPU上如何读写模型呢? 143 | 144 | **吐槽和讨论欢迎点**[这里](https://discuss.gluon.ai/t/topic/988) 145 | -------------------------------------------------------------------------------- /Basic-Tutorial/MXNet/readme.md: -------------------------------------------------------------------------------- 1 | form [here](https://zh.gluon.ai/) -------------------------------------------------------------------------------- /Basic-Tutorial/PaddlePaddle/readme.md: -------------------------------------------------------------------------------- 1 | official tutorial [here](http://www.paddlepaddle.org/docs/develop/book/01.fit_a_line/index.cn.html) -------------------------------------------------------------------------------- /Basic-Tutorial/PyTorch/feedforward_neural_network/main-gpu.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torchvision.datasets as dsets 4 | import torchvision.transforms as transforms 5 | from torch.autograd import Variable 6 | 7 | 8 | # Hyper Parameters 9 | input_size = 784 10 | hidden_size = 500 11 | num_classes = 10 12 | num_epochs = 5 13 | batch_size = 100 14 | learning_rate = 0.001 15 | 16 | # MNIST Dataset 17 | train_dataset = dsets.MNIST(root='./data', 18 | train=True, 19 | transform=transforms.ToTensor(), 20 | download=True) 21 | 22 | test_dataset = dsets.MNIST(root='./data', 23 | train=False, 24 | transform=transforms.ToTensor()) 25 | 26 | # Data Loader (Input Pipeline) 27 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 28 | batch_size=batch_size, 29 | shuffle=True) 30 | 31 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 32 | batch_size=batch_size, 33 | shuffle=False) 34 | 35 | # Neural Network Model (1 hidden layer) 36 | class Net(nn.Module): 37 | def __init__(self, input_size, hidden_size, num_classes): 38 | super(Net, self).__init__() 39 | self.fc1 = nn.Linear(input_size, hidden_size) 40 | self.relu = nn.ReLU() 41 | self.fc2 = nn.Linear(hidden_size, num_classes) 42 | 43 | def forward(self, x): 44 | out = self.fc1(x) 45 | out = self.relu(out) 46 | out = self.fc2(out) 47 | return out 48 | 49 | net = Net(input_size, hidden_size, num_classes) 50 | net.cuda() 51 | 52 | # Loss and Optimizer 53 | criterion = nn.CrossEntropyLoss() 54 | optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate) 55 | 56 | # Train the Model 57 | for epoch in range(num_epochs): 58 | for i, (images, labels) in enumerate(train_loader): 59 | # Convert torch tensor to Variable 60 | images = Variable(images.view(-1, 28*28).cuda()) 61 | labels = Variable(labels.cuda()) 62 | 63 | # Forward + Backward + Optimize 64 | optimizer.zero_grad() # zero the gradient buffer 65 | outputs = net(images) 66 | loss = criterion(outputs, labels) 67 | loss.backward() 68 | optimizer.step() 69 | 70 | if (i+1) % 100 == 0: 71 | print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' 72 | %(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0])) 73 | 74 | # Test the Model 75 | correct = 0 76 | total = 0 77 | for images, labels in test_loader: 78 | images = Variable(images.view(-1, 28*28)).cuda() 79 | outputs = net(images) 80 | _, predicted = torch.max(outputs.data, 1) 81 | total += labels.size(0) 82 | correct += (predicted.cpu() == labels).sum() 83 | 84 | print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total)) 85 | 86 | # Save the Model 87 | torch.save(net.state_dict(), 'model.pkl') 88 | -------------------------------------------------------------------------------- /Basic-Tutorial/PyTorch/feedforward_neural_network/main.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torchvision.datasets as dsets 4 | import torchvision.transforms as transforms 5 | from torch.autograd import Variable 6 | 7 | 8 | # Hyper Parameters 9 | input_size = 784 10 | hidden_size = 500 11 | num_classes = 10 12 | num_epochs = 5 13 | batch_size = 100 14 | learning_rate = 0.001 15 | 16 | # MNIST Dataset 17 | train_dataset = dsets.MNIST(root='./data', 18 | train=True, 19 | transform=transforms.ToTensor(), 20 | download=True) 21 | 22 | test_dataset = dsets.MNIST(root='./data', 23 | train=False, 24 | transform=transforms.ToTensor()) 25 | 26 | # Data Loader (Input Pipeline) 27 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 28 | batch_size=batch_size, 29 | shuffle=True) 30 | 31 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 32 | batch_size=batch_size, 33 | shuffle=False) 34 | 35 | # Neural Network Model (1 hidden layer) 36 | class Net(nn.Module): 37 | def __init__(self, input_size, hidden_size, num_classes): 38 | super(Net, self).__init__() 39 | self.fc1 = nn.Linear(input_size, hidden_size) 40 | self.relu = nn.ReLU() 41 | self.fc2 = nn.Linear(hidden_size, num_classes) 42 | 43 | def forward(self, x): 44 | out = self.fc1(x) 45 | out = self.relu(out) 46 | out = self.fc2(out) 47 | return out 48 | 49 | net = Net(input_size, hidden_size, num_classes) 50 | 51 | 52 | # Loss and Optimizer 53 | criterion = nn.CrossEntropyLoss() 54 | optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate) 55 | 56 | # Train the Model 57 | for epoch in range(num_epochs): 58 | for i, (images, labels) in enumerate(train_loader): 59 | # Convert torch tensor to Variable 60 | images = Variable(images.view(-1, 28*28)) 61 | labels = Variable(labels) 62 | 63 | # Forward + Backward + Optimize 64 | optimizer.zero_grad() # zero the gradient buffer 65 | outputs = net(images) 66 | loss = criterion(outputs, labels) 67 | loss.backward() 68 | optimizer.step() 69 | 70 | if (i+1) % 100 == 0: 71 | print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' 72 | %(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0])) 73 | 74 | # Test the Model 75 | correct = 0 76 | total = 0 77 | for images, labels in test_loader: 78 | images = Variable(images.view(-1, 28*28)) 79 | outputs = net(images) 80 | _, predicted = torch.max(outputs.data, 1) 81 | total += labels.size(0) 82 | correct += (predicted == labels).sum() 83 | 84 | print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total)) 85 | 86 | # Save the Model 87 | torch.save(net.state_dict(), 'model.pkl') -------------------------------------------------------------------------------- /Basic-Tutorial/PyTorch/linear_regression/main.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from torch.autograd import Variable 6 | 7 | 8 | # Hyper Parameters 9 | input_size = 1 10 | output_size = 1 11 | num_epochs = 60 12 | learning_rate = 0.001 13 | 14 | # Toy Dataset 15 | x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168], 16 | [9.779], [6.182], [7.59], [2.167], [7.042], 17 | [10.791], [5.313], [7.997], [3.1]], dtype=np.float32) 18 | 19 | y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573], 20 | [3.366], [2.596], [2.53], [1.221], [2.827], 21 | [3.465], [1.65], [2.904], [1.3]], dtype=np.float32) 22 | 23 | # Linear Regression Model 24 | class LinearRegression(nn.Module): 25 | def __init__(self, input_size, output_size): 26 | super(LinearRegression, self).__init__() 27 | self.linear = nn.Linear(input_size, output_size) 28 | 29 | def forward(self, x): 30 | out = self.linear(x) 31 | return out 32 | 33 | model = LinearRegression(input_size, output_size) 34 | 35 | # Loss and Optimizer 36 | criterion = nn.MSELoss() 37 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 38 | 39 | # Train the Model 40 | for epoch in range(num_epochs): 41 | # Convert numpy array to torch Variable 42 | inputs = Variable(torch.from_numpy(x_train)) 43 | targets = Variable(torch.from_numpy(y_train)) 44 | 45 | # Forward + Backward + Optimize 46 | optimizer.zero_grad() 47 | outputs = model(inputs) 48 | loss = criterion(outputs, targets) 49 | loss.backward() 50 | optimizer.step() 51 | 52 | if (epoch+1) % 5 == 0: 53 | print ('Epoch [%d/%d], Loss: %.4f' 54 | %(epoch+1, num_epochs, loss.data[0])) 55 | 56 | # Plot the graph 57 | predicted = model(Variable(torch.from_numpy(x_train))).data.numpy() 58 | plt.plot(x_train, y_train, 'ro', label='Original data') 59 | plt.plot(x_train, predicted, label='Fitted line') 60 | plt.legend() 61 | plt.show() 62 | 63 | # Save the Model 64 | torch.save(model.state_dict(), 'model.pkl') -------------------------------------------------------------------------------- /Basic-Tutorial/PyTorch/logistic_regression/main.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torchvision.datasets as dsets 4 | import torchvision.transforms as transforms 5 | from torch.autograd import Variable 6 | 7 | 8 | # Hyper Parameters 9 | input_size = 784 10 | num_classes = 10 11 | num_epochs = 5 12 | batch_size = 100 13 | learning_rate = 0.001 14 | 15 | # MNIST Dataset (Images and Labels) 16 | train_dataset = dsets.MNIST(root='./data', 17 | train=True, 18 | transform=transforms.ToTensor(), 19 | download=True) 20 | 21 | test_dataset = dsets.MNIST(root='./data', 22 | train=False, 23 | transform=transforms.ToTensor()) 24 | 25 | # Dataset Loader (Input Pipline) 26 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 27 | batch_size=batch_size, 28 | shuffle=True) 29 | 30 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 31 | batch_size=batch_size, 32 | shuffle=False) 33 | 34 | # Model 35 | class LogisticRegression(nn.Module): 36 | def __init__(self, input_size, num_classes): 37 | super(LogisticRegression, self).__init__() 38 | self.linear = nn.Linear(input_size, num_classes) 39 | 40 | def forward(self, x): 41 | out = self.linear(x) 42 | return out 43 | 44 | model = LogisticRegression(input_size, num_classes) 45 | 46 | # Loss and Optimizer 47 | # Softmax is internally computed. 48 | # Set parameters to be updated. 49 | criterion = nn.CrossEntropyLoss() 50 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 51 | 52 | # Training the Model 53 | for epoch in range(num_epochs): 54 | for i, (images, labels) in enumerate(train_loader): 55 | images = Variable(images.view(-1, 28*28)) 56 | labels = Variable(labels) 57 | 58 | # Forward + Backward + Optimize 59 | optimizer.zero_grad() 60 | outputs = model(images) 61 | loss = criterion(outputs, labels) 62 | loss.backward() 63 | optimizer.step() 64 | 65 | if (i+1) % 100 == 0: 66 | print ('Epoch: [%d/%d], Step: [%d/%d], Loss: %.4f' 67 | % (epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0])) 68 | 69 | # Test the Model 70 | correct = 0 71 | total = 0 72 | for images, labels in test_loader: 73 | images = Variable(images.view(-1, 28*28)) 74 | outputs = model(images) 75 | _, predicted = torch.max(outputs.data, 1) 76 | total += labels.size(0) 77 | correct += (predicted == labels).sum() 78 | 79 | print('Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total)) 80 | 81 | # Save the Model 82 | torch.save(model.state_dict(), 'model.pkl') -------------------------------------------------------------------------------- /Basic-Tutorial/PyTorch/readme.md: -------------------------------------------------------------------------------- 1 | ## PyTorch Tutorials 2 | 3 | Origin from [here](https://github.com/yunjey/pytorch-tutorial) -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-02-1-linear_regression.py: -------------------------------------------------------------------------------- 1 | # Lab 2 Linear Regression 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | # X and Y data 6 | x_train = [1, 2, 3] 7 | y_train = [1, 2, 3] 8 | 9 | # Try to find values for W and b to compute y_data = x_data * W + b 10 | # We know that W should be 1 and b should be 0 11 | # But let TensorFlow figure it out 12 | W = tf.Variable(tf.random_normal([1]), name='weight') 13 | b = tf.Variable(tf.random_normal([1]), name='bias') 14 | 15 | # Our hypothesis XW+b 16 | hypothesis = x_train * W + b 17 | 18 | # cost/loss function 19 | cost = tf.reduce_mean(tf.square(hypothesis - y_train)) 20 | 21 | # Minimize 22 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) 23 | train = optimizer.minimize(cost) 24 | 25 | # Launch the graph in a session. 26 | sess = tf.Session() 27 | # Initializes global variables in the graph. 28 | sess.run(tf.global_variables_initializer()) 29 | 30 | # Fit the line 31 | for step in range(2001): 32 | sess.run(train) 33 | if step % 20 == 0: 34 | print(step, sess.run(cost), sess.run(W), sess.run(b)) 35 | 36 | # Learns best fit W:[ 1.], b:[ 0.] 37 | 38 | ''' 39 | 0 2.82329 [ 2.12867713] [-0.85235667] 40 | 20 0.190351 [ 1.53392804] [-1.05059612] 41 | 40 0.151357 [ 1.45725465] [-1.02391243] 42 | ... 43 | 44 | 1920 1.77484e-05 [ 1.00489295] [-0.01112291] 45 | 1940 1.61197e-05 [ 1.00466311] [-0.01060018] 46 | 1960 1.46397e-05 [ 1.004444] [-0.01010205] 47 | 1980 1.32962e-05 [ 1.00423515] [-0.00962736] 48 | 2000 1.20761e-05 [ 1.00403607] [-0.00917497] 49 | ''' 50 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-02-2-linear_regression_feed.py: -------------------------------------------------------------------------------- 1 | # Lab 2 Linear Regression 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | # Try to find values for W and b to compute y_data = W * x_data + b 6 | # We know that W should be 1 and b should be 0 7 | # But let's use TensorFlow to figure it out 8 | W = tf.Variable(tf.random_normal([1]), name='weight') 9 | b = tf.Variable(tf.random_normal([1]), name='bias') 10 | 11 | # Now we can use X and Y in place of x_data and y_data 12 | # # placeholders for a tensor that will be always fed using feed_dict 13 | # See http://stackoverflow.com/questions/36693740/ 14 | X = tf.placeholder(tf.float32, shape=[None]) 15 | Y = tf.placeholder(tf.float32, shape=[None]) 16 | 17 | # Our hypothesis XW+b 18 | hypothesis = X * W + b 19 | 20 | # cost/loss function 21 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 22 | 23 | # Minimize 24 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) 25 | train = optimizer.minimize(cost) 26 | 27 | # Launch the graph in a session. 28 | sess = tf.Session() 29 | # Initializes global variables in the graph. 30 | sess.run(tf.global_variables_initializer()) 31 | 32 | # Fit the line 33 | for step in range(2001): 34 | cost_val, W_val, b_val, _ = \ 35 | sess.run([cost, W, b, train], 36 | feed_dict={X: [1, 2, 3], Y: [1, 2, 3]}) 37 | if step % 20 == 0: 38 | print(step, cost_val, W_val, b_val) 39 | 40 | # Learns best fit W:[ 1.], b:[ 0] 41 | ''' 42 | ... 43 | 1980 1.32962e-05 [ 1.00423515] [-0.00962736] 44 | 2000 1.20761e-05 [ 1.00403607] [-0.00917497] 45 | ''' 46 | 47 | # Testing our model 48 | print(sess.run(hypothesis, feed_dict={X: [5]})) 49 | print(sess.run(hypothesis, feed_dict={X: [2.5]})) 50 | print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) 51 | 52 | ''' 53 | [ 5.0110054] 54 | [ 2.50091505] 55 | [ 1.49687922 3.50495124] 56 | ''' 57 | 58 | 59 | # Fit the line with new training data 60 | for step in range(2001): 61 | cost_val, W_val, b_val, _ = \ 62 | sess.run([cost, W, b, train], 63 | feed_dict={X: [1, 2, 3, 4, 5], 64 | Y: [2.1, 3.1, 4.1, 5.1, 6.1]}) 65 | if step % 20 == 0: 66 | print(step, cost_val, W_val, b_val) 67 | 68 | # Testing our model 69 | print(sess.run(hypothesis, feed_dict={X: [5]})) 70 | print(sess.run(hypothesis, feed_dict={X: [2.5]})) 71 | print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) 72 | 73 | ''' 74 | 1960 3.32396e-07 [ 1.00037301] [ 1.09865296] 75 | 1980 2.90429e-07 [ 1.00034881] [ 1.09874094] 76 | 2000 2.5373e-07 [ 1.00032604] [ 1.09882331] 77 | [ 6.10045338] 78 | [ 3.59963846] 79 | [ 2.59931231 4.59996414] 80 | ''' 81 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-02-3-linear_regression_tensorflow.org.py: -------------------------------------------------------------------------------- 1 | # From https://www.tensorflow.org/get_started/get_started 2 | import tensorflow as tf 3 | 4 | # Model parameters 5 | W = tf.Variable([.3], tf.float32) 6 | b = tf.Variable([-.3], tf.float32) 7 | 8 | # Model input and output 9 | x = tf.placeholder(tf.float32) 10 | y = tf.placeholder(tf.float32) 11 | 12 | linear_model = x * W + b 13 | 14 | # cost/loss function 15 | loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares 16 | 17 | # optimizer 18 | optimizer = tf.train.GradientDescentOptimizer(0.01) 19 | train = optimizer.minimize(loss) 20 | 21 | # training data 22 | x_train = [1, 2, 3, 4] 23 | y_train = [0, -1, -2, -3] 24 | 25 | # training loop 26 | init = tf.global_variables_initializer() 27 | sess = tf.Session() 28 | sess.run(init) # reset values to wrong 29 | for i in range(1000): 30 | sess.run(train, {x: x_train, y: y_train}) 31 | 32 | # evaluate training accuracy 33 | curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train}) 34 | print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss)) 35 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-03-1-minimizing_cost_show_graph.py: -------------------------------------------------------------------------------- 1 | # Lab 3 Minimizing Cost 2 | import tensorflow as tf 3 | import matplotlib.pyplot as plt 4 | tf.set_random_seed(777) # for reproducibility 5 | 6 | X = [1, 2, 3] 7 | Y = [1, 2, 3] 8 | 9 | W = tf.placeholder(tf.float32) 10 | 11 | # Our hypothesis for linear model X * W 12 | hypothesis = X * W 13 | 14 | # cost/loss function 15 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 16 | 17 | # Launch the graph in a session. 18 | sess = tf.Session() 19 | 20 | # Variables for plotting cost function 21 | W_history = [] 22 | cost_history = [] 23 | 24 | for i in range(-30, 50): 25 | curr_W = i * 0.1 26 | curr_cost = sess.run(cost, feed_dict={W: curr_W}) 27 | W_history.append(curr_W) 28 | cost_history.append(curr_cost) 29 | 30 | # Show the cost function 31 | plt.plot(W_history, cost_history) 32 | plt.show() 33 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-03-2-minimizing_cost_gradient_update.py: -------------------------------------------------------------------------------- 1 | # Lab 3 Minimizing Cost 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | x_data = [1, 2, 3] 6 | y_data = [1, 2, 3] 7 | 8 | # Try to find values for W and b to compute y_data = W * x_data + b 9 | # We know that W should be 1 and b should be 0 10 | # But let's use TensorFlow to figure it out 11 | W = tf.Variable(tf.random_normal([1]), name='weight') 12 | 13 | X = tf.placeholder(tf.float32) 14 | Y = tf.placeholder(tf.float32) 15 | 16 | # Our hypothesis for linear model X * W 17 | hypothesis = X * W 18 | 19 | # cost/loss function 20 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 21 | 22 | # Minimize: Gradient Descent using derivative: W -= learning_rate * derivative 23 | learning_rate = 0.1 24 | gradient = tf.reduce_mean((W * X - Y) * X) 25 | descent = W - learning_rate * gradient 26 | update = W.assign(descent) 27 | 28 | # Launch the graph in a session. 29 | sess = tf.Session() 30 | # Initializes global variables in the graph. 31 | sess.run(tf.global_variables_initializer()) 32 | 33 | for step in range(21): 34 | sess.run(update, feed_dict={X: x_data, Y: y_data}) 35 | print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W)) 36 | 37 | ''' 38 | 0 1.93919 [ 1.64462376] 39 | 1 0.551591 [ 1.34379935] 40 | 2 0.156897 [ 1.18335962] 41 | 3 0.0446285 [ 1.09779179] 42 | 4 0.0126943 [ 1.05215561] 43 | 5 0.00361082 [ 1.0278163] 44 | 6 0.00102708 [ 1.01483536] 45 | 7 0.000292144 [ 1.00791216] 46 | 8 8.30968e-05 [ 1.00421977] 47 | 9 2.36361e-05 [ 1.00225055] 48 | 10 6.72385e-06 [ 1.00120032] 49 | 11 1.91239e-06 [ 1.00064015] 50 | 12 5.43968e-07 [ 1.00034142] 51 | 13 1.54591e-07 [ 1.00018203] 52 | 14 4.39416e-08 [ 1.00009704] 53 | 15 1.24913e-08 [ 1.00005174] 54 | 16 3.5322e-09 [ 1.00002754] 55 | 17 9.99824e-10 [ 1.00001466] 56 | 18 2.88878e-10 [ 1.00000787] 57 | 19 8.02487e-11 [ 1.00000417] 58 | 20 2.34053e-11 [ 1.00000226] 59 | ''' 60 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-03-3-minimizing_cost_tf_optimizer.py: -------------------------------------------------------------------------------- 1 | # Lab 3 Minimizing Cost 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | # tf Graph Input 6 | X = [1, 2, 3] 7 | Y = [1, 2, 3] 8 | 9 | # Set wrong model weights 10 | W = tf.Variable(5.0) 11 | 12 | # Linear model 13 | hypothesis = X * W 14 | 15 | # cost/loss function 16 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 17 | 18 | # Minimize: Gradient Descent Magic 19 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) 20 | train = optimizer.minimize(cost) 21 | 22 | # Launch the graph in a session. 23 | sess = tf.Session() 24 | # Initializes global variables in the graph. 25 | sess.run(tf.global_variables_initializer()) 26 | 27 | for step in range(100): 28 | print(step, sess.run(W)) 29 | sess.run(train) 30 | 31 | ''' 32 | 0 5.0 33 | 1 1.26667 34 | 2 1.01778 35 | 3 1.00119 36 | 4 1.00008 37 | ... 38 | 96 1.0 39 | 97 1.0 40 | 98 1.0 41 | 99 1.0 42 | ''' 43 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-03-X-minimizing_cost_tf_gradient.py: -------------------------------------------------------------------------------- 1 | # Lab 3 Minimizing Cost 2 | # This is optional 3 | import tensorflow as tf 4 | tf.set_random_seed(777) # for reproducibility 5 | 6 | # tf Graph Input 7 | X = [1, 2, 3] 8 | Y = [1, 2, 3] 9 | 10 | # Set wrong model weights 11 | W = tf.Variable(5.) 12 | 13 | # Linear model 14 | hypothesis = X * W 15 | 16 | # Manual gradient 17 | gradient = tf.reduce_mean((W * X - Y) * X) * 2 18 | 19 | # cost/loss function 20 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 21 | 22 | # Minimize: Gradient Descent Magic 23 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) 24 | train = optimizer.minimize(cost) 25 | 26 | # Get gradients 27 | gvs = optimizer.compute_gradients(cost, [W]) 28 | # Optional: modify gradient if necessary 29 | # gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs] 30 | # Apply gradients 31 | apply_gradients = optimizer.apply_gradients(gvs) 32 | 33 | # Launch the graph in a session. 34 | sess = tf.Session() 35 | # Initializes global variables in the graph. 36 | sess.run(tf.global_variables_initializer()) 37 | 38 | for step in range(100): 39 | print(step, sess.run([gradient, W, gvs])) 40 | sess.run(apply_gradients) 41 | # Same as sess.run(train) 42 | 43 | 44 | ''' 45 | # Apply gradients 46 | 0 [37.333332, 5.0, [(37.333336, 5.0)]] 47 | 1 [33.848888, 4.6266665, [(33.848888, 4.6266665)]] 48 | 2 [30.689657, 4.2881775, [(30.689657, 4.2881775)]] 49 | 3 [27.825287, 3.9812808, [(27.825287, 3.9812808)]] 50 | 4 [25.228262, 3.703028, [(25.228264, 3.703028)]] 51 | ... 52 | 96 [0.0030694802, 1.0003289, [(0.0030694804, 1.0003289)]] 53 | 97 [0.0027837753, 1.0002983, [(0.0027837753, 1.0002983)]] 54 | 98 [0.0025234222, 1.0002704, [(0.0025234222, 1.0002704)]] 55 | 99 [0.0022875469, 1.0002451, [(0.0022875469, 1.0002451)]] 56 | ''' 57 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-04-1-multi_variable_linear_regression.py: -------------------------------------------------------------------------------- 1 | # Lab 4 Multi-variable linear regression 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | x1_data = [73., 93., 89., 96., 73.] 6 | x2_data = [80., 88., 91., 98., 66.] 7 | x3_data = [75., 93., 90., 100., 70.] 8 | 9 | y_data = [152., 185., 180., 196., 142.] 10 | 11 | # placeholders for a tensor that will be always fed. 12 | x1 = tf.placeholder(tf.float32) 13 | x2 = tf.placeholder(tf.float32) 14 | x3 = tf.placeholder(tf.float32) 15 | 16 | Y = tf.placeholder(tf.float32) 17 | 18 | w1 = tf.Variable(tf.random_normal([1]), name='weight1') 19 | w2 = tf.Variable(tf.random_normal([1]), name='weight2') 20 | w3 = tf.Variable(tf.random_normal([1]), name='weight3') 21 | b = tf.Variable(tf.random_normal([1]), name='bias') 22 | 23 | hypothesis = x1 * w1 + x2 * w2 + x3 * w3 + b 24 | print(hypothesis) 25 | 26 | # cost/loss function 27 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 28 | 29 | # Minimize. Need a very small learning rate for this data set 30 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) 31 | train = optimizer.minimize(cost) 32 | 33 | # Launch the graph in a session. 34 | sess = tf.Session() 35 | # Initializes global variables in the graph. 36 | sess.run(tf.global_variables_initializer()) 37 | 38 | for step in range(2001): 39 | cost_val, hy_val, _ = sess.run([cost, hypothesis, train], 40 | feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: y_data}) 41 | if step % 10 == 0: 42 | print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) 43 | 44 | ''' 45 | 0 Cost: 19614.8 46 | Prediction: 47 | [ 21.69748688 39.10213089 31.82624626 35.14236832 32.55316544] 48 | 10 Cost: 14.0682 49 | Prediction: 50 | [ 145.56100464 187.94958496 178.50236511 194.86721802 146.08096313] 51 | 52 | ... 53 | 54 | 1990 Cost: 4.9197 55 | Prediction: 56 | [ 148.15084839 186.88632202 179.6293335 195.81796265 144.46044922] 57 | 2000 Cost: 4.89449 58 | Prediction: 59 | [ 148.15931702 186.8805542 179.63194275 195.81971741 144.45298767] 60 | ''' 61 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-04-2-multi_variable_matmul_linear_regression.py: -------------------------------------------------------------------------------- 1 | # Lab 4 Multi-variable linear regression 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | x_data = [[73., 80., 75.], 6 | [93., 88., 93.], 7 | [89., 91., 90.], 8 | [96., 98., 100.], 9 | [73., 66., 70.]] 10 | y_data = [[152.], 11 | [185.], 12 | [180.], 13 | [196.], 14 | [142.]] 15 | 16 | 17 | # placeholders for a tensor that will be always fed. 18 | X = tf.placeholder(tf.float32, shape=[None, 3]) 19 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 20 | 21 | W = tf.Variable(tf.random_normal([3, 1]), name='weight') 22 | b = tf.Variable(tf.random_normal([1]), name='bias') 23 | 24 | # Hypothesis 25 | hypothesis = tf.matmul(X, W) + b 26 | 27 | # Simplified cost/loss function 28 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 29 | 30 | # Minimize 31 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) 32 | train = optimizer.minimize(cost) 33 | 34 | # Launch the graph in a session. 35 | sess = tf.Session() 36 | # Initializes global variables in the graph. 37 | sess.run(tf.global_variables_initializer()) 38 | 39 | for step in range(2001): 40 | cost_val, hy_val, _ = sess.run( 41 | [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) 42 | if step % 10 == 0: 43 | print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) 44 | 45 | ''' 46 | 0 Cost: 7105.46 47 | Prediction: 48 | [[ 80.82241058] 49 | [ 92.26364136] 50 | [ 93.70250702] 51 | [ 98.09217834] 52 | [ 72.51759338]] 53 | 10 Cost: 5.89726 54 | Prediction: 55 | [[ 155.35159302] 56 | [ 181.85691833] 57 | [ 181.97254944] 58 | [ 194.21760559] 59 | [ 140.85707092]] 60 | 61 | ... 62 | 63 | 1990 Cost: 3.18588 64 | Prediction: 65 | [[ 154.36352539] 66 | [ 182.94833374] 67 | [ 181.85189819] 68 | [ 194.35585022] 69 | [ 142.03240967]] 70 | 2000 Cost: 3.1781 71 | Prediction: 72 | [[ 154.35881042] 73 | [ 182.95147705] 74 | [ 181.85035706] 75 | [ 194.35533142] 76 | [ 142.036026 ]] 77 | 78 | ''' 79 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-04-3-file_input_linear_regression.py: -------------------------------------------------------------------------------- 1 | # Lab 4 Multi-variable linear regression 2 | import tensorflow as tf 3 | import numpy as np 4 | tf.set_random_seed(777) # for reproducibility 5 | 6 | xy = np.loadtxt('data-01-test-score.csv', delimiter=',', dtype=np.float32) 7 | x_data = xy[:, 0:-1] 8 | y_data = xy[:, [-1]] 9 | 10 | # Make sure the shape and data are OK 11 | print(x_data.shape, x_data, len(x_data)) 12 | print(y_data.shape, y_data) 13 | 14 | # placeholders for a tensor that will be always fed. 15 | X = tf.placeholder(tf.float32, shape=[None, 3]) 16 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 17 | 18 | W = tf.Variable(tf.random_normal([3, 1]), name='weight') 19 | b = tf.Variable(tf.random_normal([1]), name='bias') 20 | 21 | # Hypothesis 22 | hypothesis = tf.matmul(X, W) + b 23 | 24 | # Simplified cost/loss function 25 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 26 | 27 | # Minimize 28 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) 29 | train = optimizer.minimize(cost) 30 | 31 | # Launch the graph in a session. 32 | sess = tf.Session() 33 | # Initializes global variables in the graph. 34 | sess.run(tf.global_variables_initializer()) 35 | 36 | for step in range(2001): 37 | cost_val, hy_val, _ = sess.run( 38 | [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) 39 | if step % 10 == 0: 40 | print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) 41 | 42 | # Ask my score 43 | print("Your score will be ", sess.run( 44 | hypothesis, feed_dict={X: [[100, 70, 101]]})) 45 | 46 | print("Other scores will be ", sess.run(hypothesis, 47 | feed_dict={X: [[60, 70, 110], [90, 100, 80]]})) 48 | 49 | ''' 50 | Your score will be [[ 181.73277283]] 51 | Other scores will be [[ 145.86265564] 52 | [ 187.23129272]] 53 | 54 | ''' 55 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-04-4-tf_reader_linear_regression.py: -------------------------------------------------------------------------------- 1 | # Lab 4 Multi-variable linear regression 2 | # https://www.tensorflow.org/programmers_guide/reading_data 3 | 4 | import tensorflow as tf 5 | tf.set_random_seed(777) # for reproducibility 6 | 7 | filename_queue = tf.train.string_input_producer( 8 | ['data-01-test-score.csv'], shuffle=False, name='filename_queue') 9 | 10 | reader = tf.TextLineReader() 11 | key, value = reader.read(filename_queue) 12 | 13 | # Default values, in case of empty columns. Also specifies the type of the 14 | # decoded result. 15 | record_defaults = [[0.], [0.], [0.], [0.]] 16 | xy = tf.decode_csv(value, record_defaults=record_defaults) 17 | 18 | # collect batches of csv in 19 | train_x_batch, train_y_batch = \ 20 | tf.train.batch([xy[0:-1], xy[-1:]], batch_size=10) 21 | 22 | # placeholders for a tensor that will be always fed. 23 | X = tf.placeholder(tf.float32, shape=[None, 3]) 24 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 25 | 26 | W = tf.Variable(tf.random_normal([3, 1]), name='weight') 27 | b = tf.Variable(tf.random_normal([1]), name='bias') 28 | 29 | # Hypothesis 30 | hypothesis = tf.matmul(X, W) + b 31 | 32 | # Simplified cost/loss function 33 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 34 | 35 | # Minimize 36 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) 37 | train = optimizer.minimize(cost) 38 | 39 | # Launch the graph in a session. 40 | sess = tf.Session() 41 | # Initializes global variables in the graph. 42 | sess.run(tf.global_variables_initializer()) 43 | 44 | # Start populating the filename queue. 45 | coord = tf.train.Coordinator() 46 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 47 | 48 | for step in range(2001): 49 | x_batch, y_batch = sess.run([train_x_batch, train_y_batch]) 50 | cost_val, hy_val, _ = sess.run( 51 | [cost, hypothesis, train], feed_dict={X: x_batch, Y: y_batch}) 52 | if step % 10 == 0: 53 | print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) 54 | 55 | coord.request_stop() 56 | coord.join(threads) 57 | 58 | # Ask my score 59 | print("Your score will be ", 60 | sess.run(hypothesis, feed_dict={X: [[100, 70, 101]]})) 61 | 62 | print("Other scores will be ", 63 | sess.run(hypothesis, feed_dict={X: [[60, 70, 110], [90, 100, 80]]})) 64 | 65 | ''' 66 | Your score will be [[ 177.78144836]] 67 | Other scores will be [[ 141.10997009] 68 | [ 191.17378235]] 69 | 70 | ''' 71 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-05-1-logistic_regression.py: -------------------------------------------------------------------------------- 1 | # Lab 5 Logistic Regression Classifier 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | x_data = [[1, 2], 6 | [2, 3], 7 | [3, 1], 8 | [4, 3], 9 | [5, 3], 10 | [6, 2]] 11 | y_data = [[0], 12 | [0], 13 | [0], 14 | [1], 15 | [1], 16 | [1]] 17 | 18 | # placeholders for a tensor that will be always fed. 19 | X = tf.placeholder(tf.float32, shape=[None, 2]) 20 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 21 | 22 | W = tf.Variable(tf.random_normal([2, 1]), name='weight') 23 | b = tf.Variable(tf.random_normal([1]), name='bias') 24 | 25 | # Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W))) 26 | hypothesis = tf.sigmoid(tf.matmul(X, W) + b) 27 | 28 | # cost/loss function 29 | cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * 30 | tf.log(1 - hypothesis)) 31 | 32 | train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) 33 | 34 | # Accuracy computation 35 | # True if hypothesis>0.5 else False 36 | predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) 37 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 38 | 39 | # Launch graph 40 | with tf.Session() as sess: 41 | # Initialize TensorFlow variables 42 | sess.run(tf.global_variables_initializer()) 43 | 44 | for step in range(10001): 45 | cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data}) 46 | if step % 200 == 0: 47 | print(step, cost_val) 48 | 49 | # Accuracy report 50 | h, c, a = sess.run([hypothesis, predicted, accuracy], 51 | feed_dict={X: x_data, Y: y_data}) 52 | print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a) 53 | 54 | ''' 55 | 0 1.73078 56 | 200 0.571512 57 | 400 0.507414 58 | 600 0.471824 59 | 800 0.447585 60 | ... 61 | 9200 0.159066 62 | 9400 0.15656 63 | 9600 0.154132 64 | 9800 0.151778 65 | 10000 0.149496 66 | 67 | Hypothesis: [[ 0.03074029] 68 | [ 0.15884677] 69 | [ 0.30486736] 70 | [ 0.78138196] 71 | [ 0.93957496] 72 | [ 0.98016882]] 73 | Correct (Y): [[ 0.] 74 | [ 0.] 75 | [ 0.] 76 | [ 1.] 77 | [ 1.] 78 | [ 1.]] 79 | Accuracy: 1.0 80 | ''' 81 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-05-2-logistic_regression_diabetes.py: -------------------------------------------------------------------------------- 1 | # Lab 5 Logistic Regression Classifier 2 | import tensorflow as tf 3 | import numpy as np 4 | tf.set_random_seed(777) # for reproducibility 5 | 6 | xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32) 7 | x_data = xy[:, 0:-1] 8 | y_data = xy[:, [-1]] 9 | 10 | print(x_data.shape, y_data.shape) 11 | 12 | # placeholders for a tensor that will be always fed. 13 | X = tf.placeholder(tf.float32, shape=[None, 8]) 14 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 15 | 16 | W = tf.Variable(tf.random_normal([8, 1]), name='weight') 17 | b = tf.Variable(tf.random_normal([1]), name='bias') 18 | 19 | # Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W))) 20 | hypothesis = tf.sigmoid(tf.matmul(X, W) + b) 21 | 22 | # cost/loss function 23 | cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * 24 | tf.log(1 - hypothesis)) 25 | 26 | train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) 27 | 28 | # Accuracy computation 29 | # True if hypothesis>0.5 else False 30 | predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) 31 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 32 | 33 | # Launch graph 34 | with tf.Session() as sess: 35 | # Initialize TensorFlow variables 36 | sess.run(tf.global_variables_initializer()) 37 | 38 | for step in range(10001): 39 | cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data}) 40 | if step % 200 == 0: 41 | print(step, cost_val) 42 | 43 | # Accuracy report 44 | h, c, a = sess.run([hypothesis, predicted, accuracy], 45 | feed_dict={X: x_data, Y: y_data}) 46 | print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a) 47 | 48 | ''' 49 | 0 0.82794 50 | 200 0.755181 51 | 400 0.726355 52 | 600 0.705179 53 | 800 0.686631 54 | ... 55 | 9600 0.492056 56 | 9800 0.491396 57 | 10000 0.490767 58 | 59 | ... 60 | 61 | [ 1.] 62 | [ 1.] 63 | [ 1.]] 64 | Accuracy: 0.762846 65 | ''' 66 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-06-1-softmax_classifier.py: -------------------------------------------------------------------------------- 1 | # Lab 6 Softmax Classifier 2 | import tensorflow as tf 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | x_data = [[1, 2, 1, 1], 6 | [2, 1, 3, 2], 7 | [3, 1, 3, 4], 8 | [4, 1, 5, 5], 9 | [1, 7, 5, 5], 10 | [1, 2, 5, 6], 11 | [1, 6, 6, 6], 12 | [1, 7, 7, 7]] 13 | y_data = [[0, 0, 1], 14 | [0, 0, 1], 15 | [0, 0, 1], 16 | [0, 1, 0], 17 | [0, 1, 0], 18 | [0, 1, 0], 19 | [1, 0, 0], 20 | [1, 0, 0]] 21 | 22 | X = tf.placeholder("float", [None, 4]) 23 | Y = tf.placeholder("float", [None, 3]) 24 | nb_classes = 3 25 | 26 | W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight') 27 | b = tf.Variable(tf.random_normal([nb_classes]), name='bias') 28 | 29 | # tf.nn.softmax computes softmax activations 30 | # softmax = exp(logits) / reduce_sum(exp(logits), dim) 31 | hypothesis = tf.nn.softmax(tf.matmul(X, W) + b) 32 | 33 | # Cross entropy cost/loss 34 | cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) 35 | 36 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) 37 | 38 | # Launch graph 39 | with tf.Session() as sess: 40 | sess.run(tf.global_variables_initializer()) 41 | 42 | for step in range(2001): 43 | sess.run(optimizer, feed_dict={X: x_data, Y: y_data}) 44 | if step % 200 == 0: 45 | print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data})) 46 | 47 | print('--------------') 48 | 49 | # Testing & One-hot encoding 50 | a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]}) 51 | print(a, sess.run(tf.argmax(a, 1))) 52 | 53 | print('--------------') 54 | 55 | b = sess.run(hypothesis, feed_dict={X: [[1, 3, 4, 3]]}) 56 | print(b, sess.run(tf.argmax(b, 1))) 57 | 58 | print('--------------') 59 | 60 | c = sess.run(hypothesis, feed_dict={X: [[1, 1, 0, 1]]}) 61 | print(c, sess.run(tf.argmax(c, 1))) 62 | 63 | print('--------------') 64 | 65 | all = sess.run(hypothesis, feed_dict={ 66 | X: [[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]]}) 67 | print(all, sess.run(tf.argmax(all, 1))) 68 | 69 | ''' 70 | -------------- 71 | [[ 1.38904958e-03 9.98601854e-01 9.06129117e-06]] [1] 72 | -------------- 73 | [[ 0.93119204 0.06290206 0.0059059 ]] [0] 74 | -------------- 75 | [[ 1.27327668e-08 3.34112905e-04 9.99665856e-01]] [2] 76 | -------------- 77 | [[ 1.38904958e-03 9.98601854e-01 9.06129117e-06] 78 | [ 9.31192040e-01 6.29020557e-02 5.90589503e-03] 79 | [ 1.27327668e-08 3.34112905e-04 9.99665856e-01]] [1 0 2] 80 | ''' 81 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-06-2-softmax_zoo_classifier.py: -------------------------------------------------------------------------------- 1 | # Lab 6 Softmax Classifier 2 | import tensorflow as tf 3 | import numpy as np 4 | tf.set_random_seed(777) # for reproducibility 5 | 6 | # Predicting animal type based on various features 7 | xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32) 8 | x_data = xy[:, 0:-1] 9 | y_data = xy[:, [-1]] 10 | 11 | print(x_data.shape, y_data.shape) 12 | 13 | nb_classes = 7 # 0 ~ 6 14 | 15 | X = tf.placeholder(tf.float32, [None, 16]) 16 | Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6 17 | Y_one_hot = tf.one_hot(Y, nb_classes) # one hot 18 | print("one_hot", Y_one_hot) 19 | Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes]) 20 | print("reshape", Y_one_hot) 21 | 22 | W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight') 23 | b = tf.Variable(tf.random_normal([nb_classes]), name='bias') 24 | 25 | # tf.nn.softmax computes softmax activations 26 | # softmax = exp(logits) / reduce_sum(exp(logits), dim) 27 | logits = tf.matmul(X, W) + b 28 | hypothesis = tf.nn.softmax(logits) 29 | 30 | # Cross entropy cost/loss 31 | cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, 32 | labels=Y_one_hot) 33 | cost = tf.reduce_mean(cost_i) 34 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) 35 | 36 | prediction = tf.argmax(hypothesis, 1) 37 | correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1)) 38 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 39 | # Launch graph 40 | with tf.Session() as sess: 41 | sess.run(tf.global_variables_initializer()) 42 | 43 | for step in range(2000): 44 | sess.run(optimizer, feed_dict={X: x_data, Y: y_data}) 45 | if step % 100 == 0: 46 | loss, acc = sess.run([cost, accuracy], feed_dict={ 47 | X: x_data, Y: y_data}) 48 | print("Step: {:5}\tLoss: {:.3f}\tAcc: {:.2%}".format( 49 | step, loss, acc)) 50 | 51 | # Let's see if we can predict 52 | pred = sess.run(prediction, feed_dict={X: x_data}) 53 | # y_data: (N,1) = flatten => (N, ) matches pred.shape 54 | for p, y in zip(pred, y_data.flatten()): 55 | print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y))) 56 | 57 | ''' 58 | Step: 0 Loss: 5.106 Acc: 37.62% 59 | Step: 100 Loss: 0.800 Acc: 79.21% 60 | Step: 200 Loss: 0.486 Acc: 88.12% 61 | Step: 300 Loss: 0.349 Acc: 90.10% 62 | Step: 400 Loss: 0.272 Acc: 94.06% 63 | Step: 500 Loss: 0.222 Acc: 95.05% 64 | Step: 600 Loss: 0.187 Acc: 97.03% 65 | Step: 700 Loss: 0.161 Acc: 97.03% 66 | Step: 800 Loss: 0.140 Acc: 97.03% 67 | Step: 900 Loss: 0.124 Acc: 97.03% 68 | Step: 1000 Loss: 0.111 Acc: 97.03% 69 | Step: 1100 Loss: 0.101 Acc: 99.01% 70 | Step: 1200 Loss: 0.092 Acc: 100.00% 71 | Step: 1300 Loss: 0.084 Acc: 100.00% 72 | ... 73 | [True] Prediction: 0 True Y: 0 74 | [True] Prediction: 0 True Y: 0 75 | [True] Prediction: 3 True Y: 3 76 | [True] Prediction: 0 True Y: 0 77 | [True] Prediction: 0 True Y: 0 78 | [True] Prediction: 0 True Y: 0 79 | [True] Prediction: 0 True Y: 0 80 | [True] Prediction: 3 True Y: 3 81 | [True] Prediction: 3 True Y: 3 82 | [True] Prediction: 0 True Y: 0 83 | ''' 84 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-07-2-linear_regression_without_min_max.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | 6 | xy = np.array([[828.659973, 833.450012, 908100, 828.349976, 831.659973], 7 | [823.02002, 828.070007, 1828100, 821.655029, 828.070007], 8 | [819.929993, 824.400024, 1438100, 818.97998, 824.159973], 9 | [816, 820.958984, 1008100, 815.48999, 819.23999], 10 | [819.359985, 823, 1188100, 818.469971, 818.97998], 11 | [819, 823, 1198100, 816, 820.450012], 12 | [811.700012, 815.25, 1098100, 809.780029, 813.669983], 13 | [809.51001, 816.659973, 1398100, 804.539978, 809.559998]]) 14 | 15 | x_data = xy[:, 0:-1] 16 | y_data = xy[:, [-1]] 17 | 18 | # placeholders for a tensor that will be always fed. 19 | X = tf.placeholder(tf.float32, shape=[None, 4]) 20 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 21 | 22 | W = tf.Variable(tf.random_normal([4, 1]), name='weight') 23 | b = tf.Variable(tf.random_normal([1]), name='bias') 24 | 25 | # Hypothesis 26 | hypothesis = tf.matmul(X, W) + b 27 | 28 | # Simplified cost/loss function 29 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 30 | 31 | # Minimize 32 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) 33 | train = optimizer.minimize(cost) 34 | 35 | # Launch the graph in a session. 36 | sess = tf.Session() 37 | # Initializes global variables in the graph. 38 | sess.run(tf.global_variables_initializer()) 39 | 40 | for step in range(101): 41 | cost_val, hy_val, _ = sess.run( 42 | [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) 43 | print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) 44 | 45 | 46 | ''' 47 | 0 Cost: 2.45533e+12 48 | Prediction: 49 | [[-1104436.375] 50 | [-2224342.75 ] 51 | [-1749606.75 ] 52 | [-1226179.375] 53 | [-1445287.125] 54 | [-1457459.5 ] 55 | [-1335740.5 ] 56 | [-1700924.625]] 57 | 1 Cost: 2.69762e+27 58 | Prediction: 59 | [[ 3.66371490e+13] 60 | [ 7.37543360e+13] 61 | [ 5.80198785e+13] 62 | [ 4.06716290e+13] 63 | [ 4.79336847e+13] 64 | [ 4.83371348e+13] 65 | [ 4.43026590e+13] 66 | [ 5.64060907e+13]] 67 | 2 Cost: inf 68 | Prediction: 69 | [[ -1.21438790e+21] 70 | [ -2.44468702e+21] 71 | [ -1.92314724e+21] 72 | [ -1.34811610e+21] 73 | [ -1.58882674e+21] 74 | [ -1.60219962e+21] 75 | [ -1.46847142e+21] 76 | [ -1.86965602e+21]] 77 | 3 Cost: inf 78 | Prediction: 79 | [[ 4.02525216e+28] 80 | [ 8.10324465e+28] 81 | [ 6.37453079e+28] 82 | [ 4.46851237e+28] 83 | [ 5.26638074e+28] 84 | [ 5.31070676e+28] 85 | [ 4.86744608e+28] 86 | [ 6.19722623e+28]] 87 | 4 Cost: inf 88 | Prediction: 89 | [[ -1.33422428e+36] 90 | [ -2.68593010e+36] 91 | [ -2.11292430e+36] 92 | [ -1.48114879e+36] 93 | [ -1.74561303e+36] 94 | [ -1.76030542e+36] 95 | [ -1.61338091e+36] 96 | [ -2.05415459e+36]] 97 | 5 Cost: inf 98 | Prediction: 99 | [[ inf] 100 | [ inf] 101 | [ inf] 102 | [ inf] 103 | [ inf] 104 | [ inf] 105 | [ inf] 106 | [ inf]] 107 | 6 Cost: nan 108 | Prediction: 109 | [[ nan] 110 | [ nan] 111 | [ nan] 112 | [ nan] 113 | [ nan] 114 | [ nan] 115 | [ nan] 116 | [ nan]] 117 | ''' 118 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-07-3-linear_regression_min_max.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | tf.set_random_seed(777) # for reproducibility 4 | 5 | 6 | def MinMaxScaler(data): 7 | numerator = data - np.min(data, 0) 8 | denominator = np.max(data, 0) - np.min(data, 0) 9 | # noise term prevents the zero division 10 | return numerator / (denominator + 1e-7) 11 | 12 | 13 | xy = np.array([[828.659973, 833.450012, 908100, 828.349976, 831.659973], 14 | [823.02002, 828.070007, 1828100, 821.655029, 828.070007], 15 | [819.929993, 824.400024, 1438100, 818.97998, 824.159973], 16 | [816, 820.958984, 1008100, 815.48999, 819.23999], 17 | [819.359985, 823, 1188100, 818.469971, 818.97998], 18 | [819, 823, 1198100, 816, 820.450012], 19 | [811.700012, 815.25, 1098100, 809.780029, 813.669983], 20 | [809.51001, 816.659973, 1398100, 804.539978, 809.559998]]) 21 | 22 | # very important. It does not work without it. 23 | xy = MinMaxScaler(xy) 24 | print(xy) 25 | 26 | x_data = xy[:, 0:-1] 27 | y_data = xy[:, [-1]] 28 | 29 | # placeholders for a tensor that will be always fed. 30 | X = tf.placeholder(tf.float32, shape=[None, 4]) 31 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 32 | 33 | W = tf.Variable(tf.random_normal([4, 1]), name='weight') 34 | b = tf.Variable(tf.random_normal([1]), name='bias') 35 | 36 | # Hypothesis 37 | hypothesis = tf.matmul(X, W) + b 38 | 39 | # Simplified cost/loss function 40 | cost = tf.reduce_mean(tf.square(hypothesis - Y)) 41 | 42 | # Minimize 43 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) 44 | train = optimizer.minimize(cost) 45 | 46 | # Launch the graph in a session. 47 | sess = tf.Session() 48 | # Initializes global variables in the graph. 49 | sess.run(tf.global_variables_initializer()) 50 | 51 | for step in range(101): 52 | cost_val, hy_val, _ = sess.run( 53 | [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) 54 | print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) 55 | 56 | ''' 57 | 100 Cost: 0.152254 58 | Prediction: 59 | [[ 1.63450289] 60 | [ 0.06628087] 61 | [ 0.35014752] 62 | [ 0.67070574] 63 | [ 0.61131608] 64 | [ 0.61466062] 65 | [ 0.23175186] 66 | [-0.13716528]] 67 | 68 | ''' 69 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-07-4-mnist_introduction.py: -------------------------------------------------------------------------------- 1 | # Lab 7 Learning rate and Evaluation 2 | import tensorflow as tf 3 | import random 4 | import matplotlib.pyplot as plt 5 | tf.set_random_seed(777) # for reproducibility 6 | 7 | from tensorflow.examples.tutorials.mnist import input_data 8 | # Check out https://www.tensorflow.org/get_started/mnist/beginners for 9 | # more information about the mnist dataset 10 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 11 | 12 | nb_classes = 10 13 | 14 | # MNIST data image of shape 28 * 28 = 784 15 | X = tf.placeholder(tf.float32, [None, 784]) 16 | # 0 - 9 digits recognition = 10 classes 17 | Y = tf.placeholder(tf.float32, [None, nb_classes]) 18 | 19 | W = tf.Variable(tf.random_normal([784, nb_classes])) 20 | b = tf.Variable(tf.random_normal([nb_classes])) 21 | 22 | # Hypothesis (using softmax) 23 | hypothesis = tf.nn.softmax(tf.matmul(X, W) + b) 24 | 25 | cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) 26 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) 27 | 28 | # Test model 29 | is_correct = tf.equal(tf.arg_max(hypothesis, 1), tf.arg_max(Y, 1)) 30 | # Calculate accuracy 31 | accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) 32 | 33 | # parameters 34 | training_epochs = 15 35 | batch_size = 100 36 | 37 | with tf.Session() as sess: 38 | # Initialize TensorFlow variables 39 | sess.run(tf.global_variables_initializer()) 40 | # Training cycle 41 | for epoch in range(training_epochs): 42 | avg_cost = 0 43 | total_batch = int(mnist.train.num_examples / batch_size) 44 | 45 | for i in range(total_batch): 46 | batch_xs, batch_ys = mnist.train.next_batch(batch_size) 47 | c, _ = sess.run([cost, optimizer], feed_dict={ 48 | X: batch_xs, Y: batch_ys}) 49 | avg_cost += c / total_batch 50 | 51 | print('Epoch:', '%04d' % (epoch + 1), 52 | 'cost =', '{:.9f}'.format(avg_cost)) 53 | 54 | print("Learning finished") 55 | 56 | # Test the model using test sets 57 | print("Accuracy: ", accuracy.eval(session=sess, feed_dict={ 58 | X: mnist.test.images, Y: mnist.test.labels})) 59 | 60 | # Get one and predict 61 | r = random.randint(0, mnist.test.num_examples - 1) 62 | print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1))) 63 | print("Prediction: ", sess.run( 64 | tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]})) 65 | 66 | plt.imshow( 67 | mnist.test.images[r:r + 1].reshape(28, 28), 68 | cmap='Greys', 69 | interpolation='nearest') 70 | plt.show() 71 | 72 | 73 | ''' 74 | Epoch: 0001 cost = 2.868104637 75 | Epoch: 0002 cost = 1.134684615 76 | Epoch: 0003 cost = 0.908220728 77 | Epoch: 0004 cost = 0.794199896 78 | Epoch: 0005 cost = 0.721815854 79 | Epoch: 0006 cost = 0.670184430 80 | Epoch: 0007 cost = 0.630576546 81 | Epoch: 0008 cost = 0.598888191 82 | Epoch: 0009 cost = 0.573027079 83 | Epoch: 0010 cost = 0.550497213 84 | Epoch: 0011 cost = 0.532001859 85 | Epoch: 0012 cost = 0.515517795 86 | Epoch: 0013 cost = 0.501175288 87 | Epoch: 0014 cost = 0.488425370 88 | Epoch: 0015 cost = 0.476968593 89 | Learning finished 90 | Accuracy: 0.888 91 | ''' 92 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-1-xor.py: -------------------------------------------------------------------------------- 1 | # Lab 9 XOR 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | tf.set_random_seed(777) # for reproducibility 6 | learning_rate = 0.1 7 | 8 | x_data = [[0, 0], 9 | [0, 1], 10 | [1, 0], 11 | [1, 1]] 12 | y_data = [[0], 13 | [1], 14 | [1], 15 | [0]] 16 | x_data = np.array(x_data, dtype=np.float32) 17 | y_data = np.array(y_data, dtype=np.float32) 18 | 19 | X = tf.placeholder(tf.float32, [None, 2]) 20 | Y = tf.placeholder(tf.float32, [None, 1]) 21 | 22 | W = tf.Variable(tf.random_normal([2, 1]), name='weight') 23 | b = tf.Variable(tf.random_normal([1]), name='bias') 24 | 25 | # Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W))) 26 | hypothesis = tf.sigmoid(tf.matmul(X, W) + b) 27 | 28 | # cost/loss function 29 | cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * 30 | tf.log(1 - hypothesis)) 31 | 32 | train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) 33 | 34 | # Accuracy computation 35 | # True if hypothesis>0.5 else False 36 | predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) 37 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 38 | 39 | # Launch graph 40 | with tf.Session() as sess: 41 | # Initialize TensorFlow variables 42 | sess.run(tf.global_variables_initializer()) 43 | 44 | for step in range(10001): 45 | sess.run(train, feed_dict={X: x_data, Y: y_data}) 46 | if step % 100 == 0: 47 | print(step, sess.run(cost, feed_dict={ 48 | X: x_data, Y: y_data}), sess.run(W)) 49 | 50 | # Accuracy report 51 | h, c, a = sess.run([hypothesis, predicted, accuracy], 52 | feed_dict={X: x_data, Y: y_data}) 53 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) 54 | 55 | ''' 56 | Hypothesis: [[ 0.5] 57 | [ 0.5] 58 | [ 0.5] 59 | [ 0.5]] 60 | Correct: [[ 0.] 61 | [ 0.] 62 | [ 0.] 63 | [ 0.]] 64 | Accuracy: 0.5 65 | ''' 66 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-2-xor-nn.py: -------------------------------------------------------------------------------- 1 | # Lab 9 XOR 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | tf.set_random_seed(777) # for reproducibility 6 | learning_rate = 0.1 7 | 8 | x_data = [[0, 0], 9 | [0, 1], 10 | [1, 0], 11 | [1, 1]] 12 | y_data = [[0], 13 | [1], 14 | [1], 15 | [0]] 16 | x_data = np.array(x_data, dtype=np.float32) 17 | y_data = np.array(y_data, dtype=np.float32) 18 | 19 | X = tf.placeholder(tf.float32, [None, 2]) 20 | Y = tf.placeholder(tf.float32, [None, 1]) 21 | 22 | W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1') 23 | b1 = tf.Variable(tf.random_normal([2]), name='bias1') 24 | layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) 25 | 26 | W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2') 27 | b2 = tf.Variable(tf.random_normal([1]), name='bias2') 28 | hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2) 29 | 30 | # cost/loss function 31 | cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * 32 | tf.log(1 - hypothesis)) 33 | 34 | train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) 35 | 36 | # Accuracy computation 37 | # True if hypothesis>0.5 else False 38 | predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) 39 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 40 | 41 | # Launch graph 42 | with tf.Session() as sess: 43 | # Initialize TensorFlow variables 44 | sess.run(tf.global_variables_initializer()) 45 | 46 | for step in range(10001): 47 | sess.run(train, feed_dict={X: x_data, Y: y_data}) 48 | if step % 100 == 0: 49 | print(step, sess.run(cost, feed_dict={ 50 | X: x_data, Y: y_data}), sess.run([W1, W2])) 51 | 52 | # Accuracy report 53 | h, c, a = sess.run([hypothesis, predicted, accuracy], 54 | feed_dict={X: x_data, Y: y_data}) 55 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) 56 | 57 | 58 | ''' 59 | Hypothesis: [[ 0.01338218] 60 | [ 0.98166394] 61 | [ 0.98809403] 62 | [ 0.01135799]] 63 | Correct: [[ 0.] 64 | [ 1.] 65 | [ 1.] 66 | [ 0.]] 67 | Accuracy: 1.0 68 | ''' 69 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-3-xor-nn-wide-deep.py: -------------------------------------------------------------------------------- 1 | # Lab 9 XOR 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | tf.set_random_seed(777) # for reproducibility 6 | learning_rate = 0.1 7 | 8 | x_data = [[0, 0], 9 | [0, 1], 10 | [1, 0], 11 | [1, 1]] 12 | y_data = [[0], 13 | [1], 14 | [1], 15 | [0]] 16 | x_data = np.array(x_data, dtype=np.float32) 17 | y_data = np.array(y_data, dtype=np.float32) 18 | 19 | X = tf.placeholder(tf.float32, [None, 2]) 20 | Y = tf.placeholder(tf.float32, [None, 1]) 21 | 22 | W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1') 23 | b1 = tf.Variable(tf.random_normal([10]), name='bias1') 24 | layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) 25 | 26 | W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2') 27 | b2 = tf.Variable(tf.random_normal([10]), name='bias2') 28 | layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2) 29 | 30 | W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3') 31 | b3 = tf.Variable(tf.random_normal([10]), name='bias3') 32 | layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3) 33 | 34 | W4 = tf.Variable(tf.random_normal([10, 1]), name='weight4') 35 | b4 = tf.Variable(tf.random_normal([1]), name='bias4') 36 | hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4) 37 | 38 | # cost/loss function 39 | cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * 40 | tf.log(1 - hypothesis)) 41 | 42 | train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) 43 | 44 | # Accuracy computation 45 | # True if hypothesis>0.5 else False 46 | predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) 47 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 48 | 49 | # Launch graph 50 | with tf.Session() as sess: 51 | # Initialize TensorFlow variables 52 | sess.run(tf.global_variables_initializer()) 53 | 54 | for step in range(10001): 55 | sess.run(train, feed_dict={X: x_data, Y: y_data}) 56 | if step % 100 == 0: 57 | print(step, sess.run(cost, feed_dict={ 58 | X: x_data, Y: y_data}), sess.run([W1, W2])) 59 | 60 | # Accuracy report 61 | h, c, a = sess.run([hypothesis, predicted, accuracy], 62 | feed_dict={X: x_data, Y: y_data}) 63 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) 64 | 65 | 66 | ''' 67 | Hypothesis: [[ 7.80511764e-04] 68 | [ 9.99238133e-01] 69 | [ 9.98379230e-01] 70 | [ 1.55659032e-03]] 71 | Correct: [[ 0.] 72 | [ 1.] 73 | [ 1.] 74 | [ 0.]] 75 | Accuracy: 1.0 76 | ''' 77 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-4-xor_tensorboard.py: -------------------------------------------------------------------------------- 1 | # Lab 9 XOR 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | tf.set_random_seed(777) # for reproducibility 6 | learning_rate = 0.01 7 | 8 | x_data = [[0, 0], 9 | [0, 1], 10 | [1, 0], 11 | [1, 1]] 12 | y_data = [[0], 13 | [1], 14 | [1], 15 | [0]] 16 | x_data = np.array(x_data, dtype=np.float32) 17 | y_data = np.array(y_data, dtype=np.float32) 18 | 19 | X = tf.placeholder(tf.float32, [None, 2], name='x-input') 20 | Y = tf.placeholder(tf.float32, [None, 1], name='y-input') 21 | 22 | with tf.name_scope("layer1") as scope: 23 | W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1') 24 | b1 = tf.Variable(tf.random_normal([2]), name='bias1') 25 | layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) 26 | 27 | w1_hist = tf.summary.histogram("weights1", W1) 28 | b1_hist = tf.summary.histogram("biases1", b1) 29 | layer1_hist = tf.summary.histogram("layer1", layer1) 30 | 31 | 32 | with tf.name_scope("layer2") as scope: 33 | W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2') 34 | b2 = tf.Variable(tf.random_normal([1]), name='bias2') 35 | hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2) 36 | 37 | w2_hist = tf.summary.histogram("weights2", W2) 38 | b2_hist = tf.summary.histogram("biases2", b2) 39 | hypothesis_hist = tf.summary.histogram("hypothesis", hypothesis) 40 | 41 | # cost/loss function 42 | with tf.name_scope("cost") as scope: 43 | cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * 44 | tf.log(1 - hypothesis)) 45 | cost_summ = tf.summary.scalar("cost", cost) 46 | 47 | with tf.name_scope("train") as scope: 48 | train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 49 | 50 | # Accuracy computation 51 | # True if hypothesis>0.5 else False 52 | predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) 53 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 54 | accuracy_summ = tf.summary.scalar("accuracy", accuracy) 55 | 56 | # Launch graph 57 | with tf.Session() as sess: 58 | # tensorboard --logdir=./logs/xor_logs 59 | merged_summary = tf.summary.merge_all() 60 | writer = tf.summary.FileWriter("./logs/xor_logs_r0_01") 61 | writer.add_graph(sess.graph) # Show the graph 62 | 63 | # Initialize TensorFlow variables 64 | sess.run(tf.global_variables_initializer()) 65 | 66 | for step in range(10001): 67 | summary, _ = sess.run([merged_summary, train], feed_dict={X: x_data, Y: y_data}) 68 | writer.add_summary(summary, global_step=step) 69 | 70 | if step % 100 == 0: 71 | print(step, sess.run(cost, feed_dict={ 72 | X: x_data, Y: y_data}), sess.run([W1, W2])) 73 | 74 | # Accuracy report 75 | h, c, a = sess.run([hypothesis, predicted, accuracy], 76 | feed_dict={X: x_data, Y: y_data}) 77 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) 78 | 79 | 80 | ''' 81 | Hypothesis: [[ 6.13103184e-05] 82 | [ 9.99936938e-01] 83 | [ 9.99950767e-01] 84 | [ 5.97514772e-05]] 85 | Correct: [[ 0.] 86 | [ 1.] 87 | [ 1.] 88 | [ 0.]] 89 | Accuracy: 1.0 90 | ''' 91 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-5-linear_back_prop.py: -------------------------------------------------------------------------------- 1 | # http://blog.aloni.org/posts/backprop-with-tensorflow/ 2 | # https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b#.b3rvzhx89 3 | # WIP 4 | import tensorflow as tf 5 | 6 | tf.set_random_seed(777) # reproducibility 7 | 8 | # tf Graph Input 9 | x_data = [[1.], 10 | [2.], 11 | [3.]] 12 | y_data = [[1.], 13 | [2.], 14 | [3.]] 15 | 16 | 17 | # placeholders for a tensor that will be always fed. 18 | X = tf.placeholder(tf.float32, shape=[None, 1]) 19 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 20 | 21 | # Set wrong model weights 22 | W = tf.Variable(tf.truncated_normal([1, 1])) 23 | b = tf.Variable(5.) 24 | 25 | # Forward prop 26 | hypothesis = tf.matmul(X, W) + b 27 | 28 | # diff 29 | assert hypothesis.shape.as_list() == Y.shape.as_list() 30 | diff = (hypothesis - Y) 31 | 32 | # Back prop (chain rule) 33 | d_l1 = diff 34 | d_b = d_l1 35 | d_w = tf.matmul(tf.transpose(X), d_l1) 36 | 37 | print(X, W, d_l1, d_w) 38 | 39 | # Updating network using gradients 40 | learning_rate = 0.1 41 | step = [ 42 | tf.assign(W, W - learning_rate * d_w), 43 | tf.assign(b, b - learning_rate * tf.reduce_mean(d_b)), 44 | ] 45 | 46 | # 7. Running and testing the training process 47 | RMSE = tf.reduce_mean(tf.square((Y - hypothesis))) 48 | 49 | sess = tf.InteractiveSession() 50 | init = tf.global_variables_initializer() 51 | sess.run(init) 52 | 53 | for i in range(1000): 54 | print(i, sess.run([step, RMSE], feed_dict={X: x_data, Y: y_data})) 55 | 56 | print(sess.run(hypothesis, feed_dict={X: x_data})) 57 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-6-multi-linear_back_prop.py: -------------------------------------------------------------------------------- 1 | # http://blog.aloni.org/posts/backprop-with-tensorflow/ 2 | # https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b#.b3rvzhx89 3 | # WIP 4 | import tensorflow as tf 5 | 6 | tf.set_random_seed(777) # reproducibility 7 | 8 | # tf Graph Input 9 | x_data = [[73., 80., 75.], 10 | [93., 88., 93.], 11 | [89., 91., 90.], 12 | [96., 98., 100.], 13 | [73., 66., 70.]] 14 | y_data = [[152.], 15 | [185.], 16 | [180.], 17 | [196.], 18 | [142.]] 19 | 20 | # placeholders for a tensor that will be always fed. 21 | X = tf.placeholder(tf.float32, shape=[None, 3]) 22 | Y = tf.placeholder(tf.float32, shape=[None, 1]) 23 | 24 | # Set wrong model weights 25 | W = tf.Variable(tf.truncated_normal([3, 1])) 26 | b = tf.Variable(5.) 27 | 28 | # Forward prop 29 | hypothesis = tf.matmul(X, W) + b 30 | 31 | print(hypothesis.shape, Y.shape) 32 | 33 | # diff 34 | assert hypothesis.shape.as_list() == Y.shape.as_list() 35 | diff = (hypothesis - Y) 36 | 37 | # Back prop (chain rule) 38 | d_l1 = diff 39 | d_b = d_l1 40 | d_w = tf.matmul(tf.transpose(X), d_l1) 41 | 42 | print(X, d_l1, d_w) 43 | 44 | # Updating network using gradients 45 | learning_rate = 1e-6 46 | step = [ 47 | tf.assign(W, W - learning_rate * d_w), 48 | tf.assign(b, b - learning_rate * tf.reduce_mean(d_b)), 49 | ] 50 | 51 | # 7. Running and testing the training process 52 | RMSE = tf.reduce_mean(tf.square((Y - hypothesis))) 53 | 54 | sess = tf.InteractiveSession() 55 | init = tf.global_variables_initializer() 56 | sess.run(init) 57 | 58 | for i in range(10000): 59 | print(i, sess.run([step, RMSE], feed_dict={X: x_data, Y: y_data})) 60 | 61 | print(sess.run(hypothesis, feed_dict={X: x_data})) 62 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-09-x-xor-nn-back_prop.py: -------------------------------------------------------------------------------- 1 | # Lab 9 XOR-back_prop 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | tf.set_random_seed(777) # for reproducibility 6 | learning_rate = 0.1 7 | 8 | x_data = [[0, 0], 9 | [0, 1], 10 | [1, 0], 11 | [1, 1]] 12 | y_data = [[0], 13 | [1], 14 | [1], 15 | [0]] 16 | 17 | x_data = np.array(x_data, dtype=np.float32) 18 | y_data = np.array(y_data, dtype=np.float32) 19 | 20 | X = tf.placeholder(tf.float32, [None, 2]) 21 | Y = tf.placeholder(tf.float32, [None, 1]) 22 | 23 | W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1') 24 | b1 = tf.Variable(tf.random_normal([2]), name='bias1') 25 | l1 = tf.sigmoid(tf.matmul(X, W1) + b1) 26 | 27 | W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2') 28 | b2 = tf.Variable(tf.random_normal([1]), name='bias2') 29 | Y_pred = tf.sigmoid(tf.matmul(l1, W2) + b2) 30 | 31 | # cost/loss function 32 | cost = -tf.reduce_mean(Y * tf.log(Y_pred) + (1 - Y) * 33 | tf.log(1 - Y_pred)) 34 | 35 | # Network 36 | # p1 a1 l1 p2 a2 l2 (y_pred) 37 | # X -> (*) -> (+) -> (sigmoid) -> (*) -> (+) -> (sigmoid) -> (loss) 38 | # ^ ^ ^ ^ 39 | # | | | | 40 | # W1 b1 W2 b2 41 | 42 | # Loss derivative 43 | d_Y_pred = (Y_pred - Y) / (Y_pred * (1.0 - Y_pred) + 1e-7) 44 | 45 | # Layer 2 46 | d_sigma2 = Y_pred * (1 - Y_pred) 47 | d_a2 = d_Y_pred * d_sigma2 48 | d_p2 = d_a2 49 | d_b2 = d_a2 50 | d_W2 = tf.matmul(tf.transpose(l1), d_p2) 51 | 52 | # Mean 53 | d_b2_mean = tf.reduce_mean(d_b2, axis=[0]) 54 | d_W2_mean = d_W2 / tf.cast(tf.shape(l1)[0], dtype=tf.float32) 55 | 56 | # Layer 1 57 | d_l1 = tf.matmul(d_p2, tf.transpose(W2)) 58 | d_sigma1 = l1 * (1 - l1) 59 | d_a1 = d_l1 * d_sigma1 60 | d_b1 = d_a1 61 | d_p1 = d_a1 62 | d_W1 = tf.matmul(tf.transpose(X), d_a1) 63 | 64 | # Mean 65 | d_W1_mean = d_W1 / tf.cast(tf.shape(X)[0], dtype=tf.float32) 66 | d_b1_mean = tf.reduce_mean(d_b1, axis=[0]) 67 | 68 | # Weight update 69 | step = [ 70 | tf.assign(W2, W2 - learning_rate * d_W2_mean), 71 | tf.assign(b2, b2 - learning_rate * d_b2_mean), 72 | tf.assign(W1, W1 - learning_rate * d_W1_mean), 73 | tf.assign(b1, b1 - learning_rate * d_b1_mean) 74 | ] 75 | 76 | # Accuracy computation 77 | # True if hypothesis > 0.5 else False 78 | predicted = tf.cast(Y_pred > 0.5, dtype=tf.float32) 79 | accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) 80 | 81 | # Launch graph 82 | with tf.Session() as sess: 83 | # Initialize TensorFlow variables 84 | sess.run(tf.global_variables_initializer()) 85 | 86 | print("shape", sess.run(tf.shape(X)[0], feed_dict={X: x_data})) 87 | 88 | 89 | for i in range(10001): 90 | sess.run([step, cost], feed_dict={X: x_data, Y: y_data}) 91 | if i % 1000 == 0: 92 | print(i, sess.run([cost, d_W1], feed_dict={ 93 | X: x_data, Y: y_data}), sess.run([W1, W2])) 94 | 95 | # Accuracy report 96 | h, c, a = sess.run([Y_pred, predicted, accuracy], 97 | feed_dict={X: x_data, Y: y_data}) 98 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) 99 | 100 | 101 | ''' 102 | Hypothesis: [[ 0.01338224] 103 | [ 0.98166382] 104 | [ 0.98809403] 105 | [ 0.01135806]] 106 | Correct: [[ 0.] 107 | [ 1.] 108 | [ 1.] 109 | [ 0.]] 110 | Accuracy: 1.0 111 | ''' 112 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-10-1-mnist_softmax.py: -------------------------------------------------------------------------------- 1 | # Lab 7 Learning rate and Evaluation 2 | import tensorflow as tf 3 | import random 4 | # import matplotlib.pyplot as plt 5 | from tensorflow.examples.tutorials.mnist import input_data 6 | tf.set_random_seed(777) # reproducibility 7 | 8 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 9 | # Check out https://www.tensorflow.org/get_started/mnist/beginners for 10 | # more information about the mnist dataset 11 | 12 | # parameters 13 | learning_rate = 0.001 14 | training_epochs = 15 15 | batch_size = 100 16 | 17 | # input place holders 18 | X = tf.placeholder(tf.float32, [None, 784]) 19 | Y = tf.placeholder(tf.float32, [None, 10]) 20 | 21 | # weights & bias for nn layers 22 | W = tf.Variable(tf.random_normal([784, 10])) 23 | b = tf.Variable(tf.random_normal([10])) 24 | 25 | hypothesis = tf.matmul(X, W) + b 26 | 27 | # define cost/loss & optimizer 28 | cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( 29 | logits=hypothesis, labels=Y)) 30 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 31 | 32 | # initialize 33 | sess = tf.Session() 34 | sess.run(tf.global_variables_initializer()) 35 | 36 | # train my model 37 | for epoch in range(training_epochs): 38 | avg_cost = 0 39 | total_batch = int(mnist.train.num_examples / batch_size) 40 | 41 | for i in range(total_batch): 42 | batch_xs, batch_ys = mnist.train.next_batch(batch_size) 43 | feed_dict = {X: batch_xs, Y: batch_ys} 44 | c, _ = sess.run([cost, optimizer], feed_dict=feed_dict) 45 | avg_cost += c / total_batch 46 | 47 | print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost)) 48 | 49 | print('Learning Finished!') 50 | 51 | # Test model and check accuracy 52 | correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1)) 53 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 54 | print('Accuracy:', sess.run(accuracy, feed_dict={ 55 | X: mnist.test.images, Y: mnist.test.labels})) 56 | 57 | # Get one and predict 58 | r = random.randint(0, mnist.test.num_examples - 1) 59 | print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1))) 60 | print("Prediction: ", sess.run( 61 | tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]})) 62 | 63 | # plt.imshow(mnist.test.images[r:r + 1]. 64 | # reshape(28, 28), cmap='Greys', interpolation='nearest') 65 | # plt.show() 66 | 67 | ''' 68 | Epoch: 0001 cost = 5.888845987 69 | Epoch: 0002 cost = 1.860620173 70 | Epoch: 0003 cost = 1.159035648 71 | Epoch: 0004 cost = 0.892340870 72 | Epoch: 0005 cost = 0.751155428 73 | Epoch: 0006 cost = 0.662484806 74 | Epoch: 0007 cost = 0.601544010 75 | Epoch: 0008 cost = 0.556526115 76 | Epoch: 0009 cost = 0.521186961 77 | Epoch: 0010 cost = 0.493068354 78 | Epoch: 0011 cost = 0.469686249 79 | Epoch: 0012 cost = 0.449967254 80 | Epoch: 0013 cost = 0.433519321 81 | Epoch: 0014 cost = 0.419000337 82 | Epoch: 0015 cost = 0.406490815 83 | Learning Finished! 84 | Accuracy: 0.9035 85 | ''' 86 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-10-2-mnist_nn.py: -------------------------------------------------------------------------------- 1 | # Lab 10 MNIST and NN 2 | import tensorflow as tf 3 | import random 4 | # import matplotlib.pyplot as plt 5 | 6 | from tensorflow.examples.tutorials.mnist import input_data 7 | 8 | tf.set_random_seed(777) # reproducibility 9 | 10 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 11 | # Check out https://www.tensorflow.org/get_started/mnist/beginners for 12 | # more information about the mnist dataset 13 | 14 | # parameters 15 | learning_rate = 0.001 16 | training_epochs = 15 17 | batch_size = 100 18 | 19 | # input place holders 20 | X = tf.placeholder(tf.float32, [None, 784]) 21 | Y = tf.placeholder(tf.float32, [None, 10]) 22 | 23 | # weights & bias for nn layers 24 | W1 = tf.Variable(tf.random_normal([784, 256])) 25 | b1 = tf.Variable(tf.random_normal([256])) 26 | L1 = tf.nn.relu(tf.matmul(X, W1) + b1) 27 | 28 | W2 = tf.Variable(tf.random_normal([256, 256])) 29 | b2 = tf.Variable(tf.random_normal([256])) 30 | L2 = tf.nn.relu(tf.matmul(L1, W2) + b2) 31 | 32 | W3 = tf.Variable(tf.random_normal([256, 10])) 33 | b3 = tf.Variable(tf.random_normal([10])) 34 | hypothesis = tf.matmul(L2, W3) + b3 35 | 36 | # define cost/loss & optimizer 37 | cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( 38 | logits=hypothesis, labels=Y)) 39 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 40 | 41 | # initialize 42 | sess = tf.Session() 43 | sess.run(tf.global_variables_initializer()) 44 | 45 | # train my model 46 | for epoch in range(training_epochs): 47 | avg_cost = 0 48 | total_batch = int(mnist.train.num_examples / batch_size) 49 | 50 | for i in range(total_batch): 51 | batch_xs, batch_ys = mnist.train.next_batch(batch_size) 52 | feed_dict = {X: batch_xs, Y: batch_ys} 53 | c, _ = sess.run([cost, optimizer], feed_dict=feed_dict) 54 | avg_cost += c / total_batch 55 | 56 | print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost)) 57 | 58 | print('Learning Finished!') 59 | 60 | # Test model and check accuracy 61 | correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1)) 62 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 63 | print('Accuracy:', sess.run(accuracy, feed_dict={ 64 | X: mnist.test.images, Y: mnist.test.labels})) 65 | 66 | # Get one and predict 67 | r = random.randint(0, mnist.test.num_examples - 1) 68 | print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1))) 69 | print("Prediction: ", sess.run( 70 | tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]})) 71 | 72 | # plt.imshow(mnist.test.images[r:r + 1]. 73 | # reshape(28, 28), cmap='Greys', interpolation='nearest') 74 | # plt.show() 75 | 76 | ''' 77 | Epoch: 0001 cost = 141.207671860 78 | Epoch: 0002 cost = 38.788445864 79 | Epoch: 0003 cost = 23.977515479 80 | Epoch: 0004 cost = 16.315132428 81 | Epoch: 0005 cost = 11.702554882 82 | Epoch: 0006 cost = 8.573139748 83 | Epoch: 0007 cost = 6.370995680 84 | Epoch: 0008 cost = 4.537178684 85 | Epoch: 0009 cost = 3.216900532 86 | Epoch: 0010 cost = 2.329708954 87 | Epoch: 0011 cost = 1.715552875 88 | Epoch: 0012 cost = 1.189857912 89 | Epoch: 0013 cost = 0.820965160 90 | Epoch: 0014 cost = 0.624131458 91 | Epoch: 0015 cost = 0.454633765 92 | Learning Finished! 93 | Accuracy: 0.9455 94 | ''' 95 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-10-3-mnist_nn_xavier.py: -------------------------------------------------------------------------------- 1 | # Lab 10 MNIST and Xavier 2 | import tensorflow as tf 3 | import random 4 | # import matplotlib.pyplot as plt 5 | 6 | from tensorflow.examples.tutorials.mnist import input_data 7 | 8 | tf.set_random_seed(777) # reproducibility 9 | 10 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 11 | # Check out https://www.tensorflow.org/get_started/mnist/beginners for 12 | # more information about the mnist dataset 13 | 14 | # parameters 15 | learning_rate = 0.001 16 | training_epochs = 15 17 | batch_size = 100 18 | 19 | # input place holders 20 | X = tf.placeholder(tf.float32, [None, 784]) 21 | Y = tf.placeholder(tf.float32, [None, 10]) 22 | 23 | # weights & bias for nn layers 24 | # http://stackoverflow.com/questions/33640581/how-to-do-xavier-initialization-on-tensorflow 25 | W1 = tf.get_variable("W1", shape=[784, 256], 26 | initializer=tf.contrib.layers.xavier_initializer()) 27 | b1 = tf.Variable(tf.random_normal([256])) 28 | L1 = tf.nn.relu(tf.matmul(X, W1) + b1) 29 | 30 | W2 = tf.get_variable("W2", shape=[256, 256], 31 | initializer=tf.contrib.layers.xavier_initializer()) 32 | b2 = tf.Variable(tf.random_normal([256])) 33 | L2 = tf.nn.relu(tf.matmul(L1, W2) + b2) 34 | 35 | W3 = tf.get_variable("W3", shape=[256, 10], 36 | initializer=tf.contrib.layers.xavier_initializer()) 37 | b3 = tf.Variable(tf.random_normal([10])) 38 | hypothesis = tf.matmul(L2, W3) + b3 39 | 40 | # define cost/loss & optimizer 41 | cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( 42 | logits=hypothesis, labels=Y)) 43 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 44 | 45 | # initialize 46 | sess = tf.Session() 47 | sess.run(tf.global_variables_initializer()) 48 | 49 | # train my model 50 | for epoch in range(training_epochs): 51 | avg_cost = 0 52 | total_batch = int(mnist.train.num_examples / batch_size) 53 | 54 | for i in range(total_batch): 55 | batch_xs, batch_ys = mnist.train.next_batch(batch_size) 56 | feed_dict = {X: batch_xs, Y: batch_ys} 57 | c, _ = sess.run([cost, optimizer], feed_dict=feed_dict) 58 | avg_cost += c / total_batch 59 | 60 | print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost)) 61 | 62 | print('Learning Finished!') 63 | 64 | # Test model and check accuracy 65 | correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1)) 66 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 67 | print('Accuracy:', sess.run(accuracy, feed_dict={ 68 | X: mnist.test.images, Y: mnist.test.labels})) 69 | 70 | # Get one and predict 71 | r = random.randint(0, mnist.test.num_examples - 1) 72 | print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1))) 73 | print("Prediction: ", sess.run( 74 | tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]})) 75 | 76 | # plt.imshow(mnist.test.images[r:r + 1]. 77 | # reshape(28, 28), cmap='Greys', interpolation='nearest') 78 | # plt.show() 79 | 80 | ''' 81 | Epoch: 0001 cost = 0.301498963 82 | Epoch: 0002 cost = 0.107252513 83 | Epoch: 0003 cost = 0.064888892 84 | Epoch: 0004 cost = 0.044463030 85 | Epoch: 0005 cost = 0.029951642 86 | Epoch: 0006 cost = 0.020663404 87 | Epoch: 0007 cost = 0.015853033 88 | Epoch: 0008 cost = 0.011764387 89 | Epoch: 0009 cost = 0.008598264 90 | Epoch: 0010 cost = 0.007383116 91 | Epoch: 0011 cost = 0.006839140 92 | Epoch: 0012 cost = 0.004672963 93 | Epoch: 0013 cost = 0.003979437 94 | Epoch: 0014 cost = 0.002714260 95 | Epoch: 0015 cost = 0.004707661 96 | Learning Finished! 97 | Accuracy: 0.9783 98 | ''' 99 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-10-X1-mnist_back_prop.py: -------------------------------------------------------------------------------- 1 | # http://blog.aloni.org/posts/backprop-with-tensorflow/ 2 | # https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b#.b3rvzhx89 3 | import tensorflow as tf 4 | 5 | tf.set_random_seed(777) # reproducibility 6 | 7 | # Check out https://www.tensorflow.org/get_started/mnist/beginners for 8 | # more information about the mnist dataset 9 | from tensorflow.examples.tutorials.mnist import input_data 10 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 11 | 12 | X = tf.placeholder(tf.float32, [None, 784]) 13 | Y = tf.placeholder(tf.float32, [None, 10]) 14 | 15 | w1 = tf.Variable(tf.truncated_normal([784, 30])) 16 | b1 = tf.Variable(tf.truncated_normal([1, 30])) 17 | w2 = tf.Variable(tf.truncated_normal([30, 10])) 18 | b2 = tf.Variable(tf.truncated_normal([1, 10])) 19 | 20 | 21 | def sigma(x): 22 | # sigmoid function 23 | return tf.div(tf.constant(1.0), 24 | tf.add(tf.constant(1.0), tf.exp(-x))) 25 | 26 | 27 | def sigma_prime(x): 28 | # derivative of the sigmoid function 29 | return sigma(x) * (1 - sigma(x)) 30 | 31 | # Forward prop 32 | l1 = tf.add(tf.matmul(X, w1), b1) 33 | a1 = sigma(l1) 34 | l2 = tf.add(tf.matmul(a1, w2), b2) 35 | y_pred = sigma(l2) 36 | 37 | # diff 38 | assert y_pred.shape.as_list() == Y.shape.as_list() 39 | diff = (y_pred - Y) 40 | 41 | 42 | # Back prop (chain rule) 43 | d_l2 = diff * sigma_prime(l2) 44 | d_b2 = d_l2 45 | d_w2 = tf.matmul(tf.transpose(a1), d_l2) 46 | 47 | d_a1 = tf.matmul(d_l2, tf.transpose(w2)) 48 | d_l1 = d_a1 * sigma_prime(l1) 49 | d_b1 = d_l1 50 | d_w1 = tf.matmul(tf.transpose(X), d_l1) 51 | 52 | 53 | # Updating network using gradients 54 | learning_rate = 0.5 55 | step = [ 56 | tf.assign(w1, w1 - learning_rate * d_w1), 57 | tf.assign(b1, b1 - learning_rate * 58 | tf.reduce_mean(d_b1, reduction_indices=[0])), 59 | tf.assign(w2, w2 - learning_rate * d_w2), 60 | tf.assign(b2, b2 - learning_rate * 61 | tf.reduce_mean(d_b2, reduction_indices=[0])) 62 | ] 63 | 64 | # 7. Running and testing the training process 65 | acct_mat = tf.equal(tf.argmax(y_pred, 1), tf.argmax(Y, 1)) 66 | acct_res = tf.reduce_sum(tf.cast(acct_mat, tf.float32)) 67 | 68 | sess = tf.InteractiveSession() 69 | sess.run(tf.global_variables_initializer()) 70 | 71 | for i in range(10000): 72 | batch_xs, batch_ys = mnist.train.next_batch(10) 73 | sess.run(step, feed_dict={X: batch_xs, 74 | Y: batch_ys}) 75 | if i % 1000 == 0: 76 | res = sess.run(acct_res, feed_dict={X: mnist.test.images[:1000], 77 | Y: mnist.test.labels[:1000]}) 78 | print(res) 79 | 80 | # 8. Automatic differentiation in TensorFlow 81 | cost = diff * diff 82 | step = tf.train.GradientDescentOptimizer(0.1).minimize(cost) 83 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-12-1-hello-rnn.py: -------------------------------------------------------------------------------- 1 | # Lab 12 RNN 2 | import tensorflow as tf 3 | import numpy as np 4 | tf.set_random_seed(777) # reproducibility 5 | 6 | idx2char = ['h', 'i', 'e', 'l', 'o'] 7 | # Teach hello: hihell -> ihello 8 | x_data = [[0, 1, 0, 2, 3, 3]] # hihell 9 | x_one_hot = [[[1, 0, 0, 0, 0], # h 0 10 | [0, 1, 0, 0, 0], # i 1 11 | [1, 0, 0, 0, 0], # h 0 12 | [0, 0, 1, 0, 0], # e 2 13 | [0, 0, 0, 1, 0], # l 3 14 | [0, 0, 0, 1, 0]]] # l 3 15 | 16 | y_data = [[1, 0, 2, 3, 3, 4]] # ihello 17 | 18 | num_classes = 5 19 | input_dim = 5 # one-hot size 20 | hidden_size = 5 # output from the LSTM. 5 to directly predict one-hot 21 | batch_size = 1 # one sentence 22 | sequence_length = 6 # |ihello| == 6 23 | learning_rate = 0.1 24 | 25 | X = tf.placeholder( 26 | tf.float32, [None, sequence_length, input_dim]) # X one-hot 27 | Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label 28 | 29 | cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True) 30 | initial_state = cell.zero_state(batch_size, tf.float32) 31 | outputs, _states = tf.nn.dynamic_rnn( 32 | cell, X, initial_state=initial_state, dtype=tf.float32) 33 | 34 | # FC layer 35 | X_for_fc = tf.reshape(outputs, [-1, hidden_size]) 36 | # fc_w = tf.get_variable("fc_w", [hidden_size, num_classes]) 37 | # fc_b = tf.get_variable("fc_b", [num_classes]) 38 | # outputs = tf.matmul(X_for_fc, fc_w) + fc_b 39 | outputs = tf.contrib.layers.fully_connected( 40 | inputs=X_for_fc, num_outputs=num_classes, activation_fn=None) 41 | 42 | # reshape out for sequence_loss 43 | outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes]) 44 | 45 | weights = tf.ones([batch_size, sequence_length]) 46 | sequence_loss = tf.contrib.seq2seq.sequence_loss( 47 | logits=outputs, targets=Y, weights=weights) 48 | loss = tf.reduce_mean(sequence_loss) 49 | train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) 50 | 51 | prediction = tf.argmax(outputs, axis=2) 52 | 53 | with tf.Session() as sess: 54 | sess.run(tf.global_variables_initializer()) 55 | for i in range(50): 56 | l, _ = sess.run([loss, train], feed_dict={X: x_one_hot, Y: y_data}) 57 | result = sess.run(prediction, feed_dict={X: x_one_hot}) 58 | print(i, "loss:", l, "prediction: ", result, "true Y: ", y_data) 59 | 60 | # print char using dic 61 | result_str = [idx2char[c] for c in np.squeeze(result)] 62 | print("\tPrediction str: ", ''.join(result_str)) 63 | 64 | ''' 65 | 0 loss: 1.71584 prediction: [[2 2 2 3 3 2]] true Y: [[1, 0, 2, 3, 3, 4]] 66 | Prediction str: eeelle 67 | 1 loss: 1.56447 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]] 68 | Prediction str: llllll 69 | 2 loss: 1.46284 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]] 70 | Prediction str: llllll 71 | 3 loss: 1.38073 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]] 72 | Prediction str: llllll 73 | 4 loss: 1.30603 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]] 74 | Prediction str: llllll 75 | 5 loss: 1.21498 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]] 76 | Prediction str: llllll 77 | 6 loss: 1.1029 prediction: [[3 0 3 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]] 78 | Prediction str: lhlllo 79 | 7 loss: 0.982386 prediction: [[1 0 3 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]] 80 | Prediction str: ihlllo 81 | 8 loss: 0.871259 prediction: [[1 0 3 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]] 82 | Prediction str: ihlllo 83 | 9 loss: 0.774338 prediction: [[1 0 2 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]] 84 | Prediction str: ihello 85 | 10 loss: 0.676005 prediction: [[1 0 2 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]] 86 | Prediction str: ihello 87 | 88 | ... 89 | 90 | ''' 91 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-12-2-char-seq-rnn.py: -------------------------------------------------------------------------------- 1 | # Lab 12 Character Sequence RNN 2 | import tensorflow as tf 3 | import numpy as np 4 | tf.set_random_seed(777) # reproducibility 5 | 6 | sample = " if you want you" 7 | idx2char = list(set(sample)) # index -> char 8 | char2idx = {c: i for i, c in enumerate(idx2char)} # char -> idex 9 | 10 | # hyper parameters 11 | dic_size = len(char2idx) # RNN input size (one hot size) 12 | hidden_size = len(char2idx) # RNN output size 13 | num_classes = len(char2idx) # final output size (RNN or softmax, etc.) 14 | batch_size = 1 # one sample data, one batch 15 | sequence_length = len(sample) - 1 # number of lstm rollings (unit #) 16 | learning_rate = 0.1 17 | 18 | sample_idx = [char2idx[c] for c in sample] # char to index 19 | x_data = [sample_idx[:-1]] # X data sample (0 ~ n-1) hello: hell 20 | y_data = [sample_idx[1:]] # Y label sample (1 ~ n) hello: ello 21 | 22 | X = tf.placeholder(tf.int32, [None, sequence_length]) # X data 23 | Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label 24 | 25 | x_one_hot = tf.one_hot(X, num_classes) # one hot: 1 -> 0 1 0 0 0 0 0 0 0 0 26 | cell = tf.contrib.rnn.BasicLSTMCell( 27 | num_units=hidden_size, state_is_tuple=True) 28 | initial_state = cell.zero_state(batch_size, tf.float32) 29 | outputs, _states = tf.nn.dynamic_rnn( 30 | cell, x_one_hot, initial_state=initial_state, dtype=tf.float32) 31 | 32 | # FC layer 33 | X_for_fc = tf.reshape(outputs, [-1, hidden_size]) 34 | outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None) 35 | 36 | # reshape out for sequence_loss 37 | outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes]) 38 | 39 | weights = tf.ones([batch_size, sequence_length]) 40 | sequence_loss = tf.contrib.seq2seq.sequence_loss( 41 | logits=outputs, targets=Y, weights=weights) 42 | loss = tf.reduce_mean(sequence_loss) 43 | train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) 44 | 45 | prediction = tf.argmax(outputs, axis=2) 46 | 47 | with tf.Session() as sess: 48 | sess.run(tf.global_variables_initializer()) 49 | for i in range(50): 50 | l, _ = sess.run([loss, train], feed_dict={X: x_data, Y: y_data}) 51 | result = sess.run(prediction, feed_dict={X: x_data}) 52 | 53 | # print char using dic 54 | result_str = [idx2char[c] for c in np.squeeze(result)] 55 | 56 | print(i, "loss:", l, "Prediction:", ''.join(result_str)) 57 | 58 | 59 | ''' 60 | 0 loss: 2.35377 Prediction: uuuuuuuuuuuuuuu 61 | 1 loss: 2.21383 Prediction: yy you y you 62 | 2 loss: 2.04317 Prediction: yy yoo ou 63 | 3 loss: 1.85869 Prediction: yy ou uou 64 | 4 loss: 1.65096 Prediction: yy you a you 65 | 5 loss: 1.40243 Prediction: yy you yan you 66 | 6 loss: 1.12986 Prediction: yy you wann you 67 | 7 loss: 0.907699 Prediction: yy you want you 68 | 8 loss: 0.687401 Prediction: yf you want you 69 | 9 loss: 0.508868 Prediction: yf you want you 70 | 10 loss: 0.379423 Prediction: yf you want you 71 | 11 loss: 0.282956 Prediction: if you want you 72 | 12 loss: 0.208561 Prediction: if you want you 73 | 74 | ... 75 | 76 | ''' 77 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-12-3-char-seq-softmax-only.py: -------------------------------------------------------------------------------- 1 | # Lab 12 Character Sequence Softmax only 2 | import tensorflow as tf 3 | import numpy as np 4 | tf.set_random_seed(777) # reproducibility 5 | 6 | sample = " if you want you" 7 | idx2char = list(set(sample)) # index -> char 8 | char2idx = {c: i for i, c in enumerate(idx2char)} # char -> idex 9 | 10 | # hyper parameters 11 | dic_size = len(char2idx) # RNN input size (one hot size) 12 | rnn_hidden_size = len(char2idx) # RNN output size 13 | num_classes = len(char2idx) # final output size (RNN or softmax, etc.) 14 | batch_size = 1 # one sample data, one batch 15 | sequence_length = len(sample) - 1 # number of lstm rollings (unit #) 16 | learning_rate = 0.1 17 | 18 | sample_idx = [char2idx[c] for c in sample] # char to index 19 | x_data = [sample_idx[:-1]] # X data sample (0 ~ n-1) hello: hell 20 | y_data = [sample_idx[1:]] # Y label sample (1 ~ n) hello: ello 21 | 22 | X = tf.placeholder(tf.int32, [None, sequence_length]) # X data 23 | Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label 24 | 25 | # flatten the data (ignore batches for now). No effect if the batch size is 1 26 | X_one_hot = tf.one_hot(X, num_classes) # one hot: 1 -> 0 1 0 0 0 0 0 0 0 0 27 | X_for_softmax = tf.reshape(X_one_hot, [-1, rnn_hidden_size]) 28 | 29 | # softmax layer (rnn_hidden_size -> num_classes) 30 | softmax_w = tf.get_variable("softmax_w", [rnn_hidden_size, num_classes]) 31 | softmax_b = tf.get_variable("softmax_b", [num_classes]) 32 | outputs = tf.matmul(X_for_softmax, softmax_w) + softmax_b 33 | 34 | # expend the data (revive the batches) 35 | outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes]) 36 | weights = tf.ones([batch_size, sequence_length]) 37 | 38 | # Compute sequence cost/loss 39 | sequence_loss = tf.contrib.seq2seq.sequence_loss( 40 | logits=outputs, targets=Y, weights=weights) 41 | loss = tf.reduce_mean(sequence_loss) # mean all sequence loss 42 | train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) 43 | 44 | prediction = tf.argmax(outputs, axis=2) 45 | 46 | with tf.Session() as sess: 47 | sess.run(tf.global_variables_initializer()) 48 | for i in range(3000): 49 | l, _ = sess.run([loss, train], feed_dict={X: x_data, Y: y_data}) 50 | result = sess.run(prediction, feed_dict={X: x_data}) 51 | 52 | # print char using dic 53 | result_str = [idx2char[c] for c in np.squeeze(result)] 54 | print(i, "loss:", l, "Prediction:", ''.join(result_str)) 55 | 56 | ''' 57 | 0 loss: 2.29513 Prediction: yu yny y y oyny 58 | 1 loss: 2.10156 Prediction: yu ynu y y oynu 59 | 2 loss: 1.92344 Prediction: yu you y u you 60 | 61 | .. 62 | 63 | 2997 loss: 0.277323 Prediction: yf you yant you 64 | 2998 loss: 0.277323 Prediction: yf you yant you 65 | 2999 loss: 0.277323 Prediction: yf you yant you 66 | ''' 67 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-12-4-rnn_long_char.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import tensorflow as tf 4 | import numpy as np 5 | from tensorflow.contrib import rnn 6 | 7 | tf.set_random_seed(777) # reproducibility 8 | 9 | sentence = ("if you want to build a ship, don't drum up people together to " 10 | "collect wood and don't assign them tasks and work, but rather " 11 | "teach them to long for the endless immensity of the sea.") 12 | 13 | char_set = list(set(sentence)) 14 | char_dic = {w: i for i, w in enumerate(char_set)} 15 | 16 | data_dim = len(char_set) 17 | hidden_size = len(char_set) 18 | num_classes = len(char_set) 19 | sequence_length = 10 # Any arbitrary number 20 | learning_rate = 0.1 21 | 22 | dataX = [] 23 | dataY = [] 24 | for i in range(0, len(sentence) - sequence_length): 25 | x_str = sentence[i:i + sequence_length] 26 | y_str = sentence[i + 1: i + sequence_length + 1] 27 | print(i, x_str, '->', y_str) 28 | 29 | x = [char_dic[c] for c in x_str] # x str to index 30 | y = [char_dic[c] for c in y_str] # y str to index 31 | 32 | dataX.append(x) 33 | dataY.append(y) 34 | 35 | batch_size = len(dataX) 36 | 37 | X = tf.placeholder(tf.int32, [None, sequence_length]) 38 | Y = tf.placeholder(tf.int32, [None, sequence_length]) 39 | 40 | # One-hot encoding 41 | X_one_hot = tf.one_hot(X, num_classes) 42 | print(X_one_hot) # check out the shape 43 | 44 | 45 | # Make a lstm cell with hidden_size (each unit output vector size) 46 | def lstm_cell(): 47 | cell = rnn.BasicLSTMCell(hidden_size, state_is_tuple=True) 48 | return cell 49 | 50 | multi_cells = rnn.MultiRNNCell([lstm_cell() for _ in range(2)], state_is_tuple=True) 51 | 52 | # outputs: unfolding size x hidden size, state = hidden size 53 | outputs, _states = tf.nn.dynamic_rnn(multi_cells, X_one_hot, dtype=tf.float32) 54 | 55 | # FC layer 56 | X_for_fc = tf.reshape(outputs, [-1, hidden_size]) 57 | outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None) 58 | 59 | # reshape out for sequence_loss 60 | outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes]) 61 | 62 | # All weights are 1 (equal weights) 63 | weights = tf.ones([batch_size, sequence_length]) 64 | 65 | sequence_loss = tf.contrib.seq2seq.sequence_loss( 66 | logits=outputs, targets=Y, weights=weights) 67 | mean_loss = tf.reduce_mean(sequence_loss) 68 | train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(mean_loss) 69 | 70 | sess = tf.Session() 71 | sess.run(tf.global_variables_initializer()) 72 | 73 | for i in range(500): 74 | _, l, results = sess.run( 75 | [train_op, mean_loss, outputs], feed_dict={X: dataX, Y: dataY}) 76 | for j, result in enumerate(results): 77 | index = np.argmax(result, axis=1) 78 | print(i, j, ''.join([char_set[t] for t in index]), l) 79 | 80 | # Let's print the last char of each result to check it works 81 | results = sess.run(outputs, feed_dict={X: dataX}) 82 | for j, result in enumerate(results): 83 | index = np.argmax(result, axis=1) 84 | if j is 0: # print all for the first result to make a sentence 85 | print(''.join([char_set[t] for t in index]), end='') 86 | else: 87 | print(char_set[index[-1]], end='') 88 | 89 | ''' 90 | 0 167 tttttttttt 3.23111 91 | 0 168 tttttttttt 3.23111 92 | 0 169 tttttttttt 3.23111 93 | … 94 | 499 167 of the se 0.229616 95 | 499 168 tf the sea 0.229616 96 | 499 169 the sea. 0.229616 97 | 98 | g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. 99 | 100 | ''' 101 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/lab-12-5-rnn_stock_prediction.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This script shows how to predict stock prices using a basic RNN 3 | ''' 4 | import tensorflow as tf 5 | import numpy as np 6 | import matplotlib 7 | import os 8 | 9 | tf.set_random_seed(777) # reproducibility 10 | 11 | if "DISPLAY" not in os.environ: 12 | # remove Travis CI Error 13 | matplotlib.use('Agg') 14 | 15 | import matplotlib.pyplot as plt 16 | 17 | 18 | def MinMaxScaler(data): 19 | ''' Min Max Normalization 20 | 21 | Parameters 22 | ---------- 23 | data : numpy.ndarray 24 | input data to be normalized 25 | shape: [Batch size, dimension] 26 | 27 | Returns 28 | ---------- 29 | data : numpy.ndarry 30 | normalized data 31 | shape: [Batch size, dimension] 32 | 33 | References 34 | ---------- 35 | .. [1] http://sebastianraschka.com/Articles/2014_about_feature_scaling.html 36 | 37 | ''' 38 | numerator = data - np.min(data, 0) 39 | denominator = np.max(data, 0) - np.min(data, 0) 40 | # noise term prevents the zero division 41 | return numerator / (denominator + 1e-7) 42 | 43 | 44 | # train Parameters 45 | seq_length = 7 46 | data_dim = 5 47 | hidden_dim = 10 48 | output_dim = 1 49 | learning_rate = 0.01 50 | iterations = 500 51 | 52 | # Open, High, Low, Volume, Close 53 | xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',') 54 | xy = xy[::-1] # reverse order (chronically ordered) 55 | xy = MinMaxScaler(xy) 56 | x = xy 57 | y = xy[:, [-1]] # Close as label 58 | 59 | # build a dataset 60 | dataX = [] 61 | dataY = [] 62 | for i in range(0, len(y) - seq_length): 63 | _x = x[i:i + seq_length] 64 | _y = y[i + seq_length] # Next close price 65 | print(_x, "->", _y) 66 | dataX.append(_x) 67 | dataY.append(_y) 68 | 69 | # train/test split 70 | train_size = int(len(dataY) * 0.7) 71 | test_size = len(dataY) - train_size 72 | trainX, testX = np.array(dataX[0:train_size]), np.array( 73 | dataX[train_size:len(dataX)]) 74 | trainY, testY = np.array(dataY[0:train_size]), np.array( 75 | dataY[train_size:len(dataY)]) 76 | 77 | # input place holders 78 | X = tf.placeholder(tf.float32, [None, seq_length, data_dim]) 79 | Y = tf.placeholder(tf.float32, [None, 1]) 80 | 81 | # build a LSTM network 82 | cell = tf.contrib.rnn.BasicLSTMCell( 83 | num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh) 84 | outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) 85 | Y_pred = tf.contrib.layers.fully_connected( 86 | outputs[:, -1], output_dim, activation_fn=None) # We use the last cell's output 87 | 88 | # cost/loss 89 | loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares 90 | # optimizer 91 | optimizer = tf.train.AdamOptimizer(learning_rate) 92 | train = optimizer.minimize(loss) 93 | 94 | # RMSE 95 | targets = tf.placeholder(tf.float32, [None, 1]) 96 | predictions = tf.placeholder(tf.float32, [None, 1]) 97 | rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions))) 98 | 99 | with tf.Session() as sess: 100 | init = tf.global_variables_initializer() 101 | sess.run(init) 102 | 103 | # Training step 104 | for i in range(iterations): 105 | _, step_loss = sess.run([train, loss], feed_dict={ 106 | X: trainX, Y: trainY}) 107 | print("[step: {}] loss: {}".format(i, step_loss)) 108 | 109 | # Test step 110 | test_predict = sess.run(Y_pred, feed_dict={X: testX}) 111 | rmse_val = sess.run(rmse, feed_dict={ 112 | targets: testY, predictions: test_predict}) 113 | print("RMSE: {}".format(rmse_val)) 114 | 115 | # Plot predictions 116 | plt.plot(testY) 117 | plt.plot(test_predict) 118 | plt.xlabel("Time Period") 119 | plt.ylabel("Stock Price") 120 | plt.show() 121 | -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/readme.md: -------------------------------------------------------------------------------- 1 | ### TensorFlow Tutorials 2 | 3 | Origin from [here](https://github.com/hunkim/DeepLearningZeroToAll) 4 | 5 | 6 | ### Install requirement 7 | `pip install -r requirement.txt` -------------------------------------------------------------------------------- /Basic-Tutorial/TensorFlow/requirements.txt: -------------------------------------------------------------------------------- 1 | Keras==2.0.2 2 | matplotlib==2.0.0 3 | numpy==1.12.1 4 | tensorflow==1.0.0 5 | scikit_learn==0.18.1 6 | pylint 7 | -------------------------------------------------------------------------------- /Basic-Tutorial/readme.md: -------------------------------------------------------------------------------- 1 | 基础相关的知识在这里概述,主要分为不同框架`TensorFlow`、`MXNet`、`PyTorch`、`PaddlePaddle`实现下基础内容。 2 | [TODO] 3 | 4 | 以后这里将会罗列出学习目录,更新中。 -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/README.MD: -------------------------------------------------------------------------------- 1 | > 2 | fork : [chenyuntc](https://github.com/chenyuntc/scene-baseline) 3 | thanks! 4 | ### By the way: 5 | 国内 6 | pip install visdom 7 | python -m visdom.server 8 | 会出现一些文件下载不下来(GFC原因.....),主要是 python下的visdom文件里 static文件丢失。 9 | 参考链接[issues185](https://github.com/facebookresearch/visdom/issues/185)下载文件到static下面 10 | 11 | 12 | # PyTorch Baseline for AI challenger scene 13 | AI Challenger [场景分类竞赛](https://challenger.ai/competition/scene/)的示例代码 14 | 15 | ## 使用 16 | ### 1.环境配置 17 | 安装: 18 | - [PyTorch](http://pytorch.org/),根据官网说明下载指定的版本即可。 19 | - 第三方依赖: `pip install -r requirements.txt` 20 | 21 | ### 2.数据预处理: 22 | 23 | 这一步主要是根据json文件进行简单的处理,生成二进制scene.pth,可以跳过这一步,直接下载随程序附带的scene.pth 24 | 25 | 如果你想自己生成scene.pth,修改scripts/data_process.py 中的文件路径,然后运行 26 | ```bash 27 | python scripts/data_process.py 28 | ``` 29 | 30 | ### 3.启动visdom 31 | 可视化工具[visdom](https://github.com/facebookresearch/visdom) 32 | ```bash 33 | nohup python -m visdom.server& 34 | ``` 35 | 36 | ### 4.训练 37 | 训练之前还需要新建`checkpoints`文件夹用来保存模型`mkdir checkpoints`。 38 | 注意修改utils.py 中文件路径 39 | 40 | ```bash 41 | python main.py train --model='resnet34' 42 | ``` 43 | 在 Titan Xp下,大概90分钟可以在验证集上得到大约0.938的准确率 44 | 45 | 46 | ```bash 47 | python main.py train --model='resnet365' 48 | ``` 49 | 50 | 使用place365的预训练模型resnet50, 可以在验证集达到**0.957**的top3分数, 51 | 52 | 53 | 打开浏览器 输入http://ip:8097 可以看到训练过程。visdom 中要用到两个js文件`plotly.min.js`和`react-grid-layout.min.js`,这两个js文件被防火墙所拦截~ 所以你可能需要自备梯子才能用visdom。 54 | 55 | 另外一个解决方法是: 56 | `locate locate visdom/static/index.html`,修改index.html中两行js的地址 57 | 58 | ### 5.提交 59 | ```bash 60 | python main.py submit --model='resnet34' --load-path='res34_1018_2204_0.938002232143' 61 | 62 | ``` 63 | 会在当前目录生成`result.json`文件,直接提交即可 64 | 65 | ## 关于CPU运行 66 | 把所有`.cuda()`代码去掉,就能使得程序在CPU上运行 67 | 68 | 69 | ## 各个文件说明 70 | 欢迎参考之前在[知乎专栏](https://zhuanlan.zhihu.com/p/29024978)写过的一篇关于PyTorch的文件组织安排的文章了解每个文件的作用 71 | 72 | `models/`: 存放各个模型定义,所有的模型继承自`basic_module.py`中的`BasicModule`. 73 | 74 | `models/resnet.py`: 对torchvision中的resnet18, resnet34, resnet50, resnet101 和resnet152 进行了简单的封装。 75 | 76 | `dataset.py`: 数据加载相关 77 | 78 | `main.py`: 主程序,包含训练和测试 79 | 80 | `utils.py`: 可视化工具visdom的封装,计算top3准确率函数,可配置变量(可通过命令行参数修改,也可以通过修改文件配置)等。 81 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/dataset.py: -------------------------------------------------------------------------------- 1 | #coding:utf8 2 | import torch as t 3 | from torchvision import transforms 4 | from torch.utils import data 5 | import os 6 | import PIL 7 | import random 8 | 9 | normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], 10 | std=[0.229, 0.224, 0.225]) 11 | 12 | def load(path): 13 | return PIL.Image.open(path).convert('RGB') 14 | 15 | class ClsDataset(data.Dataset): 16 | def __init__(self,opt): 17 | self.opt = opt 18 | self.datas = t.load(opt.meta_path) 19 | 20 | self.val_transforms = transforms.Compose([ 21 | transforms.Scale(opt.img_size), 22 | transforms.CenterCrop(opt.img_size), 23 | transforms.ToTensor(), 24 | normalize, 25 | ]) 26 | self.train_transforms = transforms.Compose([ 27 | transforms.RandomSizedCrop(opt.img_size), 28 | transforms.RandomHorizontalFlip(), 29 | transforms.ToTensor(), 30 | normalize, 31 | ]) 32 | self.train() 33 | 34 | def __getitem__(self,index): 35 | img_path = os.path.join(self.path,self.imgs[index]) 36 | img = load(img_path) 37 | img = self.transforms(img) 38 | return img,self.labels[index],self.imgs[index] 39 | 40 | def train(self): 41 | data = self.datas['train'] 42 | self.imgs,self.labels = data['ids'],data['labels'] 43 | self.path = self.opt.train_dir 44 | self.transforms = self.train_transforms 45 | return self 46 | 47 | def test(self): 48 | data= self.datas['test1'] 49 | self.imgs,self.labels = data['ids'],data['labels'] 50 | self.path = self.opt.test_dir 51 | self.transforms=self.val_transforms 52 | return self 53 | 54 | def val(self): 55 | data = self.datas['val'] 56 | self.imgs,self.labels = data['ids'],data['labels'] 57 | self.path = self.opt.val_dir 58 | self.transforms=self.val_transforms 59 | return self 60 | 61 | def __len__(self): 62 | return len(self.imgs) 63 | 64 | if __name__=='__main__': 65 | test 66 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/models/__init__.py: -------------------------------------------------------------------------------- 1 | from resnet import resnet18,resnet34,resnet50,resnet101,resnet152,resnet365 2 | from loss import * 3 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/models/basic_module.py: -------------------------------------------------------------------------------- 1 | 2 | #coding:utf8 3 | import torch as t 4 | import time 5 | class BasicModule(t.nn.Module): 6 | ''' 7 | 封装了nn.Module 8 | ''' 9 | 10 | def __init__(self,opt=None): 11 | super(BasicModule,self).__init__() 12 | self.model_name=str(type(self).__name__)# 默认名字 13 | self.opt = opt 14 | 15 | def load(self, path,map_location=lambda storage, loc: storage): 16 | checkpoint = t.load(path,map_location=map_location) 17 | if 'opt' in checkpoint: 18 | self.load_state_dict(checkpoint['d']) 19 | print('old config:') 20 | print(checkpoint['opt']) 21 | else: 22 | self.load_state_dict(checkpoint) 23 | # for k,v in checkpoint['opt'].items(): 24 | # setattr(self.opt,k,v) 25 | 26 | def save(self, name=''): 27 | format = u'/media/yijie/文档/dataset/checkpoints/'+self.model_name+'_%m%d_%H%M_' 28 | file_name = time.strftime(format) + str(name) 29 | 30 | state_dict = self.state_dict() 31 | opt_state_dict = dict(self.opt.state_dict()) 32 | optimizer_state_dict = self.optimizer.state_dict() 33 | 34 | t.save({'d':state_dict,'opt':opt_state_dict,'optimizer':optimizer_state_dict}, file_name) 35 | return file_name 36 | 37 | def get_optimizer(self,lr1,lr2): 38 | self.optimizer = t.optim.Adam( 39 | [ 40 | {'params': self.features.parameters(), 'lr': lr1}, 41 | {'params': self.classifier.parameters(), 'lr':lr2} 42 | ]) 43 | return self.optimizer 44 | 45 | def update_optimizer(self,lr1,lr2): 46 | param_groups = self.optimizer.param_groups 47 | param_groups[0]['lr']=lr1 48 | param_groups[1]['lr']=lr2 49 | return self.optimizer 50 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/models/loss.py: -------------------------------------------------------------------------------- 1 | #coding:utf8 2 | import torch as t 3 | def celoss(): 4 | return t.nn.CrossEntropyLoss() 5 | 6 | def bloss(): 7 | def loss(s,l): 8 | pass 9 | return loss -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/models/resnet.py: -------------------------------------------------------------------------------- 1 | #coding:utf8 2 | import torchvision as tv 3 | from torch import nn 4 | from basic_module import BasicModule 5 | import torch as t 6 | 7 | class ResNet(BasicModule): 8 | def __init__(self,model,opt=None,feature_dim=2048,name='resnet'): 9 | super(ResNet, self).__init__(opt) 10 | self.model_name=name 11 | 12 | model.avgpool = nn.AdaptiveAvgPool2d(1) 13 | del model.fc 14 | model.fc = lambda x:x 15 | self.features = model 16 | self.classifier = nn.Linear(feature_dim,80) 17 | 18 | def forward(self,x): 19 | features = self.features(x) 20 | return self.classifier(features) 21 | 22 | def resnet18(opt): 23 | model = tv.models.resnet18(pretrained=not opt.load_path) 24 | return ResNet(model,opt,feature_dim=512,name='res18') 25 | 26 | def resnet34(opt): 27 | model = tv.models.resnet34(pretrained=not opt.load_path) 28 | return ResNet(model,opt,feature_dim=512,name='res34') 29 | 30 | def resnet50(opt): 31 | model = tv.models.resnet50(pretrained=not opt.load_path) 32 | return ResNet(model,opt,name='res50') 33 | 34 | def resnet101(opt): 35 | model = tv.models.resnet101(pretrained=not opt.load_path) 36 | return ResNet(model,opt,name='res101') 37 | 38 | def resnet152(opt): 39 | model = tv.models.resnet152(pretrained=not opt.load_path) 40 | return ResNet(model,opt,name='res152') 41 | 42 | def resnet365(opt): 43 | model = t.load(u'/media/yijie/文档/dataset/checkpoints/whole_resnet50_place365.pth.tar') 44 | # model = tv.models.resnet50() 45 | return ResNet(model,opt,name='res_365') 46 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/nohup.out: -------------------------------------------------------------------------------- 1 | It's Alive! 2 | Traceback (most recent call last): 3 | File "/home/yijie/anaconda2/lib/python2.7/runpy.py", line 174, in _run_module_as_main 4 | "__main__", fname, loader, pkg_name) 5 | File "/home/yijie/anaconda2/lib/python2.7/runpy.py", line 72, in _run_code 6 | exec code in run_globals 7 | File "/home/yijie/anaconda2/lib/python2.7/site-packages/visdom/server.py", line 637, in 8 | main() 9 | File "/home/yijie/anaconda2/lib/python2.7/site-packages/visdom/server.py", line 625, in main 10 | app.listen(FLAGS.port, max_buffer_size=1024 ** 3) 11 | File "/home/yijie/anaconda2/lib/python2.7/site-packages/tornado/web.py", line 1850, in listen 12 | server.listen(port, address) 13 | File "/home/yijie/anaconda2/lib/python2.7/site-packages/tornado/tcpserver.py", line 126, in listen 14 | sockets = bind_sockets(port, address=address) 15 | File "/home/yijie/anaconda2/lib/python2.7/site-packages/tornado/netutil.py", line 194, in bind_sockets 16 | sock.bind(sockaddr) 17 | File "/home/yijie/anaconda2/lib/python2.7/socket.py", line 228, in meth 18 | return getattr(self._sock,name)(*args) 19 | socket.error: [Errno 98] Address already in use 20 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/requirements.txt: -------------------------------------------------------------------------------- 1 | visdom 2 | fire 3 | ipdb 4 | torchvision 5 | git+https://github.com/pytorch/tnt.git@master -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/scene.pth: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/scene.pth -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/scripts/data_process.py: -------------------------------------------------------------------------------- 1 | 2 | #coding:utf8 3 | # 数据预处理脚本 4 | 5 | train_ann_file = '/media/yijie/文档/dataset/ai_challenger_scene_train_20170904/scene_train_annotations_20170904.json' 6 | val_ann_file = '/media/yijie/文档/dataset/ai_challenger_scene_validation_20170908/scene_validation_annotations_20170908.json' 7 | test_dir = '/media/yijie/文档/dataset/ai_challenger_scene_test_a_20170922/scene_test_a_images_20170922' 8 | clas_file = '/media/yijie/文档/dataset/ai_challenger_scene_test_a_20170922/scene_classes.csv' 9 | 10 | import pandas as pd 11 | from collections import namedtuple 12 | # ClassName = namedtuple('ClassName',['zh','en']) 13 | # SceneDataAll = namedtuple('SceneDataAll',['train','val','test1','label']) 14 | # SceneData = namedtuple('SceneData',['ids','labels','id2ix']) 15 | 16 | 17 | a=pd.read_csv(clas_file,header=None) 18 | label_ids,label_zh,label_en = a[0],a[1],a[2] 19 | 20 | #79: ClassName(zh=u'\u96ea\u5c4b/\u51b0\u96d5(\u5c71)', en='igloo/ice_engraving')} 21 | id2label = {k:(v1.decode('utf8'),v2) for k,v1,v2 in zip(label_ids,label_zh,label_en)} 22 | # id2label = {k:ClassName(v1.decode('utf8'),v2) for k,v1,v2 in zip(label_ids,label_zh,label_en)} 23 | 24 | import json 25 | with open(train_ann_file) as f: 26 | datas = json.load(f) 27 | 28 | ids = [ii['image_id'] for ii in datas] 29 | labels = [int(ii['label_id']) for ii in datas] 30 | id2ix = {id:ix for ix,id in enumerate(ids)} 31 | 32 | 33 | #train = SceneData(ids,labels,id2ix) 34 | train = dict(ids=ids,labels = labels,id2ix=id2ix) 35 | 36 | with open(val_ann_file) as f: 37 | datas = json.load(f) 38 | 39 | ids = [ii['image_id'] for ii in datas] 40 | labels = [int(ii['label_id']) for ii in datas] 41 | id2ix = {id:ix for ix,id in enumerate(ids)} 42 | val = dict(ids=ids,labels = labels,id2ix=id2ix) 43 | # val = SceneData(ids,labels,id2ix) 44 | import os 45 | ids = os.listdir(test_dir) 46 | id2ix = {id:ix for ix,id in enumerate(ids)} 47 | # test = SceneData(ids,None,id2ix) 48 | test = dict(ids=ids,labels = labels,id2ix=id2ix) 49 | # all = SceneDataAll(train,val,test,id2label) 50 | 51 | all = dict(train=train,test1=test,val=val,id2label=id2label) 52 | import torch as t 53 | t.save(all,'scene.pth') 54 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie AIChallenger-ScenesClassfication-pytorch/utils.py: -------------------------------------------------------------------------------- 1 | #coding:utf8 2 | import visdom 3 | import numpy as np 4 | 5 | class Visualizer(): 6 | ''' 7 | 对可视化工具visdom的封装 8 | ''' 9 | def __init__(self, env, **kwargs): 10 | import visdom 11 | self.vis = visdom.Visdom(env=env, **kwargs) 12 | self.index = {} 13 | 14 | def plot_many(self, d): 15 | for k, v in d.iteritems(): 16 | self.plot(k, v) 17 | 18 | def plot(self, name, y): 19 | x = self.index.get(name, 0) 20 | self.vis.line(Y=np.array([y]), X=np.array([x]), 21 | win=unicode(name), 22 | opts=dict(title=name), 23 | update=None if x == 0 else 'append' 24 | ) 25 | self.index[name] = x + 1 26 | 27 | def topk_acc(score,label,k=3): 28 | ''' 29 | topk accuracy,默认是top3准确率 30 | ''' 31 | topk = score.topk(k)[1] 32 | label = label.view(-1,1).expand_as(topk) 33 | acc = (label == topk).float().sum()/(0.0+label.size(0)) 34 | return acc 35 | 36 | class Config: 37 | train_dir = u'/media/yijie/文档/dataset/ai_challenger_scene_train_20170904/scene_train_images_20170904' 38 | test_dir = u'/media/yijie/文档/dataset/ai_challenger_scene_test_a_20170922/scene_test_a_images_20170922' 39 | val_dir = u'/media/yijie/文档/dataset/ai_challenger_scene_validation_20170908/scene_validation_images_20170908' 40 | meta_path = u'/media/yijie/文档/git/other/scene-baseline/scene.pth' 41 | img_size=128 42 | 43 | lr1 = 0 44 | lr2 = 0.0005 45 | lr_decay = 0.5 46 | batch_size = 128 47 | max_epoch = 100 48 | debug_file = '/tmp/debugc' 49 | shuffle = True 50 | env = 'scene' # visdom env 51 | plot_every = 10 # 每10步可视化一次 52 | 53 | workers = 4 # CPU多线程加载数据 54 | load_path=None# 55 | model = 'resnet50'# 具体名称查看 models/__init__.py 56 | loss='celoss' 57 | result_path='result.json' #提交文件保存路径 58 | 59 | def parse(self,kwargs,print_=True): 60 | ''' 61 | 根据字典kwargs 更新 config参数 62 | ''' 63 | for k,v in kwargs.iteritems(): 64 | if not hasattr(self,k): 65 | raise Exception("opt has not attribute <%s>" %k) 66 | setattr(self,k,v) 67 | if print_: 68 | print('user config:') 69 | print('#################################') 70 | for k in dir(self): 71 | if not k.startswith('_') and k!='parse' and k!='state_dict': 72 | print (k,getattr(self,k)) 73 | print('#################################') 74 | return self 75 | 76 | 77 | def state_dict(self): 78 | return {k:getattr(self,k) for k in dir(self) if not k.startswith('_') and k!='parse' and k!='state_dict' } 79 | 80 | Config.parse = parse 81 | Config.state_dict = state_dict 82 | opt = Config() 83 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/test_images/image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/test_images/image1.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/test_images/image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/test_images/image2.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie Object_Detection_Tensorflow_API/object_detection/test_images/image_info.txt: -------------------------------------------------------------------------------- 1 | 2 | Image provenance: 3 | image1.jpg: https://commons.wikimedia.org/wiki/File:Baegle_dwa.jpg 4 | image2.jpg: Michael Miley, 5 | https://www.flickr.com/photos/mike_miley/4678754542/in/photolist-88rQHL-88oBVp-88oC2B-88rS6J-88rSqm-88oBLv-88oBC4 6 | 7 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie fine-tuning-gluon/fine-tuning.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from mxnet import gluon 4 | from mxnet import nd 5 | from mxnet import image 6 | from mxnet import init 7 | import sys 8 | import utils 9 | import zipfile 10 | 11 | 12 | data_dir = '/media/yijie/娱乐/tmp/hotdog' 13 | ''' 14 | fname = gluon.utils.download( 15 | 'https://apache-mxnet.s3-accelerate.amazonaws.com/gluon/dataset/hotdog.zip', 16 | path=data_dir, sha1_hash='fba480ffa8aa7e0febbb511d181409f899b9baa5') 17 | ''' 18 | fname = '/media/yijie/娱乐/tmp/hotdog/hotdog.zip' 19 | with zipfile.ZipFile(fname, 'r') as f: 20 | f.extractall(data_dir) 21 | 22 | ## pic augs 23 | train_augs = [ 24 | image.HorizontalFlipAug(.5), 25 | image.RandomCropAug((224,224)) 26 | ] 27 | 28 | test_augs = [ 29 | image.CenterCropAug((224,224)) 30 | ] 31 | 32 | def transform(data, label, augs): 33 | data = data.astype('float32') 34 | for aug in augs: 35 | data = aug(data) 36 | data = nd.transpose(data, (2,0,1)) 37 | return data, nd.array([label]).asscalar().astype('float32') 38 | 39 | train_imgs = gluon.data.vision.ImageFolderDataset( 40 | data_dir+'/hotdog/train', 41 | transform=lambda X, y: transform(X, y, train_augs)) 42 | test_imgs = gluon.data.vision.ImageFolderDataset( 43 | data_dir+'/hotdog/test', 44 | transform=lambda X, y: transform(X, y, test_augs)) 45 | 46 | 47 | # get resnet18 version2 from Internet 48 | # path: /home/yijie/.mxnet/models 49 | from mxnet.gluon.model_zoo import vision as models 50 | pretrained_net = models.resnet18_v2(pretrained=True) 51 | pretrained_net.classifier 52 | pretrained_net.features[1].params.get('weight').data()[0][0] 53 | 54 | finetune_net = models.resnet18_v2(classes=2) 55 | finetune_net.features = pretrained_net.features 56 | finetune_net.classifier.initialize(init.Xavier()) 57 | 58 | sys.path.append('..') 59 | def train(net, ctx, batch_size=64, epochs=10, learning_rate=0.01, wd=0.001): 60 | train_data = gluon.data.DataLoader(train_imgs, batch_size, shuffle=True) 61 | test_data = gluon.data.DataLoader(test_imgs, batch_size) 62 | 63 | # 确保net的初始化在ctx上 64 | net.collect_params().reset_ctx(ctx) 65 | net.hybridize() 66 | loss = gluon.loss.SoftmaxCrossEntropyLoss() 67 | # 训练 68 | trainer = gluon.Trainer(net.collect_params(), 'sgd', { 69 | 'learning_rate': learning_rate, 'wd': wd}) 70 | utils.train(train_data, test_data, net, loss, trainer, ctx, epochs) 71 | 72 | # fine-tuning 73 | ctx = utils.try_all_gpus() 74 | print("the following result is from fine-tuning......\n") 75 | train(finetune_net, ctx) 76 | 77 | 78 | # random init 79 | scratch_net = models.resnet18_v2(classes=2) 80 | scratch_net.initialize(init=init.Xavier()) 81 | print("the following result is from random-init.....\n") 82 | train(scratch_net, ctx) 83 | 84 | 85 | # do some predictions 86 | def classify_hotdog(net, fname): 87 | with open(fname, 'rb') as f: 88 | img = image.imdecode(f.read()) 89 | data, _ = transform(img, -1, test_augs) 90 | plt.imshow(data.transpose((1,2,0)).asnumpy()/255) 91 | data = data.expand_dims(axis=0) 92 | out = net(data.as_in_context(ctx[0])) 93 | out = nd.SoftmaxActivation(out) 94 | pred = int(nd.argmax(out, axis=1).asscalar()) 95 | prob = out[0][pred].asscalar() 96 | label = train_imgs.synsets 97 | return 'With prob=%f, %s'%(prob, label[pred]) 98 | 99 | classify_hotdog(finetune_net, '../img/real_hotdog.jpg') 100 | classify_hotdog(finetune_net, '../img/leg_hotdog.jpg') 101 | classify_hotdog(finetune_net, '../img/dog_hotdog.jpg') 102 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie fine-tuning-gluon/img/dog_hotdog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie fine-tuning-gluon/img/dog_hotdog.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie fine-tuning-gluon/img/leg_hotdog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie fine-tuning-gluon/img/leg_hotdog.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie fine-tuning-gluon/img/real_hotdog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie fine-tuning-gluon/img/real_hotdog.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie fine-tuning-gluon/readme.md: -------------------------------------------------------------------------------- 1 | 本工程用来学习通过微调来实现迁移学习,更多内容参考[这里](https://zh.gluon.ai/chapter_computer-vision/fine-tuning.html) -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-DogBreed-gluon/model.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | from mxnet.gluon import nn 3 | from mxnet import nd 4 | from mxnet import init 5 | 6 | class Residual(nn.HybridBlock): 7 | def __init__(self, channels, same_shape=True, **kwargs): 8 | super(Residual, self).__init__(**kwargs) 9 | self.same_shape = same_shape 10 | with self.name_scope(): 11 | strides = 1 if same_shape else 2 12 | self.conv1 = nn.Conv2D(channels, kernel_size=3, padding=1, 13 | strides=strides) 14 | self.bn1 = nn.BatchNorm() 15 | self.conv2 = nn.Conv2D(channels, kernel_size=3, padding=1) 16 | self.bn2 = nn.BatchNorm() 17 | if not same_shape: 18 | self.conv3 = nn.Conv2D(channels, kernel_size=1, 19 | strides=strides) 20 | 21 | def hybrid_forward(self, F, x): 22 | out = F.relu(self.bn1(self.conv1(x))) 23 | out = self.bn2(self.conv2(out)) 24 | if not self.same_shape: 25 | x = self.conv3(x) 26 | return F.relu(out + x) 27 | 28 | 29 | class ResNet(nn.HybridBlock): 30 | def __init__(self, num_classes, verbose=False, **kwargs): 31 | super(ResNet, self).__init__(**kwargs) 32 | self.verbose = verbose 33 | with self.name_scope(): 34 | net = self.net = nn.HybridSequential() 35 | # 模块1 36 | net.add(nn.Conv2D(channels=32, kernel_size=3, strides=1, 37 | padding=1)) 38 | net.add(nn.BatchNorm()) 39 | net.add(nn.Activation(activation='relu')) 40 | # 模块2 41 | for _ in range(3): 42 | net.add(Residual(channels=32)) 43 | # 模块3 44 | net.add(Residual(channels=64, same_shape=False)) 45 | for _ in range(2): 46 | net.add(Residual(channels=64)) 47 | # 模块4 48 | net.add(Residual(channels=128, same_shape=False)) 49 | for _ in range(2): 50 | net.add(Residual(channels=128)) 51 | # 模块5 52 | net.add(nn.GlobalAvgPool2D()) 53 | net.add(nn.Flatten()) 54 | net.add(nn.Dense(num_classes)) 55 | 56 | def hybrid_forward(self, F, x): 57 | out = x 58 | for i, b in enumerate(self.net): 59 | out = b(out) 60 | if self.verbose: 61 | print('Block %d output: %s'%(i+1, out.shape)) 62 | return out 63 | 64 | 65 | def get_net(ctx): 66 | num_outputs = 120 67 | net = ResNet(num_outputs) 68 | net.initialize(ctx=ctx, init=init.Xavier()) 69 | return net -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-DogBreed-gluon/pre_deal.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | 3 | import zipfile 4 | import math 5 | import os 6 | import shutil 7 | from collections import Counter 8 | 9 | from mxnet import autograd 10 | from mxnet import gluon 11 | from mxnet import image 12 | from mxnet import init 13 | from mxnet import nd 14 | from mxnet.gluon.data import vision 15 | import numpy as np 16 | 17 | 18 | is_zip_dir = True 19 | 20 | 21 | if not is_zip_dir: 22 | zipfiles = ['train.zip','test.zip','labels.csv.zip'] 23 | for fin in zipfiles: 24 | with zipfile.ZipFile(data_dir+'/'+fin,'r') as zin: 25 | zin.extractall(data_dir) 26 | 27 | def reorg_dog_data(data_dir, label_file, train_dir, test_dir, input_dir, 28 | valid_ratio): 29 | # 读取训练数据标签。 30 | with open(os.path.join(data_dir, label_file), 'r') as f: 31 | # 跳过文件头行(栏名称)。 32 | lines = f.readlines()[1:] 33 | tokens = [l.rstrip().split(',') for l in lines] 34 | idx_label = dict(((idx, label) for idx, label in tokens)) 35 | labels = set(idx_label.values()) 36 | 37 | num_train = len(os.listdir(os.path.join(data_dir, train_dir))) 38 | # 训练集中数量最少一类的狗的数量。 39 | min_num_train_per_label = ( 40 | Counter(idx_label.values()).most_common()[:-2:-1][0][1]) 41 | # 验证集中每类狗的数量。 42 | num_valid_per_label = math.floor(min_num_train_per_label * valid_ratio) 43 | label_count = dict() 44 | 45 | def mkdir_if_not_exist(path): 46 | if not os.path.exists(os.path.join(*path)): 47 | os.makedirs(os.path.join(*path)) 48 | 49 | # 整理训练和验证集。 50 | for train_file in os.listdir(os.path.join(data_dir, train_dir)): 51 | idx = train_file.split('.')[0] 52 | label = idx_label[idx] 53 | mkdir_if_not_exist([data_dir, input_dir, 'train_valid', label]) 54 | shutil.copy(os.path.join(data_dir, train_dir, train_file), 55 | os.path.join(data_dir, input_dir, 'train_valid', label)) 56 | if label not in label_count or label_count[label] < num_valid_per_label: 57 | mkdir_if_not_exist([data_dir, input_dir, 'valid', label]) 58 | shutil.copy(os.path.join(data_dir, train_dir, train_file), 59 | os.path.join(data_dir, input_dir, 'valid', label)) 60 | label_count[label] = label_count.get(label, 0) + 1 61 | else: 62 | mkdir_if_not_exist([data_dir, input_dir, 'train', label]) 63 | shutil.copy(os.path.join(data_dir, train_dir, train_file), 64 | os.path.join(data_dir, input_dir, 'train', label)) 65 | 66 | # 整理测试集。 67 | mkdir_if_not_exist([data_dir, input_dir, 'test', 'unknown']) 68 | for test_file in os.listdir(os.path.join(data_dir, test_dir)): 69 | shutil.copy(os.path.join(data_dir, test_dir, test_file), 70 | os.path.join(data_dir, input_dir, 'test', 'unknown')) 71 | 72 | 73 | def transform_train(data, label): 74 | im = image.imresize(data.astype('float32') / 255, 96, 96) 75 | auglist = image.CreateAugmenter(data_shape=(3, 96, 96), resize=0, 76 | rand_crop=False, rand_resize=False, rand_mirror=True, 77 | mean=None, std=None, 78 | brightness=0, contrast=0, 79 | saturation=0, hue=0, 80 | pca_noise=0, rand_gray=0, inter_method=2) 81 | for aug in auglist: 82 | im = aug(im) 83 | # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 84 | im = nd.transpose(im, (2,0,1)) 85 | return (im, nd.array([label]).asscalar().astype('float32')) 86 | 87 | def transform_test(data, label): 88 | im = image.imresize(data.astype('float32') / 255, 96, 96) 89 | im = nd.transpose(im, (2,0,1)) 90 | return (im, nd.array([label]).asscalar().astype('float32')) -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-DogBreed-gluon/readme.md: -------------------------------------------------------------------------------- 1 | [Dog Breed Identification](https://www.kaggle.com/c/dog-breed-identification) 2 | ## use gluon 3 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-DogBreed-gluon/visual_visdom.py: -------------------------------------------------------------------------------- 1 | #coding:utf8 2 | import visdom 3 | import numpy as np 4 | 5 | 6 | class Visualizer(): 7 | ''' 8 | 对可视化工具visdom的封装 9 | ''' 10 | def __init__(self, env, **kwargs): 11 | import visdom 12 | self.vis = visdom.Visdom(env=env, **kwargs) 13 | self.index = {} 14 | 15 | def plot_many(self, d): 16 | for k, v in d.iteritems(): 17 | self.plot(k, v) 18 | 19 | def plot(self, name, y): 20 | x = self.index.get(name, 0) 21 | self.vis.line(Y=np.array([y]), X=np.array([x]), 22 | win=unicode(name), 23 | opts=dict(title=name), 24 | update=None if x == 0 else 'append' 25 | ) 26 | self.index[name] = x + 1 27 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/gluon/Loss22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie kaggle-IsIceberg/gluon/Loss22.png -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/gluon/dataset_aug.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie kaggle-IsIceberg/gluon/dataset_aug.py -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/gluon/vggnet.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | from mxnet.gluon import nn 3 | from mxnet import nd 4 | from mxnet import init 5 | 6 | ## VGG 的模块 7 | ##kernel(3×3) 的卷积层然后接上一个池化层,之后再将这个模块重复多次 8 | 9 | class vgg_block(nn.HybridBlock): 10 | """docstring for vgg_block""" 11 | def __init__(self, num_convs,channels): 12 | super(vgg_block, self).__init__() 13 | self.num_convs = num_convs 14 | 15 | with self.name_scope(): 16 | self.conv1 = nn.Conv2D(channels=channels,kernel_size=3,padding=1,activation="relu") 17 | if num_convs == 2: 18 | self.conv2 = nn.Conv2D(channels=channels,kernel_size=3,padding=1,activation="relu") 19 | #self.pool = nn.MaxPool2D(pool_size=2,strides=2) 20 | self.maxpool = nn.MaxPool2D(pool_size=2, strides=2) 21 | 22 | def hybrid_forward(self,F,x): 23 | out = self.conv1(x) 24 | if self.num_convs == 2: 25 | out = self.conv2(out) 26 | out = self.maxpool(out) 27 | return out 28 | 29 | 30 | class VggNet(nn.HybridBlock): 31 | """docstring for VggNet""" 32 | def __init__(self, num_outputs,verbose=False): 33 | super(VggNet, self).__init__() 34 | self.verbose = verbose 35 | architecture = ((1,64), (1,128), (2,256), (2,512), (2,512)) 36 | with self.name_scope(): 37 | net = self.net = nn.HybridSequential() 38 | 39 | for (num_convs, channels) in architecture: 40 | net.add(vgg_block(num_convs, channels)) 41 | #net.add(nn.MaxPool2D(pool_size=2, strides=2)) 42 | 43 | net.add(nn.Flatten()) 44 | net.add(nn.Dense(1024,activation="relu")) 45 | net.add(nn.Dropout(.8)) 46 | net.add(nn.Dense(1024,activation="relu")) 47 | net.add(nn.Dropout(.8)) 48 | net.add(nn.Dense(num_outputs)) 49 | 50 | def hybrid_forward(self,F,x): 51 | out = x 52 | for i,b in enumerate(self.net): 53 | out = b(out) 54 | if self.verbose: 55 | print('Block %d output: %s'%(i+1, out.shape)) 56 | return out 57 | 58 | def vgg_net(ctx): 59 | num_outputs = 2 60 | net = VggNet(num_outputs) 61 | net.initialize(ctx=ctx, init=init.Xavier()) 62 | return net 63 | 64 | 65 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/pytorch/gene_csv.py: -------------------------------------------------------------------------------- 1 | # This Python 3 environment comes with many helpful analytics libraries installed 2 | # It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python 3 | # For example, here's several helpful packages to load in 4 | 5 | import numpy as np # linear algebra 6 | import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) 7 | 8 | # Input data files are available in the "../input/" directory. 9 | # For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory 10 | 11 | from subprocess import check_output 12 | print(check_output(["ls", "../input"]).decode("utf8")) 13 | 14 | # Any results you write to the current directory are saved as output. 15 | 16 | 17 | def ensemble(): 18 | stacked_1 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.166586_submission.csv') 19 | stacked_2 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.167542_submission.csv') 20 | stacked_3 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.167621_submission.csv') 21 | stacked_4 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.166586_submission.csv') 22 | stacked_5 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.168133_submission.csv') 23 | stacked_6 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.170522_submission.csv') 24 | stacked_7 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.180278_submission.csv') 25 | stacked_8 = pd.read_csv('./pth/kfold/' + 'ResNetLike_0.185876_submission.csv') 26 | sub = pd.DataFrame() 27 | sub['id'] = stacked_1['id'] 28 | sub['is_iceberg'] = np.exp(np.mean( 29 | [ 30 | stacked_1['is_iceberg'].apply(lambda x: np.log(x)), \ 31 | stacked_2['is_iceberg'].apply(lambda x: np.log(x)), \ 32 | stacked_3['is_iceberg'].apply(lambda x: np.log(x)), \ 33 | stacked_4['is_iceberg'].apply(lambda x: np.log(x)), \ 34 | stacked_5['is_iceberg'].apply(lambda x: np.log(x)), \ 35 | stacked_6['is_iceberg'].apply(lambda x: np.log(x)), \ 36 | stacked_7['is_iceberg'].apply(lambda x: np.log(x)), \ 37 | stacked_8['is_iceberg'].apply(lambda x: np.log(x)), \ 38 | ], axis=0)) 39 | sub.to_csv('./pth/kfold/' + 'ensamble.csv', index=False, float_format='%.6f') -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/pytorch/main.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | 3 | #sys module 4 | import os,json,time,fire,ipdb,tqdm 5 | import math 6 | import torch 7 | 8 | 9 | #user module 10 | from utils import opt,Visualizer 11 | import models 12 | from models import diff_net as net 13 | from Iceberg_dataset import readSuffleData,getTrainValLoaders,getCustomTrainValLoaders,fixSeed 14 | from iceberg_classfier import generateSingleModel,testModel 15 | 16 | 17 | use_cuda = torch.cuda.is_available() 18 | if use_cuda: 19 | num_workers = 0 20 | else: 21 | num_workers = 4 22 | 23 | 24 | 25 | vis = Visualizer(env=opt.env) 26 | 27 | 28 | def savePred(df_pred, val_score): 29 | csv_path = str(val_score) + '_sample_submission.csv' 30 | df_pred.to_csv(csv_path, columns=('id', 'is_iceberg'), index=None) 31 | print(csv_path) 32 | 33 | 34 | def train(**kwargs): 35 | # train model 36 | opt.parse(kwargs) 37 | 38 | batch_size,validationRatio = opt.batch_size,opt.validationRatio 39 | LR,epoch,global_seed = opt.LR,opt.epoch,opt.global_seed 40 | vis.vis.env = opt.env 41 | 42 | fixSeed(opt.global_seed) 43 | 44 | min_loss = 10 45 | 46 | model = models.SimpleNet() 47 | for i in range(epoch): 48 | print ("Ensamble number:" + str(i)) 49 | #model = models.ResNetLike(BasicBlock, [1, 3, 3, 1], num_channels=2, num_classes=1) 50 | data, full_img = readSuffleData(opt.global_seed,opt.BASE_FOLDER) 51 | train_loader, val_loader, train_ds, val_ds = getTrainValLoaders(data,full_img,batch_size,num_workers,validationRatio) 52 | #train_loader, val_loader, train_ds, val_ds = getCustomTrainValLoaders(data,full_img,batch_size,num_workers,validationRatio,global_seed) 53 | 54 | model, val_result,train_result= generateSingleModel(model,train_loader, val_loader, train_ds, val_ds,LR,opt.global_epoches) 55 | # if min_loss 国内网站可能在pip的时候有一些会下载不下来,需要自行下载,参考链接[issues185](https://github.com/facebookresearch/visdom/issues/185)下载文件到static下面 17 | 18 | ```bash 19 | nohup python -m visdom.server& 20 | ``` 21 | 22 | ### 3.训练样本并生成提交文件 23 | 24 | python -B main.py train (--model= model = "SimpleNet" --batch_size=256 -epoch=10) 25 | 在kaggle提交能够到0.2048 26 | 27 | 28 | ## jupyter notebook 29 | 运行 30 | pytorch_Iceberg_classifier.ipynb 31 | 32 | 33 | 34 | 主要内容: 35 | -- model diff_net.py 存放模型; 36 | -- Iceberg_dataset.py 数据前期处理 readSuffleData、getTrainValLoaders 37 | -- iceberg_classfier.py generateSingleModel模型训练、testModel给出预测 38 | 39 | ### 其他 40 | kaggle的discussion有提到通过对获得的多个csv文件进行不同ensemble,从而降低loss,能够完成0.14几,见essemble.ipynb(省略数据集) 41 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/pytorch/utils.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import numpy as np 3 | import visdom 4 | 5 | 6 | class Visualizer(): 7 | ''' 8 | 对可视化工具visdom的封装 9 | ''' 10 | def __init__(self, env, **kwargs): 11 | import visdom 12 | self.vis = visdom.Visdom(env=env, **kwargs) 13 | self.index = {} 14 | 15 | def plot_many(self, d): 16 | for k, v in d.iteritems(): 17 | self.plot(k, v) 18 | 19 | def plot(self, name, y): 20 | x = self.index.get(name, 0) 21 | self.vis.line(Y=np.array([y]), X=np.array([x]), 22 | win=unicode(name), 23 | opts=dict(title=name), 24 | update=None if x == 0 else 'append' 25 | ) 26 | self.index[name] = x + 1 27 | 28 | 29 | class Config: 30 | 31 | BASE_FOLDER = u'/media/yijie/文档/dataset/kaggle_Iceberg' 32 | batch_size = 128 33 | global_epoches = 55 34 | epoch = 50 35 | validationRatio = 0.11 36 | period = 20 37 | num_workers = 4 38 | LR = 0.0005 39 | MOMENTUM = 0.95 40 | global_seed=998 41 | env = 'Iceberg' # visdom env 42 | workers = 4 # 多线程加载数据 43 | model = "SimpleNet" 44 | result_path='submit.csv' #提交文件保存路径 45 | 46 | def parse(self,kwargs,print_=True): 47 | ''' 48 | 根据字典kwargs 更新 config参数 49 | ''' 50 | for k,v in kwargs.iteritems(): 51 | if not hasattr(self,k): 52 | raise Exception("opt has not attribute <%s>" %k) 53 | setattr(self,k,v) 54 | if print_: 55 | print('user config:') 56 | print('#################################') 57 | for k in dir(self): 58 | if not k.startswith('_') and k!='parse' and k!='state_dict': 59 | print k,getattr(self,k) 60 | print('#################################') 61 | return self 62 | 63 | 64 | def state_dict(self): 65 | return {k:getattr(self,k) for k in dir(self) if not k.startswith('_') and k!='parse' and k!='state_dict' } 66 | 67 | Config.parse = parse 68 | Config.state_dict = state_dict 69 | opt = Config() -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-IsIceberg/readme.md: -------------------------------------------------------------------------------- 1 | ## kaggle competion 2 | 3 | [Iceberg Classifier Challenge](https://www.kaggle.com/c/statoil-iceberg-classifier-challenge/data) 4 | 5 | > using satellite data to build a computer vision based surveillance system 6 | 7 | 8 | 9 | 10 | # Data fields 11 | 12 | [download here](https://www.kaggle.com/c/statoil-iceberg-classifier-challenge/data) 13 | 14 | ## train.json, test.json 15 | 16 | The data \(`train.json`,`test.json`\) is presented in`json`format. The files consist of a list of images, and for each image, you can find the following fields: 17 | 18 | * **id** 19 | * the id of the image 20 | * **band\_1, band\_2** 21 | * the 22 | [flattened](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.flatten.html) 23 | image data. Each band has 75x75 pixel values in the list, so the list has 5625 elements. Note that these values are not the normal non-negative integers in image files since they have physical meanings - these are 24 | **float** 25 | numbers with unit being 26 | [dB](https://en.wikipedia.org/wiki/Decibel) 27 | . Band 1 and Band 2 are signals characterized by radar backscatter produced from different polarizations at a particular incidence angle. The polarizations correspond to HH \(transmit/receive horizontally\) and HV \(transmit horizontally and receive vertically\). More background on the satellite imagery can be found 28 | [here](https://www.kaggle.com/c/statoil-iceberg-classifier-challenge#Background) 29 | . 30 | * **inc\_angle** 31 | * the incidence angle of which the image was taken. Note that this field has missing data marked as "na", and those images with "na" incidence angles are all in the training data to prevent leakage. 32 | * **is\_iceberg** 33 | * the target variable, set to 1 if it is an iceberg, and 0 if it is a ship. This field only exists in 34 | `train.json` 35 | . 36 | 37 | ![图片](../../img/kaggle_IsIceberg.PNG) 38 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-cifar10-gluon/Loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie kaggle-cifar10-gluon/Loss.png -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-cifar10-gluon/model.py: -------------------------------------------------------------------------------- 1 | from mxnet.gluon import nn 2 | from mxnet import nd 3 | from mxnet import init 4 | 5 | 6 | class Residual(nn.HybridBlock): 7 | def __init__(self, channels, same_shape=True, **kwargs): 8 | super(Residual, self).__init__(**kwargs) 9 | self.same_shape = same_shape 10 | with self.name_scope(): 11 | strides = 1 if same_shape else 2 12 | self.conv1 = nn.Conv2D(channels, kernel_size=3, padding=1, 13 | strides=strides) 14 | self.bn1 = nn.BatchNorm() 15 | self.conv2 = nn.Conv2D(channels, kernel_size=3, padding=1) 16 | self.bn2 = nn.BatchNorm() 17 | if not same_shape: 18 | self.conv3 = nn.Conv2D(channels, kernel_size=1, 19 | strides=strides) 20 | 21 | def hybrid_forward(self, F, x): 22 | out = F.relu(self.bn1(self.conv1(x))) 23 | out = self.bn2(self.conv2(out)) 24 | if not self.same_shape: 25 | x = self.conv3(x) 26 | return F.relu(out + x) 27 | 28 | 29 | class ResNet(nn.HybridBlock): 30 | def __init__(self, num_classes,drop_prob, verbose=False, **kwargs): 31 | super(ResNet, self).__init__(**kwargs) 32 | self.verbose = verbose 33 | with self.name_scope(): 34 | net = self.net = nn.HybridSequential() 35 | # block 1 36 | net.add(nn.Conv2D(channels=32, kernel_size=3, strides=1, padding=1)) 37 | net.add(nn.BatchNorm()) 38 | net.add(nn.Activation(activation='relu')) 39 | # block 2 40 | for _ in range(3): 41 | net.add(Residual(channels=32)) 42 | # block 3 43 | net.add(Residual(channels=64, same_shape=False)) 44 | for _ in range(2): 45 | net.add(Residual(channels=64)) 46 | # block 4 47 | net.add(Residual(channels=128, same_shape=False)) 48 | for _ in range(2): 49 | net.add(Residual(channels=128)) 50 | # block 5 51 | net.add(nn.AvgPool2D(pool_size=8)) 52 | net.add(nn.Flatten()) 53 | net.add(nn.Dropout(drop_prob)) 54 | net.add(nn.Dense(num_classes)) 55 | 56 | def hybrid_forward(self, F, x): 57 | out = x 58 | for i, b in enumerate(self.net): 59 | out = b(out) 60 | if self.verbose: 61 | print('Block %d output: %s'%(i+1, out.shape)) 62 | return out 63 | 64 | 65 | def get_net(ctx): 66 | num_outputs = 10 67 | drop_prob = 0.8 68 | net = ResNet(num_outputs,drop_prob) 69 | net.initialize(ctx=ctx,init=init.Xavier()) 70 | return net -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-cifar10-gluon/pre_deal.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import shutil 5 | from mxnet import autograd 6 | from mxnet import gluon 7 | from mxnet import image 8 | from mxnet import init 9 | from mxnet import nd 10 | from mxnet.gluon.data import vision 11 | import numpy as np 12 | 13 | # sorting the dataset 14 | def reorg_cifar10_data(data_dir, label_file, train_dir, test_dir, input_dir, valid_ratio): 15 | # 读取训练数据标签。 16 | with open(os.path.join(data_dir, label_file), 'r') as f: 17 | # 跳过文件头行(栏名称) 18 | lines = f.readlines()[1:] 19 | tokens = [l.rstrip().split(',') for l in lines] 20 | idx_label = dict(((int(idx), label) for idx, label in tokens)) 21 | labels = set(idx_label.values()) 22 | 23 | num_train = len(os.listdir(os.path.join(data_dir, train_dir))) 24 | num_train_tuning = int(num_train * (1 - valid_ratio)) 25 | assert 0 < num_train_tuning < num_train 26 | num_train_tuning_per_label = num_train_tuning // len(labels) 27 | label_count = dict() 28 | 29 | def mkdir_if_not_exist(path): 30 | if not os.path.exists(os.path.join(*path)): 31 | os.makedirs(os.path.join(*path)) 32 | 33 | # 整理训练和验证集。 34 | for train_file in os.listdir(os.path.join(data_dir, train_dir)): 35 | idx = int(train_file.split('.')[0]) 36 | label = idx_label[idx] 37 | mkdir_if_not_exist([data_dir, input_dir, 'train_valid', label]) 38 | shutil.copy(os.path.join(data_dir, train_dir, train_file), 39 | os.path.join(data_dir, input_dir, 'train_valid', label)) 40 | if label not in label_count or label_count[label] < num_train_tuning_per_label: 41 | mkdir_if_not_exist([data_dir, input_dir, 'train', label]) 42 | shutil.copy(os.path.join(data_dir, train_dir, train_file), 43 | os.path.join(data_dir, input_dir, 'train', label)) 44 | label_count[label] = label_count.get(label, 0) + 1 45 | else: 46 | mkdir_if_not_exist([data_dir, input_dir, 'valid', label]) 47 | shutil.copy(os.path.join(data_dir, train_dir, train_file), 48 | os.path.join(data_dir, input_dir, 'valid', label)) 49 | 50 | # 整理测试集。 51 | mkdir_if_not_exist([data_dir, input_dir, 'test', 'unknown']) 52 | for test_file in os.listdir(os.path.join(data_dir, test_dir)): 53 | shutil.copy(os.path.join(data_dir, test_dir, test_file), 54 | os.path.join(data_dir, input_dir, 'test', 'unknown')) 55 | 56 | 57 | def transform_train(data, label): 58 | im = data.astype('float32') / 255 59 | auglist = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, 60 | rand_crop=True, rand_resize=True, rand_mirror=True, 61 | mean=np.array([0.4914, 0.4822, 0.4465]), 62 | std=np.array([0.2023, 0.1994, 0.2010]), 63 | brightness=0, contrast=0, 64 | saturation=0, hue=0, 65 | pca_noise=0, rand_gray=0, inter_method=2) 66 | for aug in auglist: 67 | im = aug(im) 68 | # 将数据格式从"高*宽*通道"改为"通道*高*宽"。 69 | im = nd.transpose(im, (2,0,1)) 70 | return (im, nd.array([label]).asscalar().astype('float32')) 71 | 72 | # 测试时,无需对图像做标准化以外的增强数据处理。 73 | def transform_test(data, label): 74 | im = data.astype('float32') / 255 75 | auglist = image.CreateAugmenter(data_shape=(3, 32, 32), 76 | mean=np.array([0.4914, 0.4822, 0.4465]), 77 | std=np.array([0.2023, 0.1994, 0.2010])) 78 | for aug in auglist: 79 | im = aug(im) 80 | im = nd.transpose(im, (2,0,1)) 81 | return (im, nd.array([label]).asscalar().astype('float32')) 82 | 83 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie kaggle-cifar10-gluon/readme.md: -------------------------------------------------------------------------------- 1 | 2 | * from [zh.gluon.ai](http://zh.gluon.ai/chapter_computer-vision/kaggle-gluon-cifar10.html), 3 | you can follow the instructions to build the network 4 | 5 | ### notes: 6 | you can find enviroment.yml from [gloun-tutorials-zh](https://github.com/mli/gluon-tutorials-zh) and download to build your env. 7 | conda env create -f environment.yml 8 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.swp 3 | 4 | #folder 5 | */__pycache__ 6 | data -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/README.md: -------------------------------------------------------------------------------- 1 | Add:`Modify the code to make it run sucessfully on Windows10 (python3.5+,tenforflow,opencv3)` 2 | 3 | Origin From [Here](https://github.com/hizhangp/yolo_tensorflow) 4 | ## YOLO_tensorflow 5 | 6 | Tensorflow implementation of [YOLO](https://arxiv.org/pdf/1506.02640.pdf), including training and test phase. 7 | 8 | ### Installation 9 | 10 | 1. Clone yolo_tensorflow repository 11 | ```Shell 12 | $ git clone https://github.com/hizhangp/yolo_tensorflow.git 13 | $ cd yolo_tensorflow 14 | ``` 15 | 16 | 2. Download Pascal VOC dataset, and create correct directories 17 | ```Shell 18 | $ ./download_data.sh 19 | ``` 20 | 21 | 3. Download [YOLO_small](https://drive.google.com/file/d/0B5aC8pI-akZUNVFZMmhmcVRpbTA/view?usp=sharing) 22 | weight file and put it in `data/weight` 23 | 24 | 4. Modify configuration in `yolo/config.py` 25 | 26 | 5. Training 27 | ```Shell 28 | $ python train.py 29 | ``` 30 | 31 | 6. Test 32 | ```Shell 33 | $ python test.py 34 | ``` 35 | 36 | ### Requirements 37 | 1. Tensorflow 38 | 39 | 2. OpenCV 40 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/download_data.sh: -------------------------------------------------------------------------------- 1 | echo "Creating data directory..." 2 | mkdir -p data && cd data 3 | mkdir weights 4 | mkdir pascal_voc 5 | 6 | echo "Downloading Pascal VOC 2012 data..." 7 | wget http://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tar 8 | 9 | echo "Extracting VOC data..." 10 | tar xf VOCtrainval_06-Nov-2007.tar 11 | 12 | mv VOCdevkit pascal_voc/. 13 | 14 | echo "Done." 15 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/test/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie yolo-tensorflow/test/cat.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/test/person.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie yolo-tensorflow/test/person.jpg -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie yolo-tensorflow/utils/__init__.py -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/utils/timer.py: -------------------------------------------------------------------------------- 1 | import time, datetime 2 | 3 | class Timer(object): 4 | ''' 5 | A simple timer. 6 | ''' 7 | def __init__(self): 8 | self.init_time = time.time() 9 | self.total_time = 0. 10 | self.calls = 0 11 | self.start_time = 0. 12 | self.diff = 0. 13 | self.average_time = 0. 14 | self.remain_time = 0. 15 | 16 | def tic(self): 17 | # using time.time instead of time.clock because time time.clock 18 | # does not normalize for multithreading 19 | self.start_time = time.time() 20 | 21 | def toc(self, average=True): 22 | self.diff = time.time() - self.start_time 23 | self.total_time += self.diff 24 | self.calls += 1 25 | self.average_time = self.total_time / self.calls 26 | if average: 27 | return self.average_time 28 | else: 29 | return self.diff 30 | 31 | def remain(self, iters, max_iters): 32 | if iters == 0: 33 | self.remain_time = 0 34 | else: 35 | self.remain_time = (time.time() - self.init_time) * \ 36 | (max_iters - iters) / iters 37 | return str(datetime.timedelta(seconds=int(self.remain_time))) 38 | -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/yolo/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Computer-vision/@PeterChenYijie yolo-tensorflow/yolo/__init__.py -------------------------------------------------------------------------------- /Computer-vision/@PeterChenYijie yolo-tensorflow/yolo/config.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # 4 | # path and dataset parameter 5 | # 6 | 7 | DATA_PATH = 'data' 8 | 9 | PASCAL_PATH = os.path.join(DATA_PATH, 'pascal_voc') 10 | 11 | CACHE_PATH = os.path.join(PASCAL_PATH, 'cache') 12 | 13 | OUTPUT_DIR = os.path.join(PASCAL_PATH, 'output') 14 | 15 | WEIGHTS_DIR = os.path.join(PASCAL_PATH, 'weight') 16 | 17 | #WEIGHTS_FILE = None 18 | WEIGHTS_FILE = os.path.join(DATA_PATH, 'weights', 'YOLO_small.ckpt') 19 | 20 | CLASSES = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 21 | 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 22 | 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 23 | 'train', 'tvmonitor'] 24 | 25 | FLIPPED = True 26 | 27 | 28 | # 29 | # model parameter 30 | # 31 | 32 | IMAGE_SIZE = 448 33 | 34 | CELL_SIZE = 7 35 | 36 | BOXES_PER_CELL = 2 37 | 38 | ALPHA = 0.1 39 | 40 | DISP_CONSOLE = False 41 | 42 | OBJECT_SCALE = 1.0 43 | NOOBJECT_SCALE = 1.0 44 | CLASS_SCALE = 2.0 45 | COORD_SCALE = 5.0 46 | 47 | 48 | # 49 | # solver parameter 50 | # 51 | 52 | GPU = '' 53 | 54 | LEARNING_RATE = 0.0001 55 | 56 | DECAY_STEPS = 30000 57 | 58 | DECAY_RATE = 0.1 59 | 60 | STAIRCASE = True 61 | 62 | BATCH_SIZE = 4 63 | 64 | MAX_ITER = 15000 65 | 66 | SUMMARY_ITER = 10 67 | 68 | SAVE_ITER = 1000 69 | 70 | 71 | # 72 | # test parameter 73 | # 74 | 75 | THRESHOLD = 0.2 76 | 77 | IOU_THRESHOLD = 0.5 78 | -------------------------------------------------------------------------------- /Computer-vision/@chenyuntc GAN-PyTorch/README.md: -------------------------------------------------------------------------------- 1 | > Origin from [here](https://github.com/chenyuntc/pytorch-GAN) 2 | 3 | Log: 4 | * Fix it to make it on `Windows10`、`python36`、`PyTorch` correctly. 5 | * Later,I will do more work on GAN.Let us expect... 6 | 7 | 8 | # pytorch-GAN 9 | 10 | A minimal implementaion of DCGAN/WGAN in PyTorch with jupyter notebooks. 11 | This is the first program I write in PyTorch when I was learning PyTorch. 12 | 13 | NOTE, I've rewriten it to make it even simpler,updating... 14 | -------------------------------------------------------------------------------- /Computer-vision/@ypwhs kaggle-DogBreed-gluon/README.md: -------------------------------------------------------------------------------- 1 | *adding*: 2 | modify to make it run on Windows10 . 3 | 4 | 原始代码来自[这里](https://github.com/ypwhs/DogBreed_gluon),感谢 @ypwhs 5 | # 使用 gluon 实现狗品种分类 6 | 7 | 本项目是为了熟悉 gluon 操作的练手作,可以在测试集上获得0.28228的分数。 8 | 9 | 环境参考: 10 | 11 | * mxnet 0.12.1 12 | * numpy 1.13.3 13 | * tqdm 4.11.2 14 | * pandas 0.20.3 15 | * sklearn 0.19.0 16 | 17 | # 思路 18 | 19 | 复现步骤分为以下几步: 20 | 21 | * 下载并解压数据集:[https://www.kaggle.com/c/dog-breed-identification/data](https://www.kaggle.com/c/dog-breed-identification/data) 22 | * 运行 [preprocessing.ipynb](preprocessing.ipynb),构建 for_train 和 for_test 的文件夹结构,为 [ImageFolderDataset](https://mxnet.incubator.apache.org/api/python/gluon/data.html?highlight=imagefolderdataset#mxnet.gluon.data.vision.ImageFolderDataset) 做准备 23 | * 运行 [get_features.ipynb](get_features.ipynb),导出 resnet152_v1, densenet161 和 inception_v3 等模型输出的特征,参考[model_zoo](https://mxnet.incubator.apache.org/versions/master/api/python/gluon/model_zoo.html) 24 | * 运行 [model.ipynb](model.ipynb),拼接特征,训练一个2层的神经网络,然后在测试集上进行预测 25 | 26 | # Update 27 | 28 | 为了确定使用的预训练模型是否是最好的,我对所有预训练模型进行了迁移学习: 29 | 30 | * [get_features_v3.ipynb](get_features_v3.ipynb) 导出所有预训练模型输出的特征 31 | * [transfer_learning_all_pretrained_models.ipynb](transfer_learning_all_pretrained_models.ipynb) 对所有预训练模型分别进行迁移学习,按 val_loss 进行排序 32 | 33 | model | val_loss 34 | ----|---- 35 | inceptionv3 | 0.296050225385 36 | resnet152_v1 | 0.399359531701 37 | resnet101_v1 | 0.410383010283 38 | densenet161 | 0.418100789189 39 | densenet201 | 0.453403010964 40 | resnet50_v2 | 0.484435886145 41 | resnet50_v1 | 0.496179759502 42 | densenet169 | 0.512498702854 43 | resnet34_v2 | 0.536734519526 44 | vgg19_bn | 0.557294445112 45 | vgg16_bn | 0.586511127651 46 | resnet34_v1 | 0.591432901099 47 | densenet121 | 0.591716498137 48 | vgg19 | 0.619780953974 49 | vgg16 | 0.669267293066 50 | vgg13_bn | 0.702507363632 51 | vgg11_bn | 0.708396691829 52 | vgg13 | 0.756541173905 53 | resnet18_v2 | 0.761708110571 54 | vgg11 | 0.789955694228 55 | resnet18_v1 | 0.832537706941 56 | squeezenet1.1 | 1.6066500321 57 | squeezenet1.0 | 1.62178872526 58 | alexnet | 1.77026221156 59 | 60 | 可以看到 densenet 并没有那么好,于是我只使用 inceptionv3 和 resnet152_v1 的特征,进行了融合迁移学习,获得了 0.27143 的分数。 61 | 62 | # Update 63 | 64 | 使用 [Stanford Dogs Dataset](http://vision.stanford.edu/aditya86/ImageNetDogs/) 进行训练,使用了 inceptionv3 和 resnet152_v1,最终获得了 0.00398 的分数。 65 | 66 | * [get_features_v3.ipynb](get_features_v3.ipynb) 导出所有预训练模型输出的特征 67 | * [stanford.ipynb](stanford.ipynb) 导出 Stanford Dogs Dataset 对应的特征,然后进行迁移学习,最后在测试集上进行预测 68 | -------------------------------------------------------------------------------- /Computer-vision/readme.md: -------------------------------------------------------------------------------- 1 | 计算机视觉相关的论文 将在[这里](./../Note/Niubility/Computer-Version-Paper(updating).md)更新 -------------------------------------------------------------------------------- /Extra/@PeterChenyijie paddlepaddle/readme.md: -------------------------------------------------------------------------------- 1 | for the basic visuial -------------------------------------------------------------------------------- /Extra/TensorBoard-Official/README.md: -------------------------------------------------------------------------------- 1 | Origin from Here:[link](https://github.com/decentralion/tf-dev-summit-tensorboard-tutorial) 2 | 3 | # TensorBoard Dev Summit Tutorial 4 | 5 | This contains a runnable code companion to the TensorBoard tutorial at the 6 | 2017 TensorFlow Dev Summit. 7 | 8 | You can watch the tutorial [on YouTube](https://www.youtube.com/watch?v=eBbEDRsCmv4&t=1105s). 9 | 10 | Happy TensorBoarding! -------------------------------------------------------------------------------- /Extra/TensorBoard-Official/sprite_1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Extra/TensorBoard-Official/sprite_1024.png -------------------------------------------------------------------------------- /Extra/TensorBoard-simply-usage/readme.md: -------------------------------------------------------------------------------- 1 | TensorBoard 简单使用。 2 | -------------------------------------------------------------------------------- /Extra/TensorBoard-simply-usage/vis_tensorboard.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib as mp 3 | 4 | import matplotlib.pyplot as plt 5 | import tensorflow as tf 6 | import tensorflow.contrib.slim as slim 7 | from tensorflow.examples.tutorials.mnist import input_data 8 | import math 9 | import os 10 | import datetime 11 | 12 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 13 | 14 | tf.reset_default_graph() 15 | batchSize = 128 16 | learning_rate = 0.001 17 | OUTPUT_DIR = "tmp" 18 | logs_path = os.path.join( 19 | OUTPUT_DIR, datetime.datetime.now().strftime('%Y_%m_%d_%H_%M')) 20 | if not os.path.exists(logs_path): 21 | os.makedirs(logs_path) 22 | 23 | 24 | x = tf.placeholder(tf.float32, [None, 784],name="x-in") 25 | true_y = tf.placeholder(tf.float32, [None, 10],name="y-in") 26 | keep_prob = tf.placeholder("float") 27 | 28 | x_image = tf.reshape(x,[-1,28,28,1]) 29 | tf.summary.image('input', x_image, 3) 30 | 31 | 32 | 33 | hidden_1 = slim.conv2d(x_image,32,[3,3],scope="hidden_1") 34 | pool_1 = slim.max_pool2d(hidden_1,[2,2],scope="pool_1") 35 | 36 | hidden_2 = slim.conv2d(pool_1,64,[3,3],scope="hidden_2") 37 | pool_2 = slim.max_pool2d(hidden_2,[2,2],scope="pool_2") 38 | 39 | 40 | out_y = slim.fully_connected(slim.flatten(pool_2),10,activation_fn=tf.nn.softmax,scope="out_y") 41 | 42 | with tf.name_scope('Accuracy'): 43 | correct_prediction = tf.equal(tf.argmax(out_y,1), tf.argmax(true_y,1)) 44 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 45 | 46 | # specify cost function 47 | with tf.name_scope('cross_entropy'): 48 | cross_entropy = -tf.reduce_sum(true_y*tf.log(out_y)) 49 | 50 | train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy,name="Adam") 51 | 52 | 53 | # create a summary for our cost and accuracy 54 | tf.summary.scalar("cost", cross_entropy) 55 | tf.summary.scalar("accuracy", accuracy) 56 | 57 | # merge all summaries into a single "operation" which we can execute in a session 58 | summary_op = tf.summary.merge_all() 59 | saver = tf.train.Saver() 60 | 61 | with tf.Session() as sess: 62 | # variables need to be initialized before we can use them 63 | sess.run(tf.global_variables_initializer()) 64 | # create log writer object 65 | writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph()) 66 | 67 | for i in range(1001): 68 | batch = mnist.train.next_batch(batchSize) 69 | _,summary = sess.run([train_step,summary_op], feed_dict={x:batch[0],true_y:batch[1], keep_prob:0.5}) 70 | writer.add_summary(summary,i) 71 | if i % 100 == 0 and i != 0: 72 | trainAccuracy = sess.run(accuracy, feed_dict={x:batch[0],true_y:batch[1], keep_prob:1.0}) 73 | saver.save(sess,"tmp/my-model",i) 74 | print("step %d, training accuracy %g"%(i, trainAccuracy)) 75 | 76 | testAccuracy = sess.run(accuracy, feed_dict={x:mnist.test.images[:256],true_y:mnist.test.labels[:256], keep_prob:1.0}) 77 | print("test accuracy %g"%(testAccuracy)) 78 | 79 | 80 | Use = mnist.test.images[10] 81 | plt.imshow(np.reshape(Use,[28,28]), interpolation="nearest", cmap="gray") 82 | r = 10 83 | print("Prediction: ", sess.run( 84 | tf.argmax(out_y, 1), feed_dict={x: mnist.test.images[r:r + 1]})) 85 | 86 | print("Probs: ", sess.run( 87 | out_y, feed_dict={x: mnist.test.images[r:r + 1]})) 88 | -------------------------------------------------------------------------------- /Extra/readme.md: -------------------------------------------------------------------------------- 1 | 这里是一些其他相关。 2 | 需要时能够完成的。。 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 yijie chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Natural-language-processing/@PeterChenYijie rnn-scratch gluon/data/jaychou_lyrics.txt.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/Natural-language-processing/@PeterChenYijie rnn-scratch gluon/data/jaychou_lyrics.txt.zip -------------------------------------------------------------------------------- /Natural-language-processing/readme.md: -------------------------------------------------------------------------------- 1 | 一些NLP的项目。 2 | 依托项目来对整个NLP进行理解。基础知识请访问这里 3 | -------------------------------------------------------------------------------- /Note/AI_hardware_advanced_technology.md: -------------------------------------------------------------------------------- 1 | AI 硬件相关,了解AI芯片硬件架构以及设计。 2 | > 前言: AI 属于应用层。毋庸置疑的是,AI 硬件的提升给以 AI 发挥重要起到很大的帮助。因此,很有必要了解 AI 硬件这些年的 3 | 一些进展。 4 | 5 | 先收集一波链接再说。 6 | * [谢源:计算存储一体化,在存储里做深度学习,架构创新实现下一代AI芯片](https://mp.weixin.qq.com/s/gtgPYf939uYRzxAab_LZLQ) 7 | * [人工智能芯片:发展史、GPU、FPGA 和专用集成电路 ](http://www.sohu.com/a/194513449_465591) 8 | * [知乎专栏·AI 芯片产业生态梳理](https://zhuanlan.zhihu.com/p/31987113) 9 | * [一文看懂人工智能芯片的产业生态及竞争格局](https://www.leiphone.com/news/201709/uuJFzAxdoBY7bzEL.html) 10 | * [详细分析人工智能芯片 CPU/GPU/FPGA有何差异?](http://www.sohu.com/a/131606094_470053) 11 | * [人工智能芯片行业发展趋势与投资前景分析](http://www.qianjia.com/html/2017-12/18_281049.html) 12 | 13 | 14 | 分割线 15 | --- 16 | 下面为个人总结和学习内容: 17 | -------------------------------------------------------------------------------- /Note/DeepLearning.md: -------------------------------------------------------------------------------- 1 | ## 深度学习相关 2 | 3 | 深度学习路线图框架 4 | 1 深度学习历史和基础 5 | * 1.0 书籍 6 | * 1.1 调查 7 | * 1.2 深度信念网络(DBN)(深度学习里程碑) 8 | * 1.3 ImageNet 演变(深度学习突破点) 9 | * 1.4 语音识别的演变 10 | 11 | 2 深度学习 12 | * 2.1 模型 13 | * 2.2 优化 14 | * 2.3 无监督学习/深度生成模型 15 | * 2.4 循环神经网络 / 序列模型 16 | * 2.5 神经图灵机 17 | * 2.6 深度强化学习 18 | * 2.7 深度迁移学习 /终身学习 / 19 | * 2.8 一次学习(深度学习) 20 | 21 | 3 应用 22 | * 3.1 自然语言处理(NLP) 23 | * 3.2 物体检测 24 | * 3.3 视觉追踪 25 | * 3.4 看图说话 26 | * 3.5 机器翻译 27 | * 3.6 机器人技术 28 | * 3.7 艺术方面 29 | * 3.8 对象分割 30 | 31 | 32 | ### 1.流行的CNN框架 33 | * LetNet-5 [pdf][pdf-0.1] 34 | * AlexNet [pdf][pdf-0.2] 35 | * VGGNet [pdf][pdf-0.3] 36 | * GoogLeNet [pdf][pdf-0.4] 37 | * ResNet [pdf][pdf-0.5] 38 | 39 | 40 | 41 | ### 2.目标检测与物体识别 42 | * 物体检测: 43 | > 基于CNN检测器分为两类:一类,[SSD](https://arxiv.org/abs/1512.02325)、[YOLO](https://arxiv.org/abs/1506.02640)、[Retina- 44 | Net](https://arxiv.org/abs/1708.02002),另一类为,[Faster R-CNN](https://arxiv.org/abs/1506.01497)、[R-FCN](https://arxiv.org/abs/1605.06409) 45 | 和 [Mask-RCNN](https://arxiv.org/abs/1703.06870). 46 | 47 | 综述性文章: 48 | [1]. [卷积神经网络在图像分类和目标检测应用综述](https://drive.google.com/open?id=1BOSOFxRpTCF59OIUtZeLQh9bjZAUeyJr) 49 | 50 | 51 | * 迁移学习 52 | > [迁移学习资料汇总](https://github.com/jindongwang/transferlearning) 53 | 54 | 其他: 55 | > 这里放的是学习的resource 56 | 个人学习深度学习更新放在[这里](hhttps://github.com/PeterChenYijie/DeepLearningZeroToALL) 57 | focus在自己做项目以及学习上的一些学习 58 | 59 | 60 | 61 | --- 62 | [pdf-0.1]:http://yann.lecun.com/exdb/publis/pdf/lecun-89e.pdf 63 | [pdf-0.2]: http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf 64 | [pdf-0.3]: https://arxiv.org/pdf/1409.1556.pdf 65 | [pdf-0.4]: http://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Szegedy_Going_Deeper_With_2015_CVPR_paper.pdf 66 | [pdf-0.5]: https://arxiv.org/pdf/1512.03385.pdf -------------------------------------------------------------------------------- /Note/Driverless.md: -------------------------------------------------------------------------------- 1 | 自动驾驶 无人驾驶技术与报告 2 | 3 | 4 | * [收藏 | 厚势汽车2017文章目录:带您读懂汽车新科技](http://www.g.com.cn/tech/21940775/) 5 | * [AI 与自动驾驶汽车研究报告](https://pan.baidu.com/s/1zWiXHvXiDRVkH5so9gkYwQ) 6 | * [知乎专栏·自动驾驶技术综述论文简介](https://zhuanlan.zhihu.com/p/27490744) 7 | * [自动驾驶计算机视觉研究综述:难题、数据集与前沿成果](https://baijiahao.baidu.com/po/feed/share?context=%7B%22nid%22%3A%22news_2949034404307287551%22%2C%22sourceFrom%22%3A%22bjh%22%7D&fr=bjhauthor&type=news) -------------------------------------------------------------------------------- /Note/Mechine_Learning.md: -------------------------------------------------------------------------------- 1 | # 添加机器学习知识节点 2 | 机器学习 3 | 4 | 推荐书籍: 5 | * 周志华《机器学习》 6 | ![周志华书籍封面信息](../img/book_2.jpg) 7 | * 李航的《统计学习方法》 8 | ![李航书籍封面](../img/book_1.jpg) 9 | * Deep Learning英文版[[web]](http://www.deeplearningbook.org/) 10 | (中文版[[web]](https://github.com/exacity/deeplearningbook-chinese)下载请[点击](https://github.com/exacity/deeplearningbook-chinese/releases/download/v0.5-beta/dlbook_cn_v0.5-beta.pdf)) 11 | ![Deep Learning](../img/book_Deep%20Learning.jpg) 12 | * 机器学习实战 13 | ![machine in action](../img/book_Machine%20Learning%20in%20Action.jpg) 14 | 15 | 16 | > 机器学习算法python实现[参考](https://github.com/lawlite19/MachineLearning_Python) 17 | > 机器学习资源 |Machine learning Resources[参考](https://github.com/allmachinelearning/MachineLearning) 18 | 19 | 知识点(具体更新学习放在[这里](https://github.com/PeterChenYijie/MachineLearningZeroToALL)): 20 | 1.线性回归 21 | 2.逻辑回归 22 | 3.BP神经网络 23 | 4.SVM支持向量机 24 | 5.K-Means聚类算法 25 | 6.PCA主成分分析 26 | 27 | -------------------------------------------------------------------------------- /Note/Niubility/GitHub_Niubility.md: -------------------------------------------------------------------------------- 1 | 这里放 收集的一些 优秀的 `Github 仓库`: 2 | 3 | 4 | Face recognition 5 | MegaFace人脸识别冠军:[Github](https://github.com/deepinsight/insightface),框架:MXNet 6 | 7 | 8 | #### NLP 9 | [X].,**Nested LSTM Cell**(新型的多级记忆的 RNN 架构——嵌套 LSTM),[GitHub](https://github.com/hannw/nlstm),论文:[[pdf]](https://arxiv.org/abs/1801.10308) 10 | 11 | [X].**开放知识表示方法OKR**,[GitHub](https://github.com/vered1986/OKR),论文:[[pdf]](https://vered1986.github.io/papers/okr.pdf) 12 | 13 | [X].**deconvolutional text autoencoder**,[GitHub](https://github.com/dreasysnail/textCNN_public),论文:[[pdf]](https://arxiv.org/pdf/1708.04729.pdf) 14 | 15 | [X].**KBGAN: Adversarial Learning for Knowledge Graph Embeddings**,[GitHub](https://github.com/cai-lw/KBGAN) 16 | 17 | [X].**MobiRNN: Efficient Recurrent Neural Network Execution on Mobile GPU**,[GitHub](https://github.com/csarron/MobiRnn) 18 | 19 | 20 | #### Computer Version 21 | [X].`GAN`,**Spectral Normalization for Generative Adversarial Networks**,[GitHub](https://github.com/pfnet-research/sngan_projection) 22 | 23 | [X].`GAN`,**AmbientGAN: Generative models from lossy measurements**,[GitHub](https://github.com/shinseung428/ambientGAN_TF) 24 | 25 | [X].`Object Tracking`,**Parallel Tracking and Verifying**,[Link](http://www.dabi.temple.edu/~hbling/code/PTAV/ptav.htm) 26 | 27 | [X].`Scene Recognition`,**Depth CNNs for RGB-D scene recognition: learning from scratch better than transferring from RGB-CNNs**,[GitHub](https://github.com/songxinhang/D-CNN) 28 | 29 | [X].`Image Segment`,`U-Net`,**TernausNet: U-Net with VGG11 Encoder Pre-Trained on ImageNet for Image**,[GitHub](https://github.com/ternaus/TernausNet) 30 | 31 | 32 | -------------------------------------------------------------------------------- /Note/Niubility/recommend_paper.md: -------------------------------------------------------------------------------- 1 | ## Name 2 | **[X]** Authors. "**Title**." Press. (Year). 3 | [[pdf]](link) **(comment)** 4 | :star::star::star::star::star: 5 | 6 | 7 | --- 8 | **Ian Goodfellow 强推:GAN 日期:2018-03-01** 9 | ### GAN 10 | **[1]** Tero Karras,Timo Aila, Samuli Laine & Jaakko Lehtinen."**Progressive Growing of GANs for Improved Quality, Stability, and Variation**" 11 | [[pdf]](https://arxiv.org/pdf/1710.10196.pdf)**(迄今最真实的 GAN:英伟达渐进增大方式训练 GAN,生成前所未有高清图像 12 | [Github](https://github.com/tkarras/progressive_growing_of_gans))** 13 | :star::star::star::star::star: 14 | 15 | **[2]** Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida. "**Spectral Normalization for Generative Adversarial Networks**." .(2018). 16 | [[pdf]](https://openreview.net/pdf?id=B1QRgziT-) **(一个 GAN 生成 ImageNet 全部 1000 类物体)** 17 | :star::star::star::star::star: 18 | 19 | **[3]** Takeru Miyato, Masanori Koyama. "**cGANs with Projection Discriminator**." Press. (Year). 20 | [[pdf]](https://openreview.net/pdf?id=ByS1VpgRZ) **(cGANs [GitHub](https://github.com/pfnet-research/sngan_projection))** 21 | :star::star::star::star::star: 22 | 23 | **[4]** T-C Wang, M-Y Liu, J-Y Zhu. "**High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs**." Press. (2017). 24 | [[prj]](https://tcwang0509.github.io/pix2pixHD/) **(NVIDIA和UC Berkeley的研究 `Pix2pixHD`mark起来呐)** 25 | :star::star::star::star::star: 26 | 27 | **[5]** M Lucic, K Kurach, M Michalski, S Gelly, O Bousquet . "**Are GANs Created Equal? A Large-Scale Study**." Press. (2017). 28 | [[pdf]](https://arxiv.org/pdf/1711.10337.pdf) **(【谷歌大脑团队 GAN 生态权威报告】6 种优化 GAN 模型对比,最优秀的仍是原始版本)** 29 | :star::star::star::star::star: 30 | 31 | **[6]** Gulrajani, F Ahmed, M Arjovsky, V Dumoulin, A Courville . "**Improved Training of Wasserstein GANs**." Press. (Year). 32 | [[pdf]](https://arxiv.org/pdf/1704.00028.pdf) **(`WGAN`)** 33 | :star::star::star::star::star: 34 | 35 | **[7]** Han Zhang et. al. "**StackGAN++: Realistic Image Synthesis with Stacked Generative Adversarial Networks**." Press. (Year). 36 | [[pdf]](https://arxiv.org/pdf/1710.10916.pdf) **(文本-图像合成)** 37 | :star::star::star::star::star: 38 | 39 | **[8]** B K. Beaulieu-Jones, Z S Wu, C Williams, C S. Greene . "**Privacy-preserving generative deep neural networks support clinical data sharing**." Press. (2017). 40 | [[pdf]](https://www.biorxiv.org/content/early/2017/07/05/159756) **(comment)** 41 | :star::star::star::star::star: 42 | 43 | **[9]** L Mescheder, S Nowozin, A Geiger . "**Adversarial Variational Bayes: Unifying Variational Autoencoders and Generative Adversarial Networks**." Press. (2017). 44 | [[pdf]](https://arxiv.org/pdf/1701.04722.pdf) **(变分贝叶斯)** 45 | :star::star::star::star::star: 46 | 47 | **[10]** V Nagarajan, J. Z Kolter. "**Gradient descent GAN optimization is locally stable**." Press. (2017). 48 | [[pdf]](link) **(GAN的优化方式)** 49 | :star::star::star::star::star: 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Note/image_processing.md: -------------------------------------------------------------------------------- 1 | ## 添加图像处理 2 | 对于图像的理解,首推冈萨雷斯的《数字图像处理》 3 | 4 | CV 算法主要有 5 个步骤:`特征感知`、`图像预处理`、`特征提取`、`特征筛选`、`推理预测与识别` 5 | 6 | 对于想要了解图像处理,首先应当对图像的形成有一定的认知和了解。这里主要对的是相机成像内容, 7 | 其他例如`X射线成像`、`紫外,红外波段成像`、`声学成像`、`微波`、`无线电`等需要后期更新。 8 | 总结内容主要有: 9 | > * [图像成像原理] 10 | > * CMOS 和 CCD 的区别 11 | > * RGB 以及对应相关的转换 12 | > * 图像噪声产生的原因 13 | 14 | 对于图像处理需要更多在数学上的理解,而不是只是应用层。主要要理解: 15 | > * 像素的操作、通道分离和合并 16 | > * 直方图 17 | > * 膨胀、腐蚀、开运算、闭运算 18 | > * 基本滤波、Sobel 算子、拉普拉斯算子,Canny 算子 19 | > * 轮廓检测 20 | 21 | 补充 22 | **特征提取** 23 | 24 | 25 | 图像成像原理 26 | > 应当了解数字图像感知部件:物体设备(该设备对我们希望成像物体发射的能量很敏感)、数字化器件 27 | (将物理感知装置的输出转化为数字形式)。对于数字视频摄像机,其通过传感器(与光强成比例输出)得到模拟信号, 28 | 再通过数字转换器进行数字化格式输出,当然这种形式下得到的图像只是灰度图片。关于如何得到彩色图片呢以及目前数码照片 29 | 是怎么得到呢?这里需要两个概念:滤波片以及拜尔阵列。通俗的解释参见 30 | [通俗理解 相机成像:拜尔与上帝的对话](http://bbs.fengniao.com/forum/3055346.html) 31 | 32 | CMOS 和 CCD 33 | > [链接](http://www.elecfans.com/d/616475.html) 34 | 35 | > 目前相机的图像传感器主要是 `CMOS` 和 `CCD` ,或者说未来可能出现新的一种图像传感器?对于这两种图像传感器应当明白其原理以及 36 | 两者之间的优缺点。 37 | 38 | 39 | >简单而言,`CCD` 效果很好(成像质量高、灵敏度高、噪声低、响应速度快,图像即便小,无残像、像素集成度也高),但是成本太高; 40 | 相比, `CMOS` 读取信息简单,输出信息速率快,耗电小、体积小、重点轻,关键还是便宜。 41 | 42 | -------------------------------------------------------------------------------- /Note/readme.md: -------------------------------------------------------------------------------- 1 | > 这里是笔记的目录: 2 | 3 | * [图像处理](image_processing.md) 4 | 5 | * [机器学习](Mechine_Learning.md) 6 | * [深度学习 基本概念 先读](./../Basic-Tutorial/readme.md) 7 | * [深度学习术语](terminology.md) 8 | * [深度学习论文阅读路线图 ·中文版](Deep_Learning_Papers_Reading_Roadmap.md) 9 | * [深度学习 综述文章](review.md) 10 | * [深度学习笔记](DeepLearning.md) 11 | * [阅读论文 总结 这里](paper_read.md) 12 | * [盘点2017深度学习论文](Niubility/paper2017.md) 13 | * [计算机视觉相关论文整合](Niubility/Computer-Version-Paper(updating).md) 14 | * [AI 硬件技术发展](AI_hardware_advanced_technology.md) 15 | * [(已移动到这里)](https://github.com/PeterChenYijie/Weekly_clean_Wechat_favorites)~~每周清空微信收藏部分~~ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 动手学习 DeepLearning 2 | [![MIT license](https://img.shields.io/dub/l/vibe-d.svg)](https://github.com/PeterChenYijie/DeepLearningZeroToALL/blob/master/LICENSE) 3 | 4 | # 从0到1学习深度学习 5 | 对于初学者,我建议先参考这些github项目: 6 | * TensorFlow:[TensorFlow Basic Tutorial Labs](https://github.com/hunkim/DeepLearningZeroToAll) 7 | * MXNet:[gluon-tutorials-zh](https://github.com/mli/gluon-tutorials-zh) 8 | * PyTorch:[PyTorch-tutorial](https://github.com/yunjey/pytorch-tutorial)、 9 | [code-of-learn-deep-learning-with-pytorch](https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch) 10 | * PaddlePaddle:[Quick Start](http://www.paddlepaddle.org/docs/develop/documentation/zh/getstarted/quickstart_cn.html) 11 | 12 | 本工程建立用来记录个人深度学习的过程。当然,在整个学习过程中,将尝试用不同的深度学习框架来进行学习。以便提供更多学习范例。 13 | 目录大致分为: 14 | * 深度学习基础 15 | * 深度学习的初步学习教程 16 | * [TensorFlow](Basic-Tutorial/TensorFlow)  17 | * [MXNet](Basic-Tutorial/MXNet/gluon-basics) 18 | * [PyTorch](Basic-Tutorial/PyTorch/) 19 | * [PaddlePaddle](Basic-Tutorial/PaddlePaddle) 20 | * [笔记](Note) 21 | * [计算机视觉](Computer-vision) 22 | * [自然语言处理](Natural-language-processing) 23 | * [其他](Extra) 24 | 25 | 这个项目还是进行中,仍旧需要大伙共同协助完成。我们希望得到你的推送,这里我们有一些提醒: 26 | 27 | > * 详细描述使用框架、比赛内容,用户(如@Zhangsan cifar10-tensorflow) 28 | > * 不必放你自己的数据(创建你自己的一个连接分享就好)                    29 | > * 为更好确保你的工程能够为人所用,写一个 requirement.txt 和readme.md 30 | 31 | 最后,希望大家能够积极踊跃参与! 32 | 33 | 一些链接: 34 | * [吴恩达 Coursera 深度学习 代码](https://github.com/Kulbear/deep-learning-coursera)    35 | * [深度学习阅读路线图](https://github.com/songrotek/Deep-Learning-Papers-Reading-Roadmap) 36 | `论文pdf`:百度网盘[链接](https://pan.baidu.com/s/1ghgvowr) 37 | * [中文文案排版指北(简体中文版)](Copywriting%20Guidelines.md) 38 | * [专业术语 updating](Note/terminology.md) 39 | 40 | 41 | > *** [声明]此仓库是我开源到Github上的,可以遵守相关的开源协议进行使用。这个仓库中包含有很多研究者的论文、硕博士论文以及Github其他作者的开源仓库等都来源于在网上的下载,仅作为学术研究使用。如果作者或出版社有异议,请联系我进行删除。一切都是为了更好地学术!*** 42 | 43 | 44 | 45 | # Learn DeepLearning from Zero to One 46 | 47 | For newer,I strongly recommend to read the following GitHub project: 48 | * TensorFlow:[TensorFlow Basic Tutorial Labs](https://github.com/hunkim/DeepLearningZeroToAll) 49 | * MXNet:[gluon-tutorials-zh](https://github.com/mli/gluon-tutorials-zh) 50 | * PyTorch:[PyTorch-tutorial](https://github.com/yunjey/pytorch-tutorial)、 51 | [code-of-learn-deep-learning-with-pytorch](https://github.com/SherlockLiao/code-of-learn-deep-learning-with-pytorch) 52 | * PaddlePaddle:[Quick Start](http://www.paddlepaddle.org/docs/develop/documentation/zh/getstarted/quickstart_cn.html) 53 | 54 | 55 | 56 | 57 | This project is built to record personal DeepLearning process.Of course,during the process,I will try to use some 58 | different frame to learn for providing more remarkable examples. 59 | The Table will be listed on: 60 | * basic of DeepLearning 61 | * tutorial 62 | * [TensorFlow](Basic-Tutorial/TensorFlow)  63 | * [MXNet](Basic-Tutorial/MXNet/gluon-basics) 64 | * [PyTorch](Basic-Tutorial/PyTorch) 65 | * [PaddlePaddle](Basic-Tutorial/PaddlePaddle) 66 | * [note](Note) 67 | * [CV](Computer-vision) 68 | * [NLP](Natural-language-processing) 69 | * [extra](Extra) 70 | 71 | This project is still ongoing,I hope more guys can join it and push your State-Of-Art code.Before you push,I hope you can read those guides: 72 | > * describe frame,content,user(such @ZhangSan cifar-10-tensorflow) 73 | > * Not need to put your own data (just use links to share the data) 74 | > * Better to write requirement.txt and readme.md 75 | 76 | Some Links: 77 | * [Deep Learning Papers Reading Roadmap](https://github.com/songrotek/Deep-Learning-Papers-Reading-Roadmap) 78 | * [English Copywriting Guidelines](Copywriting%20Guidelines.en.md) 79 | * [Terminology updating](Note/terminology.md) 80 | -------------------------------------------------------------------------------- /img/20180306151440.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/20180306151440.png -------------------------------------------------------------------------------- /img/Adagrad formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/Adagrad formula.png -------------------------------------------------------------------------------- /img/Adam formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/Adam formula.png -------------------------------------------------------------------------------- /img/AlexNet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/AlexNet.png -------------------------------------------------------------------------------- /img/MegDet_Large Learning Policy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/MegDet_Large Learning Policy.png -------------------------------------------------------------------------------- /img/MegDet_multiDevice_BN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/MegDet_multiDevice_BN.png -------------------------------------------------------------------------------- /img/MegDet_sublinear_memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/MegDet_sublinear_memory.png -------------------------------------------------------------------------------- /img/Momentum formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/Momentum formula.png -------------------------------------------------------------------------------- /img/NAG formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/NAG formula.png -------------------------------------------------------------------------------- /img/RMSprop formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/RMSprop formula.png -------------------------------------------------------------------------------- /img/VGG16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/VGG16.png -------------------------------------------------------------------------------- /img/book_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/book_1.jpg -------------------------------------------------------------------------------- /img/book_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/book_2.jpg -------------------------------------------------------------------------------- /img/book_Deep Learning.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/book_Deep Learning.jpg -------------------------------------------------------------------------------- /img/book_Machine Learning in Action.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/book_Machine Learning in Action.jpg -------------------------------------------------------------------------------- /img/comp.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/comp.tif -------------------------------------------------------------------------------- /img/kaggle_IsIceberg.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/kaggle_IsIceberg.PNG -------------------------------------------------------------------------------- /img/paper_one.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DeepInDeeper/DeepLearningZeroToALL/8f629e326a84a4272e66f34ba5f918576a595c70/img/paper_one.png -------------------------------------------------------------------------------- /mxnet_environment.yml: -------------------------------------------------------------------------------- 1 | name: gluon 2 | dependencies: 3 | - python=3 4 | - jupyter 5 | - matplotlib 6 | - pandas 7 | - pip: 8 | - requests 9 | - mxnet>=1.0.1b20180123 10 | --------------------------------------------------------------------------------