├── .DS_Store ├── .gitignore ├── CONTRIBUTING.md ├── README.md ├── code_session_version ├── lab-02-1-Simple-Linear-Regression-session.ipynb ├── lab-03-1-Linear-Regression-and-Minimizing-Cost-session.ipynb ├── lab-04-1-Multi-variable-Linear-Regression-session.ipynb ├── lab-05-1-logistic_regression-session.ipynb ├── lab-05-2-logistic_regression_diabetes-session.ipynb ├── lab-06-1-softmax_classifier.ipynb ├── lab-06-2-softmax_zoo_classifier.ipynb ├── lab-06-2-softmax_zoo_classifier.py ├── lab-07-1-learning_rate_and_evaluation-session.ipynb ├── lab-09-1-XOR-session.ipynb ├── lab-09-2-XOR-NN-session.ipynb ├── lab-09-3-XOR-NN-Wide-Deep-session.ipynb ├── lab-09-4-XOR-tensorboard.ipynb ├── lab-10-1-mnist_nn_softmax.py ├── lab-10-2-mnist_nn_relu.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.py ├── lab-11-0-cnn-basics-session.ipynb ├── lab-11-1-mnist-cnn-keras-session.ipynb ├── lab-12-1-many-to-one-session.ipynb ├── lab-12-2-many-to-one-stacking-session.ipynb ├── lab-12-3-many-to-many-session.ipynb ├── lab-12-4-many-to-many-bidirectional-session.ipynb ├── lab-12-5-seq-to-seq-session.ipynb └── lab-12-6-seq-to-seq-with-attention-session.ipynb ├── data-01-test-score.csv ├── data-02-stock_daily.csv ├── data-03-diabetes.csv ├── data-04-zoo.csv ├── data_in └── ChatBotData.csv ├── docker ├── v1.12.0-cuda9.0.Dockerfile └── v1.12.0.Dockerfile ├── docker_user_guide.md ├── figs ├── clone.png ├── docker_ip.png ├── fork.png ├── fork_after.png ├── jupyter_allow_root.png ├── jupyter_token.png └── nothing ├── lab-01-basics.ipynb ├── lab-02-1-Simple-Linear-Regression-eager.ipynb ├── lab-03-1-Linear-Regression-and-Minimizing-Cost-eager.ipynb ├── lab-04-1-Multi-variable-Linear-Regression--Regression-eager.ipynb ├── lab-05-1-logistic_regression-eager.ipynb ├── lab-05-2-logistic_regression_diabetes-eager.ipynb ├── lab-06-1-softmax_classifier-eager.ipynb ├── lab-06-2-softmax_zoo_classifier-eager.ipynb ├── lab-07-1-learning_rate_and_evaluation-eager.ipynb ├── lab-07-2-linear_regression_without_min_max.ipynb ├── lab-07-3-linear_regression_eager (preprocess, decay, l2_loss).ipynb ├── lab-07-4-mnist_introduction.ipynb ├── lab-07-5-fashion-mnist_introduction.ipynb ├── lab-07-6-IMDB-introduction.ipynb ├── lab-09-1-XOR-eager.ipynb ├── lab-09-2-XOR-NN-eager.ipynb ├── lab-09-3-XOR-NN-Wide-Deep-eager.ipynb ├── lab-09-4-XOR-tensorboard-eager.ipynb ├── lab-10-1-1-mnist_nn_softmax.ipynb ├── lab-10-1-2-mnist_nn_relu.ipynb ├── lab-10-2-1-mnist_nn_xavier.ipynb ├── lab-10-2-2-mnist_nn_deep.ipynb ├── lab-10-3-mnist_nn_dropout.ipynb ├── lab-10-4-mnist_nn_batchnorm.ipynb ├── lab-11-0-cnn-basics-keras-eager.ipynb ├── lab-11-1-mnist-cnn-keras-sequential-eager.ipynb ├── lab-11-2-mnist-cnn-keras-functional-eager.ipynb ├── lab-11-3-mnist-cnn-keras-subclassing-eager.ipynb ├── lab-11-4-mnist-cnn-ensemble-keras-eager.ipynb ├── lab-11-5-mnist-cnn-best-keras-eager.ipynb ├── lab-12-0-rnn-basics-keras-eager.ipynb ├── lab-12-1-many-to-one-keras-eager.ipynb ├── lab-12-2-many-to-one-stacking-keras-eager.ipynb ├── lab-12-3-many-to-many-keras-eager.ipynb ├── lab-12-4-many-to-many-bidirectional-keras-eager.ipynb ├── lab-12-5-seq-to-seq-keras-eager.ipynb ├── lab-12-6-seq-to-seq-with-attention-keras-eager.ipynb ├── lab-12-7-bonus-seq-to-seq-with-attention-chatbot-keras-eager.ipynb ├── origin_source ├── 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 ├── others ├── lab-03-2-Linear-Regression-and-Minimizing-Cost-keras.ipynb ├── lab-09-4-XOR-tensorboard-keras.ipynb ├── lab-12-1-many-to-one-keras.ipynb ├── lab-12-2-many-to-one-stacking-keras.ipynb ├── lab-12-3-many-to-many-keras.ipynb └── lab-12-4-many-to-many-bidirectional-keras.ipynb ├── requirements.txt ├── run_jupyter_docker.sh └── tf_2.x ├── data-01-test-score.csv ├── data-02-stock_daily.csv ├── data-03-diabetes.csv ├── data-04-zoo.csv ├── data_in └── ChatBotData.csv ├── lab-02-1-Simple-Linear-Regression-eager.ipynb ├── lab-03-1-Linear-Regression-and-Minimizing-Cost-eager.ipynb ├── lab-04-1-Multi-variable-Linear-Regression--Regression-eager.ipynb ├── lab-05-1-logistic_regression-eager.ipynb ├── lab-05-2-logistic_regression_diabetes-eager.ipynb ├── lab-06-1-softmax_classifier-eager.ipynb ├── lab-06-2-softmax_zoo_classifier-eager.ipynb ├── lab-07-1-learning_rate_and_evaluation-eager.ipynb ├── lab-07-2-linear_regression_without_min_max.ipynb ├── lab-07-3-linear_regression_eager (preprocess, decay, l2_loss).ipynb ├── lab-07-4-mnist_introduction.ipynb ├── lab-07-5-fashion-mnist_introduction.ipynb ├── lab-07-6-IMDB-introduction.ipynb ├── lab-09-1-XOR-eager.ipynb ├── lab-09-2-XOR-NN-eager.ipynb ├── lab-09-3-XOR-NN-Wide-Deep-eager.ipynb ├── lab-09-4-XOR-tensorboard-eager.ipynb ├── lab-10-1-1-mnist_nn_softmax.ipynb ├── lab-10-1-2-mnist_nn_relu.ipynb ├── lab-10-2-1-mnist_nn_xavier.ipynb ├── lab-10-2-2-mnist_nn_deep.ipynb ├── lab-10-3-mnist_nn_dropout.ipynb ├── lab-10-4-mnist_nn_batchnorm.ipynb ├── lab-11-0-cnn-basics-keras-eager.ipynb ├── lab-11-1-mnist-cnn-keras-sequential-eager.ipynb ├── lab-11-2-mnist-cnn-keras-functional-eager.ipynb ├── lab-11-3-mnist-cnn-keras-subclassing-eager.ipynb ├── lab-11-4-mnist-cnn-ensemble-keras-eager.ipynb ├── lab-11-5-mnist-cnn-best-keras-eager.ipynb ├── lab-12-0-rnn-basics-keras-eager.ipynb ├── lab-12-1-many-to-one-keras-eager.ipynb ├── lab-12-2-many-to-one-stacking-keras-eager.ipynb ├── lab-12-3-many-to-many-keras-eager.ipynb ├── lab-12-4-many-to-many-bidirectional-keras-eager.ipynb ├── lab-12-5-seq-to-seq-keras-eager.ipynb ├── lab-12-6-seq-to-seq-with-attention-keras-eager.ipynb └── lab-12-7-bonus-seq-to-seq-with-attention-chatbot-keras-eager.ipynb /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ipython notebook checkpoints 2 | .ipynb_checkpoints 3 | 4 | data_out/ -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # ‘모두가 만드는 모두를 위한 딥러닝’ 참여 방법!! (Contribution) 2 | 3 | ## Precheck steps : 사전 확인 4 | 5 | * 작업을 시작하기 전에 먼저 이슈를 남겨 두세요. 왜냐면 6 | * 여러분이 무엇을 하고 있는지 사람들에게 알리는 데 도움이 됩니다. 7 | * 제안하는 문제가 이 Repo와 무관할 수 있습니다. 8 | * 그런 방식으로 코드를 유지하는 게 우리의 의도일 수도 있습니다. ([KISS](https://en.wikipedia.org/wiki/KISS_principle)) 9 | * 여러분은 Git을 어떻게 사용하는지 알아야합니다. 10 | * 그렇지 않다면, "Git 사용 방법"을 검색한 후, 무언가를 하기 전에 그것들을 읽어 보세요. 개발자로서 살아남기 위해서는 필수적인 기술입니다. 11 | * [Git tutorial](https://try.github.io/levels/1/challenges/1)을 참고하세요. 12 | 13 | ## Contribution guidelines 14 | 15 | 이 문서는 Contribution 프로세스를 안내합니다. 16 | 17 | ### Step 1: Fork 18 | 19 | Fork 버튼을 눌러 [GitHub](https://github.com/deeplearningzerotoall/TensorFlow.git)에 프로젝트를 Fork하세요. 이 단계는 작업을 시작할 수 있게 여러분의 계정에 복사하게 됩니다. 20 | 21 | ### Step 2: Local computer에 다운로드하세요 22 | 23 | ```bash 24 | $ git clone https://github.com/`YOUR_GITHUB_NAME`/TensorFlow.git 25 | $ cd TensorFlow 26 | ``` 27 | 28 | ### Step 3: Setup an upstream 29 | 30 | 변경 사항이 있을 경우, 쉽게 Pull할 수 있도록 이 Repo에 대한 링크를 설정해야 합니다. 31 | 32 | ```bash 33 | $ git remote add upstream https://github.com/deeplearningzerotoall/TensorFlow.git 34 | ``` 35 | 36 | 저장소에 업데이트가 있는 경우 로컬 복사본과 repository 를 업데이트할 수 있습니다. 37 | 38 | ```bash 39 | $ git pull upstream master && git push origin master 40 | ``` 41 | 42 | ### Step 4: Make a branch 43 | 44 | Master branch는 Pull Request들을 계속 병합되고 수정되기 때문에 Master branch를 직접 수정하지는 않는 게 좋습니다. 45 | 46 | 그리고 의미 있는 이름으로 Branch를 만드는 걸 잊지마세요! 47 | 48 | 49 | Example: 50 | ```bash 51 | $ git checkout -b hotfix/lab10 -t origin/master 52 | ``` 53 | 54 | 새로운 Branch를 만든 후에 자유롭게 코드를 수정하세요! 55 | 56 | **주의: 여러분 제안한 Issue와 관련이 없는 다른 것들을 고치고 마세요!** 57 | 58 | 만약에 다른 문제가 있다면, 따로 이슈를 제안하시길 바랍니다. 59 | 60 | ### Step 5: Commit 61 | 62 | 이메일/사용자 이름을 설정하세요. 63 | 64 | ```bash 65 | $ git config --global user.name "Sung Kim" 66 | $ git config --global user.email "sungkim@email.com" 67 | ``` 68 | 69 | 그리고 필요한 파일을 추가 후, Commit 하세요. 70 | ```bash 71 | $ git add my/changed/files 72 | $ git commit 73 | ``` 74 | 75 | Notes 76 | * 다른 사람들도 알아 볼 수 있게 명확한 Commit 메시지를 쓰세요! 77 | 78 | * 예시: 79 | ```text 80 | Short (50 chars or less) summary of changes 81 | 82 | More detailed explanatory text, if necessary. Wrap it to about 72 83 | characters or so. In some contexts, the first line is treated as the 84 | subject of an email and the rest of the text as the body. The blank 85 | line separating the summary from the body is critical (unless you omit 86 | the body entirely); tools like rebase can get confused if you run the 87 | two together. 88 | 89 | Further paragraphs come after blank lines. 90 | 91 | - Bullet points are okay, too 92 | 93 | - Typically a hyphen or asterisk is used for the bullet, preceded by a 94 | single space, with blank lines in between, but conventions vary here 95 | ``` 96 | 97 | ### Step 6: (Optional) Rebase your branch 98 | 99 | 수정이 평소보다 더 오래 걸려서, 여러분의 레포지토리는 뒤쳐진 옛날 버전일 가능성이 높습니다. 항상 레포지토리를 최신 버전으로 동기화하세요. 100 | ```bash 101 | $ git fetch upstream 102 | $ git rebase upstream/master 103 | ``` 104 | 105 | ### Step 7: Push 106 | 107 | 여러분의 repo를 push하기전에 ‘Autopep8’을 실행해주세요! 108 | 109 | E501(최대 문자 줄 제한)을 제외한 모든 PEP8형식을 따라주세요. 110 | 111 | **잊지마세요, 가독성이 최우선입니다!** 112 | 113 | * 예시: 114 | 115 | ```bash 116 | $ autopep8 . -r -i --ignore E501 117 | $ git push -u origin hotfix/lab10 118 | ``` 119 | 120 | 121 | ### Step 8: Creating the PR 122 | 이제 여러분의 브라우저와 repo를 열면 "compare & pull request."라는 초록색 버튼을 보실 수 있습니다. 123 | 124 | * 좋은 제목을 작성하세요. 125 | * 여러분이 수정한 파일 이름만 쓰지마시고 자세하게 설명해주세요. 126 | * **여러분이 했던 것과 여러분이 왜 했었는지를 설명해주세요.** 127 | * 관련된 issue번호도 추가해주세요. 128 | 129 | 축하합니다! 여러분의 PR은 Collaborator들에게 검토받을겁니다. 130 | 여러분의 PR이 CI Test도 통과했는지 체크하세요. 131 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 모두를 위한 딥러닝 시즌 2 : 모두가 만드는 모두를 위한 딥러닝 2 | 3 | 모두가 만드는 모두를 위한 딥러닝 시즌 2에 오신 여러분들 환영합니다. 4 | 5 | ## Getting Started 6 | 7 | 아래 링크에서 슬라이드와 영상을 통해 학습을 시작할 수 있습니다. 8 | 9 | * Slide: http://bit.ly/2LQMKvk 10 | * YouTube: http://bit.ly/2HHrybT 11 | 12 | ### Docker 사용자를 위한 안내 13 | 14 | 동일한 실습 환경을 위해 docker를 사용하실 분은 [docker_user_guide.md](docker_user_guide.md) 파일을 참고하세요! :) 15 | 16 | ### Install Requirements 17 | 18 | ```bash 19 | pip install -r requirements.txt 20 | ``` 21 | 22 | --- 23 | 24 | ## TensorFlow 25 | 26 | Deep Learning Zero to All - TensorFlow 27 | 28 | 모든 코드는 Tensorflow 1.12(stable)를 기반으로 작성했으며 Tensorflow 2.0이 출시되는 대로 추후 반영할 예정입니다. 29 | 30 | ## Standarad of Code 31 | 32 | 코드는 Tensorflow 공식 홈페이지 권장에 따라 Keras + Eager로 작성했으며 33 | 34 | Session 버전은 code_session_version / Keras 버전은 other에서 확인하실 수 있습니다. 35 | 36 | ## Contributions/Comments 37 | 38 | 언제나 여러분들의 참여를 환영합니다. Comments나 Pull requests를 남겨주세요. 39 | 40 | We always welcome your comments and pull requests. 41 | 42 | ## 목차 43 | 44 | ### PART 1: Basic Machine Learning 45 | 46 | * Lec 01: 기본적인 Machine Learning의 용어와 개념 설명 47 | * Lab 01: (추가 예정) 48 | * Lec 02: Simple Linear Regression 49 | * Lab 02: Simple Linear Regression를 TensorFlow로 구현하기 50 | * Lec 03: Linear Regression and How to minimize cost 51 | * Lab 03: Linear Regression and How to minimize cost를 TensorFlow로 구현하기 52 | * Lec 04: Multi-variable Linear Regression 53 | * Lab 04: Multi-variable Linear Regression를 TensorFlow로 구현하기 54 | * Lec 05-1: Logistic Regression/Classification의 소개 55 | * Lec 05-2: Logistic Regression/Classification의 cost 함수, 최소화 56 | * Lab 05-3: Logistic Regression/Classification를 TensorFlow로 구현하기 57 | * Lec 06-1: Softmax Regression: 기본 개념 소개 58 | * Lec 06-2: Softmax Classifier의 cost 함수 59 | * Lab 06-1: Softmax classifier를 TensorFlow로 구현하기 60 | * Lab 06-2: Fancy Softmax classifier를 TensorFlow로 구현하기 61 | * Lab 07-1: Application & Tips: 학습률(Learning Rate)과 데이터 전처리(Data Preprocessing) 62 | * Lab 07-2-1: Application & Tips: 오버피팅(Overfitting) & Solutions 63 | * Lab 07-2-2: Application & Tips: 학습률, 전처리, 오버피팅을 TensorFlow로 실습 64 | * Lab 07-3-1: Application & Tips: Data & Learning 65 | * Lab 07-3-2: Application & Tips: 다양한 Dataset으로 실습 66 | 67 | ### PART 2: Basic Deep Learning 68 | 69 | * Lec 08-1: 딥러닝의 기본 개념: 시작과 XOR 문제 70 | * Lec 08-2: 딥러닝의 기본 개념 2: Back-propagation 과 2006/2007 '딥'의 출현 71 | * Lec 09-1: XOR 문제 딥러닝으로 풀기 72 | * Lec 09-2: 딥넷트웍 학습 시키기 (backpropagation) 73 | * Lab 09-1: Neural Net for XOR 74 | * Lab 09-2: Tensorboard (Neural Net for XOR) 75 | * Lab 10-1: Sigmoid 보다 ReLU가 더 좋아 76 | * Lab 10-2: Weight 초기화 잘해보자 77 | * Lab 10-3: Dropout 78 | * Lab 10-4: Batch Normalization 79 | 80 | ### PART 3: Convolutional Neural Network 81 | 82 | * Lec 11-1: ConvNet의 Conv 레이어 만들기 83 | * Lec 11-2: ConvNet Max pooling 과 Full Network 84 | * Lec 11-3: ConvNet의 활용 예 85 | * Lab 11-0-1: CNN Basic: Convolution 86 | * Lab 11-0-2: CNN Basic: Pooling 87 | * Lab 11-1: mnist cnn keras sequential eager 88 | * Lab 11-2: mnist cnn keras functional eager 89 | * Lab-11-3: mnist cnn keras subclassing eager 90 | * Lab-11-4: mnist cnn keras ensemble eager 91 | * Lab-11-5: mnist cnn best keras eager 92 | 93 | ### PART 4: Recurrent Neural Network 94 | 95 | * Lec 12: NN의 꽃 RNN 이야기 96 | * Lab 12-0: rnn basics 97 | * Lab 12-1: many to one (word sentiment classification) 98 | * Lab 12-2: many to one stacked (sentence classification, stacked) 99 | * Lab 12-3: many to many (simple pos-tagger training) 100 | * Lab 12-4: many to many bidirectional (simpled pos-tagger training, bidirectional) 101 | * Lab 12-5: seq to seq (simple neural machine translation) 102 | * Lab 12-6: seq to seq with attention (simple neural machine translation, attention) 103 | -------------------------- 104 | 105 | ### 함께 만든 이들 106 | 107 | Main Instructor 108 | * Prof. Kim (https://github.com/hunkim) 109 | 110 | Main Creator 111 | * 김보섭 (https://github.com/aisolab) 112 | * 김수상 (https://github.com/healess) 113 | * 김준호 (https://github.com/taki0112) 114 | * 신성진 (https://github.com/aiscientist) 115 | * 이승준 (https://github.com/FinanceData) 116 | * 이진원 (https://github.com/jwlee-ml) 117 | 118 | Docker Developer 119 | * 오상준 (https://github.com/juneoh) 120 | 121 | Support 122 | * 네이버 커넥트재단 : 이효은, 장지수, 임우담 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /code_session_version/lab-02-1-Simple-Linear-Regression-session.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# 02 - Simple Liner Regression\n", 12 | "\n", 13 | "\n" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "slideshow": { 21 | "slide_type": "slide" 22 | } 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "import tensorflow as tf\n", 27 | "import numpy as np" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "collapsed": true, 34 | "slideshow": { 35 | "slide_type": "slide" 36 | } 37 | }, 38 | "source": [ 39 | "# Hypothesis and Cost\n", 40 | "### Hypothesis \n", 41 | "$$ H(x) = Wx + b $$\n", 42 | "\n", 43 | "\n", 44 | "### Cost\n", 45 | "$$ cost(W)=\\frac { 1 }{ m } \\sum _{i=1}^{m}{ { (W{ x }^{ i }-y^{ i } })^{ 2 } } $$\n", 46 | "\n", 47 | "* Which Hypothesis is better ?\n", 48 | "* cost function: 편차제곱의 평균\n", 49 | "* learning의 목표는 cost(W,b)를 최소화하는 (W,b)를 구하는 것\n" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "metadata": { 56 | "slideshow": { 57 | "slide_type": "slide" 58 | } 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "0 0.021165216 [0.9768667] [0.1905179]\n", 66 | "30 0.00094205124 [0.9643522] [0.08103587]\n", 67 | "60 0.00021878286 [0.9828208] [0.03905234]\n", 68 | "90 5.0810035e-05 [0.99172115] [0.01881986]\n", 69 | "120 1.1800398e-05 [0.9960103] [0.00906954]\n", 70 | "150 2.7407639e-06 [0.9980773] [0.00437076]\n", 71 | "180 6.3646576e-07 [0.99907345] [0.00210634]\n", 72 | "210 1.477923e-07 [0.9995535] [0.0010151]\n", 73 | "240 3.4324824e-08 [0.9997848] [0.00048916]\n", 74 | "270 7.965265e-09 [0.9998963] [0.00023569]\n", 75 | "300 1.8508265e-09 [0.99995] [0.00011358]\n", 76 | "330 4.2869885e-10 [0.9999759] [5.47589e-05]\n", 77 | "360 9.924861e-11 [0.9999884] [2.6355296e-05]\n", 78 | "390 2.3211063e-11 [0.9999944] [1.27257e-05]\n", 79 | "420 5.366966e-12 [0.9999973] [6.121506e-06]\n", 80 | "450 1.2505552e-12 [0.9999987] [2.9425917e-06]\n", 81 | "480 3.2684966e-13 [0.99999934] [1.4405549e-06]\n", 82 | "510 6.158037e-14 [0.99999964] [7.570888e-07]\n", 83 | "540 6.158037e-14 [0.99999976] [5.504595e-07]\n", 84 | "570 4.7369517e-15 [0.99999994] [1.9283151e-07]\n", 85 | "600 0.0 [1.] [5.772759e-08]\n", 86 | "630 0.0 [1.] [5.772759e-08]\n", 87 | "660 0.0 [1.] [5.772759e-08]\n", 88 | "690 0.0 [1.] [5.772759e-08]\n", 89 | "720 0.0 [1.] [5.772759e-08]\n", 90 | "750 0.0 [1.] [5.772759e-08]\n", 91 | "780 0.0 [1.] [5.772759e-08]\n", 92 | "810 0.0 [1.] [5.772759e-08]\n", 93 | "840 0.0 [1.] [5.772759e-08]\n", 94 | "870 0.0 [1.] [5.772759e-08]\n", 95 | "900 0.0 [1.] [5.772759e-08]\n", 96 | "930 0.0 [1.] [5.772759e-08]\n", 97 | "960 0.0 [1.] [5.772759e-08]\n", 98 | "990 0.0 [1.] [5.772759e-08]\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "import tensorflow as tf\n", 104 | "\n", 105 | "x_data = [1, 2, 3]\n", 106 | "y_data = [1, 2, 3]\n", 107 | "\n", 108 | "# (W=1, b=0 가 되어야 한다는 것은 미리 알고 있다) \n", 109 | "W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n", 110 | "b = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n", 111 | "\n", 112 | "hypothesis = W * x_data + b\n", 113 | "\n", 114 | "cost = tf.reduce_mean(tf.square(hypothesis - y_data))\n", 115 | "\n", 116 | "a = tf.Variable(0.1)\n", 117 | "optimizer = tf.train.GradientDescentOptimizer(a)\n", 118 | "train = optimizer.minimize(cost)\n", 119 | "\n", 120 | "sess = tf.Session()\n", 121 | "sess.run(tf.initializers.global_variables())\n", 122 | "\n", 123 | "for step in range(1000):\n", 124 | " sess.run(train)\n", 125 | " if step % 30 == 0:\n", 126 | " print(step, sess.run(cost), sess.run(W), sess.run(b))" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": { 132 | "slideshow": { 133 | "slide_type": "slide" 134 | } 135 | }, 136 | "source": [ 137 | "# placeholder" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 2, 143 | "metadata": { 144 | "slideshow": { 145 | "slide_type": "-" 146 | } 147 | }, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "0 0.23415472 [0.74881464] [0.94065374]\n", 154 | "30 0.025842478 [0.81329197] [0.4244312]\n", 155 | "60 0.006001683 [0.91002274] [0.20453936]\n", 156 | "90 0.0013938443 [0.9566387] [0.09857045]\n", 157 | "120 0.00032370738 [0.97910357] [0.04750248]\n", 158 | "150 7.5177835e-05 [0.9899297] [0.02289217]\n", 159 | "180 1.7459637e-05 [0.995147] [0.01103208]\n", 160 | "210 4.0546915e-06 [0.9976613] [0.00531649]\n", 161 | "240 9.417765e-07 [0.99887294] [0.00256211]\n", 162 | "270 2.1865871e-07 [0.9994569] [0.00123471]\n", 163 | "300 5.0772787e-08 [0.9997383] [0.00059503]\n", 164 | "330 1.1799879e-08 [0.9998739] [0.0002868]\n", 165 | "360 2.7365417e-09 [0.9999392] [0.00013821]\n", 166 | "390 6.3606365e-10 [0.99997073] [6.660059e-05]\n", 167 | "420 1.4789237e-10 [0.9999859] [3.210142e-05]\n", 168 | "450 3.4811858e-11 [0.9999932] [1.548364e-05]\n", 169 | "480 8.000711e-12 [0.9999967] [7.480722e-06]\n", 170 | "510 1.9184654e-12 [0.9999984] [3.5944997e-06]\n", 171 | "540 4.92643e-13 [0.9999992] [1.734835e-06]\n", 172 | "570 9.473903e-14 [0.9999996] [8.526869e-07]\n", 173 | "600 6.158037e-14 [0.9999997] [5.98374e-07]\n", 174 | "630 3.7895614e-14 [0.99999994] [2.7253517e-07]\n", 175 | "660 0.0 [1.] [5.7958374e-08]\n", 176 | "690 0.0 [1.] [5.7958374e-08]\n", 177 | "720 0.0 [1.] [5.7958374e-08]\n", 178 | "750 0.0 [1.] [5.7958374e-08]\n", 179 | "780 0.0 [1.] [5.7958374e-08]\n", 180 | "810 0.0 [1.] [5.7958374e-08]\n", 181 | "840 0.0 [1.] [5.7958374e-08]\n", 182 | "870 0.0 [1.] [5.7958374e-08]\n", 183 | "900 0.0 [1.] [5.7958374e-08]\n", 184 | "930 0.0 [1.] [5.7958374e-08]\n", 185 | "960 0.0 [1.] [5.7958374e-08]\n", 186 | "990 0.0 [1.] [5.7958374e-08]\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "import tensorflow as tf\n", 192 | "\n", 193 | "x_data = [1, 2, 3]\n", 194 | "y_data = [1, 2, 3]\n", 195 | "\n", 196 | "W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n", 197 | "b = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n", 198 | "\n", 199 | "X = tf.placeholder(tf.float32)\n", 200 | "Y = tf.placeholder(tf.float32)\n", 201 | "\n", 202 | "hypothesis = W * X + b\n", 203 | "cost = tf.reduce_mean(tf.square(hypothesis - Y))\n", 204 | "\n", 205 | "a = tf.Variable(0.1) \n", 206 | "optimizer = tf.train.GradientDescentOptimizer(a)\n", 207 | "train = optimizer.minimize(cost)\n", 208 | "\n", 209 | "sess = tf.Session()\n", 210 | "sess.run(tf.initializers.global_variables())\n", 211 | "\n", 212 | "for step in range(1000):\n", 213 | " sess.run(train, feed_dict={X: x_data, Y: y_data})\n", 214 | " if step % 30 == 0:\n", 215 | " print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W), sess.run(b))" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 3, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "[5.]\n", 228 | "[2.5]\n" 229 | ] 230 | } 231 | ], 232 | "source": [ 233 | "print(sess.run(hypothesis, feed_dict={X: 5}))\n", 234 | "print(sess.run(hypothesis, feed_dict={X: 2.5}))" 235 | ] 236 | } 237 | ], 238 | "metadata": { 239 | "celltoolbar": "Slideshow", 240 | "kernelspec": { 241 | "display_name": "Python 3", 242 | "language": "python", 243 | "name": "python3" 244 | }, 245 | "language_info": { 246 | "codemirror_mode": { 247 | "name": "ipython", 248 | "version": 3 249 | }, 250 | "file_extension": ".py", 251 | "mimetype": "text/x-python", 252 | "name": "python", 253 | "nbconvert_exporter": "python", 254 | "pygments_lexer": "ipython3", 255 | "version": "3.6.7" 256 | } 257 | }, 258 | "nbformat": 4, 259 | "nbformat_minor": 1 260 | } 261 | -------------------------------------------------------------------------------- /code_session_version/lab-05-2-logistic_regression_diabetes-session.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 05 Logistic Classification (diabetes) - Session\n", 8 | "* Logistic Classfication을 diabetes data를 활용하여 모델을 만들어 보도록 하겠습니다\n", 9 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "1.12.0\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import numpy as np\n", 27 | "import matplotlib.pyplot as plt\n", 28 | "%matplotlib inline\n", 29 | "import tensorflow as tf\n", 30 | "\n", 31 | "tf.set_random_seed(777) # for reproducibility\n", 32 | "\n", 33 | "print(tf.__version__)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "### 강의에 설명할 Data입니다" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": { 47 | "scrolled": true 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "(759, 8) (759, 1)\n", 55 | "[[-0.294118 0.487437 0.180328 ... -0.53117 -0.0333333 0. ]\n", 56 | " [-0.882353 -0.145729 0.0819672 ... -0.766866 -0.666667 1. ]\n", 57 | " [-0.0588235 0.839196 0.0491803 ... -0.492741 -0.633333 0. ]\n", 58 | " ...\n", 59 | " [-0.411765 0.21608 0.180328 ... -0.857387 -0.7 1. ]\n", 60 | " [-0.882353 0.266332 -0.0163934 ... -0.768574 -0.133333 0. ]\n", 61 | " [-0.882353 -0.0653266 0.147541 ... -0.797609 -0.933333 1. ]]\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)\n", 67 | "x_train = xy[:, 0:-1]\n", 68 | "y_train = xy[:, [-1]]\n", 69 | "\n", 70 | "print(x_train.shape, y_train.shape)\n", 71 | "print(xy)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "## Tensorflow Session\n", 79 | "### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", 80 | "* Tensorflow data API를 통해 학습시킬 값들을 담는다 (Batch Size는 한번에 학습시킬 Size로 정한다)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(len(x_train)).repeat()\n", 90 | "iter = dataset.make_initializable_iterator()\n", 91 | "features, labels = iter.get_next()" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", 99 | "* W와 b은 학습을 통해 생성되는 모델에 쓰이는 Wegith와 Bias (초기값을 variable : 0이나 Random값으로 가능 tf.random_normal([2, 1]) )" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 4, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "W = tf.Variable(tf.random_normal([8, 1]), name='weight')\n", 109 | "b = tf.Variable(tf.random_normal([1]), name='bias')" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "### Sigmoid 함수를 가설로 선언합니다\n", 117 | "* Sigmoid는 아래 그래프와 같이 0과 1의 값만을 리턴합니다 tf.sigmoid(tf.matmul(X, W) + b)와 같습니다\n", 118 | "\n", 119 | "$$\n", 120 | "\\begin{align}\n", 121 | "sigmoid(x) & = \\frac{1}{1+e^{-x}} \\\\\\\\\\\n", 122 | "\\end{align}\n", 123 | "$$\n", 124 | "\n", 125 | "![sigmoid](https://upload.wikimedia.org/wikipedia/commons/8/88/Logistic-curve.svg)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 5, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [ 134 | "hypothesis = tf.div(1., 1. + tf.exp(tf.matmul(features, W) + b))" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "### 가설을 검증할 Cost 함수를 정의합니다\n", 142 | "$$\n", 143 | "\\begin{align}\n", 144 | "cost(h(x),y) & = −log(h(x)) & if & y=1 \\\\\\\\\\\n", 145 | "cost(h(x),y) & = -log(1−h(x)) & if & y=0\n", 146 | "\\end{align}\n", 147 | "$$" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "* 위 두수식을 합치면 아래과 같습니다\n", 155 | "$$\n", 156 | "\\begin{align}\n", 157 | "cost(h(x),y) & = −y log(h(x))−(1−y)log(1−h(x))\n", 158 | "\\end{align}\n", 159 | "$$" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 6, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "cost = -tf.reduce_mean(labels * tf.log(hypothesis) + (1 - labels) * tf.log(1 - hypothesis))\n", 169 | "train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "### 추론한 값은 0.5를 기준(Sigmoid 그래프 참조)로 0과 1의 값을 리턴합니다.\n", 177 | "* Sigmoid 함수를 통해 예측값이 0.5보다 크면 1을 반환하고 0.5보다 작으면 0으로 반환합니다." 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 7, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [ 186 | "predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "### 가설을 통해 실재 값과 비교한 정확도를 측정합니다" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 8, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, labels), dtype=tf.float32))" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "### Tensorflow를 통한 실행을 위해 Session를 선언합니다.\n", 210 | "* 위의 Data를 Cost함수를 통해 학습시킨 후 모델을 생성합니다. " 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 9, 216 | "metadata": { 217 | "scrolled": true 218 | }, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "Iter: 0, Loss: 0.7398, Acc: 0.6495388746261597\n", 225 | "Iter: 1000, Loss: 0.5888, Acc: 0.6837944388389587\n", 226 | "Iter: 2000, Loss: 0.5506, Acc: 0.7048748135566711\n", 227 | "Iter: 3000, Loss: 0.5272, Acc: 0.7351778745651245\n", 228 | "Iter: 4000, Loss: 0.5122, Acc: 0.7536231875419617\n", 229 | "Iter: 5000, Loss: 0.5021, Acc: 0.7602108120918274\n", 230 | "Iter: 6000, Loss: 0.4951, Acc: 0.7654808759689331\n", 231 | "Iter: 7000, Loss: 0.4901, Acc: 0.7707509994506836\n", 232 | "Iter: 8000, Loss: 0.4864, Acc: 0.7747035622596741\n", 233 | "Iter: 9000, Loss: 0.4837, Acc: 0.7694334387779236\n", 234 | "Iter: 10000, Loss: 0.4816, Acc: 0.7667984366416931\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "EPOCHS = 10001\n", 240 | "\n", 241 | "with tf.Session() as sess:\n", 242 | " sess.run(tf.global_variables_initializer())\n", 243 | " for step in range(EPOCHS):\n", 244 | " sess.run(iter.initializer)\n", 245 | " _, loss_value, acc_value = sess.run([train, cost, accuracy])\n", 246 | " if step % 1000 == 0:\n", 247 | " print(\"Iter: {}, Loss: {:.4f}, Acc: {}\".format(step, loss_value, acc_value))" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | } 257 | ], 258 | "metadata": { 259 | "kernelspec": { 260 | "display_name": "Python 3", 261 | "language": "python", 262 | "name": "python3" 263 | }, 264 | "language_info": { 265 | "codemirror_mode": { 266 | "name": "ipython", 267 | "version": 3 268 | }, 269 | "file_extension": ".py", 270 | "mimetype": "text/x-python", 271 | "name": "python", 272 | "nbconvert_exporter": "python", 273 | "pygments_lexer": "ipython3", 274 | "version": "3.6.5" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 2 279 | } 280 | -------------------------------------------------------------------------------- /code_session_version/lab-06-1-softmax_classifier.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 06-1 Softmax Classification\n", 8 | "* Softmax를 사용하여 분류를 진행합니다.\n", 9 | "* 기존의 Version과변화가 없으므로 시즌1의 강의와 코드를 참조하시기 바랍니다.: [시즌1 lab-06-01 코드](https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-06-1-softmax_classifier.py)\n", 10 | "\n", 11 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": { 18 | "scrolled": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "import tensorflow as tf\n", 23 | "import numpy as np\n", 24 | "tf.set_random_seed(777) # for reproducibility" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 4, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "x_data = [[1, 2, 1, 1],\n", 34 | " [2, 1, 3, 2],\n", 35 | " [3, 1, 3, 4],\n", 36 | " [4, 1, 5, 5],\n", 37 | " [1, 7, 5, 5],\n", 38 | " [1, 2, 5, 6],\n", 39 | " [1, 6, 6, 6],\n", 40 | " [1, 7, 7, 7]]\n", 41 | "y_data = [[0, 0, 1],\n", 42 | " [0, 0, 1],\n", 43 | " [0, 0, 1],\n", 44 | " [0, 1, 0],\n", 45 | " [0, 1, 0],\n", 46 | " [0, 1, 0],\n", 47 | " [1, 0, 0],\n", 48 | " [1, 0, 0]]\n", 49 | "\n", 50 | "X = tf.placeholder(\"float\", [None, 4])\n", 51 | "Y = tf.placeholder(\"float\", [None, 3])\n", 52 | "nb_classes = 3" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 5, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "0 14.149523\n", 65 | "200 0.5816432\n", 66 | "400 0.46831578\n", 67 | "600 0.37114018\n", 68 | "800 0.27992386\n", 69 | "1000 0.23644295\n", 70 | "1200 0.2142207\n", 71 | "1400 0.19572866\n", 72 | "1600 0.18008918\n", 73 | "1800 0.16669035\n", 74 | "2000 0.15508719\n", 75 | "--------------\n", 76 | "[[1.0038184e-02 9.8995495e-01 6.8317431e-06]] [1]\n", 77 | "--------------\n", 78 | "[[0.8034606 0.18528557 0.01125388]] [0]\n", 79 | "--------------\n", 80 | "[[8.6216332e-09 3.0195317e-04 9.9969804e-01]] [2]\n", 81 | "--------------\n", 82 | "[[1.0038184e-02 9.8995495e-01 6.8317431e-06]\n", 83 | " [8.0346060e-01 1.8528555e-01 1.1253879e-02]\n", 84 | " [8.6216332e-09 3.0195317e-04 9.9969804e-01]] [1 0 2]\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight')\n", 90 | "b = tf.Variable(tf.random_normal([nb_classes]), name='bias')\n", 91 | "\n", 92 | "# tf.nn.softmax computes softmax activations\n", 93 | "# softmax = exp(logits) / reduce_sum(exp(logits), dim)\n", 94 | "hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)\n", 95 | "\n", 96 | "# Cross entropy cost/loss\n", 97 | "cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))\n", 98 | "\n", 99 | "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)\n", 100 | "\n", 101 | "# Launch graph\n", 102 | "with tf.Session() as sess:\n", 103 | " sess.run(tf.global_variables_initializer())\n", 104 | "\n", 105 | " for step in range(2001):\n", 106 | " _, cost_val = sess.run([optimizer, cost], feed_dict={X: x_data, Y: y_data})\n", 107 | "\n", 108 | " if step % 200 == 0:\n", 109 | " print(step, cost_val)\n", 110 | "\n", 111 | " print('--------------')\n", 112 | " # Testing & One-hot encoding\n", 113 | " a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})\n", 114 | " print(a, sess.run(tf.argmax(a, 1)))\n", 115 | "\n", 116 | " print('--------------')\n", 117 | " b = sess.run(hypothesis, feed_dict={X: [[1, 3, 4, 3]]})\n", 118 | " print(b, sess.run(tf.argmax(b, 1)))\n", 119 | "\n", 120 | " print('--------------')\n", 121 | " c = sess.run(hypothesis, feed_dict={X: [[1, 1, 0, 1]]})\n", 122 | " print(c, sess.run(tf.argmax(c, 1)))\n", 123 | "\n", 124 | " print('--------------')\n", 125 | " all = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]]})\n", 126 | " print(all, sess.run(tf.argmax(all, 1)))" 127 | ] 128 | } 129 | ], 130 | "metadata": { 131 | "kernelspec": { 132 | "display_name": "Python 3", 133 | "language": "python", 134 | "name": "python3" 135 | }, 136 | "language_info": { 137 | "codemirror_mode": { 138 | "name": "ipython", 139 | "version": 3 140 | }, 141 | "file_extension": ".py", 142 | "mimetype": "text/x-python", 143 | "name": "python", 144 | "nbconvert_exporter": "python", 145 | "pygments_lexer": "ipython3", 146 | "version": "3.6.5" 147 | } 148 | }, 149 | "nbformat": 4, 150 | "nbformat_minor": 2 151 | } 152 | -------------------------------------------------------------------------------- /code_session_version/lab-06-2-softmax_zoo_classifier.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 06 Softmax Zoo_classifier\n", 8 | "\n", 9 | "* Softmax를 사용하여 분류를 진행합니다.\n", 10 | "* 기존의 Version과변화가 없으므로 시즌1의 강의와 코드를 참조하시기 바랍니다.: [시즌1 lab-06-02 코드](https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-06-2-softmax_zoo_classifier.py)\n", 11 | "\n", 12 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": { 19 | "scrolled": true 20 | }, 21 | "outputs": [], 22 | "source": [ 23 | "# Lab 6 Softmax Classifier\n", 24 | "import tensorflow as tf\n", 25 | "import numpy as np\n", 26 | "tf.set_random_seed(777) # for reproducibility" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "(101, 16) (101, 1)\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "# Predicting animal type based on various features\n", 44 | "xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)\n", 45 | "x_data = xy[:, 0:-1]\n", 46 | "y_data = xy[:, [-1]]\n", 47 | "\n", 48 | "print(x_data.shape, y_data.shape)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 4, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "one_hot: Tensor(\"one_hot_1:0\", shape=(?, 1, 7), dtype=float32)\n", 61 | "reshape one_hot: Tensor(\"Reshape_1:0\", shape=(?, 7), dtype=float32)\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "nb_classes = 7 # 0 ~ 6\n", 67 | "\n", 68 | "X = tf.placeholder(tf.float32, [None, 16])\n", 69 | "Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6\n", 70 | "\n", 71 | "Y_one_hot = tf.one_hot(Y, nb_classes) # one hot\n", 72 | "print(\"one_hot:\", Y_one_hot)\n", 73 | "Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])\n", 74 | "print(\"reshape one_hot:\", Y_one_hot)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 6, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "Step: 0\tCost: 4.096\tAcc: 6.93%\n", 87 | "Step: 100\tCost: 0.825\tAcc: 84.16%\n", 88 | "Step: 200\tCost: 0.527\tAcc: 90.10%\n", 89 | "Step: 300\tCost: 0.396\tAcc: 91.09%\n", 90 | "Step: 400\tCost: 0.316\tAcc: 94.06%\n", 91 | "Step: 500\tCost: 0.262\tAcc: 95.05%\n", 92 | "Step: 600\tCost: 0.223\tAcc: 97.03%\n", 93 | "Step: 700\tCost: 0.192\tAcc: 97.03%\n", 94 | "Step: 800\tCost: 0.167\tAcc: 97.03%\n", 95 | "Step: 900\tCost: 0.146\tAcc: 97.03%\n", 96 | "Step: 1000\tCost: 0.130\tAcc: 97.03%\n", 97 | "Step: 1100\tCost: 0.116\tAcc: 97.03%\n", 98 | "Step: 1200\tCost: 0.104\tAcc: 99.01%\n", 99 | "Step: 1300\tCost: 0.095\tAcc: 100.00%\n", 100 | "Step: 1400\tCost: 0.087\tAcc: 100.00%\n", 101 | "Step: 1500\tCost: 0.081\tAcc: 100.00%\n", 102 | "Step: 1600\tCost: 0.075\tAcc: 100.00%\n", 103 | "Step: 1700\tCost: 0.070\tAcc: 100.00%\n", 104 | "Step: 1800\tCost: 0.066\tAcc: 100.00%\n", 105 | "Step: 1900\tCost: 0.062\tAcc: 100.00%\n", 106 | "Step: 2000\tCost: 0.058\tAcc: 100.00%\n", 107 | "[True] Prediction: 0 True Y: 0\n", 108 | "[True] Prediction: 0 True Y: 0\n", 109 | "[True] Prediction: 3 True Y: 3\n", 110 | "[True] Prediction: 0 True Y: 0\n", 111 | "[True] Prediction: 0 True Y: 0\n", 112 | "[True] Prediction: 0 True Y: 0\n", 113 | "[True] Prediction: 0 True Y: 0\n", 114 | "[True] Prediction: 3 True Y: 3\n", 115 | "[True] Prediction: 3 True Y: 3\n", 116 | "[True] Prediction: 0 True Y: 0\n", 117 | "[True] Prediction: 0 True Y: 0\n", 118 | "[True] Prediction: 1 True Y: 1\n", 119 | "[True] Prediction: 3 True Y: 3\n", 120 | "[True] Prediction: 6 True Y: 6\n", 121 | "[True] Prediction: 6 True Y: 6\n", 122 | "[True] Prediction: 6 True Y: 6\n", 123 | "[True] Prediction: 1 True Y: 1\n", 124 | "[True] Prediction: 0 True Y: 0\n", 125 | "[True] Prediction: 3 True Y: 3\n", 126 | "[True] Prediction: 0 True Y: 0\n", 127 | "[True] Prediction: 1 True Y: 1\n", 128 | "[True] Prediction: 1 True Y: 1\n", 129 | "[True] Prediction: 0 True Y: 0\n", 130 | "[True] Prediction: 1 True Y: 1\n", 131 | "[True] Prediction: 5 True Y: 5\n", 132 | "[True] Prediction: 4 True Y: 4\n", 133 | "[True] Prediction: 4 True Y: 4\n", 134 | "[True] Prediction: 0 True Y: 0\n", 135 | "[True] Prediction: 0 True Y: 0\n", 136 | "[True] Prediction: 0 True Y: 0\n", 137 | "[True] Prediction: 5 True Y: 5\n", 138 | "[True] Prediction: 0 True Y: 0\n", 139 | "[True] Prediction: 0 True Y: 0\n", 140 | "[True] Prediction: 1 True Y: 1\n", 141 | "[True] Prediction: 3 True Y: 3\n", 142 | "[True] Prediction: 0 True Y: 0\n", 143 | "[True] Prediction: 0 True Y: 0\n", 144 | "[True] Prediction: 1 True Y: 1\n", 145 | "[True] Prediction: 3 True Y: 3\n", 146 | "[True] Prediction: 5 True Y: 5\n", 147 | "[True] Prediction: 5 True Y: 5\n", 148 | "[True] Prediction: 1 True Y: 1\n", 149 | "[True] Prediction: 5 True Y: 5\n", 150 | "[True] Prediction: 1 True Y: 1\n", 151 | "[True] Prediction: 0 True Y: 0\n", 152 | "[True] Prediction: 0 True Y: 0\n", 153 | "[True] Prediction: 6 True Y: 6\n", 154 | "[True] Prediction: 0 True Y: 0\n", 155 | "[True] Prediction: 0 True Y: 0\n", 156 | "[True] Prediction: 0 True Y: 0\n", 157 | "[True] Prediction: 0 True Y: 0\n", 158 | "[True] Prediction: 5 True Y: 5\n", 159 | "[True] Prediction: 4 True Y: 4\n", 160 | "[True] Prediction: 6 True Y: 6\n", 161 | "[True] Prediction: 0 True Y: 0\n", 162 | "[True] Prediction: 0 True Y: 0\n", 163 | "[True] Prediction: 1 True Y: 1\n", 164 | "[True] Prediction: 1 True Y: 1\n", 165 | "[True] Prediction: 1 True Y: 1\n", 166 | "[True] Prediction: 1 True Y: 1\n", 167 | "[True] Prediction: 3 True Y: 3\n", 168 | "[True] Prediction: 3 True Y: 3\n", 169 | "[True] Prediction: 2 True Y: 2\n", 170 | "[True] Prediction: 0 True Y: 0\n", 171 | "[True] Prediction: 0 True Y: 0\n", 172 | "[True] Prediction: 0 True Y: 0\n", 173 | "[True] Prediction: 0 True Y: 0\n", 174 | "[True] Prediction: 0 True Y: 0\n", 175 | "[True] Prediction: 0 True Y: 0\n", 176 | "[True] Prediction: 0 True Y: 0\n", 177 | "[True] Prediction: 0 True Y: 0\n", 178 | "[True] Prediction: 1 True Y: 1\n", 179 | "[True] Prediction: 6 True Y: 6\n", 180 | "[True] Prediction: 3 True Y: 3\n", 181 | "[True] Prediction: 0 True Y: 0\n", 182 | "[True] Prediction: 0 True Y: 0\n", 183 | "[True] Prediction: 2 True Y: 2\n", 184 | "[True] Prediction: 6 True Y: 6\n", 185 | "[True] Prediction: 1 True Y: 1\n", 186 | "[True] Prediction: 1 True Y: 1\n", 187 | "[True] Prediction: 2 True Y: 2\n", 188 | "[True] Prediction: 6 True Y: 6\n", 189 | "[True] Prediction: 3 True Y: 3\n", 190 | "[True] Prediction: 1 True Y: 1\n", 191 | "[True] Prediction: 0 True Y: 0\n", 192 | "[True] Prediction: 6 True Y: 6\n", 193 | "[True] Prediction: 3 True Y: 3\n", 194 | "[True] Prediction: 1 True Y: 1\n", 195 | "[True] Prediction: 5 True Y: 5\n", 196 | "[True] Prediction: 4 True Y: 4\n", 197 | "[True] Prediction: 2 True Y: 2\n", 198 | "[True] Prediction: 2 True Y: 2\n", 199 | "[True] Prediction: 3 True Y: 3\n", 200 | "[True] Prediction: 0 True Y: 0\n", 201 | "[True] Prediction: 0 True Y: 0\n", 202 | "[True] Prediction: 1 True Y: 1\n", 203 | "[True] Prediction: 0 True Y: 0\n", 204 | "[True] Prediction: 5 True Y: 5\n", 205 | "[True] Prediction: 0 True Y: 0\n", 206 | "[True] Prediction: 6 True Y: 6\n", 207 | "[True] Prediction: 1 True Y: 1\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')\n", 213 | "b = tf.Variable(tf.random_normal([nb_classes]), name='bias')\n", 214 | "\n", 215 | "# tf.nn.softmax computes softmax activations\n", 216 | "# softmax = exp(logits) / reduce_sum(exp(logits), dim)\n", 217 | "logits = tf.matmul(X, W) + b\n", 218 | "hypothesis = tf.nn.softmax(logits)\n", 219 | "\n", 220 | "# Cross entropy cost/loss\n", 221 | "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,\n", 222 | " labels=tf.stop_gradient([Y_one_hot])))\n", 223 | "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)\n", 224 | "\n", 225 | "prediction = tf.argmax(hypothesis, 1)\n", 226 | "correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1))\n", 227 | "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 228 | "\n", 229 | "# Launch graph\n", 230 | "with tf.Session() as sess:\n", 231 | " sess.run(tf.global_variables_initializer())\n", 232 | "\n", 233 | " for step in range(2001):\n", 234 | " _, cost_val, acc_val = sess.run([optimizer, cost, accuracy], feed_dict={X: x_data, Y: y_data})\n", 235 | " \n", 236 | " if step % 100 == 0:\n", 237 | " print(\"Step: {:5}\\tCost: {:.3f}\\tAcc: {:.2%}\".format(step, cost_val, acc_val))\n", 238 | "\n", 239 | " # Let's see if we can predict\n", 240 | " pred = sess.run(prediction, feed_dict={X: x_data})\n", 241 | " # y_data: (N,1) = flatten => (N, ) matches pred.shape\n", 242 | " for p, y in zip(pred, y_data.flatten()):\n", 243 | " print(\"[{}] Prediction: {} True Y: {}\".format(p == int(y), p, int(y)))" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | } 253 | ], 254 | "metadata": { 255 | "kernelspec": { 256 | "display_name": "Python 3", 257 | "language": "python", 258 | "name": "python3" 259 | }, 260 | "language_info": { 261 | "codemirror_mode": { 262 | "name": "ipython", 263 | "version": 3 264 | }, 265 | "file_extension": ".py", 266 | "mimetype": "text/x-python", 267 | "name": "python", 268 | "nbconvert_exporter": "python", 269 | "pygments_lexer": "ipython3", 270 | "version": "3.6.5" 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 2 275 | } 276 | -------------------------------------------------------------------------------- /code_session_version/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_v2(logits=logits, 32 | labels=tf.stop_gradient([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 | -------------------------------------------------------------------------------- /code_session_version/lab-10-1-mnist_nn_softmax.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from tensorflow.keras.utils import to_categorical 5 | from tensorflow.keras.datasets import mnist 6 | from time import time 7 | import os 8 | 9 | def save(sess, saver, checkpoint_dir, model_name, step): 10 | 11 | if not os.path.exists(checkpoint_dir): 12 | os.makedirs(checkpoint_dir) 13 | 14 | saver.save(sess, os.path.join(checkpoint_dir, model_name + '.model'), global_step=step) 15 | 16 | 17 | def load(sess, saver, checkpoint_dir): 18 | print(" [*] Reading checkpoints...") 19 | 20 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 21 | if ckpt : 22 | ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 23 | saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) 24 | counter = int(ckpt_name.split('-')[-1]) 25 | print(" [*] Success to read {}".format(ckpt_name)) 26 | return True, counter 27 | else: 28 | print(" [*] Failed to find a checkpoint") 29 | return False, 0 30 | 31 | def normalize(X_train, X_test): 32 | X_train = X_train / 255.0 33 | X_test = X_test / 255.0 34 | 35 | return X_train, X_test 36 | 37 | def load_mnist() : 38 | (train_data, train_labels), (test_data, test_labels) = mnist.load_data() 39 | train_data = np.expand_dims(train_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 40 | test_data = np.expand_dims(test_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 41 | 42 | train_data, test_data = normalize(train_data, test_data) 43 | 44 | train_labels = to_categorical(train_labels, 10) # [N,] -> [N, 10] 45 | test_labels = to_categorical(test_labels, 10) # [N,] -> [N, 10] 46 | 47 | return train_data, train_labels, test_data, test_labels 48 | 49 | def classification_loss(logit, label) : 50 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit)) 51 | prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1)) 52 | accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) 53 | 54 | return loss, accuracy 55 | 56 | def network(x, reuse=False) : 57 | with tf.variable_scope('network', reuse=reuse) : 58 | x = tf.layers.flatten(x) # [N, 28, 28, 1] -> [N, 784] 59 | 60 | weight_init = tf.random_normal_initializer() 61 | 62 | # [N, 784] -> [N, 10] 63 | hypothesis = tf.layers.dense(inputs=x, units=10, use_bias=True, kernel_initializer=weight_init, name='fully_connected_logit') 64 | 65 | return hypothesis # hypothesis = logit 66 | 67 | 68 | """ dataset """ 69 | train_x, train_y, test_x, test_y = load_mnist() 70 | 71 | """ parameters """ 72 | learning_rate = 0.001 73 | batch_size = 128 74 | 75 | training_epochs = 1 76 | training_iterations = len(train_x) // batch_size 77 | 78 | img_size = 28 79 | c_dim = 1 80 | label_dim = 10 81 | 82 | train_flag = True 83 | 84 | """ Graph Input using Dataset API """ 85 | train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).\ 86 | shuffle(buffer_size=100000).\ 87 | prefetch(buffer_size=batch_size).\ 88 | batch(batch_size).\ 89 | repeat() 90 | 91 | test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y)).\ 92 | shuffle(buffer_size=100000).\ 93 | prefetch(buffer_size=len(test_x)).\ 94 | batch(len(test_x)).\ 95 | repeat() 96 | 97 | """ Model """ 98 | train_iterator = train_dataset.make_one_shot_iterator() 99 | test_iterator = test_dataset.make_one_shot_iterator() 100 | 101 | train_inputs, train_labels = train_iterator.get_next() 102 | test_inputs, test_labels = test_iterator.get_next() 103 | 104 | train_logits = network(train_inputs) 105 | test_logits = network(test_inputs, reuse=True) 106 | 107 | train_loss, train_accuracy = classification_loss(logit=train_logits, label=train_labels) 108 | _, test_accuracy = classification_loss(logit=test_logits, label=test_labels) 109 | 110 | """ Training """ 111 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(train_loss) 112 | 113 | """" Summary """ 114 | summary_train_loss = tf.summary.scalar("train_loss", train_loss) 115 | summary_train_accuracy = tf.summary.scalar("train_accuracy", train_accuracy) 116 | 117 | summary_test_accuracy = tf.summary.scalar("test_accuracy", test_accuracy) 118 | 119 | train_summary = tf.summary.merge([summary_train_loss, summary_train_accuracy]) 120 | test_summary = tf.summary.merge([summary_test_accuracy]) 121 | 122 | 123 | with tf.Session() as sess : 124 | tf.global_variables_initializer().run() 125 | start_time = time() 126 | 127 | saver = tf.train.Saver() 128 | checkpoint_dir = 'checkpoints' 129 | logs_dir = 'logs' 130 | 131 | model_dir = 'nn_softmax' 132 | model_name = 'dense' 133 | 134 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 135 | logs_dir = os.path.join(logs_dir, model_dir) 136 | 137 | 138 | if train_flag : 139 | writer = tf.summary.FileWriter(logs_dir, sess.graph) 140 | else : 141 | writer = None 142 | 143 | 144 | # restore check-point if it exits 145 | could_load, checkpoint_counter = load(sess, saver, checkpoint_dir) 146 | 147 | if could_load: 148 | start_epoch = (int)(checkpoint_counter / training_iterations) 149 | start_batch_index = checkpoint_counter - start_epoch * training_iterations 150 | counter = checkpoint_counter 151 | print(" [*] Load SUCCESS") 152 | else: 153 | start_epoch = 0 154 | start_batch_index = 0 155 | counter = 1 156 | print(" [!] Load failed...") 157 | 158 | if train_flag : 159 | """ Training phase """ 160 | for epoch in range(start_epoch, training_epochs) : 161 | for idx in range(start_batch_index, training_iterations) : 162 | 163 | # train 164 | _, summary_str, train_loss_val, train_accuracy_val = sess.run([optimizer, train_summary, train_loss, train_accuracy]) 165 | writer.add_summary(summary_str, counter) 166 | 167 | # test 168 | summary_str, test_accuracy_val = sess.run([test_summary, test_accuracy]) 169 | writer.add_summary(summary_str, counter) 170 | 171 | counter += 1 172 | print("Epoch: [%2d] [%5d/%5d] time: %4.4f, train_loss: %.8f, train_accuracy: %.2f, test_Accuracy: %.2f" \ 173 | % (epoch, idx, training_iterations, time() - start_time, train_loss_val, train_accuracy_val, test_accuracy_val)) 174 | 175 | start_batch_index = 0 176 | save(sess, saver, checkpoint_dir, model_name, counter) 177 | 178 | save(sess, saver, checkpoint_dir, model_name, counter) 179 | print('Learning Finished!') 180 | 181 | test_accuracy_val = sess.run(test_accuracy) 182 | print("Test accuracy: %.8f" % (test_accuracy_val)) 183 | 184 | else : 185 | """ Test phase """ 186 | test_accuracy_val = sess.run(test_accuracy) 187 | print("Test accuracy: %.8f" % (test_accuracy_val)) 188 | 189 | """ Get test image """ 190 | r = np.random.randint(low=0, high=len(test_x) - 1) 191 | print("Label: ", np.argmax(test_y[r: r+1], axis=-1)) 192 | print("Prediction: ", sess.run(tf.argmax(test_logits, axis=-1), feed_dict={test_inptus: test_x[r: r+1]})) 193 | 194 | plt.imshow(test_x[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') 195 | plt.show() 196 | -------------------------------------------------------------------------------- /code_session_version/lab-10-2-mnist_nn_relu.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from tensorflow.keras.utils import to_categorical 5 | from tensorflow.keras.datasets import mnist 6 | from time import time 7 | import os 8 | 9 | def save(sess, saver, checkpoint_dir, model_name, step): 10 | 11 | if not os.path.exists(checkpoint_dir): 12 | os.makedirs(checkpoint_dir) 13 | 14 | saver.save(sess, os.path.join(checkpoint_dir, model_name + '.model'), global_step=step) 15 | 16 | 17 | def load(sess, saver, checkpoint_dir): 18 | print(" [*] Reading checkpoints...") 19 | 20 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 21 | if ckpt : 22 | ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 23 | saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) 24 | counter = int(ckpt_name.split('-')[-1]) 25 | print(" [*] Success to read {}".format(ckpt_name)) 26 | return True, counter 27 | else: 28 | print(" [*] Failed to find a checkpoint") 29 | return False, 0 30 | 31 | def normalize(X_train, X_test): 32 | X_train = X_train / 255.0 33 | X_test = X_test / 255.0 34 | 35 | return X_train, X_test 36 | 37 | def load_mnist() : 38 | (train_data, train_labels), (test_data, test_labels) = mnist.load_data() 39 | train_data = np.expand_dims(train_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 40 | test_data = np.expand_dims(test_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 41 | 42 | train_data, test_data = normalize(train_data, test_data) 43 | 44 | train_labels = to_categorical(train_labels, 10) # [N,] -> [N, 10] 45 | test_labels = to_categorical(test_labels, 10) # [N,] -> [N, 10] 46 | 47 | return train_data, train_labels, test_data, test_labels 48 | 49 | def classification_loss(logit, label) : 50 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit)) 51 | prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1)) 52 | accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) 53 | 54 | return loss, accuracy 55 | 56 | def network(x, reuse=False) : 57 | with tf.variable_scope('network', reuse=reuse) : 58 | x = tf.layers.flatten(x) # [N, 28, 28, 1] -> [N, 784] 59 | 60 | weight_init = tf.random_normal_initializer() 61 | 62 | for i in range(2): 63 | # [N, 784] -> [N, 256] -> [N, 256] 64 | x = tf.layers.dense(inputs=x, units=256, use_bias=True, 65 | kernel_initializer=weight_init, 66 | name='fully_connected_' + str(i)) 67 | x = tf.nn.relu(x) 68 | 69 | # [N, 256] -> [N, 10] 70 | hypothesis = tf.layers.dense(inputs=x, units=10, use_bias=True, 71 | kernel_initializer=weight_init, 72 | name='fully_connected_logit') 73 | 74 | return hypothesis # hypothesis = logit 75 | 76 | 77 | """ dataset """ 78 | train_x, train_y, test_x, test_y = load_mnist() 79 | 80 | """ parameters """ 81 | learning_rate = 0.001 82 | batch_size = 128 83 | 84 | training_epochs = 1 85 | training_iterations = len(train_x) // batch_size 86 | 87 | img_size = 28 88 | c_dim = 1 89 | label_dim = 10 90 | 91 | train_flag = True 92 | 93 | """ Graph Input using Dataset API """ 94 | train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).\ 95 | shuffle(buffer_size=100000).\ 96 | prefetch(buffer_size=batch_size).\ 97 | batch(batch_size).\ 98 | repeat() 99 | 100 | test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y)).\ 101 | shuffle(buffer_size=100000).\ 102 | prefetch(buffer_size=len(test_x)).\ 103 | batch(len(test_x)).\ 104 | repeat() 105 | 106 | """ Model """ 107 | train_iterator = train_dataset.make_one_shot_iterator() 108 | test_iterator = test_dataset.make_one_shot_iterator() 109 | 110 | train_inputs, train_labels = train_iterator.get_next() 111 | test_inputs, test_labels = test_iterator.get_next() 112 | 113 | train_logits = network(train_inputs) 114 | test_logits = network(test_inputs, reuse=True) 115 | 116 | train_loss, train_accuracy = classification_loss(logit=train_logits, label=train_labels) 117 | _, test_accuracy = classification_loss(logit=test_logits, label=test_labels) 118 | 119 | """ Training """ 120 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(train_loss) 121 | 122 | """" Summary """ 123 | summary_train_loss = tf.summary.scalar("train_loss", train_loss) 124 | summary_train_accuracy = tf.summary.scalar("train_accuracy", train_accuracy) 125 | 126 | summary_test_accuracy = tf.summary.scalar("test_accuracy", test_accuracy) 127 | 128 | train_summary = tf.summary.merge([summary_train_loss, summary_train_accuracy]) 129 | test_summary = tf.summary.merge([summary_test_accuracy]) 130 | 131 | 132 | with tf.Session() as sess : 133 | tf.global_variables_initializer().run() 134 | start_time = time() 135 | 136 | saver = tf.train.Saver() 137 | checkpoint_dir = 'checkpoints' 138 | logs_dir = 'logs' 139 | 140 | model_dir = 'nn_relu' 141 | model_name = 'dense' 142 | 143 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 144 | logs_dir = os.path.join(logs_dir, model_dir) 145 | 146 | 147 | if train_flag : 148 | writer = tf.summary.FileWriter(logs_dir, sess.graph) 149 | else : 150 | writer = None 151 | 152 | # restore check-point if it exits 153 | could_load, checkpoint_counter = load(sess, saver, checkpoint_dir) 154 | 155 | if could_load: 156 | start_epoch = (int)(checkpoint_counter / training_iterations) 157 | start_batch_index = checkpoint_counter - start_epoch * training_iterations 158 | counter = checkpoint_counter 159 | print(" [*] Load SUCCESS") 160 | else: 161 | start_epoch = 0 162 | start_batch_index = 0 163 | counter = 1 164 | print(" [!] Load failed...") 165 | 166 | if train_flag : 167 | """ Training phase """ 168 | for epoch in range(start_epoch, training_epochs) : 169 | for idx in range(start_batch_index, training_iterations) : 170 | # train 171 | _, summary_str, train_loss_val, train_accuracy_val = sess.run([optimizer, train_summary, train_loss, train_accuracy]) 172 | writer.add_summary(summary_str, counter) 173 | 174 | # test 175 | summary_str, test_accuracy_val = sess.run([test_summary, test_accuracy]) 176 | writer.add_summary(summary_str, counter) 177 | 178 | counter += 1 179 | print("Epoch: [%2d] [%5d/%5d] time: %4.4f, train_loss: %.8f, train_accuracy: %.2f, test_Accuracy: %.2f" \ 180 | % (epoch, idx, training_iterations, time() - start_time, train_loss_val, train_accuracy_val, test_accuracy_val)) 181 | 182 | start_batch_index = 0 183 | save(sess, saver, checkpoint_dir, model_name, counter) 184 | 185 | save(sess, saver, checkpoint_dir, model_name, counter) 186 | print('Learning Finished!') 187 | 188 | test_accuracy_val = sess.run(test_accuracy) 189 | print("Test accuracy: %.8f" % (test_accuracy_val)) 190 | 191 | else : 192 | """ Test phase """ 193 | test_accuracy_val = sess.run(test_accuracy) 194 | print("Test accuracy: %.8f" % (test_accuracy_val)) 195 | 196 | """ Get test image """ 197 | r = np.random.randint(low=0, high=len(test_x) - 1) 198 | print("Label: ", np.argmax(test_y[r: r+1], axis=-1)) 199 | print("Prediction: ", sess.run(tf.argmax(test_logits, axis=-1), feed_dict={test_inputs: test_x[r: r+1]})) 200 | 201 | plt.imshow(test_x[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') 202 | plt.show() 203 | 204 | -------------------------------------------------------------------------------- /code_session_version/lab-10-3-mnist_nn_xavier.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.contrib as tf_contrib 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from tensorflow.keras.utils import to_categorical 6 | from tensorflow.keras.datasets import mnist 7 | from time import time 8 | import os 9 | 10 | def save(sess, saver, checkpoint_dir, model_name, step): 11 | 12 | if not os.path.exists(checkpoint_dir): 13 | os.makedirs(checkpoint_dir) 14 | 15 | saver.save(sess, os.path.join(checkpoint_dir, model_name + '.model'), global_step=step) 16 | 17 | 18 | def load(sess, saver, checkpoint_dir): 19 | print(" [*] Reading checkpoints...") 20 | 21 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 22 | if ckpt : 23 | ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 24 | saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) 25 | counter = int(ckpt_name.split('-')[-1]) 26 | print(" [*] Success to read {}".format(ckpt_name)) 27 | return True, counter 28 | else: 29 | print(" [*] Failed to find a checkpoint") 30 | return False, 0 31 | 32 | 33 | def normalize(X_train, X_test): 34 | X_train = X_train / 255.0 35 | X_test = X_test / 255.0 36 | 37 | return X_train, X_test 38 | 39 | def load_mnist() : 40 | (train_data, train_labels), (test_data, test_labels) = mnist.load_data() 41 | train_data = np.expand_dims(train_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 42 | test_data = np.expand_dims(test_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 43 | 44 | train_data, test_data = normalize(train_data, test_data) 45 | 46 | train_labels = to_categorical(train_labels, 10) # [N,] -> [N, 10] 47 | test_labels = to_categorical(test_labels, 10) # [N,] -> [N, 10] 48 | 49 | return train_data, train_labels, test_data, test_labels 50 | 51 | def classification_loss(logit, label) : 52 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit)) 53 | prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1)) 54 | accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) 55 | 56 | return loss, accuracy 57 | 58 | def network(x, reuse=False) : 59 | xavier = tf_contrib.layers.xavier_initializer() 60 | 61 | with tf.variable_scope('network', reuse=reuse) : 62 | x = tf.layers.flatten(x) # [N, 28, 28, 1] -> [N, 784] 63 | 64 | for i in range(2) : 65 | # [N, 784] -> [N, 256] -> [N, 256] 66 | x = tf.layers.dense(inputs=x, units=256, use_bias=True, kernel_initializer=xavier, name='fully_connected_' + str(i)) 67 | x = tf.nn.relu(x) 68 | 69 | # [N, 256] -> [N, 10] 70 | hypothesis = tf.layers.dense(inputs=x, units=10, use_bias=True, kernel_initializer=xavier, name='fully_connected_logit') 71 | 72 | return hypothesis # hypothesis = logit 73 | 74 | 75 | """ dataset """ 76 | train_x, train_y, test_x, test_y = load_mnist() 77 | 78 | """ parameters """ 79 | learning_rate = 0.001 80 | batch_size = 128 81 | 82 | training_epochs = 1 83 | training_iterations = len(train_x) // batch_size 84 | 85 | img_size = 28 86 | c_dim = 1 87 | label_dim = 10 88 | 89 | train_flag = True 90 | 91 | """ Graph Input using Dataset API """ 92 | train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).\ 93 | shuffle(buffer_size=100000).\ 94 | prefetch(buffer_size=batch_size).\ 95 | batch(batch_size).\ 96 | repeat() 97 | 98 | test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y)).\ 99 | shuffle(buffer_size=100000).\ 100 | prefetch(buffer_size=len(test_x)).\ 101 | batch(len(test_x)).\ 102 | repeat() 103 | 104 | """ Model """ 105 | train_iterator = train_dataset.make_one_shot_iterator() 106 | test_iterator = test_dataset.make_one_shot_iterator() 107 | 108 | train_inputs, train_labels = train_iterator.get_next() 109 | test_inputs, test_labels = test_iterator.get_next() 110 | 111 | train_logits = network(train_inputs) 112 | test_logits = network(test_inputs, reuse=True) 113 | 114 | train_loss, train_accuracy = classification_loss(logit=train_logits, label=train_labels) 115 | _, test_accuracy = classification_loss(logit=test_logits, label=test_labels) 116 | 117 | """ Training """ 118 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(train_loss) 119 | 120 | """" Summary """ 121 | summary_train_loss = tf.summary.scalar("train_loss", train_loss) 122 | summary_train_accuracy = tf.summary.scalar("train_accuracy", train_accuracy) 123 | 124 | summary_test_accuracy = tf.summary.scalar("test_accuracy", test_accuracy) 125 | 126 | train_summary = tf.summary.merge([summary_train_loss, summary_train_accuracy]) 127 | test_summary = tf.summary.merge([summary_test_accuracy]) 128 | 129 | 130 | with tf.Session() as sess : 131 | tf.global_variables_initializer().run() 132 | start_time = time() 133 | 134 | saver = tf.train.Saver() 135 | checkpoint_dir = 'checkpoints' 136 | logs_dir = 'logs' 137 | 138 | model_dir = 'nn_xavier' 139 | model_name = 'dense' 140 | 141 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 142 | logs_dir = os.path.join(logs_dir, model_dir) 143 | 144 | 145 | if train_flag : 146 | writer = tf.summary.FileWriter(logs_dir, sess.graph) 147 | else : 148 | writer = None 149 | 150 | # restore check-point if it exits 151 | could_load, checkpoint_counter = load(sess, saver, checkpoint_dir) 152 | 153 | if could_load: 154 | start_epoch = (int)(checkpoint_counter / training_iterations) 155 | start_batch_index = checkpoint_counter - start_epoch * training_iterations 156 | counter = checkpoint_counter 157 | print(" [*] Load SUCCESS") 158 | else: 159 | start_epoch = 0 160 | start_batch_index = 0 161 | counter = 1 162 | print(" [!] Load failed...") 163 | 164 | if train_flag : 165 | """ Training phase """ 166 | for epoch in range(start_epoch, training_epochs) : 167 | for idx in range(start_batch_index, training_iterations) : 168 | 169 | # train 170 | _, summary_str, train_loss_val, train_accuracy_val = sess.run([optimizer, train_summary, train_loss, train_accuracy]) 171 | writer.add_summary(summary_str, counter) 172 | 173 | # test 174 | summary_str, test_accuracy_val = sess.run([test_summary, test_accuracy]) 175 | writer.add_summary(summary_str, counter) 176 | 177 | counter += 1 178 | print("Epoch: [%2d] [%5d/%5d] time: %4.4f, train_loss: %.8f, train_accuracy: %.2f, test_Accuracy: %.2f" \ 179 | % (epoch, idx, training_iterations, time() - start_time, train_loss_val, train_accuracy_val, test_accuracy_val)) 180 | 181 | start_batch_index = 0 182 | save(sess, saver, checkpoint_dir, model_name, counter) 183 | 184 | save(sess, saver, checkpoint_dir, model_name, counter) 185 | print('Learning Finished!') 186 | 187 | test_accuracy_val = sess.run(test_accuracy) 188 | print("Test accuracy: %.8f" % (test_accuracy_val)) 189 | 190 | else : 191 | """ Test phase """ 192 | test_accuracy_val = sess.run(test_accuracy) 193 | print("Test accuracy: %.8f" % (test_accuracy_val)) 194 | 195 | """ Get test image """ 196 | r = np.random.randint(low=0, high=len(test_x) - 1) 197 | print("Label: ", np.argmax(test_y[r: r+1], axis=-1)) 198 | print("Prediction: ", sess.run(tf.argmax(test_logits, axis=-1), feed_dict={test_inputs: test_x[r: r+1]})) 199 | 200 | plt.imshow(test_x[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') 201 | plt.show() 202 | 203 | -------------------------------------------------------------------------------- /code_session_version/lab-10-4-mnist_nn_deep.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.contrib as tf_contrib 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from tensorflow.keras.utils import to_categorical 6 | from tensorflow.keras.datasets import mnist 7 | from time import time 8 | import os 9 | 10 | def save(sess, saver, checkpoint_dir, model_name, step): 11 | 12 | if not os.path.exists(checkpoint_dir): 13 | os.makedirs(checkpoint_dir) 14 | 15 | saver.save(sess, os.path.join(checkpoint_dir, model_name + '.model'), global_step=step) 16 | 17 | 18 | def load(sess, saver, checkpoint_dir): 19 | print(" [*] Reading checkpoints...") 20 | 21 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 22 | if ckpt : 23 | ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 24 | saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) 25 | counter = int(ckpt_name.split('-')[-1]) 26 | print(" [*] Success to read {}".format(ckpt_name)) 27 | return True, counter 28 | else: 29 | print(" [*] Failed to find a checkpoint") 30 | return False, 0 31 | 32 | def normalize(X_train, X_test): 33 | X_train = X_train / 255.0 34 | X_test = X_test / 255.0 35 | 36 | return X_train, X_test 37 | 38 | def load_mnist() : 39 | (train_data, train_labels), (test_data, test_labels) = mnist.load_data() 40 | train_data = np.expand_dims(train_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 41 | test_data = np.expand_dims(test_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 42 | 43 | train_data, test_data = normalize(train_data, test_data) 44 | 45 | train_labels = to_categorical(train_labels, 10) # [N,] -> [N, 10] 46 | test_labels = to_categorical(test_labels, 10) # [N,] -> [N, 10] 47 | 48 | return train_data, train_labels, test_data, test_labels 49 | 50 | def classification_loss(logit, label) : 51 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit)) 52 | prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1)) 53 | accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) 54 | 55 | return loss, accuracy 56 | 57 | def network(x, reuse=False) : 58 | xavier = tf_contrib.layers.xavier_initializer() 59 | 60 | with tf.variable_scope('network', reuse=reuse) : 61 | x = tf.layers.flatten(x) # [N, 28, 28, 1] -> [N, 784] 62 | 63 | for i in range(4) : 64 | # [N, 784] -> [N, 512] -> [N, 512] -> [N, 512] -> [N, 512] 65 | x = tf.layers.dense(inputs=x, units=512, use_bias=True, kernel_initializer=xavier, name='fully_connected_' + str(i)) 66 | x = tf.nn.relu(x) 67 | 68 | # [N, 512] -> [N, 10] 69 | hypothesis = tf.layers.dense(inputs=x, units=10, use_bias=True, kernel_initializer=xavier, name='fully_connected_logit') 70 | 71 | return hypothesis # hypothesis = logit 72 | 73 | 74 | """ dataset """ 75 | train_x, train_y, test_x, test_y = load_mnist() 76 | 77 | """ parameters """ 78 | learning_rate = 0.001 79 | batch_size = 128 80 | 81 | training_epochs = 1 82 | training_iterations = len(train_x) // batch_size 83 | 84 | img_size = 28 85 | c_dim = 1 86 | label_dim = 10 87 | 88 | train_flag = True 89 | 90 | """ Graph Input using Dataset API """ 91 | train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).\ 92 | shuffle(buffer_size=100000).\ 93 | prefetch(buffer_size=batch_size).\ 94 | batch(batch_size).\ 95 | repeat() 96 | 97 | test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y)).\ 98 | shuffle(buffer_size=100000).\ 99 | prefetch(buffer_size=len(test_x)).\ 100 | batch(len(test_x)).\ 101 | repeat() 102 | 103 | """ Model """ 104 | train_iterator = train_dataset.make_one_shot_iterator() 105 | test_iterator = test_dataset.make_one_shot_iterator() 106 | 107 | train_inputs, train_labels = train_iterator.get_next() 108 | test_inputs, test_labels = test_iterator.get_next() 109 | 110 | train_logits = network(train_inputs) 111 | test_logits = network(test_inputs, reuse=True) 112 | 113 | train_loss, train_accuracy = classification_loss(logit=train_logits, label=train_labels) 114 | _, test_accuracy = classification_loss(logit=test_logits, label=test_labels) 115 | 116 | """ Training """ 117 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(train_loss) 118 | 119 | """" Summary """ 120 | summary_train_loss = tf.summary.scalar("train_loss", train_loss) 121 | summary_train_accuracy = tf.summary.scalar("train_accuracy", train_accuracy) 122 | 123 | summary_test_accuracy = tf.summary.scalar("test_accuracy", test_accuracy) 124 | 125 | train_summary = tf.summary.merge([summary_train_loss, summary_train_accuracy]) 126 | test_summary = tf.summary.merge([summary_test_accuracy]) 127 | 128 | 129 | with tf.Session() as sess : 130 | tf.global_variables_initializer().run() 131 | start_time = time() 132 | 133 | saver = tf.train.Saver() 134 | checkpoint_dir = 'checkpoints' 135 | logs_dir = 'logs' 136 | 137 | model_dir = 'nn_deep' 138 | model_name = 'dense' 139 | 140 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 141 | logs_dir = os.path.join(logs_dir, model_dir) 142 | 143 | if train_flag : 144 | writer = tf.summary.FileWriter(logs_dir, sess.graph) 145 | else : 146 | writer = None 147 | 148 | # restore check-point if it exits 149 | could_load, checkpoint_counter = load(sess, saver, checkpoint_dir) 150 | 151 | if could_load: 152 | start_epoch = (int)(checkpoint_counter / training_iterations) 153 | start_batch_index = checkpoint_counter - start_epoch * training_iterations 154 | counter = checkpoint_counter 155 | print(" [*] Load SUCCESS") 156 | else: 157 | start_epoch = 0 158 | start_batch_index = 0 159 | counter = 1 160 | print(" [!] Load failed...") 161 | 162 | if train_flag : 163 | """ Training phase """ 164 | for epoch in range(start_epoch, training_epochs) : 165 | for idx in range(start_batch_index, training_iterations) : 166 | 167 | # train 168 | _, summary_str, train_loss_val, train_accuracy_val = sess.run( 169 | [optimizer, train_summary, train_loss, train_accuracy]) 170 | writer.add_summary(summary_str, counter) 171 | 172 | # test 173 | summary_str, test_accuracy_val = sess.run([test_summary, test_accuracy]) 174 | writer.add_summary(summary_str, counter) 175 | 176 | counter += 1 177 | print("Epoch: [%2d] [%5d/%5d] time: %4.4f, train_loss: %.8f, train_accuracy: %.2f, test_Accuracy: %.2f" \ 178 | % (epoch, idx, training_iterations, time() - start_time, train_loss_val, train_accuracy_val, 179 | test_accuracy_val)) 180 | 181 | start_batch_index = 0 182 | save(sess, saver, checkpoint_dir, model_name, counter) 183 | 184 | save(sess, saver, checkpoint_dir, model_name, counter) 185 | print('Learning Finished!') 186 | 187 | test_accuracy_val = sess.run(test_accuracy) 188 | print("Test accuracy: %.8f" % (test_accuracy_val)) 189 | 190 | else : 191 | """ Test phase """ 192 | 193 | test_accuracy_val = sess.run(test_accuracy) 194 | print("Test accuracy: %.8f" % (test_accuracy_val)) 195 | 196 | """ Get test image """ 197 | r = np.random.randint(low=0, high=len(test_x) - 1) 198 | print("Label: ", np.argmax(test_y[r: r+1], axis=-1)) 199 | print("Prediction: ", sess.run(tf.argmax(test_logits, axis=-1), feed_dict={test_inputs: test_x[r: r+1]})) 200 | 201 | plt.imshow(test_x[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') 202 | plt.show() 203 | -------------------------------------------------------------------------------- /code_session_version/lab-10-5-mnist_nn_dropout.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.contrib as tf_contrib 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from tensorflow.keras.utils import to_categorical 6 | from tensorflow.keras.datasets import mnist 7 | from time import time 8 | import os 9 | 10 | def save(sess, saver, checkpoint_dir, model_name, step): 11 | 12 | if not os.path.exists(checkpoint_dir): 13 | os.makedirs(checkpoint_dir) 14 | 15 | saver.save(sess, os.path.join(checkpoint_dir, model_name + '.model'), global_step=step) 16 | 17 | 18 | def load(sess, saver, checkpoint_dir): 19 | print(" [*] Reading checkpoints...") 20 | 21 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 22 | if ckpt : 23 | ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 24 | saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) 25 | counter = int(ckpt_name.split('-')[-1]) 26 | print(" [*] Success to read {}".format(ckpt_name)) 27 | return True, counter 28 | else: 29 | print(" [*] Failed to find a checkpoint") 30 | return False, 0 31 | 32 | def normalize(X_train, X_test): 33 | X_train = X_train / 255.0 34 | X_test = X_test / 255.0 35 | 36 | return X_train, X_test 37 | 38 | def load_mnist() : 39 | (train_data, train_labels), (test_data, test_labels) = mnist.load_data() 40 | train_data = np.expand_dims(train_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 41 | test_data = np.expand_dims(test_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 42 | 43 | train_data, test_data = normalize(train_data, test_data) 44 | 45 | train_labels = to_categorical(train_labels, 10) # [N,] -> [N, 10] 46 | test_labels = to_categorical(test_labels, 10) # [N,] -> [N, 10] 47 | 48 | return train_data, train_labels, test_data, test_labels 49 | 50 | def classification_loss(logit, label) : 51 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit)) 52 | prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1)) 53 | accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) 54 | 55 | return loss, accuracy 56 | 57 | def network(x, dropout_rate=0, reuse=False) : 58 | xavier = tf_contrib.layers.xavier_initializer() 59 | 60 | with tf.variable_scope('network', reuse=reuse) : 61 | x = tf.layers.flatten(x) # [N, 28, 28, 1] -> [N, 784] 62 | 63 | for i in range(4) : 64 | # [N, 784] -> [N, 512] -> [N, 512] -> [N, 512] -> [N, 512] 65 | x = tf.layers.dense(inputs=x, units=512, use_bias=True, kernel_initializer=xavier, name='fully_connected_' + str(i)) 66 | x = tf.nn.relu(x) 67 | x = tf.layers.dropout(inputs=x, rate=dropout_rate) 68 | 69 | # [N, 512] -> [N, 10] 70 | hypothesis = tf.layers.dense(inputs=x, units=10, use_bias=True, kernel_initializer=xavier, name='fully_connected_logit') 71 | 72 | return hypothesis # hypothesis = logit 73 | 74 | 75 | """ dataset """ 76 | train_x, train_y, test_x, test_y = load_mnist() 77 | 78 | """ parameters """ 79 | learning_rate = 0.001 80 | batch_size = 128 81 | 82 | training_epochs = 10 83 | training_iterations = len(train_x) // batch_size 84 | 85 | img_size = 28 86 | c_dim = 1 87 | label_dim = 10 88 | 89 | train_flag = True 90 | 91 | """ Graph Input using Dataset API """ 92 | train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).\ 93 | shuffle(buffer_size=100000).\ 94 | prefetch(buffer_size=batch_size).\ 95 | batch(batch_size).\ 96 | repeat() 97 | 98 | test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y)).\ 99 | shuffle(buffer_size=100000).\ 100 | prefetch(buffer_size=len(test_x)).\ 101 | batch(len(test_x)).\ 102 | repeat() 103 | 104 | """ Dropout rate""" 105 | dropout_rate = tf.placeholder(tf.float32, name='dropout_rate') 106 | 107 | """ Model """ 108 | train_iterator = train_dataset.make_one_shot_iterator() 109 | test_iterator = test_dataset.make_one_shot_iterator() 110 | 111 | train_inputs, train_labels = train_iterator.get_next() 112 | test_inputs, test_labels = test_iterator.get_next() 113 | 114 | train_logits = network(train_inputs) 115 | test_logits = network(test_inputs, reuse=True) 116 | 117 | train_loss, train_accuracy = classification_loss(logit=train_logits, label=train_labels) 118 | _, test_accuracy = classification_loss(logit=test_logits, label=test_labels) 119 | 120 | """ Training """ 121 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(train_loss) 122 | 123 | """" Summary """ 124 | summary_train_loss = tf.summary.scalar("train_loss", train_loss) 125 | summary_train_accuracy = tf.summary.scalar("train_accuracy", train_accuracy) 126 | 127 | summary_test_accuracy = tf.summary.scalar("test_accuracy", test_accuracy) 128 | 129 | train_summary = tf.summary.merge([summary_train_loss, summary_train_accuracy]) 130 | test_summary = tf.summary.merge([summary_test_accuracy]) 131 | 132 | 133 | with tf.Session() as sess : 134 | tf.global_variables_initializer().run() 135 | start_time = time() 136 | 137 | saver = tf.train.Saver() 138 | checkpoint_dir = 'checkpoints' 139 | logs_dir = 'logs' 140 | 141 | model_dir = 'nn_dropout' 142 | model_name = 'dense' 143 | 144 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 145 | logs_dir = os.path.join(logs_dir, model_dir) 146 | 147 | if train_flag : 148 | writer = tf.summary.FileWriter(logs_dir, sess.graph) 149 | else : 150 | writer = None 151 | 152 | # restore check-point if it exits 153 | could_load, checkpoint_counter = load(sess, saver, checkpoint_dir) 154 | 155 | if could_load: 156 | start_epoch = (int)(checkpoint_counter / training_iterations) 157 | start_batch_index = checkpoint_counter - start_epoch * training_iterations 158 | counter = checkpoint_counter 159 | print(" [*] Load SUCCESS") 160 | else: 161 | start_epoch = 0 162 | start_batch_index = 0 163 | counter = 1 164 | print(" [!] Load failed...") 165 | 166 | if train_flag : 167 | """ Training phase """ 168 | for epoch in range(start_epoch, training_epochs) : 169 | for idx in range(start_batch_index, training_iterations) : 170 | 171 | train_feed_dict = { 172 | dropout_rate: 0.3 173 | } 174 | 175 | test_feed_dict = { 176 | dropout_rate: 0.0 177 | } 178 | 179 | # train 180 | _, summary_str, train_loss_val, train_accuracy_val = sess.run([optimizer, train_summary, train_loss, train_accuracy], feed_dict=train_feed_dict) 181 | writer.add_summary(summary_str, counter) 182 | 183 | # test 184 | summary_str, test_accuracy_val = sess.run([test_summary, test_accuracy], feed_dict=test_feed_dict) 185 | writer.add_summary(summary_str, counter) 186 | 187 | counter += 1 188 | print("Epoch: [%2d] [%5d/%5d] time: %4.4f, train_loss: %.8f, train_accuracy: %.2f, test_Accuracy: %.2f" \ 189 | % (epoch, idx, training_iterations, time() - start_time, train_loss_val, train_accuracy_val, test_accuracy_val)) 190 | 191 | start_batch_index = 0 192 | save(sess, saver, checkpoint_dir, model_name, counter) 193 | 194 | save(sess, saver, checkpoint_dir, model_name, counter) 195 | print('Learning Finished!') 196 | 197 | test_feed_dict = { 198 | dropout_rate: 0.0 199 | } 200 | 201 | test_accuracy_val = sess.run(test_accuracy, feed_dict=test_feed_dict) 202 | print("Test accuracy: %.8f" % (test_accuracy_val)) 203 | 204 | else : 205 | """ Test phase """ 206 | test_feed_dict = { 207 | dropout_rate: 0.0 208 | } 209 | 210 | test_accuracy_val = sess.run(test_accuracy, feed_dict=test_feed_dict) 211 | print("Test accuracy: %.8f" % (test_accuracy_val)) 212 | 213 | """ Get test image """ 214 | r = np.random.randint(low=0, high=len(test_x) - 1) 215 | print("Label: ", np.argmax(test_y[r: r+1], axis=-1)) 216 | print("Prediction: ", sess.run(tf.argmax(test_logits, axis=-1), feed_dict={test_inputs: test_x[r: r+1]})) 217 | 218 | plt.imshow(test_x[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') 219 | plt.show() 220 | -------------------------------------------------------------------------------- /code_session_version/lab-10-6-mnist_nn_batchnorm.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.contrib as tf_contrib 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from tensorflow.keras.utils import to_categorical 6 | from tensorflow.keras.datasets import mnist 7 | from time import time 8 | import os 9 | 10 | 11 | def save(sess, saver, checkpoint_dir, model_name, step): 12 | 13 | if not os.path.exists(checkpoint_dir): 14 | os.makedirs(checkpoint_dir) 15 | 16 | saver.save(sess, os.path.join(checkpoint_dir, model_name + '.model'), global_step=step) 17 | 18 | 19 | def load(sess, saver, checkpoint_dir): 20 | print(" [*] Reading checkpoints...") 21 | 22 | ckpt = tf.train.get_checkpoint_state(checkpoint_dir) 23 | if ckpt : 24 | ckpt_name = os.path.basename(ckpt.model_checkpoint_path) 25 | saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) 26 | counter = int(ckpt_name.split('-')[-1]) 27 | print(" [*] Success to read {}".format(ckpt_name)) 28 | return True, counter 29 | else: 30 | print(" [*] Failed to find a checkpoint") 31 | return False, 0 32 | 33 | def normalize(X_train, X_test): 34 | X_train = X_train / 255.0 35 | X_test = X_test / 255.0 36 | 37 | return X_train, X_test 38 | 39 | def load_mnist() : 40 | (train_data, train_labels), (test_data, test_labels) = mnist.load_data() 41 | train_data = np.expand_dims(train_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 42 | test_data = np.expand_dims(test_data, axis=-1) # [N, 28, 28] -> [N, 28, 28, 1] 43 | 44 | train_data, test_data = normalize(train_data, test_data) 45 | 46 | train_labels = to_categorical(train_labels, 10) # [N,] -> [N, 10] 47 | test_labels = to_categorical(test_labels, 10) # [N,] -> [N, 10] 48 | 49 | return train_data, train_labels, test_data, test_labels 50 | 51 | def classification_loss(logit, label) : 52 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit)) 53 | prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1)) 54 | accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32)) 55 | 56 | return loss, accuracy 57 | 58 | def network(x, is_training=True, reuse=False) : 59 | xavier = tf_contrib.layers.xavier_initializer() 60 | 61 | with tf.variable_scope('network', reuse=reuse) : 62 | x = tf.layers.flatten(x) # [N, 28, 28, 1] -> [N, 784] 63 | 64 | for i in range(4) : 65 | # [N, 784] -> [N, 512] -> [N, 512] -> [N, 512] -> [N, 512] 66 | x = tf.layers.dense(inputs=x, units=512, use_bias=True, kernel_initializer=xavier, name='fully_connected_' + str(i)) 67 | x = tf.layers.batch_normalization(inputs=x, training=is_training, name='batch_norm_' + str(i)) 68 | x = tf.nn.relu(x) 69 | 70 | # [N, 256] -> [N, 10] 71 | hypothesis = tf.layers.dense(inputs=x, units=10, use_bias=True, kernel_initializer=xavier, name='fully_connected_logit') 72 | 73 | return hypothesis # hypothesis = logit 74 | 75 | 76 | """ dataset """ 77 | train_x, train_y, test_x, test_y = load_mnist() 78 | 79 | """ parameters """ 80 | learning_rate = 0.001 81 | batch_size = 128 82 | 83 | training_epochs = 10 84 | training_iterations = len(train_x) // batch_size 85 | 86 | img_size = 28 87 | c_dim = 1 88 | label_dim = 10 89 | 90 | train_flag = True 91 | 92 | """ Graph Input using Dataset API """ 93 | train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).\ 94 | shuffle(buffer_size=100000).\ 95 | prefetch(buffer_size=batch_size).\ 96 | batch(batch_size).\ 97 | repeat() 98 | 99 | test_dataset = tf.data.Dataset.from_tensor_slices((test_x, test_y)).\ 100 | shuffle(buffer_size=100000).\ 101 | prefetch(buffer_size=len(test_x)).\ 102 | batch(len(test_x)).\ 103 | repeat() 104 | 105 | """ Model """ 106 | train_iterator = train_dataset.make_one_shot_iterator() 107 | test_iterator = test_dataset.make_one_shot_iterator() 108 | 109 | train_inputs, train_labels = train_iterator.get_next() 110 | test_inputs, test_labels = test_iterator.get_next() 111 | 112 | train_logits = network(train_inputs) 113 | test_logits = network(test_inputs, is_training=False, reuse=True) 114 | 115 | train_loss, train_accuracy = classification_loss(logit=train_logits, label=train_labels) 116 | _, test_accuracy = classification_loss(logit=test_logits, label=test_labels) 117 | 118 | """ Training """ 119 | with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)) : 120 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(train_loss) 121 | 122 | """" Summary """ 123 | summary_train_loss = tf.summary.scalar("train_loss", train_loss) 124 | summary_train_accuracy = tf.summary.scalar("train_accuracy", train_accuracy) 125 | 126 | summary_test_accuracy = tf.summary.scalar("test_accuracy", test_accuracy) 127 | 128 | train_summary = tf.summary.merge([summary_train_loss, summary_train_accuracy]) 129 | test_summary = tf.summary.merge([summary_test_accuracy]) 130 | 131 | 132 | with tf.Session() as sess : 133 | tf.global_variables_initializer().run() 134 | start_time = time() 135 | 136 | saver = tf.train.Saver() 137 | checkpoint_dir = 'checkpoints' 138 | logs_dir = 'logs' 139 | 140 | model_dir = 'nn_batchnorm' 141 | model_name = 'dense' 142 | 143 | checkpoint_dir = os.path.join(checkpoint_dir, model_dir) 144 | logs_dir = os.path.join(logs_dir, model_dir) 145 | 146 | if train_flag : 147 | writer = tf.summary.FileWriter(logs_dir, sess.graph) 148 | else : 149 | writer = None 150 | 151 | # restore check-point if it exits 152 | could_load, checkpoint_counter = load(sess, saver, checkpoint_dir) 153 | 154 | if could_load: 155 | start_epoch = (int)(checkpoint_counter / training_iterations) 156 | start_batch_index = checkpoint_counter - start_epoch * training_iterations 157 | counter = checkpoint_counter 158 | print(" [*] Load SUCCESS") 159 | else: 160 | start_epoch = 0 161 | start_batch_index = 0 162 | counter = 1 163 | print(" [!] Load failed...") 164 | 165 | if train_flag : 166 | """ Training phase """ 167 | for epoch in range(start_epoch, training_epochs) : 168 | for idx in range(start_batch_index, training_iterations) : 169 | 170 | # train 171 | _, summary_str, train_loss_val, train_accuracy_val = sess.run([optimizer, train_summary, train_loss, train_accuracy]) 172 | writer.add_summary(summary_str, counter) 173 | 174 | # test 175 | summary_str, test_accuracy_val = sess.run([test_summary, test_accuracy]) 176 | writer.add_summary(summary_str, counter) 177 | 178 | counter += 1 179 | print("Epoch: [%2d] [%5d/%5d] time: %4.4f, train_loss: %.8f, train_accuracy: %.2f, test_Accuracy: %.2f" \ 180 | % (epoch, idx, training_iterations, time() - start_time, train_loss_val, train_accuracy_val, test_accuracy_val)) 181 | 182 | start_batch_index = 0 183 | save(sess, saver, checkpoint_dir, model_name, counter) 184 | 185 | save(sess, saver, checkpoint_dir, model_name, counter) 186 | print('Learning Finished!') 187 | 188 | test_accuracy_val = sess.run(test_accuracy) 189 | print("Test accuracy: %.8f" % (test_accuracy_val)) 190 | 191 | else : 192 | """ Test phase """ 193 | test_accuracy_val = sess.run(test_accuracy) 194 | print("Test accuracy: %.8f" % (test_accuracy_val)) 195 | 196 | """ Get test image """ 197 | r = np.random.randint(low=0, high=len(test_x) - 1) 198 | print("Label: ", np.argmax(test_y[r: r+1], axis=-1)) 199 | print("Prediction: ", sess.run(tf.argmax(test_logits, axis=-1), feed_dict={test_inputs: test_x[r: r+1]})) 200 | 201 | plt.imshow(test_x[r:r + 1].reshape(28, 28), cmap='Greys', interpolation='nearest') 202 | plt.show() 203 | -------------------------------------------------------------------------------- /code_session_version/lab-12-1-many-to-one-session.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 12-1 many to one\n", 8 | "### word sentiment classification \n", 9 | "* many to one \n", 10 | "* variable input sequence length" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "1.12.0\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "import numpy as np\n", 28 | "import tensorflow as tf\n", 29 | "from tensorflow import keras\n", 30 | "from tensorflow.keras import layers\n", 31 | "from tensorflow.nn import embedding_lookup\n", 32 | "from tensorflow.nn import dynamic_rnn\n", 33 | "from tensorflow.keras.preprocessing.sequence import pad_sequences\n", 34 | "import matplotlib.pyplot as plt\n", 35 | "%matplotlib inline\n", 36 | "\n", 37 | "print(tf.__version__)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "### Prepairing dataset" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "['', ' ', 'a', 'b', 'd', 'e', 'g', 'o', 'r', 's', 'w']\n", 57 | "{0: '', 1: ' ', 2: 'a', 3: 'b', 4: 'd', 5: 'e', 6: 'g', 7: 'o', 8: 'r', 9: 's', 10: 'w'}\n", 58 | "{'': 0, ' ': 1, 'a': 2, 'b': 3, 'd': 4, 'e': 5, 'g': 6, 'o': 7, 'r': 8, 's': 9, 'w': 10}\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "# example data\n", 64 | "words = ['good', 'bad', 'worse', 'so good']\n", 65 | "y_data = [1,0,0,1]\n", 66 | "\n", 67 | "# generating a token dictionary\n", 68 | "char_set = [''] + sorted(list(set(''.join(words))))\n", 69 | "idx2char = {idx : char for idx, char in enumerate(char_set)}\n", 70 | "char2idx = {char : idx for idx, char in enumerate(char_set)}\n", 71 | "\n", 72 | "print(char_set)\n", 73 | "print(idx2char)\n", 74 | "print(char2idx)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 3, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "[[6, 7, 7, 4], [3, 2, 4], [10, 7, 8, 9, 5], [9, 7, 1, 6, 7, 7, 4]]\n", 87 | "[4, 3, 5, 7]\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "# converting token to Idx\n", 93 | "x_data = list(map(lambda word : [char2idx.get(char) for char in word], words))\n", 94 | "x_data_len = list(map(lambda word : len(word), x_data))\n", 95 | "\n", 96 | "print(x_data)\n", 97 | "print(x_data_len)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "[[ 6 7 7 4 0 0 0 0 0 0]\n", 110 | " [ 3 2 4 0 0 0 0 0 0 0]\n", 111 | " [10 7 8 9 5 0 0 0 0 0]\n", 112 | " [ 9 7 1 6 7 7 4 0 0 0]]\n", 113 | "[4, 3, 5, 7]\n", 114 | "[1, 0, 0, 1]\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "# padding \n", 120 | "max_sequence = 10\n", 121 | "x_data = pad_sequences(sequences = x_data, maxlen = max_sequence, padding='post',\n", 122 | " truncating='post')\n", 123 | "\n", 124 | "print(x_data)\n", 125 | "print(x_data_len)\n", 126 | "print(y_data)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "### Creating model" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 5, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "class Model:\n", 143 | " def __init__(self, x, x_len, y, hidden_dim, num_classes, dic):\n", 144 | " # input\n", 145 | " self.x = x\n", 146 | " self.x_len = x_len\n", 147 | " self.y = y\n", 148 | " self.dic = dic\n", 149 | " \n", 150 | " # token representation\n", 151 | " one_hot = tf.Variable(np.eye(len(self.dic)), dtype=tf.float32, trainable=False)\n", 152 | " x_batch = embedding_lookup(params=one_hot, ids=self.x)\n", 153 | " \n", 154 | " # rnn\n", 155 | " rnn_cell = layers.SimpleRNNCell(units=hidden_dim)\n", 156 | " _, state = dynamic_rnn(cell=rnn_cell, inputs=x_batch, sequence_length=self.x_len,\n", 157 | " dtype=tf.float32)\n", 158 | " \n", 159 | " # output\n", 160 | " self.score = layers.Dense(units=num_classes)(state)\n", 161 | "\n", 162 | " # loss\n", 163 | " self.ce_loss = tf.losses.sparse_softmax_cross_entropy(labels=self.y, logits=self.score)\n", 164 | " \n", 165 | " # prediction\n", 166 | " self.prediction = tf.argmax(input=self.score, axis=-1)\n", 167 | " \n", 168 | " def predict(self, sess, x, x_len):\n", 169 | " feed_prediction = {self.x : x, self.x_len : x_len}\n", 170 | " return sess.run(self.prediction, feed_dict=feed_prediction)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "### Training model" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 6, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "\n" 190 | ] 191 | } 192 | ], 193 | "source": [ 194 | "# training hyper-paramter\n", 195 | "lr = .01\n", 196 | "epochs = 30\n", 197 | "batch_size = 2\n", 198 | "\n", 199 | "# generating data pipeline \n", 200 | "tr_dataset = tf.data.Dataset.from_tensor_slices((x_data, y_data, x_data_len))\n", 201 | "tr_dataset = tr_dataset.shuffle(buffer_size=4)\n", 202 | "tr_dataset = tr_dataset.batch(batch_size=2)\n", 203 | "\n", 204 | "print(tr_dataset)\n", 205 | "tr_iterator = tr_dataset.make_initializable_iterator()\n", 206 | "x_mb, y_mb, x_mb_len = tr_iterator.get_next()\n", 207 | "\n", 208 | "# creating model\n", 209 | "model = Model(x=x_mb, y=y_mb, x_len=x_mb_len, hidden_dim=10, num_classes=2, dic=char2idx)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 7, 215 | "metadata": {}, 216 | "outputs": [], 217 | "source": [ 218 | "# creating optimizer\n", 219 | "opt = tf.train.AdamOptimizer(learning_rate = lr)\n", 220 | "training_op = opt.minimize(loss=model.ce_loss)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 8, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "name": "stdout", 230 | "output_type": "stream", 231 | "text": [ 232 | "epoch : 5, tr_loss : 0.404\n", 233 | "epoch : 10, tr_loss : 0.129\n", 234 | "epoch : 15, tr_loss : 0.038\n", 235 | "epoch : 20, tr_loss : 0.015\n", 236 | "epoch : 25, tr_loss : 0.009\n", 237 | "epoch : 30, tr_loss : 0.006\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))\n", 243 | "sess = tf.Session(config = sess_config)\n", 244 | "sess.run(tf.global_variables_initializer())\n", 245 | "\n", 246 | "tr_loss_hist = []\n", 247 | "\n", 248 | "for epoch in range(epochs):\n", 249 | " avg_tr_loss = 0\n", 250 | " tr_step = 0\n", 251 | " \n", 252 | " # training\n", 253 | " sess.run(tr_iterator.initializer)\n", 254 | " try:\n", 255 | " while True:\n", 256 | " _, tr_loss = sess.run([training_op, model.ce_loss])\n", 257 | " avg_tr_loss += tr_loss\n", 258 | " tr_step += 1\n", 259 | " except tf.errors.OutOfRangeError:\n", 260 | " avg_tr_loss /= tr_step\n", 261 | " \n", 262 | " if (epoch + 1) % 5 == 0:\n", 263 | " print('epoch : {:3}, tr_loss : {:.3f}'.format(epoch+1, avg_tr_loss))" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "### Checking performance" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 9, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "yhat = model.predict(sess, x_data, x_data_len)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 10, 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stdout", 289 | "output_type": "stream", 290 | "text": [ 291 | "acc : 100.00%\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "print('acc : {:.2%}'.format(np.mean(yhat == y_data)))" 297 | ] 298 | } 299 | ], 300 | "metadata": { 301 | "kernelspec": { 302 | "display_name": "Python 3", 303 | "language": "python", 304 | "name": "python3" 305 | }, 306 | "language_info": { 307 | "codemirror_mode": { 308 | "name": "ipython", 309 | "version": 3 310 | }, 311 | "file_extension": ".py", 312 | "mimetype": "text/x-python", 313 | "name": "python", 314 | "nbconvert_exporter": "python", 315 | "pygments_lexer": "ipython3", 316 | "version": "3.6.8" 317 | }, 318 | "varInspector": { 319 | "cols": { 320 | "lenName": 16, 321 | "lenType": 16, 322 | "lenVar": 40 323 | }, 324 | "kernels_config": { 325 | "python": { 326 | "delete_cmd_postfix": "", 327 | "delete_cmd_prefix": "del ", 328 | "library": "var_list.py", 329 | "varRefreshCmd": "print(var_dic_list())" 330 | }, 331 | "r": { 332 | "delete_cmd_postfix": ") ", 333 | "delete_cmd_prefix": "rm(", 334 | "library": "var_list.r", 335 | "varRefreshCmd": "cat(var_dic_list()) " 336 | } 337 | }, 338 | "types_to_exclude": [ 339 | "module", 340 | "function", 341 | "builtin_function_or_method", 342 | "instance", 343 | "_Feature" 344 | ], 345 | "window_display": false 346 | } 347 | }, 348 | "nbformat": 4, 349 | "nbformat_minor": 2 350 | } 351 | -------------------------------------------------------------------------------- /code_session_version/lab-12-3-many-to-many-session.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 12-3 many to many\n", 8 | "### simple pos-tagger training \n", 9 | "* many to many\n", 10 | "* variable input sequence length" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "1.12.0\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "import numpy as np\n", 28 | "import tensorflow as tf\n", 29 | "from tensorflow import keras\n", 30 | "from tensorflow.keras import layers\n", 31 | "from tensorflow.nn import embedding_lookup\n", 32 | "from tensorflow.nn.rnn_cell import *\n", 33 | "from tensorflow.nn import dynamic_rnn\n", 34 | "from tensorflow.keras.preprocessing.sequence import pad_sequences\n", 35 | "import matplotlib.pyplot as plt\n", 36 | "%matplotlib inline\n", 37 | "\n", 38 | "print(tf.__version__)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "### Prepairing dataset" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "sentences = [['I', 'feel', 'hungry'],\n", 55 | " ['tensorflow', 'is', 'very', 'difficult'],\n", 56 | " ['tensorflow', 'is', 'a', 'framework', 'for', 'deep', 'learning'],\n", 57 | " ['tensorflow', 'is', 'very', 'fast', 'changing']]\n", 58 | "pos = [['pronoun', 'verb', 'adjective'],\n", 59 | " ['noun', 'verb', 'adverb', 'adjective'],\n", 60 | " ['noun', 'verb', 'determiner', 'noun', 'preposition', 'adjective', 'noun'],\n", 61 | " ['noun', 'verb', 'adverb', 'adjective', 'verb']]" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "### Preprocessing dataset" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "{'': 0, 'I': 1, 'a': 2, 'changing': 3, 'deep': 4, 'difficult': 5, 'fast': 6, 'feel': 7, 'for': 8, 'framework': 9, 'hungry': 10, 'is': 11, 'learning': 12, 'tensorflow': 13, 'very': 14}\n", 81 | "{0: '', 1: 'I', 2: 'a', 3: 'changing', 4: 'deep', 5: 'difficult', 6: 'fast', 7: 'feel', 8: 'for', 9: 'framework', 10: 'hungry', 11: 'is', 12: 'learning', 13: 'tensorflow', 14: 'very'}\n", 82 | "15\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "# word의 dictionary\n", 88 | "word_list = sum(sentences, [])\n", 89 | "word_list = sorted(set(word_list))\n", 90 | "word_list = [''] + word_list\n", 91 | "word2idx = {word : idx for idx, word in enumerate(word_list)}\n", 92 | "idx2word = {idx : word for idx, word in enumerate(word_list)}\n", 93 | "\n", 94 | "print(word2idx)\n", 95 | "print(idx2word)\n", 96 | "print(len(idx2word))" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 4, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "{'': 0, 'adjective': 1, 'adverb': 2, 'determiner': 3, 'noun': 4, 'preposition': 5, 'pronoun': 6, 'verb': 7}\n", 109 | "{0: '', 1: 'adjective', 2: 'adverb', 3: 'determiner', 4: 'noun', 5: 'preposition', 6: 'pronoun', 7: 'verb'}\n", 110 | "8\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# pos (part of speech)의 dictionary\n", 116 | "pos_list = sum(pos, [])\n", 117 | "pos_list = sorted(set(pos_list))\n", 118 | "pos_list = [''] + pos_list\n", 119 | "pos2idx = {pos : idx for idx, pos in enumerate(pos_list)}\n", 120 | "idx2pos = {idx : pos for idx, pos in enumerate(pos_list)}\n", 121 | "\n", 122 | "print(pos2idx)\n", 123 | "print(idx2pos)\n", 124 | "print(len(pos2idx))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 5, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "max_sequence = 10\n", 134 | "x_data = list(map(lambda sentence : [word2idx.get(token) for token in sentence], sentences))\n", 135 | "x_data = pad_sequences(sequences = x_data, maxlen = max_sequence,padding='post')\n", 136 | "x_data_mask = ((x_data != 0) * 1).astype(np.float32)\n", 137 | "x_data_len = np.array(list(map(lambda sentence : len(sentence), sentences)))\n", 138 | "\n", 139 | "y_data = list(map(lambda sentence : [pos2idx.get(token) for token in sentence], pos))\n", 140 | "y_data = pad_sequences(sequences = y_data, maxlen = max_sequence,padding='post')\n", 141 | "# y_data = np.expand_dims(y_data, -1)\n", 142 | "# y_data = np.apply_along_axis(lambda row : to_categorical(row), -1, y_data)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "### Creating model" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 6, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "class Model:\n", 159 | " def __init__(self, x, x_len, y, hidden_dim, num_classes, max_sequence, dic):\n", 160 | " # input\n", 161 | " self.x = x\n", 162 | " self.x_len = x_len\n", 163 | " self.y = y\n", 164 | " self.dic = dic\n", 165 | " \n", 166 | " # token representation\n", 167 | " one_hot = tf.Variable(np.eye(len(self.dic)), dtype=tf.float32, trainable=False)\n", 168 | " x_batch = embedding_lookup(params=one_hot, ids=self.x) \n", 169 | " \n", 170 | " # rnn\n", 171 | " rnn_cell = BasicRNNCell(num_units=hidden_dim, activation=tf.nn.tanh)\n", 172 | " score_cell = tf.contrib.rnn.OutputProjectionWrapper(cell=rnn_cell, output_size=num_classes)\n", 173 | " self.outputs, _= dynamic_rnn(cell=score_cell, inputs=x_batch, sequence_length=self.x_len,\n", 174 | " dtype=tf.float32)\n", 175 | " \n", 176 | " # loss\n", 177 | " masking = tf.sequence_mask(lengths=self.x_len, maxlen=max_sequence, dtype=tf.float32)\n", 178 | " self.sequence_loss = tf.contrib.seq2seq.sequence_loss(logits=self.outputs, targets=y,\n", 179 | " weights=masking)\n", 180 | " \n", 181 | " # prediction\n", 182 | " self.prediction = tf.argmax(input=self.outputs, axis=-1)\n", 183 | " \n", 184 | " def predict(self, sess, x, x_len):\n", 185 | " feed_prediction = {self.x : x, self.x_len : x_len}\n", 186 | " return sess.run(self.prediction, feed_dict=feed_prediction)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "### Training model" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 7, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "\n", 206 | "WARNING:tensorflow:From :14: BasicRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n", 207 | "Instructions for updating:\n", 208 | "This class is equivalent as tf.keras.layers.SimpleRNNCell, and will be replaced by that in Tensorflow 2.0.\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "# Creating a training operation\n", 214 | "lr = .1\n", 215 | "epochs = 30\n", 216 | "batch_size = 2\n", 217 | "\n", 218 | "# generating data pipeline\n", 219 | "tr_dataset = tf.data.Dataset.from_tensor_slices((x_data,y_data,x_data_len))\n", 220 | "tr_dataset = tr_dataset.shuffle(buffer_size = 4)\n", 221 | "tr_dataset = tr_dataset.batch(batch_size = 2)\n", 222 | "\n", 223 | "print(tr_dataset)\n", 224 | "tr_iterator = tr_dataset.make_initializable_iterator()\n", 225 | "x_mb, y_mb, x_mb_len = tr_iterator.get_next()\n", 226 | "\n", 227 | "# creating model\n", 228 | "model = Model(x=x_mb, x_len=x_mb_len, y=y_mb, hidden_dim=10, num_classes=len(pos2idx),\n", 229 | " max_sequence=10, dic=word2idx)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 8, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "opt = tf.train.AdamOptimizer(learning_rate = lr)\n", 239 | "training_op = opt.minimize(loss=model.sequence_loss)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 9, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "epoch : 5, tr_loss : 0.247\n", 252 | "epoch : 10, tr_loss : 0.018\n", 253 | "epoch : 15, tr_loss : 0.004\n", 254 | "epoch : 20, tr_loss : 0.001\n", 255 | "epoch : 25, tr_loss : 0.001\n", 256 | "epoch : 30, tr_loss : 0.001\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))\n", 262 | "sess = tf.Session(config = sess_config)\n", 263 | "sess.run(tf.global_variables_initializer())\n", 264 | "\n", 265 | "tr_loss_hist = []\n", 266 | "\n", 267 | "for epoch in range(epochs):\n", 268 | " avg_tr_loss = 0\n", 269 | " tr_step = 0\n", 270 | " \n", 271 | " # training\n", 272 | " sess.run(tr_iterator.initializer)\n", 273 | " try:\n", 274 | " while True:\n", 275 | " _, tr_loss = sess.run([training_op, model.sequence_loss])\n", 276 | " avg_tr_loss += tr_loss\n", 277 | " tr_step += 1\n", 278 | " except tf.errors.OutOfRangeError:\n", 279 | " avg_tr_loss /= tr_step\n", 280 | " \n", 281 | " if (epoch + 1) % 5 == 0:\n", 282 | " print('epoch : {:3}, tr_loss : {:.3f}'.format(epoch+1, avg_tr_loss))" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "### Checking performance" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 10, 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [ 298 | "yhat = model.predict(sess, x_data, x_data_len)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 11, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/plain": [ 309 | "array([[6, 7, 1, 0, 0, 0, 0, 0, 0, 0],\n", 310 | " [4, 7, 2, 1, 0, 0, 0, 0, 0, 0],\n", 311 | " [4, 7, 3, 4, 5, 1, 4, 0, 0, 0],\n", 312 | " [4, 7, 2, 1, 7, 0, 0, 0, 0, 0]])" 313 | ] 314 | }, 315 | "execution_count": 11, 316 | "metadata": {}, 317 | "output_type": "execute_result" 318 | } 319 | ], 320 | "source": [ 321 | "yhat" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 12, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "array([[6, 7, 1, 0, 0, 0, 0, 0, 0, 0],\n", 333 | " [4, 7, 2, 1, 0, 0, 0, 0, 0, 0],\n", 334 | " [4, 7, 3, 4, 5, 1, 4, 0, 0, 0],\n", 335 | " [4, 7, 2, 1, 7, 0, 0, 0, 0, 0]], dtype=int32)" 336 | ] 337 | }, 338 | "execution_count": 12, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "y_data" 345 | ] 346 | } 347 | ], 348 | "metadata": { 349 | "kernelspec": { 350 | "display_name": "Python 3", 351 | "language": "python", 352 | "name": "python3" 353 | }, 354 | "language_info": { 355 | "codemirror_mode": { 356 | "name": "ipython", 357 | "version": 3 358 | }, 359 | "file_extension": ".py", 360 | "mimetype": "text/x-python", 361 | "name": "python", 362 | "nbconvert_exporter": "python", 363 | "pygments_lexer": "ipython3", 364 | "version": "3.6.8" 365 | } 366 | }, 367 | "nbformat": 4, 368 | "nbformat_minor": 2 369 | } 370 | -------------------------------------------------------------------------------- /data-01-test-score.csv: -------------------------------------------------------------------------------- 1 | 73,80,75,152 2 | 93,88,93,185 3 | 89,91,90,180 4 | 96,98,100,196 5 | 73,66,70,142 6 | 53,46,55,101 7 | 69,74,77,149 8 | 47,56,60,115 9 | 87,79,90,175 10 | 79,70,88,164 11 | 69,70,73,141 12 | 70,65,74,141 13 | 93,95,91,184 14 | 79,80,73,152 15 | 70,73,78,148 16 | 93,89,96,192 17 | 78,75,68,147 18 | 81,90,93,183 19 | 88,92,86,177 20 | 78,83,77,159 21 | 82,86,90,177 22 | 86,82,89,175 23 | 78,83,85,175 24 | 76,83,71,149 25 | 96,93,95,192 -------------------------------------------------------------------------------- /data-04-zoo.csv: -------------------------------------------------------------------------------- 1 | # https://archive.ics.uci.edu/ml/machine-learning-databases/zoo/zoo.data,,,,,,,,,,,,,,,, 2 | # 1. animal name: (deleted),,,,,,,,,,,,,,,, 3 | # 2. hair Boolean",,,,,,,,,,,,,,,, 4 | # 3. feathers Boolean",,,,,,,,,,,,,,,, 5 | # 4. eggs Boolean",,,,,,,,,,,,,,,, 6 | # 5. milk Boolean",,,,,,,,,,,,,,,, 7 | # 6. airborne Boolean",,,,,,,,,,,,,,,, 8 | # 7. aquatic Boolean",,,,,,,,,,,,,,,, 9 | # 8. predator Boolean",,,,,,,,,,,,,,,, 10 | # 9. toothed Boolean",,,,,,,,,,,,,,,, 11 | # 10. backbone Boolean",,,,,,,,,,,,,,,, 12 | # 11. breathes Boolean",,,,,,,,,,,,,,,, 13 | # 12. venomous Boolean",,,,,,,,,,,,,,,, 14 | # 13. fins Boolean",,,,,,,,,,,,,,,, 15 | # 14. legs Numeric (set of values: {0",2,4,5,6,8}),,,,,,,,,,, 16 | # 15. tail Boolean",,,,,,,,,,,,,,,, 17 | # 16. domestic Boolean",,,,,,,,,,,,,,,, 18 | # 17. catsize Boolean",,,,,,,,,,,,,,,, 19 | # 18. type Numeric (integer values in range [0",6]),,,,,,,,,,,,,,, 20 | 1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0 21 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 22 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 23 | 1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0 24 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 25 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 26 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 27 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,1,0,3 28 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 29 | 1,0,0,1,0,0,0,1,1,1,0,0,4,0,1,0,0 30 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 31 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,1 32 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 33 | 0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,6 34 | 0,0,1,0,0,1,1,0,0,0,0,0,4,0,0,0,6 35 | 0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,6 36 | 0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,1 37 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 38 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3 39 | 0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0 40 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,1 41 | 0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,0,1 42 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 43 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,1,1 44 | 0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,5 45 | 0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,4 46 | 0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,4 47 | 1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,0 48 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 49 | 1,0,0,1,0,0,1,1,1,1,0,0,2,0,1,1,0 50 | 0,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,5 51 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 52 | 1,0,0,1,0,0,0,1,1,1,0,0,2,0,0,1,0 53 | 0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,1 54 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,3 55 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,0,0 56 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,0 57 | 0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,1 58 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 59 | 1,0,1,0,1,0,0,0,0,1,1,0,6,0,1,0,5 60 | 1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,5 61 | 0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,0,1 62 | 0,0,1,0,1,0,1,0,0,1,0,0,6,0,0,0,5 63 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 64 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 65 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 66 | 0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,6 67 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 68 | 1,0,0,1,0,1,1,1,1,1,0,0,4,1,0,1,0 69 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,0 70 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 71 | 1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,5 72 | 0,0,1,0,0,1,1,1,1,1,0,0,4,1,0,0,4 73 | 0,0,1,0,0,1,1,0,0,0,0,0,8,0,0,1,6 74 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,0 75 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 76 | 0,1,1,0,0,0,0,0,1,1,0,0,2,1,0,1,1 77 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,1 78 | 0,1,1,0,0,1,1,0,1,1,0,0,2,1,0,1,1 79 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 80 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3 81 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 82 | 0,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,2 83 | 1,0,1,1,0,1,1,0,1,1,0,0,4,1,0,1,0 84 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 85 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 86 | 0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0 87 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 88 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,1,1,0 89 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 90 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 91 | 0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,1,1 92 | 0,0,0,0,0,0,1,0,0,1,1,0,8,1,0,0,6 93 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,3 94 | 1,0,0,1,0,1,1,1,1,1,0,1,0,0,0,1,0 95 | 1,0,0,1,0,1,1,1,1,1,0,1,2,1,0,1,0 96 | 0,0,0,0,0,1,1,1,1,0,1,0,0,1,0,0,2 97 | 0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,6 98 | 0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,1 99 | 0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,1 100 | 0,0,1,0,0,0,1,1,1,1,0,0,0,1,0,0,2 101 | 0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,6 102 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,3 103 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 104 | 1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,0,0 105 | 0,0,1,0,0,1,1,0,0,0,0,0,5,0,0,0,6 106 | 0,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1,3 107 | 0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,1,1 108 | 0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,5 109 | 0,0,1,0,0,1,0,1,1,1,0,0,4,0,0,0,4 110 | 0,0,1,0,0,0,0,0,1,1,0,0,4,1,0,1,2 111 | 0,0,1,0,0,0,1,1,1,1,0,0,4,1,0,0,2 112 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3 113 | 1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,0 114 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,0 115 | 0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,1,1 116 | 1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,1,0 117 | 1,0,1,0,1,0,0,0,0,1,1,0,6,0,0,0,5 118 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 119 | 0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,6 120 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 -------------------------------------------------------------------------------- /docker/v1.12.0-cuda9.0.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:18.04 2 | LABEL maintainer "June Oh " 3 | 4 | RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates apt-transport-https gnupg curl && \ 5 | rm -rf /var/lib/apt/lists/* && \ 6 | curl -fsSL https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub | apt-key add - && \ 7 | echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /" > /etc/apt/sources.list.d/cuda.list && \ 8 | echo "deb https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 /" > /etc/apt/sources.list.d/nvidia-ml.list 9 | ENV CUDA_VERSION 9.0.176 10 | ENV CUDA_PKG_VERSION 9-0=$CUDA_VERSION-1 11 | RUN apt-get update && apt-get install -y --no-install-recommends \ 12 | cuda-cudart-$CUDA_PKG_VERSION && \ 13 | ln -s cuda-9.0 /usr/local/cuda && \ 14 | rm -rf /var/lib/apt/lists/* 15 | # nvidia-docker 1.0 16 | LABEL com.nvidia.volumes.needed="nvidia_driver" 17 | LABEL com.nvidia.cuda.version="${CUDA_VERSION}" 18 | RUN echo "/usr/local/nvidia/lib" >> /etc/ld.so.conf.d/nvidia.conf && \ 19 | echo "/usr/local/nvidia/lib64" >> /etc/ld.so.conf.d/nvidia.conf 20 | ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:${PATH} 21 | ENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64 22 | # nvidia-container-runtime 23 | ENV NVIDIA_VISIBLE_DEVICES all 24 | ENV NVIDIA_DRIVER_CAPABILITIES compute,utility 25 | ENV NVIDIA_REQUIRE_CUDA "cuda>=9.0" 26 | 27 | ENV NCCL_VERSION 2.3.5 28 | RUN apt-get update && apt-get install -y --no-install-recommends \ 29 | cuda-libraries-$CUDA_PKG_VERSION \ 30 | cuda-cublas-9-0=9.0.176.4-1 \ 31 | libnccl2=$NCCL_VERSION-2+cuda9.0 && \ 32 | apt-mark hold libnccl2 && \ 33 | rm -rf /var/lib/apt/lists/* 34 | 35 | RUN apt-get update && apt-get install -y --no-install-recommends \ 36 | cuda-libraries-dev-$CUDA_PKG_VERSION \ 37 | cuda-nvml-dev-$CUDA_PKG_VERSION \ 38 | cuda-minimal-build-$CUDA_PKG_VERSION \ 39 | cuda-command-line-tools-$CUDA_PKG_VERSION \ 40 | cuda-core-9-0=9.0.176.3-1 \ 41 | cuda-cublas-dev-9-0=9.0.176.4-1 \ 42 | libnccl-dev=$NCCL_VERSION-2+cuda9.0 && \ 43 | rm -rf /var/lib/apt/lists/* 44 | ENV LIBRARY_PATH /usr/local/cuda/lib64/stubs 45 | 46 | ENV CUDNN_VERSION 7.1.2.21 47 | LABEL com.nvidia.cudnn.version="${CUDNN_VERSION}" 48 | RUN apt-get update && apt-get install -y --no-install-recommends \ 49 | libcudnn7=$CUDNN_VERSION-1+cuda9.1 \ 50 | libcudnn7-dev=$CUDNN_VERSION-1+cuda9.1 && \ 51 | apt-mark hold libcudnn7 && \ 52 | rm -rf /var/lib/apt/lists/* 53 | 54 | ENV TZ=Asia/Seoul 55 | RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone 56 | RUN echo "XKBMODEL=\"pc105\"\n \ 57 | XKBLAYOUT=\"us\"\n \ 58 | XKBVARIANT=\"\"\n \ 59 | XKBOPTIONS=\"\"" > /etc/default/keyboard 60 | 61 | RUN apt-get update && apt-get install -y --no-install-recommends \ 62 | build-essential \ 63 | cmake \ 64 | git \ 65 | curl \ 66 | ca-certificates \ 67 | libjpeg-dev \ 68 | libpng-dev \ 69 | g++ \ 70 | openjdk-8-jdk \ 71 | sudo \ 72 | apt-utils \ 73 | man \ 74 | software-properties-common \ 75 | tzdata \ 76 | locales &&\ 77 | rm -rf /var/lib/apt/lists/* 78 | 79 | RUN locale-gen ko_KR.UTF-8 80 | ENV LANG="ko_KR.UTF-8" LANGUAGE="ko_KR:en" LC_ALL="ko_KR.UTF-8" 81 | 82 | RUN curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \ 83 | chmod +x ~/miniconda.sh && \ 84 | ~/miniconda.sh -b -p /opt/conda && \ 85 | rm ~/miniconda.sh 86 | ENV PATH /opt/conda/bin:$PATH 87 | RUN conda install python=3.6 jupyter pip=9.0.1 88 | RUN conda clean -ya 89 | RUN echo "export PATH=/opt/conda/bin:\$PATH" > /etc/profile.d/conda.sh 90 | 91 | RUN pip install scipy scikit-learn pandas cython visdom matplotlib konlpy 92 | 93 | RUN pip install tensorflow-gpu==1.12.0 94 | 95 | RUN pip install --upgrade pip 96 | RUN rm -rf ~/.ssh/pip 97 | 98 | ENV PYTHONUNBUFFERED=1 99 | WORKDIR /root 100 | 101 | CMD ["jupyter", "notebook", "--allow-root", "--ip=0.0.0.0", "--NotebookApp.custom_display_url='http://localhost:8888'"] 102 | -------------------------------------------------------------------------------- /docker/v1.12.0.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:18.04 2 | LABEL maintainer "June Oh " 3 | 4 | ENV TZ=Asia/Seoul 5 | RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone 6 | RUN echo "XKBMODEL=\"pc105\"\n \ 7 | XKBLAYOUT=\"us\"\n \ 8 | XKBVARIANT=\"\"\n \ 9 | XKBOPTIONS=\"\"" > /etc/default/keyboard 10 | 11 | RUN apt-get update && apt-get install -y --no-install-recommends \ 12 | build-essential \ 13 | cmake \ 14 | git \ 15 | curl \ 16 | vim \ 17 | ca-certificates \ 18 | libjpeg-dev \ 19 | libpng-dev \ 20 | sudo \ 21 | apt-utils \ 22 | man \ 23 | tmux \ 24 | less \ 25 | wget \ 26 | iputils-ping \ 27 | zsh \ 28 | htop \ 29 | software-properties-common \ 30 | tzdata \ 31 | locales \ 32 | openssh-server \ 33 | g++ \ 34 | openjdk-8-jdk \ 35 | xauth \ 36 | rsync &&\ 37 | rm -rf /var/lib/apt/lists/* 38 | 39 | RUN locale-gen ko_KR.UTF-8 40 | ENV LANG="ko_KR.UTF-8" LANGUAGE="ko_KR:en" LC_ALL="ko_KR.UTF-8" 41 | 42 | RUN curl -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \ 43 | chmod +x ~/miniconda.sh && \ 44 | ~/miniconda.sh -b -p /opt/conda && \ 45 | rm ~/miniconda.sh 46 | ENV PATH /opt/conda/bin:$PATH 47 | RUN conda install python=3.6 jupyter pip=9.0.1 48 | RUN conda clean -ya 49 | RUN echo "export PATH=/opt/conda/bin:\$PATH" > /etc/profile.d/conda.sh 50 | 51 | RUN pip install scipy scikit-learn pandas cython visdom matplotlib konlpy 52 | 53 | RUN pip install tensorflow==1.12.0 54 | 55 | RUN pip install --upgrade pip 56 | RUN rm -rf ~/.cache/pip 57 | 58 | ENV PYTHONUNBUFFERED=1 59 | RUN mkdir /workspace 60 | WORKDIR /workspace 61 | 62 | CMD ["jupyter", "notebook", "--allow-root", "--ip=0.0.0.0", "--NotebookApp.custom_display_url='http://localhost:8888'"] 63 | -------------------------------------------------------------------------------- /docker_user_guide.md: -------------------------------------------------------------------------------- 1 | # Docker User Guide 2 | 3 | 도커를 사용하시는 분들을 위해 작성하는 도커 사용법입니다. 4 | 5 | ## Docker 설치 안내 6 | 7 | [Docker 소개 및 안내](https://youtu.be/7eldOrjQVi0) - 오상준 8 | 9 | ## Docker 설치후 안내 10 | 11 | ### Docker 처음 실행 12 | 13 | 도커 터미널 실행 후 아래 그림에 있는 IP 를 잘 기억해두세요! 14 | 15 | 앞으로 이 IP 를 사용하여 도커 컨테이너와 소통을 하게 됩니다. 16 | 17 | ![이미지](figs/docker_ip.png) 18 | 19 | ### 도커 이미지 다운로드 받기 20 | 21 | hub.docker 에서 도커 이미지를 받아서 컨테이너를 사용할 준비를 합니다. (로그인이 필요 할 수도 있습니다. https://hub.docker.com 에서 가입하세요!) 22 | 23 | ``` 24 | $ docker pull deeplearningzerotoall/tensorflow 25 | ``` 26 | 27 | 만약에 특별히 다른 버전을 지정하고 싶으면 저희 [docker hub](https://hub.docker.com/r/deeplearningzerotoall/tensorflow) 에 접속하셔서 원하시는 버전을 확인합니다. 28 | 29 | 이미지를 받으셨으면 `docker images` 명령어를 쳐서 도커 이미지의 존재를 확인해봅니다. 30 | 31 | ``` 32 | $ docker images 33 | REPOSITORY TAG ... 34 | hello-world latest ... 35 | deeplearningzerotoall/tensorflow latest ... 36 | ``` 37 | 38 | ### 도커 이미지로부터 컨테이너 실행하고 끄기 39 | 40 | 도커 이미지로부터 컨테이너를 생성하고 실행합니다. 이때 `--name` 뒤에 원하는 이름을 설정할 수 있습니다. 이번 가이드에서는 `tf` 로 하겠습니다. 41 | 42 | ``` 43 | $ docker run -i -t --name tf -p 8888:8888 -p 6006:6006 deeplearningzerotoall/tensorflow /bin/bash 44 | ``` 45 | 46 | 이제 거의 다 왔습니다. 도커 컨테이너를 만들었고 root 계정으로 컨테이너 내부 쉘에 접속한 상태입니다. 47 | 48 | ``` 49 | root@[고유번호]:/workspace# 50 | ``` 51 | 52 | 만약에 완전히 컨테이너를 끄고 싶다면 `exit` 명령어를 입력하세요! 53 | 54 | ``` 55 | root@[고유번호]:/workspace# exit 56 | ``` 57 | 58 | `docker ps -a` 명령어를 사용해 현재 도커 컨테이너가 켜져있는 여부를 확인 할 수 있습니다. **STATUS** 를 확인하세요 59 | 60 | ``` 61 | $ docker ps -a 62 | ``` 63 | 현재 실행 중인 컨테이너만 확인하고 싶은 경우에는 `docker ps` 명령어를 이용해서 확인할 수 있습니다. 64 | 65 | ### 도커 컨테이너 자유롭게 드나들기 66 | 67 | **주의**: `docker run` 명령어는 한 번 만 실행하시면 됩니다. 종료된 컨테이너를 다시 실행하려면 아래와 같이 실행합니다. 68 | 69 | ``` 70 | $ docker start tf 71 | ``` 72 | 73 | 이 단계는 컨테이너를 키기만 했을 뿐입니다. 컨테이너 터미널에 접속하려면 아래와 같이 실행합니다. 그러면 컨테이너 내부 터미널로 이동하게 됩니다. 74 | 75 | ``` 76 | $ docker attach tf 77 | root@[고유번호]:/workspace# 78 | ``` 79 | 80 | 만약에 컨테이너를 켜둔채 잠시 밖으로 나오고 싶다면 `Ctrl+P` + `Ctrl+Q` 를 사용합니다. 마찬가지로 다시 접속하려면 `docker attach` 명령어를 사용하면 됩니다. 81 | 82 | ## 공부를 위한 Fork 및 Git Clone 83 | 84 | 이제 본격적으로 공부를 시작하기 위해 모두를 위한 딥러닝 [Github](https://github.com/deeplearningzerotoall/TensorFlow) 에 접속합니다. 85 | 86 | 자신의 Github 계정으로 접속하시고, 아래 그림과 같이 오른쪽 상단에 있는 `Fork` 버튼을 누릅니다. 87 | 88 | ![이미지](figs/fork.png) 89 | 90 | `Fork` 가 완료되면 자신의 레포지토리(오른쪽 위 상단의 프로필 이미지 클릭후, Your repositories)에 가시면 본 github 으로부터 Fork 된 repository 가 생성되어 있습니다. 91 | 92 | ![이미지](figs/fork_after.png) 93 | 94 | 해당 레포지토리를 클릭하여 접속하시고, 초록색 버튼인 `Clone or download`를 눌러서 하단의 링크를 복사 혹은 기억해둡니다. 95 | 96 | ![이미지](figs/clone.png) 97 | 98 | 다시 도커 컨테이너 내부로 돌아와서 복사한 링크를 `git clone` 명령어와 함께 붙여넣거나 타이핑해줍니다. 99 | 100 | ``` 101 | root@[고유번호]:/workspace# git clone https://github.com/[github계정이름]/TensorFlow.git 102 | ``` 103 | 104 | 완료가 되면 이제 TensorFlow 디렉토리로 이동합니다. 105 | 106 | ``` 107 | root@[고유번호]:/workspace# cd TensorFlow 108 | root@[고유번호]:/workspace/TensorFlow# 109 | ``` 110 | 111 | 필수 패키지를 설치해줍니다. 112 | 113 | ``` 114 | root@[고유번호]:/workspace/TensorFlow# pip install --upgrade pip 115 | root@[고유번호]:/workspace/TensorFlow# pip install -r requirements.txt 116 | ``` 117 | 118 | 거의 다왔습니다. 이제 `jupyter notebook` 만 실행하시면 됩니다. 119 | 120 | ``` 121 | root@[고유번호]:/workspace/TensorFlow# jupyter notebook --ip 0.0.0.0 --allow-root 122 | ``` 123 | 124 | 위 명령어를 매번 치기 귀찮으신 분들을 위해 쉘 스크립트로 만들어 두었습니다. 125 | 126 | ``` 127 | root@[고유번호]:/workspace/TensorFlow# sh run_jupyter_docker.sh 128 | ``` 129 | 130 | `jupyter notebook` 처음 실행후 아래 이미지 처럼 도커 터미널에 긴 토큰이 생기는데 해당 토큰을 복사해둡니다. 131 | 132 | ![이미지](figs/jupyter_token.png) 133 | 134 | 이제 인터넷 창에 제일 처음이 도커실행시 주어진 IP 주소를 이용해 `[IP주소]:8888` 를 입력합니다. 그러면 아래 화면이 등장합니다. 135 | 136 | ![이미지](figs/jupyter_allow_root.png) 137 | 138 | 아까 복사해둔 토큰을 입력합니다. 처음 실행이라면 패스워드를 설정할 수도 있습니다. 다음 접속시 패스워드만 입력하면 되기 때문에 설정하는 것을 권장합니다. 139 | 140 | `jupyter notebook` 을 종료하려면 도커 터미널에 돌아와서 `Ctrl+c` 를 두번 하시면 종료됩니다. 141 | 142 | 이제 모든 가이드가 끝났습니다. 열심히 공부하시길 바랍니다! :) 143 | 144 | 145 | -------------------------------------------------------------------------------- /figs/clone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/figs/clone.png -------------------------------------------------------------------------------- /figs/docker_ip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/figs/docker_ip.png -------------------------------------------------------------------------------- /figs/fork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/figs/fork.png -------------------------------------------------------------------------------- /figs/fork_after.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/figs/fork_after.png -------------------------------------------------------------------------------- /figs/jupyter_allow_root.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/figs/jupyter_allow_root.png -------------------------------------------------------------------------------- /figs/jupyter_token.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeplearningzerotoall/TensorFlow/feab13059b6c5a674c50c7c2d22dc62c7e157934/figs/jupyter_token.png -------------------------------------------------------------------------------- /figs/nothing: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /lab-01-basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Getting Started With TensorFlow\n", 12 | "https://www.tensorflow.org/get_started/get_started" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "## Check TF version" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 1, 25 | "metadata": { 26 | "collapsed": false, 27 | "slideshow": { 28 | "slide_type": "slide" 29 | } 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "'1.0.0'" 36 | ] 37 | }, 38 | "execution_count": 1, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "import tensorflow as tf\n", 45 | "tf.__version__" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## Hello TensorFlow!" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "b'Hello, TensorFlow!'\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "# Create a constant op\n", 72 | "# This op is added as a node to the default graph\n", 73 | "hello = tf.constant(\"Hello, TensorFlow!\")\n", 74 | "\n", 75 | "# start a TF session\n", 76 | "sess = tf.Session()\n", 77 | "\n", 78 | "# run the op and get result\n", 79 | "print(sess.run(hello))" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": { 85 | "slideshow": { 86 | "slide_type": "slide" 87 | } 88 | }, 89 | "source": [ 90 | "## Tensors" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 3, 96 | "metadata": { 97 | "collapsed": false, 98 | "slideshow": { 99 | "slide_type": "slide" 100 | } 101 | }, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]" 107 | ] 108 | }, 109 | "execution_count": 3, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "3 # a rank 0 tensor; this is a scalar with shape []\n", 116 | "[1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3]\n", 117 | "[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]\n", 118 | "[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "## Computational Graph" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 4, 131 | "metadata": { 132 | "collapsed": false 133 | }, 134 | "outputs": [], 135 | "source": [ 136 | "node1 = tf.constant(3.0, tf.float32)\n", 137 | "node2 = tf.constant(4.0) # also tf.float32 implicitly\n", 138 | "node3 = tf.add(node1, node2)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 5, 144 | "metadata": { 145 | "collapsed": false, 146 | "scrolled": true 147 | }, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "node1: Tensor(\"Const_1:0\", shape=(), dtype=float32) node2: Tensor(\"Const_2:0\", shape=(), dtype=float32)\n", 154 | "node3: Tensor(\"Add:0\", shape=(), dtype=float32)\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "print(\"node1:\", node1, \"node2:\", node2)\n", 160 | "print(\"node3: \", node3)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "![Graph](https://www.tensorflow.org/images/getting_started_adder.png)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 6, 173 | "metadata": { 174 | "collapsed": false, 175 | "scrolled": true 176 | }, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "sess.run(node1, node2): [3.0, 4.0]\n", 183 | "sess.run(node3): 7.0\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "sess = tf.Session()\n", 189 | "print(\"sess.run(node1, node2): \", sess.run([node1, node2]))\n", 190 | "print(\"sess.run(node3): \", sess.run(node3))" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 7, 196 | "metadata": { 197 | "collapsed": false 198 | }, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "7.5\n", 205 | "[ 3. 7.]\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "a = tf.placeholder(tf.float32)\n", 211 | "b = tf.placeholder(tf.float32)\n", 212 | "adder_node = a + b # + provides a shortcut for tf.add(a, b)\n", 213 | "\n", 214 | "print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))\n", 215 | "print(sess.run(adder_node, feed_dict={a: [1,3], b: [2, 4]}))" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 8, 221 | "metadata": { 222 | "collapsed": false 223 | }, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "22.5\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "add_and_triple = adder_node * 3.\n", 235 | "print(sess.run(add_and_triple, feed_dict={a: 3, b:4.5}))" 236 | ] 237 | } 238 | ], 239 | "metadata": { 240 | "kernelspec": { 241 | "display_name": "Python 3", 242 | "language": "python", 243 | "name": "python3" 244 | }, 245 | "language_info": { 246 | "codemirror_mode": { 247 | "name": "ipython", 248 | "version": 3 249 | }, 250 | "file_extension": ".py", 251 | "mimetype": "text/x-python", 252 | "name": "python", 253 | "nbconvert_exporter": "python", 254 | "pygments_lexer": "ipython3", 255 | "version": "3.6.0" 256 | } 257 | }, 258 | "nbformat": 4, 259 | "nbformat_minor": 0 260 | } 261 | -------------------------------------------------------------------------------- /lab-05-2-logistic_regression_diabetes-eager.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 05 Logistic Classification (diabetes) - Eager Execution\n", 8 | "* Logistic Classfication을 diabetes data를 활용하여 모델을 만들어 보도록 하겠습니다\n", 9 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "1.12.0\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import numpy as np\n", 27 | "import matplotlib.pyplot as plt\n", 28 | "%matplotlib inline\n", 29 | "import tensorflow as tf\n", 30 | "\n", 31 | "tf.enable_eager_execution()\n", 32 | "tf.set_random_seed(777) # for reproducibility\n", 33 | "print(tf.__version__)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "### 강의에 설명할 Data입니다" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": { 47 | "scrolled": true 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "(759, 8) (759, 1)\n", 55 | "[[-0.294118 0.487437 0.180328 ... -0.53117 -0.0333333 0. ]\n", 56 | " [-0.882353 -0.145729 0.0819672 ... -0.766866 -0.666667 1. ]\n", 57 | " [-0.0588235 0.839196 0.0491803 ... -0.492741 -0.633333 0. ]\n", 58 | " ...\n", 59 | " [-0.411765 0.21608 0.180328 ... -0.857387 -0.7 1. ]\n", 60 | " [-0.882353 0.266332 -0.0163934 ... -0.768574 -0.133333 0. ]\n", 61 | " [-0.882353 -0.0653266 0.147541 ... -0.797609 -0.933333 1. ]]\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)\n", 67 | "x_train = xy[:, 0:-1]\n", 68 | "y_train = xy[:, [-1]]\n", 69 | "\n", 70 | "print(x_train.shape, y_train.shape)\n", 71 | "print(xy)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "## Tensorflow Eager\n", 79 | "### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", 80 | "* Tensorflow data API를 통해 학습시킬 값들을 담는다 (Batch Size는 한번에 학습시킬 Size로 정한다)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(len(x_train))" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", 97 | "* W와 b은 학습을 통해 생성되는 모델에 쓰이는 Wegith와 Bias (초기값을 variable : 0이나 Random값으로 가능 tf.random_normal([2, 1]) )" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "W = tf.Variable(tf.random_normal([8, 1]), name='weight')\n", 107 | "b = tf.Variable(tf.random_normal([1]), name='bias')" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "### Sigmoid 함수를 가설로 선언합니다\n", 115 | "* Sigmoid는 아래 그래프와 같이 0과 1의 값만을 리턴합니다 tf.sigmoid(tf.matmul(X, W) + b)와 같습니다\n", 116 | "\n", 117 | "$$\n", 118 | "\\begin{align}\n", 119 | "sigmoid(x) & = \\frac{1}{1+e^{-x}} \\\\\\\\\\\n", 120 | "\\end{align}\n", 121 | "$$\n", 122 | "\n", 123 | "![sigmoid](https://upload.wikimedia.org/wikipedia/commons/8/88/Logistic-curve.svg)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 5, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "def logistic_regression(features):\n", 133 | " hypothesis = tf.div(1., 1. + tf.exp(tf.matmul(features, W) + b))\n", 134 | " return hypothesis" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "### 가설을 검증할 Cost 함수를 정의합니다\n", 142 | "$$\n", 143 | "\\begin{align}\n", 144 | "cost(h(x),y) & = −log(h(x)) & if & y=1 \\\\\\\\\\\n", 145 | "cost(h(x),y) & = -log(1−h(x)) & if & y=0\n", 146 | "\\end{align}\n", 147 | "$$" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "* 위 두수식을 합치면 아래과 같습니다\n", 155 | "$$\n", 156 | "\\begin{align}\n", 157 | "cost(h(x),y) & = −y log(h(x))−(1−y)log(1−h(x))\n", 158 | "\\end{align}\n", 159 | "$$" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 6, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "def loss_fn(hypothesis, features, labels):\n", 169 | " cost = -tf.reduce_mean(labels * tf.log(logistic_regression(features)) + (1 - labels) * tf.log(1 - hypothesis))\n", 170 | " return cost\n", 171 | "\n", 172 | "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "### 추론한 값은 0.5를 기준(Sigmoid 그래프 참조)로 0과 1의 값을 리턴합니다.\n", 180 | "* Sigmoid 함수를 통해 예측값이 0.5보다 크면 1을 반환하고 0.5보다 작으면 0으로 반환합니다." 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 7, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "def accuracy_fn(hypothesis, labels):\n", 190 | " predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)\n", 191 | " accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, labels), dtype=tf.int32))\n", 192 | " return accuracy" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "### GradientTape를 통해 경사값을 계산합니다." 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 8, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "def grad(hypothesis, features, labels):\n", 209 | " with tf.GradientTape() as tape:\n", 210 | " loss_value = loss_fn(logistic_regression(features),features,labels)\n", 211 | " return tape.gradient(loss_value, [W,b])" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "### Tensorflow를 통한 실행을 위해 Session를 선언합니다.\n", 219 | "* 위의 Data를 Cost함수를 통해 학습시킨 후 모델을 생성합니다. " 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 9, 225 | "metadata": { 226 | "scrolled": true 227 | }, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "Iter: 0, Loss: 0.6556\n", 234 | "Iter: 100, Loss: 0.6188\n", 235 | "Iter: 200, Loss: 0.5980\n", 236 | "Iter: 300, Loss: 0.5854\n", 237 | "Iter: 400, Loss: 0.5769\n", 238 | "Iter: 500, Loss: 0.5704\n", 239 | "Iter: 600, Loss: 0.5648\n", 240 | "Iter: 700, Loss: 0.5599\n", 241 | "Iter: 800, Loss: 0.5555\n", 242 | "Iter: 900, Loss: 0.5513\n", 243 | "Iter: 1000, Loss: 0.5475\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "EPOCHS = 1001\n", 249 | "\n", 250 | "for step in range(EPOCHS):\n", 251 | " for features, labels in iter(dataset):\n", 252 | " grads = grad(logistic_regression(features), features, labels)\n", 253 | " optimizer.apply_gradients(grads_and_vars=zip(grads,[W,b]))\n", 254 | " if step % 100 == 0:\n", 255 | " print(\"Iter: {}, Loss: {:.4f}\".format(step, loss_fn(logistic_regression(features),features,labels)))" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [] 264 | } 265 | ], 266 | "metadata": { 267 | "kernelspec": { 268 | "display_name": "Python 3", 269 | "language": "python", 270 | "name": "python3" 271 | }, 272 | "language_info": { 273 | "codemirror_mode": { 274 | "name": "ipython", 275 | "version": 3 276 | }, 277 | "file_extension": ".py", 278 | "mimetype": "text/x-python", 279 | "name": "python", 280 | "nbconvert_exporter": "python", 281 | "pygments_lexer": "ipython3", 282 | "version": "3.6.7" 283 | } 284 | }, 285 | "nbformat": 4, 286 | "nbformat_minor": 2 287 | } 288 | -------------------------------------------------------------------------------- /lab-06-2-softmax_zoo_classifier-eager.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 06 Softmax Zoo_classifier-eager\n", 8 | "\n", 9 | "* Softmax를 사용하여 Zoo 데이터를 활용하여 분류를 진행합니다.\n", 10 | "\n", 11 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "\n", 24 | "WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.\n", 25 | "For more information, please see:\n", 26 | " * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n", 27 | " * https://github.com/tensorflow/addons\n", 28 | "If you depend on functionality not listed there, please file an issue.\n", 29 | "\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "import tensorflow as tf\n", 35 | "import numpy as np\n", 36 | "tf.enable_eager_execution()\n", 37 | "tf.set_random_seed(777) # for reproducibility\n", 38 | "tfe = tf.contrib.eager" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 15, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "(101, 16) (101, 7)\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.int32) #tf1.13.1에서는 np.int32, 이전에는 np.float32\n", 56 | "x_data = xy[:, 0:-1]\n", 57 | "y_data = xy[:, [-1]]\n", 58 | "\n", 59 | "nb_classes = 7 # 0 ~ 6\n", 60 | "\n", 61 | "# Make Y data as onehot shape\n", 62 | "Y_one_hot = tf.one_hot(list(y_data), nb_classes)\n", 63 | "Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])\n", 64 | "\n", 65 | "print(x_data.shape, Y_one_hot.shape)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "#Weight and bias setting\n", 75 | "W = tfe.Variable(tf.random_normal([16, nb_classes]), name='weight')\n", 76 | "b = tfe.Variable(tf.random_normal([nb_classes]), name='bias')\n", 77 | "variables = [W, b]\n", 78 | "\n", 79 | "# tf.nn.softmax computes softmax activations\n", 80 | "# softmax = exp(logits) / reduce_sum(exp(logits), dim)\n", 81 | "def logit_fn(X):\n", 82 | " return tf.matmul(X, W) + b\n", 83 | "\n", 84 | "def hypothesis(X):\n", 85 | " return tf.nn.softmax(logit_fn(X))\n", 86 | "\n", 87 | "def cost_fn(X, Y):\n", 88 | " logits = logit_fn(X)\n", 89 | " cost_i = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,\n", 90 | " labels=Y)\n", 91 | " cost = tf.reduce_mean(cost_i)\n", 92 | " \n", 93 | " return cost\n", 94 | "\n", 95 | "def grad_fn(X, Y):\n", 96 | " with tf.GradientTape() as tape:\n", 97 | " loss = cost_fn(X, Y)\n", 98 | " grads = tape.gradient(loss, variables)\n", 99 | " return grads\n", 100 | " \n", 101 | "def prediction(X, Y):\n", 102 | " pred = tf.argmax(hypothesis(X), 1)\n", 103 | " correct_prediction = tf.equal(pred, tf.argmax(Y, 1))\n", 104 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 105 | "\n", 106 | " return accuracy" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "Steps: 1 Loss: 3.635028839111328, Acc: 0.1683168262243271\n", 119 | "Steps: 100 Loss: 0.5194157958030701, Acc: 0.7920792102813721\n", 120 | "Steps: 200 Loss: 0.31850090622901917, Acc: 0.9108911156654358\n", 121 | "Steps: 300 Loss: 0.23534874618053436, Acc: 0.9405940771102905\n", 122 | "Steps: 400 Loss: 0.18872138857841492, Acc: 0.9504950642585754\n", 123 | "Steps: 500 Loss: 0.158460333943367, Acc: 0.9504950642585754\n", 124 | "Steps: 600 Loss: 0.13703754544258118, Acc: 0.9900990128517151\n", 125 | "Steps: 700 Loss: 0.1209898293018341, Acc: 0.9900990128517151\n", 126 | "Steps: 800 Loss: 0.10847964137792587, Acc: 1.0\n", 127 | "Steps: 900 Loss: 0.09843041747808456, Acc: 1.0\n", 128 | "Steps: 1000 Loss: 0.09016557037830353, Acc: 1.0\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "def fit(X, Y, epochs=1000, verbose=100):\n", 134 | " optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)\n", 135 | "\n", 136 | " for i in range(epochs):\n", 137 | " grads = grad_fn(X, Y)\n", 138 | " optimizer.apply_gradients(zip(grads, variables))\n", 139 | " if (i==0) | ((i+1)%verbose==0):\n", 140 | "# print('Loss at epoch %d: %f' %(i+1, cost_fn(X, Y).numpy()))\n", 141 | " acc = prediction(X, Y).numpy()\n", 142 | " loss = cost_fn(X, Y).numpy() \n", 143 | " print('Steps: {} Loss: {}, Acc: {}'.format(i+1, loss, acc))\n", 144 | "\n", 145 | "fit(x_data, Y_one_hot)" 146 | ] 147 | } 148 | ], 149 | "metadata": { 150 | "kernelspec": { 151 | "display_name": "Python 3", 152 | "language": "python", 153 | "name": "python3" 154 | }, 155 | "language_info": { 156 | "codemirror_mode": { 157 | "name": "ipython", 158 | "version": 3 159 | }, 160 | "file_extension": ".py", 161 | "mimetype": "text/x-python", 162 | "name": "python", 163 | "nbconvert_exporter": "python", 164 | "pygments_lexer": "ipython3", 165 | "version": "3.6.5" 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 2 170 | } 171 | -------------------------------------------------------------------------------- /lab-07-4-mnist_introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 07-4 MNIST introduction\n", 8 | "* MNIST(Modified National Institute of Standards and Technology database)는 0~9까지의 손으로 쓴 숫자들로 이루어진 대형 데이터베이스\n", 9 | "\n", 10 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "1.12.0\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "import numpy as np\n", 28 | "import tensorflow as tf\n", 29 | "tf.set_random_seed(777) # for reproducibility\n", 30 | "print(tf.__version__)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "### 강의에 설명할 Data입니다\n", 38 | "* Tensorflow 2.0 버전에 맞게 Keras를 활용한 MNIST를 분류 모델 생성" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 2, 44 | "metadata": { 45 | "scrolled": true 46 | }, 47 | "outputs": [], 48 | "source": [ 49 | "mnist = tf.keras.datasets.mnist\n", 50 | "\n", 51 | "(x_train, y_train),(x_test, y_test) = mnist.load_data()\n", 52 | "x_train, x_test = x_train / 255.0, x_test / 255.0" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "### Tensorflow Keras\n", 60 | "#### 위 Data를 기준으로 분류 모델을 만들도록 하겠습니다\n", 61 | "* Tensorflow keras API를 통해 모델에 대한 정의" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "model = tf.keras.models.Sequential([\n", 71 | " tf.keras.layers.Flatten(),\n", 72 | " tf.keras.layers.Dense(512, activation=tf.nn.relu),\n", 73 | " tf.keras.layers.Dropout(0.2),\n", 74 | " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", 75 | "])" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "* Adam Optimizer과 Cross Entropy Loss 선언" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 4, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "model.compile(optimizer='adam',\n", 92 | " loss='sparse_categorical_crossentropy',\n", 93 | " metrics=['accuracy'])" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "* 5 Epoch로 학습할 Data로 학습 수행" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 5, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "Epoch 1/5\n", 113 | "60000/60000 [==============================] - 8s 127us/step - loss: 0.2024 - acc: 0.9401\n", 114 | "Epoch 2/5\n", 115 | "60000/60000 [==============================] - 7s 121us/step - loss: 0.0812 - acc: 0.9747\n", 116 | "Epoch 3/5\n", 117 | "60000/60000 [==============================] - 7s 122us/step - loss: 0.0529 - acc: 0.9831\n", 118 | "Epoch 4/5\n", 119 | "60000/60000 [==============================] - 7s 122us/step - loss: 0.0366 - acc: 0.9883\n", 120 | "Epoch 5/5\n", 121 | "60000/60000 [==============================] - 7s 121us/step - loss: 0.0274 - acc: 0.9907\n" 122 | ] 123 | }, 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "" 128 | ] 129 | }, 130 | "execution_count": 5, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "model.fit(x_train, y_train, epochs=5)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "* 모델을 평가할 Test 데이타에 대한 정의" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 6, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "10000/10000 [==============================] - 0s 40us/step\n" 156 | ] 157 | }, 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "[0.065501897697401, 0.9806]" 162 | ] 163 | }, 164 | "execution_count": 6, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "model.evaluate(x_test, y_test)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [] 179 | } 180 | ], 181 | "metadata": { 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "language": "python", 185 | "name": "python3" 186 | }, 187 | "language_info": { 188 | "codemirror_mode": { 189 | "name": "ipython", 190 | "version": 3 191 | }, 192 | "file_extension": ".py", 193 | "mimetype": "text/x-python", 194 | "name": "python", 195 | "nbconvert_exporter": "python", 196 | "pygments_lexer": "ipython3", 197 | "version": "3.6.5" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 2 202 | } 203 | -------------------------------------------------------------------------------- /lab-12-0-rnn-basics-keras-eager.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 12-0 rnn basics" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "1.12.0\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "# setup\n", 25 | "import numpy as np\n", 26 | "import tensorflow as tf\n", 27 | "from tensorflow import keras\n", 28 | "from tensorflow.keras import layers\n", 29 | "from tensorflow.keras import Sequential, Model\n", 30 | "\n", 31 | "print(tf.__version__)\n", 32 | "tf.enable_eager_execution()" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "### Preparing dataset" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "# One hot encoding for each char in 'hello'\n", 49 | "h = [1, 0, 0, 0]\n", 50 | "e = [0, 1, 0, 0]\n", 51 | "l = [0, 0, 1, 0]\n", 52 | "o = [0, 0, 0, 1]" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "### One cell: 4 (input_dim) in 2 (hidden_size)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "![image](https://cloud.githubusercontent.com/assets/901975/23348727/cc981856-fce7-11e6-83ea-4b187473466b.png)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "x_data: [[[1. 0. 0. 0.]]], shape: (1, 1, 4)\n", 79 | "outputs: [[[0.32261637 0.5036928 ]]], shape: (1, 1, 2)\n", 80 | "states: [[0.32261637 0.5036928 ]], shape: (1, 2)\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "# One cell RNN input_dim (4) -> output_dim (2)\n", 86 | "x_data = np.array([[h]], dtype=np.float32)\n", 87 | "\n", 88 | "hidden_size = 2\n", 89 | "cell = layers.SimpleRNNCell(units=hidden_size) # creating SimpleRNNCell\n", 90 | "rnn = layers.RNN(cell, return_sequences=True, return_state=True) # analogous to tf.nn.dynamic_rnn\n", 91 | "outputs, states = rnn(x_data)\n", 92 | "\n", 93 | "print('x_data: {}, shape: {}'.format(x_data, x_data.shape))\n", 94 | "print('outputs: {}, shape: {}'.format(outputs, outputs.shape))\n", 95 | "print('states: {}, shape: {}'.format(states, states.shape))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 4, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "x_data: [[[1. 0. 0. 0.]]], shape: (1, 1, 4)\n", 108 | "outputs: [[[-0.5746312 0.6728443]]], shape: (1, 1, 2)\n", 109 | "states: [[-0.5746312 0.6728443]], shape: (1, 2)\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "# equivalent to above case\n", 115 | "rnn = layers.SimpleRNN(units=hidden_size, return_sequences=True,\n", 116 | " return_state=True) # layers.SimpleRNNCell + layers.RNN\n", 117 | "\n", 118 | "outputs, states = rnn(x_data)\n", 119 | "\n", 120 | "print('x_data: {}, shape: {}'.format(x_data, x_data.shape))\n", 121 | "print('outputs: {}, shape: {}'.format(outputs, outputs.shape))\n", 122 | "print('states: {}, shape: {}'.format(states, states.shape))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Unfolding to n sequences" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "![image](https://cloud.githubusercontent.com/assets/901975/23383634/649efd0a-fd82-11e6-925d-8041242743b0.png)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 5, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "x_data: [[[1. 0. 0. 0.]\n", 149 | " [0. 1. 0. 0.]\n", 150 | " [0. 0. 1. 0.]\n", 151 | " [0. 0. 1. 0.]\n", 152 | " [0. 0. 0. 1.]]], shape: (1, 5, 4) \n", 153 | "\n", 154 | "outputs: [[[ 0.36337885 0.73452437]\n", 155 | " [ 0.23541063 -0.28505793]\n", 156 | " [-0.19638212 -0.54805404]\n", 157 | " [-0.589804 -0.65221256]\n", 158 | " [-0.8427679 0.19108507]]], shape: (1, 5, 2) \n", 159 | "\n", 160 | "states: [[-0.8427679 0.19108507]], shape: (1, 2)\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "# One cell RNN input_dim (4) -> output_dim (2). sequence: 5\n", 166 | "x_data = np.array([[h, e, l, l, o]], dtype=np.float32)\n", 167 | "\n", 168 | "hidden_size = 2\n", 169 | "rnn = layers.SimpleRNN(units=2, return_sequences=True, return_state=True) \n", 170 | "outputs, states = rnn(x_data)\n", 171 | "\n", 172 | "print('x_data: {}, shape: {} \\n'.format(x_data, x_data.shape))\n", 173 | "print('outputs: {}, shape: {} \\n'.format(outputs, outputs.shape))\n", 174 | "print('states: {}, shape: {}'.format(states, states.shape))" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "### Batching input" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "![image](https://cloud.githubusercontent.com/assets/901975/23383681/9943a9fc-fd82-11e6-8121-bd187994e249.png)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 6, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "x_data: [[[1. 0. 0. 0.]\n", 201 | " [0. 1. 0. 0.]\n", 202 | " [0. 0. 1. 0.]\n", 203 | " [0. 0. 1. 0.]\n", 204 | " [0. 0. 0. 1.]]\n", 205 | "\n", 206 | " [[0. 1. 0. 0.]\n", 207 | " [0. 0. 0. 1.]\n", 208 | " [0. 0. 1. 0.]\n", 209 | " [0. 0. 1. 0.]\n", 210 | " [0. 0. 1. 0.]]\n", 211 | "\n", 212 | " [[0. 0. 1. 0.]\n", 213 | " [0. 0. 1. 0.]\n", 214 | " [0. 1. 0. 0.]\n", 215 | " [0. 1. 0. 0.]\n", 216 | " [0. 0. 1. 0.]]], shape: (3, 5, 4) \n", 217 | "\n", 218 | "outputs: [[[-0.56743866 -0.23173441]\n", 219 | " [ 0.8334968 0.30382484]\n", 220 | " [-0.93795335 -0.53330624]\n", 221 | " [-0.0874121 0.45240858]\n", 222 | " [-0.52966034 -0.5007928 ]]\n", 223 | "\n", 224 | " [[ 0.58572567 0.00302648]\n", 225 | " [-0.8668559 -0.19943324]\n", 226 | " [-0.10968345 0.14605513]\n", 227 | " [-0.6695559 -0.29440066]\n", 228 | " [-0.30860662 0.20964848]]\n", 229 | "\n", 230 | " [[-0.73496014 -0.17280702]\n", 231 | " [-0.23251031 0.10149368]\n", 232 | " [ 0.723902 -0.06399264]\n", 233 | " [-0.0544093 -0.03742061]\n", 234 | " [-0.7118617 -0.12900047]]], shape: (3, 5, 2) \n", 235 | "\n", 236 | "states: [[-0.52966034 -0.5007928 ]\n", 237 | " [-0.30860662 0.20964848]\n", 238 | " [-0.7118617 -0.12900047]], shape: (3, 2)\n" 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "# One cell RNN input_dim (4) -> output_dim (2). sequence: 5, batch 3\n", 244 | "# 3 batches 'hello', 'eolll', 'lleel'\n", 245 | "x_data = np.array([[h, e, l, l, o],\n", 246 | " [e, o, l, l, l],\n", 247 | " [l, l, e, e, l]], dtype=np.float32)\n", 248 | "\n", 249 | "hidden_size = 2\n", 250 | "rnn = layers.SimpleRNN(units=2, return_sequences=True, return_state=True) \n", 251 | "outputs, states = rnn(x_data)\n", 252 | "\n", 253 | "print('x_data: {}, shape: {} \\n'.format(x_data, x_data.shape))\n", 254 | "print('outputs: {}, shape: {} \\n'.format(outputs, outputs.shape))\n", 255 | "print('states: {}, shape: {}'.format(states, states.shape))" 256 | ] 257 | } 258 | ], 259 | "metadata": { 260 | "kernelspec": { 261 | "display_name": "Python 3", 262 | "language": "python", 263 | "name": "python3" 264 | }, 265 | "language_info": { 266 | "codemirror_mode": { 267 | "name": "ipython", 268 | "version": 3 269 | }, 270 | "file_extension": ".py", 271 | "mimetype": "text/x-python", 272 | "name": "python", 273 | "nbconvert_exporter": "python", 274 | "pygments_lexer": "ipython3", 275 | "version": "3.6.8" 276 | } 277 | }, 278 | "nbformat": 4, 279 | "nbformat_minor": 1 280 | } 281 | -------------------------------------------------------------------------------- /origin_source/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 | -------------------------------------------------------------------------------- /origin_source/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 | -------------------------------------------------------------------------------- /origin_source/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 | -------------------------------------------------------------------------------- /origin_source/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 | -------------------------------------------------------------------------------- /origin_source/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 | 56 | # train/test split 57 | train_size = int(len(xy) * 0.7) 58 | train_set = xy[0:train_size] 59 | test_set = xy[train_size - seq_length:] # Index from [train_size - seq_length] to utilize past sequence 60 | 61 | # Scale each 62 | train_set = MinMaxScaler(train_set) 63 | test_set = MinMaxScaler(test_set) 64 | 65 | # build datasets 66 | def build_dataset(time_series, seq_length): 67 | dataX = [] 68 | dataY = [] 69 | for i in range(0, len(time_series) - seq_length): 70 | _x = time_series[i:i + seq_length, :] 71 | _y = time_series[i + seq_length, [-1]] # Next close price 72 | print(_x, "->", _y) 73 | dataX.append(_x) 74 | dataY.append(_y) 75 | return np.array(dataX), np.array(dataY) 76 | 77 | trainX, trainY = build_dataset(train_set, seq_length) 78 | testX, testY = build_dataset(test_set, seq_length) 79 | 80 | # input place holders 81 | X = tf.placeholder(tf.float32, [None, seq_length, data_dim]) 82 | Y = tf.placeholder(tf.float32, [None, 1]) 83 | 84 | # build a LSTM network 85 | cell = tf.contrib.rnn.BasicLSTMCell( 86 | num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh) 87 | outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) 88 | Y_pred = tf.contrib.layers.fully_connected( 89 | outputs[:, -1], output_dim, activation_fn=None) # We use the last cell's output 90 | 91 | # cost/loss 92 | loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares 93 | # optimizer 94 | optimizer = tf.train.AdamOptimizer(learning_rate) 95 | train = optimizer.minimize(loss) 96 | 97 | # RMSE 98 | targets = tf.placeholder(tf.float32, [None, 1]) 99 | predictions = tf.placeholder(tf.float32, [None, 1]) 100 | rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions))) 101 | 102 | with tf.Session() as sess: 103 | init = tf.global_variables_initializer() 104 | sess.run(init) 105 | 106 | # Training step 107 | for i in range(iterations): 108 | _, step_loss = sess.run([train, loss], feed_dict={ 109 | X: trainX, Y: trainY}) 110 | print("[step: {}] loss: {}".format(i, step_loss)) 111 | 112 | # Test step 113 | test_predict = sess.run(Y_pred, feed_dict={X: testX}) 114 | rmse_val = sess.run(rmse, feed_dict={ 115 | targets: testY, predictions: test_predict}) 116 | print("RMSE: {}".format(rmse_val)) 117 | 118 | # Plot predictions 119 | plt.plot(testY) 120 | plt.plot(test_predict) 121 | plt.xlabel("Time Period") 122 | plt.ylabel("Stock Price") 123 | plt.show() 124 | -------------------------------------------------------------------------------- /others/lab-03-2-Linear-Regression-and-Minimizing-Cost-keras.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# 04 - 01 Multiple Input\n", 12 | "\n", 13 | "" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "slideshow": { 21 | "slide_type": "slide" 22 | } 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "import tensorflow as tf\n", 27 | "import numpy as np" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "Overwriting data-01-test-score.csv\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "%%writefile \"data-01-test-score.csv\"\n", 45 | "73,80,75,152\n", 46 | "93,88,93,185\n", 47 | "89,91,90,180\n", 48 | "96,98,100,196\n", 49 | "73,66,70,142\n", 50 | "53,46,55,101\n", 51 | "69,74,77,149\n", 52 | "47,56,60,115\n", 53 | "87,79,90,175\n", 54 | "79,70,88,164\n", 55 | "69,70,73,141\n", 56 | "70,65,74,141\n", 57 | "93,95,91,184\n", 58 | "79,80,73,152\n", 59 | "70,73,78,148\n", 60 | "93,89,96,192\n", 61 | "78,75,68,147\n", 62 | "81,90,93,183\n", 63 | "88,92,86,177\n", 64 | "78,83,77,159\n", 65 | "82,86,90,177\n", 66 | "86,82,89,175\n", 67 | "78,83,85,175\n", 68 | "76,83,71,149\n", 69 | "96,93,95,192" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 3, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "x_data [[ 73. 80. 75.]\n", 82 | " [ 93. 88. 93.]\n", 83 | " [ 89. 91. 90.]\n", 84 | " [ 96. 98. 100.]\n", 85 | " [ 73. 66. 70.]\n", 86 | " [ 53. 46. 55.]\n", 87 | " [ 69. 74. 77.]\n", 88 | " [ 47. 56. 60.]\n", 89 | " [ 87. 79. 90.]\n", 90 | " [ 79. 70. 88.]\n", 91 | " [ 69. 70. 73.]\n", 92 | " [ 70. 65. 74.]\n", 93 | " [ 93. 95. 91.]\n", 94 | " [ 79. 80. 73.]\n", 95 | " [ 70. 73. 78.]\n", 96 | " [ 93. 89. 96.]\n", 97 | " [ 78. 75. 68.]\n", 98 | " [ 81. 90. 93.]\n", 99 | " [ 88. 92. 86.]\n", 100 | " [ 78. 83. 77.]\n", 101 | " [ 82. 86. 90.]\n", 102 | " [ 86. 82. 89.]\n", 103 | " [ 78. 83. 85.]\n", 104 | " [ 76. 83. 71.]\n", 105 | " [ 96. 93. 95.]]\n", 106 | "y_data [[152.]\n", 107 | " [185.]\n", 108 | " [180.]\n", 109 | " [196.]\n", 110 | " [142.]\n", 111 | " [101.]\n", 112 | " [149.]\n", 113 | " [115.]\n", 114 | " [175.]\n", 115 | " [164.]\n", 116 | " [141.]\n", 117 | " [141.]\n", 118 | " [184.]\n", 119 | " [152.]\n", 120 | " [148.]\n", 121 | " [192.]\n", 122 | " [147.]\n", 123 | " [183.]\n", 124 | " [177.]\n", 125 | " [159.]\n", 126 | " [177.]\n", 127 | " [175.]\n", 128 | " [175.]\n", 129 | " [149.]\n", 130 | " [192.]]\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "xy = np.loadtxt('data-01-test-score.csv', delimiter=',')\n", 136 | "x_data = xy[:, 0:-1]\n", 137 | "y_data = xy[:, [-1]]\n", 138 | "\n", 139 | "print(\"x_data\", x_data)\n", 140 | "print(\"y_data\", y_data)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 4, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "" 152 | ] 153 | }, 154 | "execution_count": 4, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "from tensorflow.keras.models import Sequential\n", 161 | "from tensorflow.keras.layers import Dense\n", 162 | "\n", 163 | "model = Sequential()\n", 164 | "model.add(Dense(input_dim=3, units=1))\n", 165 | "\n", 166 | "model.compile(loss='mse', optimizer='rmsprop')\n", 167 | "model.fit(x_data, y_data, epochs=2000, verbose=0)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 5, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "data": { 177 | "text/plain": [ 178 | "array([[2.7772412]], dtype=float32)" 179 | ] 180 | }, 181 | "execution_count": 5, 182 | "metadata": {}, 183 | "output_type": "execute_result" 184 | } 185 | ], 186 | "source": [ 187 | "model.predict(np.array([[0, 2, 1]]))" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 6, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "array([[5.0239377]], dtype=float32)" 199 | ] 200 | }, 201 | "execution_count": 6, 202 | "metadata": {}, 203 | "output_type": "execute_result" 204 | } 205 | ], 206 | "source": [ 207 | "model.predict(np.array([[0, 9, -1]]))" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 7, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stderr", 217 | "output_type": "stream", 218 | "text": [ 219 | "Using TensorFlow backend.\n" 220 | ] 221 | }, 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "[[-186.60214]]\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "from keras.models import Sequential\n", 232 | "from keras.layers import Dense, Activation\n", 233 | "from keras.optimizers import RMSprop\n", 234 | "import numpy as np\n", 235 | "\n", 236 | "x_data = np.array(\n", 237 | " [[73., 80., 75.],\n", 238 | " [93., 88., 93.],\n", 239 | " [89., 91., 90.],\n", 240 | " [96., 98., 100.],\n", 241 | " [73., 66., 70.]]\n", 242 | ")\n", 243 | "\n", 244 | "y_data = np.array(\n", 245 | " [152.,185., 180., 196., 142.]\n", 246 | ")\n", 247 | "\n", 248 | "model = Sequential()\n", 249 | "model.add(Dense(input_dim=3, units=1))\n", 250 | "model.add(Activation('linear'))\n", 251 | "\n", 252 | "rmsprop = RMSprop(lr=1e-10)\n", 253 | "model.compile(loss='mse', optimizer=rmsprop)\n", 254 | "model.fit(x_data, y_data, epochs=1000, verbose=0)\n", 255 | "\n", 256 | "y_predict = model.predict(np.array([[95., 100., 80]]))\n", 257 | "print(y_predict)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 8, 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "" 269 | ] 270 | }, 271 | "execution_count": 8, 272 | "metadata": {}, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "source": [ 277 | "from keras.models import Sequential\n", 278 | "from keras.layers import Dense\n", 279 | "import numpy as np\n", 280 | "\n", 281 | "x_data = np.array(\n", 282 | " [[73., 80., 75.],\n", 283 | " [93., 88., 93.],\n", 284 | " [89., 91., 90.],\n", 285 | " [96., 98., 100.],\n", 286 | " [73., 66., 70.]]\n", 287 | ")\n", 288 | "\n", 289 | "y_data = np.array(\n", 290 | " [152.,185., 180., 196., 142.]\n", 291 | ")\n", 292 | "\n", 293 | "model = Sequential()\n", 294 | "model.add(Dense(input_dim=3, units=1))\n", 295 | "\n", 296 | "model.compile(loss='mse', optimizer='rmsprop')\n", 297 | "model.fit(x_data, y_data,epochs=1000,verbose=0)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 9, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "[[193.99263]]\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "y_predict = model.predict(np.array([[96., 98., 99]]))\n", 315 | "print(y_predict)" 316 | ] 317 | } 318 | ], 319 | "metadata": { 320 | "celltoolbar": "Slideshow", 321 | "kernelspec": { 322 | "display_name": "TF-GPU", 323 | "language": "python", 324 | "name": "tf-gpu" 325 | }, 326 | "language_info": { 327 | "codemirror_mode": { 328 | "name": "ipython", 329 | "version": 3 330 | }, 331 | "file_extension": ".py", 332 | "mimetype": "text/x-python", 333 | "name": "python", 334 | "nbconvert_exporter": "python", 335 | "pygments_lexer": "ipython3", 336 | "version": "3.6.7" 337 | } 338 | }, 339 | "nbformat": 4, 340 | "nbformat_minor": 1 341 | } 342 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tensorflow 2 | numpy 3 | matplotlib 4 | keras 5 | scikit_learn 6 | -------------------------------------------------------------------------------- /run_jupyter_docker.sh: -------------------------------------------------------------------------------- 1 | jupyter notebook --ip=0.0.0.0 --allow-root 2 | -------------------------------------------------------------------------------- /tf_2.x/data-01-test-score.csv: -------------------------------------------------------------------------------- 1 | 73,80,75,152 2 | 93,88,93,185 3 | 89,91,90,180 4 | 96,98,100,196 5 | 73,66,70,142 6 | 53,46,55,101 7 | 69,74,77,149 8 | 47,56,60,115 9 | 87,79,90,175 10 | 79,70,88,164 11 | 69,70,73,141 12 | 70,65,74,141 13 | 93,95,91,184 14 | 79,80,73,152 15 | 70,73,78,148 16 | 93,89,96,192 17 | 78,75,68,147 18 | 81,90,93,183 19 | 88,92,86,177 20 | 78,83,77,159 21 | 82,86,90,177 22 | 86,82,89,175 23 | 78,83,85,175 24 | 76,83,71,149 25 | 96,93,95,192 -------------------------------------------------------------------------------- /tf_2.x/data-04-zoo.csv: -------------------------------------------------------------------------------- 1 | # https://archive.ics.uci.edu/ml/machine-learning-databases/zoo/zoo.data,,,,,,,,,,,,,,,, 2 | # 1. animal name: (deleted),,,,,,,,,,,,,,,, 3 | # 2. hair Boolean",,,,,,,,,,,,,,,, 4 | # 3. feathers Boolean",,,,,,,,,,,,,,,, 5 | # 4. eggs Boolean",,,,,,,,,,,,,,,, 6 | # 5. milk Boolean",,,,,,,,,,,,,,,, 7 | # 6. airborne Boolean",,,,,,,,,,,,,,,, 8 | # 7. aquatic Boolean",,,,,,,,,,,,,,,, 9 | # 8. predator Boolean",,,,,,,,,,,,,,,, 10 | # 9. toothed Boolean",,,,,,,,,,,,,,,, 11 | # 10. backbone Boolean",,,,,,,,,,,,,,,, 12 | # 11. breathes Boolean",,,,,,,,,,,,,,,, 13 | # 12. venomous Boolean",,,,,,,,,,,,,,,, 14 | # 13. fins Boolean",,,,,,,,,,,,,,,, 15 | # 14. legs Numeric (set of values: {0",2,4,5,6,8}),,,,,,,,,,, 16 | # 15. tail Boolean",,,,,,,,,,,,,,,, 17 | # 16. domestic Boolean",,,,,,,,,,,,,,,, 18 | # 17. catsize Boolean",,,,,,,,,,,,,,,, 19 | # 18. type Numeric (integer values in range [0",6]),,,,,,,,,,,,,,, 20 | 1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0 21 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 22 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 23 | 1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0 24 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 25 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 26 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 27 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,1,0,3 28 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 29 | 1,0,0,1,0,0,0,1,1,1,0,0,4,0,1,0,0 30 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 31 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,1 32 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 33 | 0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,6 34 | 0,0,1,0,0,1,1,0,0,0,0,0,4,0,0,0,6 35 | 0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,6 36 | 0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,1 37 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 38 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3 39 | 0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0 40 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,1 41 | 0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,0,1 42 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 43 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,1,1 44 | 0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,5 45 | 0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,4 46 | 0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,4 47 | 1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,0 48 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 49 | 1,0,0,1,0,0,1,1,1,1,0,0,2,0,1,1,0 50 | 0,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,5 51 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 52 | 1,0,0,1,0,0,0,1,1,1,0,0,2,0,0,1,0 53 | 0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,1 54 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,3 55 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,0,0 56 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,0 57 | 0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,0,1 58 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 59 | 1,0,1,0,1,0,0,0,0,1,1,0,6,0,1,0,5 60 | 1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,5 61 | 0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,0,1 62 | 0,0,1,0,1,0,1,0,0,1,0,0,6,0,0,0,5 63 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 64 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 65 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 66 | 0,0,1,0,0,1,1,0,0,0,0,0,6,0,0,0,6 67 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 68 | 1,0,0,1,0,1,1,1,1,1,0,0,4,1,0,1,0 69 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,0 70 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 71 | 1,0,1,0,1,0,0,0,0,1,0,0,6,0,0,0,5 72 | 0,0,1,0,0,1,1,1,1,1,0,0,4,1,0,0,4 73 | 0,0,1,0,0,1,1,0,0,0,0,0,8,0,0,1,6 74 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,0,0 75 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0 76 | 0,1,1,0,0,0,0,0,1,1,0,0,2,1,0,1,1 77 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,1,0,1 78 | 0,1,1,0,0,1,1,0,1,1,0,0,2,1,0,1,1 79 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 80 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3 81 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3 82 | 0,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,2 83 | 1,0,1,1,0,1,1,0,1,1,0,0,4,1,0,1,0 84 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 85 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 86 | 0,0,0,1,0,1,1,1,1,1,0,1,0,1,0,1,0 87 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 88 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,1,1,0 89 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 90 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,1,0 91 | 0,1,1,0,0,0,1,0,1,1,0,0,2,1,0,1,1 92 | 0,0,0,0,0,0,1,0,0,1,1,0,8,1,0,0,6 93 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,3 94 | 1,0,0,1,0,1,1,1,1,1,0,1,0,0,0,1,0 95 | 1,0,0,1,0,1,1,1,1,1,0,1,2,1,0,1,0 96 | 0,0,0,0,0,1,1,1,1,0,1,0,0,1,0,0,2 97 | 0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,6 98 | 0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,1 99 | 0,1,1,0,1,1,1,0,1,1,0,0,2,1,0,0,1 100 | 0,0,1,0,0,0,1,1,1,1,0,0,0,1,0,0,2 101 | 0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,6 102 | 0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,3 103 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 104 | 1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,0,0 105 | 0,0,1,0,0,1,1,0,0,0,0,0,5,0,0,0,6 106 | 0,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1,3 107 | 0,1,1,0,1,1,0,0,1,1,0,0,2,1,0,1,1 108 | 0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,5 109 | 0,0,1,0,0,1,0,1,1,1,0,0,4,0,0,0,4 110 | 0,0,1,0,0,0,0,0,1,1,0,0,4,1,0,1,2 111 | 0,0,1,0,0,0,1,1,1,1,0,0,4,1,0,0,2 112 | 0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3 113 | 1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,0 114 | 1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,0 115 | 0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,1,1 116 | 1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,1,0 117 | 1,0,1,0,1,0,0,0,0,1,1,0,6,0,0,0,5 118 | 1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0 119 | 0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,6 120 | 0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1 -------------------------------------------------------------------------------- /tf_2.x/lab-05-2-logistic_regression_diabetes-eager.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 05 Logistic Classification (diabetes) - Eager Execution\n", 8 | "* Logistic Classfication을 diabetes data를 활용하여 모델을 만들어 보도록 하겠습니다\n", 9 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "2.1.0\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import numpy as np\n", 27 | "import matplotlib.pyplot as plt\n", 28 | "%matplotlib inline\n", 29 | "import tensorflow as tf\n", 30 | "\n", 31 | "tf.random.set_seed(777) # for reproducibility\n", 32 | "print(tf.__version__)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "### 강의에 설명할 Data입니다" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "scrolled": true 47 | }, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "(759, 8) (759, 1)\n", 54 | "[[-0.294118 0.487437 0.180328 ... -0.53117 -0.0333333 0. ]\n", 55 | " [-0.882353 -0.145729 0.0819672 ... -0.766866 -0.666667 1. ]\n", 56 | " [-0.0588235 0.839196 0.0491803 ... -0.492741 -0.633333 0. ]\n", 57 | " ...\n", 58 | " [-0.411765 0.21608 0.180328 ... -0.857387 -0.7 1. ]\n", 59 | " [-0.882353 0.266332 -0.0163934 ... -0.768574 -0.133333 0. ]\n", 60 | " [-0.882353 -0.0653266 0.147541 ... -0.797609 -0.933333 1. ]]\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)\n", 66 | "x_train = xy[:, 0:-1]\n", 67 | "y_train = xy[:, [-1]]\n", 68 | "\n", 69 | "print(x_train.shape, y_train.shape)\n", 70 | "print(xy)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "## Tensorflow Eager\n", 78 | "### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", 79 | "* Tensorflow data API를 통해 학습시킬 값들을 담는다 (Batch Size는 한번에 학습시킬 Size로 정한다)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(len(x_train))" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "### 위 Data를 기준으로 가설의 검증을 통해 Logistic Classification 모델을 만들도록 하겠습니다\n", 96 | "* W와 b은 학습을 통해 생성되는 모델에 쓰이는 Wegith와 Bias (초기값을 variable : 0이나 Random값으로 가능 tf.random_normal([2, 1]) )" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 4, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "W = tf.Variable(tf.random.normal((8, 1)), name='weight')\n", 106 | "b = tf.Variable(tf.random.normal((1,)), name='bias')" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "### Sigmoid 함수를 가설로 선언합니다\n", 114 | "* Sigmoid는 아래 그래프와 같이 0과 1의 값만을 리턴합니다 tf.sigmoid(tf.matmul(X, W) + b)와 같습니다\n", 115 | "\n", 116 | "$$\n", 117 | "\\begin{align}\n", 118 | "sigmoid(x) & = \\frac{1}{1+e^{-x}} \\\\\\\\\\\n", 119 | "\\end{align}\n", 120 | "$$\n", 121 | "\n", 122 | "![sigmoid](https://upload.wikimedia.org/wikipedia/commons/8/88/Logistic-curve.svg)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 5, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "def logistic_regression(features):\n", 132 | " hypothesis = tf.divide(1., 1. + tf.exp(tf.matmul(features, W) + b))\n", 133 | " return hypothesis" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "### 가설을 검증할 Cost 함수를 정의합니다\n", 141 | "$$\n", 142 | "\\begin{align}\n", 143 | "cost(h(x),y) & = −log(h(x)) & if & y=1 \\\\\\\\\\\n", 144 | "cost(h(x),y) & = -log(1−h(x)) & if & y=0\n", 145 | "\\end{align}\n", 146 | "$$" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "* 위 두수식을 합치면 아래과 같습니다\n", 154 | "$$\n", 155 | "\\begin{align}\n", 156 | "cost(h(x),y) & = −y log(h(x))−(1−y)log(1−h(x))\n", 157 | "\\end{align}\n", 158 | "$$" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 7, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "def loss_fn(hypothesis, features, labels):\n", 168 | " cost = -tf.reduce_mean(labels * tf.math.log(logistic_regression(features)) + (1 - labels) * tf.math.log(1 - hypothesis))\n", 169 | " return cost\n", 170 | "\n", 171 | "optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "### 추론한 값은 0.5를 기준(Sigmoid 그래프 참조)로 0과 1의 값을 리턴합니다.\n", 179 | "* Sigmoid 함수를 통해 예측값이 0.5보다 크면 1을 반환하고 0.5보다 작으면 0으로 반환합니다." 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 8, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "def accuracy_fn(hypothesis, labels):\n", 189 | " predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)\n", 190 | " accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, labels), dtype=tf.int32))\n", 191 | " return accuracy" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "### GradientTape를 통해 경사값을 계산합니다." 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 9, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "def grad(hypothesis, features, labels):\n", 208 | " with tf.GradientTape() as tape:\n", 209 | " loss_value = loss_fn(logistic_regression(features),features,labels)\n", 210 | " return tape.gradient(loss_value, [W,b])" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "### 학습을 실행합니다. " 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 10, 223 | "metadata": { 224 | "scrolled": true 225 | }, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "Iter: 0, Loss: 0.6556\n", 232 | "Iter: 100, Loss: 0.6188\n", 233 | "Iter: 200, Loss: 0.5980\n", 234 | "Iter: 300, Loss: 0.5854\n", 235 | "Iter: 400, Loss: 0.5769\n", 236 | "Iter: 500, Loss: 0.5704\n", 237 | "Iter: 600, Loss: 0.5648\n", 238 | "Iter: 700, Loss: 0.5599\n", 239 | "Iter: 800, Loss: 0.5555\n", 240 | "Iter: 900, Loss: 0.5513\n", 241 | "Iter: 1000, Loss: 0.5475\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "EPOCHS = 1001\n", 247 | "\n", 248 | "for step in range(EPOCHS):\n", 249 | " for features, labels in iter(dataset):\n", 250 | " grads = grad(logistic_regression(features), features, labels)\n", 251 | " optimizer.apply_gradients(grads_and_vars=zip(grads,[W,b]))\n", 252 | " if step % 100 == 0:\n", 253 | " print(\"Iter: {}, Loss: {:.4f}\".format(step, loss_fn(logistic_regression(features),features,labels)))" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Python 3", 267 | "language": "python", 268 | "name": "python3" 269 | }, 270 | "language_info": { 271 | "codemirror_mode": { 272 | "name": "ipython", 273 | "version": 3 274 | }, 275 | "file_extension": ".py", 276 | "mimetype": "text/x-python", 277 | "name": "python", 278 | "nbconvert_exporter": "python", 279 | "pygments_lexer": "ipython3", 280 | "version": "3.7.3" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 4 285 | } 286 | -------------------------------------------------------------------------------- /tf_2.x/lab-06-2-softmax_zoo_classifier-eager.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 06 Softmax Zoo_classifier-eager\n", 8 | "\n", 9 | "* Softmax를 사용하여 Zoo 데이터를 활용하여 분류를 진행합니다.\n", 10 | "\n", 11 | "### 기본 Library 선언 및 Tensorflow 버전 확인" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "2.1.0\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "import tensorflow as tf\n", 29 | "import numpy as np\n", 30 | "\n", 31 | "print(tf.__version__)\n", 32 | "\n", 33 | "tf.random.set_seed(777) # for reproducibility" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "(101, 16) (101, 7)\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)\n", 51 | "x_data = xy[:, 0:-1]\n", 52 | "y_data = xy[:, -1]\n", 53 | "\n", 54 | "nb_classes = 7 # 0 ~ 6\n", 55 | "\n", 56 | "# Make Y data as onehot shape\n", 57 | "Y_one_hot = tf.one_hot(y_data.astype(np.int32), nb_classes)\n", 58 | "\n", 59 | "print(x_data.shape, Y_one_hot.shape)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "#Weight and bias setting\n", 69 | "W = tf.Variable(tf.random.normal((16, nb_classes)), name='weight')\n", 70 | "b = tf.Variable(tf.random.normal((nb_classes,)), name='bias')\n", 71 | "variables = [W, b]\n", 72 | "\n", 73 | "# tf.nn.softmax computes softmax activations\n", 74 | "# softmax = exp(logits) / reduce_sum(exp(logits), dim)\n", 75 | "def logit_fn(X):\n", 76 | " return tf.matmul(X, W) + b\n", 77 | "\n", 78 | "def hypothesis(X):\n", 79 | " return tf.nn.softmax(logit_fn(X))\n", 80 | "\n", 81 | "def cost_fn(X, Y):\n", 82 | " logits = logit_fn(X)\n", 83 | " cost_i = tf.keras.losses.categorical_crossentropy(y_true=Y, y_pred=logits, \n", 84 | " from_logits=True) \n", 85 | " cost = tf.reduce_mean(cost_i) \n", 86 | " return cost\n", 87 | "\n", 88 | "def grad_fn(X, Y):\n", 89 | " with tf.GradientTape() as tape:\n", 90 | " loss = cost_fn(X, Y)\n", 91 | " grads = tape.gradient(loss, variables)\n", 92 | " return grads\n", 93 | " \n", 94 | "def prediction(X, Y):\n", 95 | " pred = tf.argmax(hypothesis(X), 1)\n", 96 | " correct_prediction = tf.equal(pred, tf.argmax(Y, 1))\n", 97 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 98 | "\n", 99 | " return accuracy" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 4, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "Steps: 1 Loss: 3.635028839111328, Acc: 0.1683168262243271\n", 112 | "Steps: 100 Loss: 0.5194157958030701, Acc: 0.7920792102813721\n", 113 | "Steps: 200 Loss: 0.31850090622901917, Acc: 0.9108911156654358\n", 114 | "Steps: 300 Loss: 0.23534879088401794, Acc: 0.9405940771102905\n", 115 | "Steps: 400 Loss: 0.1887214034795761, Acc: 0.9504950642585754\n", 116 | "Steps: 500 Loss: 0.158460333943367, Acc: 0.9504950642585754\n", 117 | "Steps: 600 Loss: 0.13703754544258118, Acc: 0.9900990128517151\n", 118 | "Steps: 700 Loss: 0.12098979949951172, Acc: 0.9900990128517151\n", 119 | "Steps: 800 Loss: 0.10847963392734528, Acc: 1.0\n", 120 | "Steps: 900 Loss: 0.09843038767576218, Acc: 1.0\n", 121 | "Steps: 1000 Loss: 0.09016558527946472, Acc: 1.0\n" 122 | ] 123 | } 124 | ], 125 | "source": [ 126 | "def fit(X, Y, epochs=1000, verbose=100):\n", 127 | " optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)\n", 128 | "\n", 129 | " for i in range(epochs):\n", 130 | " grads = grad_fn(X, Y)\n", 131 | " optimizer.apply_gradients(zip(grads, variables))\n", 132 | " if (i==0) | ((i+1)%verbose==0):\n", 133 | "# print('Loss at epoch %d: %f' %(i+1, cost_fn(X, Y).numpy()))\n", 134 | " acc = prediction(X, Y).numpy()\n", 135 | " loss = cost_fn(X, Y).numpy() \n", 136 | " print('Steps: {} Loss: {}, Acc: {}'.format(i+1, loss, acc))\n", 137 | "\n", 138 | "fit(x_data, Y_one_hot)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [] 147 | } 148 | ], 149 | "metadata": { 150 | "kernelspec": { 151 | "display_name": "Python 3", 152 | "language": "python", 153 | "name": "python3" 154 | }, 155 | "language_info": { 156 | "codemirror_mode": { 157 | "name": "ipython", 158 | "version": 3 159 | }, 160 | "file_extension": ".py", 161 | "mimetype": "text/x-python", 162 | "name": "python", 163 | "nbconvert_exporter": "python", 164 | "pygments_lexer": "ipython3", 165 | "version": "3.7.3" 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 4 170 | } 171 | -------------------------------------------------------------------------------- /tf_2.x/lab-07-4-mnist_introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 07-4 MNIST introduction\n", 8 | "* MNIST(Modified National Institute of Standards and Technology database)는 0~9까지의 손으로 쓴 숫자들로 이루어진 대형 데이터베이스\n", 9 | "\n", 10 | "### 기본 Library 선언 및 TensorFlow 버전 확인" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "2.1.0\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "import numpy as np\n", 28 | "import tensorflow as tf\n", 29 | "\n", 30 | "tf.random.set_seed(777) # for reproducibility\n", 31 | "print(tf.__version__)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "### 강의에 설명할 Data입니다\n", 39 | "* Tensorflow 2.0 버전에 맞게 Keras를 활용한 MNIST를 분류 모델 생성" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "scrolled": true 47 | }, 48 | "outputs": [], 49 | "source": [ 50 | "mnist = tf.keras.datasets.mnist\n", 51 | "\n", 52 | "(x_train, y_train),(x_test, y_test) = mnist.load_data()\n", 53 | "x_train, x_test = x_train / 255.0, x_test / 255.0" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Tensorflow Keras\n", 61 | "#### 위 Data를 기준으로 분류 모델을 만들도록 하겠습니다\n", 62 | "* Tensorflow keras API를 통해 모델에 대한 정의" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "model = tf.keras.models.Sequential([\n", 72 | " tf.keras.layers.Flatten(),\n", 73 | " tf.keras.layers.Dense(512, activation=tf.nn.relu),\n", 74 | " tf.keras.layers.Dropout(0.2),\n", 75 | " tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n", 76 | "])" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "* Adam Optimizer과 Cross Entropy Loss 선언" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "model.compile(optimizer='adam',\n", 93 | " loss='sparse_categorical_crossentropy',\n", 94 | " metrics=['accuracy'])" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "* 5 Epoch로 학습할 Data로 학습 수행" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 5, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "name": "stdout", 111 | "output_type": "stream", 112 | "text": [ 113 | "Train on 60000 samples\n", 114 | "Epoch 1/5\n", 115 | "60000/60000 [==============================] - 4s 69us/sample - loss: 0.2193 - accuracy: 0.9344\n", 116 | "Epoch 2/5\n", 117 | "60000/60000 [==============================] - 4s 66us/sample - loss: 0.0957 - accuracy: 0.9706\n", 118 | "Epoch 3/5\n", 119 | "60000/60000 [==============================] - 4s 65us/sample - loss: 0.0689 - accuracy: 0.9782\n", 120 | "Epoch 4/5\n", 121 | "60000/60000 [==============================] - 4s 63us/sample - loss: 0.0528 - accuracy: 0.9830\n", 122 | "Epoch 5/5\n", 123 | "60000/60000 [==============================] - 4s 64us/sample - loss: 0.0428 - accuracy: 0.9861\n" 124 | ] 125 | }, 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "" 130 | ] 131 | }, 132 | "execution_count": 5, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "model.fit(x_train, y_train, epochs=5)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "* 모델을 평가할 Test 데이타에 대한 정의" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 6, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "10000/10000 [==============================] - 0s 27us/sample - loss: 0.0704 - accuracy: 0.9787\n" 158 | ] 159 | }, 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "[0.07035357448178693, 0.9787]" 164 | ] 165 | }, 166 | "execution_count": 6, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "model.evaluate(x_test, y_test)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [] 181 | } 182 | ], 183 | "metadata": { 184 | "kernelspec": { 185 | "display_name": "Python 3", 186 | "language": "python", 187 | "name": "python3" 188 | }, 189 | "language_info": { 190 | "codemirror_mode": { 191 | "name": "ipython", 192 | "version": 3 193 | }, 194 | "file_extension": ".py", 195 | "mimetype": "text/x-python", 196 | "name": "python", 197 | "nbconvert_exporter": "python", 198 | "pygments_lexer": "ipython3", 199 | "version": "3.7.6" 200 | } 201 | }, 202 | "nbformat": 4, 203 | "nbformat_minor": 4 204 | } 205 | -------------------------------------------------------------------------------- /tf_2.x/lab-12-0-rnn-basics-keras-eager.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab 12-0 rnn basics" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "# setup\n", 17 | "import numpy as np\n", 18 | "import tensorflow as tf\n", 19 | "from tensorflow import keras\n", 20 | "from tensorflow.keras import layers\n", 21 | "from tensorflow.keras import Sequential, Model\n", 22 | "\n", 23 | "print(tf.__version__)" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "### Preparing dataset" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "# One hot encoding for each char in 'hello'\n", 40 | "h = [1, 0, 0, 0]\n", 41 | "e = [0, 1, 0, 0]\n", 42 | "l = [0, 0, 1, 0]\n", 43 | "o = [0, 0, 0, 1]" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "### One cell: 4 (input_dim) in 2 (hidden_size)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "![image](https://cloud.githubusercontent.com/assets/901975/23348727/cc981856-fce7-11e6-83ea-4b187473466b.png)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "x_data: [[[1. 0. 0. 0.]]], shape: (1, 1, 4)\n", 70 | "outputs: [[[0.29165375 0.30209106]]], shape: (1, 1, 2)\n", 71 | "states: [[0.29165375 0.30209106]], shape: (1, 2)\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "# One cell RNN input_dim (4) -> output_dim (2)\n", 77 | "x_data = np.array([[h]], dtype=np.float32)\n", 78 | "\n", 79 | "hidden_size = 2\n", 80 | "cell = layers.SimpleRNNCell(units=hidden_size) # creating SimpleRNNCell\n", 81 | "rnn = layers.RNN(cell, return_sequences=True, return_state=True) # analogous to tf.nn.dynamic_rnn\n", 82 | "outputs, states = rnn(x_data)\n", 83 | "\n", 84 | "print('x_data: {}, shape: {}'.format(x_data, x_data.shape))\n", 85 | "print('outputs: {}, shape: {}'.format(outputs, outputs.shape))\n", 86 | "print('states: {}, shape: {}'.format(states, states.shape))" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "x_data: [[[1. 0. 0. 0.]]], shape: (1, 1, 4)\n", 99 | "outputs: [[[-0.7117611 -0.28176913]]], shape: (1, 1, 2)\n", 100 | "states: [[-0.7117611 -0.28176913]], shape: (1, 2)\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "# equivalent to above case\n", 106 | "rnn = layers.SimpleRNN(units=hidden_size, return_sequences=True,\n", 107 | " return_state=True) # layers.SimpleRNNCell + layers.RNN\n", 108 | "\n", 109 | "outputs, states = rnn(x_data)\n", 110 | "\n", 111 | "print('x_data: {}, shape: {}'.format(x_data, x_data.shape))\n", 112 | "print('outputs: {}, shape: {}'.format(outputs, outputs.shape))\n", 113 | "print('states: {}, shape: {}'.format(states, states.shape))" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "### Unfolding to n sequences" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "![image](https://cloud.githubusercontent.com/assets/901975/23383634/649efd0a-fd82-11e6-925d-8041242743b0.png)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 5, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "x_data: [[[1. 0. 0. 0.]\n", 140 | " [0. 1. 0. 0.]\n", 141 | " [0. 0. 1. 0.]\n", 142 | " [0. 0. 1. 0.]\n", 143 | " [0. 0. 0. 1.]]], shape: (1, 5, 4) \n", 144 | "\n", 145 | "outputs: [[[0.47337022 0.3929786 ]\n", 146 | " [0.39179134 0.8714687 ]\n", 147 | " [0.20688158 0.670787 ]\n", 148 | " [0.08288943 0.5157672 ]\n", 149 | " [0.16079137 0.22633114]]], shape: (1, 5, 2) \n", 150 | "\n", 151 | "states: [[0.16079137 0.22633114]], shape: (1, 2)\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "# One cell RNN input_dim (4) -> output_dim (2). sequence: 5\n", 157 | "x_data = np.array([[h, e, l, l, o]], dtype=np.float32)\n", 158 | "\n", 159 | "hidden_size = 2\n", 160 | "rnn = layers.SimpleRNN(units=2, return_sequences=True, return_state=True) \n", 161 | "outputs, states = rnn(x_data)\n", 162 | "\n", 163 | "print('x_data: {}, shape: {} \\n'.format(x_data, x_data.shape))\n", 164 | "print('outputs: {}, shape: {} \\n'.format(outputs, outputs.shape))\n", 165 | "print('states: {}, shape: {}'.format(states, states.shape))" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "### Batching input" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "![image](https://cloud.githubusercontent.com/assets/901975/23383681/9943a9fc-fd82-11e6-8121-bd187994e249.png)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 6, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "x_data: [[[1. 0. 0. 0.]\n", 192 | " [0. 1. 0. 0.]\n", 193 | " [0. 0. 1. 0.]\n", 194 | " [0. 0. 1. 0.]\n", 195 | " [0. 0. 0. 1.]]\n", 196 | "\n", 197 | " [[0. 1. 0. 0.]\n", 198 | " [0. 0. 0. 1.]\n", 199 | " [0. 0. 1. 0.]\n", 200 | " [0. 0. 1. 0.]\n", 201 | " [0. 0. 1. 0.]]\n", 202 | "\n", 203 | " [[0. 0. 1. 0.]\n", 204 | " [0. 0. 1. 0.]\n", 205 | " [0. 1. 0. 0.]\n", 206 | " [0. 1. 0. 0.]\n", 207 | " [0. 0. 1. 0.]]], shape: (3, 5, 4) \n", 208 | "\n", 209 | "outputs: [[[-0.645808 0.72146475]\n", 210 | " [-0.72302413 0.39660427]\n", 211 | " [-0.6221616 -0.04099922]\n", 212 | " [-0.37129605 0.24864785]\n", 213 | " [ 0.31115216 0.09358862]]\n", 214 | "\n", 215 | " [[ 0.03242071 0.55417895]\n", 216 | " [ 0.437557 -0.37179688]\n", 217 | " [ 0.57976246 -0.10018117]\n", 218 | " [ 0.5495411 -0.38326326]\n", 219 | " [ 0.6407725 -0.1557154 ]]\n", 220 | "\n", 221 | " [[ 0.0893703 -0.14354324]\n", 222 | " [ 0.24137822 -0.0806519 ]\n", 223 | " [ 0.26843232 0.49897614]\n", 224 | " [-0.04325086 0.06279302]\n", 225 | " [ 0.01772678 -0.16835481]]], shape: (3, 5, 2) \n", 226 | "\n", 227 | "states: [[ 0.31115216 0.09358862]\n", 228 | " [ 0.6407725 -0.1557154 ]\n", 229 | " [ 0.01772678 -0.16835481]], shape: (3, 2)\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "# One cell RNN input_dim (4) -> output_dim (2). sequence: 5, batch 3\n", 235 | "# 3 batches 'hello', 'eolll', 'lleel'\n", 236 | "x_data = np.array([[h, e, l, l, o],\n", 237 | " [e, o, l, l, l],\n", 238 | " [l, l, e, e, l]], dtype=np.float32)\n", 239 | "\n", 240 | "hidden_size = 2\n", 241 | "rnn = layers.SimpleRNN(units=2, return_sequences=True, return_state=True) \n", 242 | "outputs, states = rnn(x_data)\n", 243 | "\n", 244 | "print('x_data: {}, shape: {} \\n'.format(x_data, x_data.shape))\n", 245 | "print('outputs: {}, shape: {} \\n'.format(outputs, outputs.shape))\n", 246 | "print('states: {}, shape: {}'.format(states, states.shape))" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": null, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [] 255 | } 256 | ], 257 | "metadata": { 258 | "kernelspec": { 259 | "display_name": "Python 3", 260 | "language": "python", 261 | "name": "python3" 262 | }, 263 | "language_info": { 264 | "codemirror_mode": { 265 | "name": "ipython", 266 | "version": 3 267 | }, 268 | "file_extension": ".py", 269 | "mimetype": "text/x-python", 270 | "name": "python", 271 | "nbconvert_exporter": "python", 272 | "pygments_lexer": "ipython3", 273 | "version": "3.7.3" 274 | } 275 | }, 276 | "nbformat": 4, 277 | "nbformat_minor": 1 278 | } 279 | --------------------------------------------------------------------------------