Skip to content

Instantly share code, notes, and snippets.

@ecjang
Created January 21, 2018 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecjang/39d6dd18645a0b50f8db15e56a103970 to your computer and use it in GitHub Desktop.
Save ecjang/39d6dd18645a0b50f8db15e56a103970 to your computer and use it in GitHub Desktop.
001. Install & Basic
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*** 02. ML lec 01 - 기본적인 Machine Learning 의 용어와 개념 설명 ***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. What's ML?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Explicit Programming : 정확히 구분해서 만든 프로그램 \n",
" - 자동 운전을 프로그래밍하려고 하면 너무나 많은 룰이 필요.\n",
"- Arthur Samuel (1959) : 스스로 학습할 수 있는 프로그램을 개발, "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Learning Type"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. **Superviceed** : 정해진 데이터(Traing Set)로 학습 / ex> 개와 고양이 사진을 이용해 둘을 구분하는 능력을 학습시킴\n",
" \n",
" 1. Regression : 일반적인 방식 | 공부시간별로 점수 예측하기 \n",
" 2. Binary Classification : 참·거짓으로 구분 | 합격·불합격으로 나눔 \n",
" 3. MultiLabel Classification : 등급으로 나눔 | A,B,C,D,E 등급 \n",
" \n",
" \n",
"2. **Unsuperviced** : 미리 레이블을 정하기 어려운 자료로 학습 / ex> 비슷한 뉴스를 모으기, 단어 유추하기"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-----"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***03. ML lab 01 - TensorFlow의 설치및 기본적인 operations (new)***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Tensorflow "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 구글에서 만든 오픈소스 라이브러리. / 가장 많이 사용하고, 자료가 풍부.\n",
"- `Tensorflow is an open source software library for numerical computation using data flow graph`\n",
"- 데이터 플로우 그래프를 사용해서 수치적인 계산을 하는 오픈소스 라이브러리\n",
"\n",
" 1. Data Flow Graph\n",
" - 그래프 : 노드와 엣지로 구성. 노드간 엣지로 연결되어 있는 것.\n",
" - 이런 그래프들간의 관계를 나타낸 것 Data Flow Graph\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.5.4 |Anaconda, Inc.| (default, Sep 19 2017, 08:15:17) [MSC v.1900 64 bit (AMD64)]'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Python 버전 체크 : 3.5버전에서만 tensrflow 설치 가능\n",
"import sys\n",
"sys.version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Install Tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2-1. Install Tensorflow : `pip install --upgrade tensorflow`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `pip install --upgrade tensorflow` / `pip install --upgrade tensorflow-gpu`\n",
"- [Installing TensorFlow Link : https://www.tensorflow.org/install/](https://www.tensorflow.org/install/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! pip install --upgrade tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2-2. Install Tensorflow with Anaconda Prompt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Anaconda install : https://www.anaconda.com/download/\n",
"2. Set Conda : `conda create -n tensorflow python=3.5` \n",
"3. Activate Conda : `activae tensorflow`\n",
"4. install tensorflow : `pip install --ignore-install --upgrade tensorflow`\n",
" - 파이썬 3.5버전의 가상환경을 만들고 tensorflow를 설치"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!conda create -n tensorflow python=3.5\n",
"!activate tensorflow\n",
"!pip install --ignore-install --upgrade tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2-3. Install Tensorflow in Jupyer Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Create Environment : `conda create -n py35 python=3.5 anaconda`\n",
"2. Activate py35 : `activate py35`\n",
"3. install Tensorflow : `install -c conda-forge tensorflow` or `pip install --upgrade tensorflow`\n",
"4. Kernel install : `python -m ipykernel install --name py35`\n",
" - http://bryan7.tistory.com/719"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!conda create -n py35 python=3.5 anaconda\n",
"!activate py35\n",
"!conda install -c conda-forge tensorflow\n",
"!python -m ipykernel install --name py35"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2-4 Check install : Check Version"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.4.0'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import tensorflow as tf\n",
"tf.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3 Jupyter Nootbook Kernel Setting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3-1 Kernel List : `jupyter kernelspec list`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 설치된 쥬피터 노트북의 커널 확인 : `jupyter kernelspec list`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available kernels:\n",
" python3 C:\\Users\\Insilicogen\\AppData\\Roaming\\jupyter\\kernels\\python3\n",
" py35 C:\\ProgramData\\jupyter\\kernels\\py35\n",
" python3.5 C:\\ProgramData\\jupyter\\kernels\\python3.5\n"
]
}
],
"source": [
"# 설치된 커널 확인\n",
"!jupyter kernelspec list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3-2 Kernel Remove : `ipython kernelspec uninstall [NAME]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 커널 목록 확인후 원하는 커널을 삭제\n",
"!ipython kernelspec uninstall py35"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3-3 ipython Envinorment install Kernel : `python -m ipykernel install --name [NAME]`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://stackoverflow.com/questions/28831854/how-do-i-add-python3-kernel-to-jupyter-ipython"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 가상환경이 있을 경우 먼저 지우고 실행\n",
"!deactivate py35\n",
"!conda remove --name py35 --all"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Python 3.5 가상환경 만들고 커널 추가하기\n",
"!conda create -n py35 python=3.5\n",
"!activate py35\n",
"!python -m ipykernel install --name py35"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4 Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4-1 Constant Print"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- b'Stirng' -> 'b' :Bytes literals."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'Hello Tensorflow'\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"hello = tf.constant('Hello Tensorflow')\n",
"sess = tf.Session()\n",
"print(sess.run(hello))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Mechanics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5-1 Node"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# 1.Graph(Tensor) Build\n",
"node1 = tf.constant(3.0, tf.float32)\n",
"node2 = tf.constant(4.0) # also tf.float32 implicitly\n",
"node3 = tf.add(node1, node2) # node1, node2가 연결"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node1: Tensor(\"Const_2:0\", shape=(), dtype=float32) node2: Tensor(\"Const_3:0\", shape=(), dtype=float32)\n",
"node3: Tensor(\"Add:0\", shape=(), dtype=float32)\n"
]
}
],
"source": [
"# 2.Feed Data Run Graph (Sess)\n",
"print('node1: ', node1, 'node2: ', node2)\n",
"print('node3: ', node3) # 그냥 tensor일뿐 결과값이 없다."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sess.run(node1, node2): [3.0, 4.0]\n",
"sess.run(node3) 7.0\n"
]
}
],
"source": [
"# 3.Update Variables\n",
"sess = tf.Session()\n",
"print(\"sess.run(node1, node2): \", sess.run([node1, node2]))\n",
"print(\"sess.run(node3)\", sess.run(node3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. 그래프를 `build`하여 만듬.\n",
"2. `sess.run(op)` 로 그래프를 실행.\n",
"3. 결과값을 출력하거나 갱신.\n",
"\n",
"<img src=\"img/mechaincs.jpg\" width=\"70%\", align=\"left\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5-2 Placeholder : 변수처럼 임의의 값을 설정"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.5\n",
"[3. 7.]\n"
]
}
],
"source": [
"# deef_dict으로 placeholder에 값을 넘긴다.\n",
"a = tf.placeholder(tf.float32)\n",
"b = tf.placeholder(tf.float32)\n",
"adder_node = a + b\n",
"\n",
"print(sess.run(adder_node, feed_dict={a: 3, b:4.5}))\n",
"print(sess.run(adder_node, feed_dict={a: [1,3], b: [2,4]}))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5-3 Tensors : Array와 같은 의미"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **Rank** : 차원\n",
" 1. s(Scalar) : Rank 0, 일반적인 숫자\n",
" 1. v(Vector) : Rank 1, 1차원 배열\n",
" 1. m(Matrix) : Rank 2, 2차원 배열\n",
" 1. t(3-Tensor) : Rank3, 3차원 배열\n",
" 1. n(n-Tensor) : RankN , N차원 배열\n",
"\n",
"\n",
"- **Shapes** :차원안의 요소의 개수\n",
" 1. t=[[1,2,3], [4,5,6], [7,8,9]] : 요소가 3개가 있고, 배열이 3개가 있다. (3 3) or [3,3]\n",
"\n",
"\n",
"- **Types** : 데이터 타입, 주로 float32를 사용.\n",
" 1. DT_FLOAT : tf.float32, 32bit floating point.\n",
" 1. DT_Double : tf.float64, 64bit floating point.\n",
" 1. DT_INT8 : tf.int8, 8bits signed integer.\n",
" 1. DT_INT16 : tf.int16, 16bits signed integer.\n",
" 1. DT_INT32 : tf.int32, 32bits signed integer.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 #a rank 0 tensor: this is a scalar with shape[]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1.0, 2.0, 3.0]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1., 2., 3.] # a rank 1 tensor: this is a vector with shap[3]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor: a matrix with shape[2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[[[1., 2., 3.], [7., 8., 9.]]] # a rank 3 tensor with shape[2,1,3]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3.5",
"language": "python",
"name": "python3.5"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment