6. 머신 러닝이란
“머신 러닝 또는 기계 학습은 인공 지능의 한 분야로, 컴퓨터가 학습할 수
있도록 하는 알고리즘과 기술을 개발하는 분야를 말한다.” - 위키피디아
“Field of study that gives computers the ability to learn without being
explicitly programmed.” - Arthur Samuel, 1959
6
7. AI ⊃ 머신러닝 ⊃ 딥러닝
• 딥러닝이란 인공신경망(Artificial Neural Network) 알고리즘을 사용한 머
신러닝의 한 분야이다.
7
11. 머신 러닝 분류
• 감독 여부나 주어지는 정보에 따라
• 지도 학습(supervised learning)
• 알고있는 샘플과 정답(레이블)을 이용한 학습
• 스팸필터, 붓꽃 품종, 암진단
• 비지도 학습(unsupervised learning)
• 정답이 없는 샘플을 이용하여 데이터를 재표현
• 차원축소, 군집
• 강화 학습(reinforcement learning)
• 환경으로부터 받은 보상을 이용하여 학습
• 게임, 로봇
11
12. 딥 러닝의 분야
• 음성인식(Speech Recognition)
• Google Assistant
• Amazon Echo
• SKT NUGU
• 이미지인식(Image Recognition)
• Google Photo
• 자율주행 자동차
• 자연어처리(Natural Language Processing, NLP)
• 기계번역(Machine Translation)
• 챗봇(chatbot)
• 게임, 그림, 음악, 로봇, ...
12
13. 손글씨 숫자 인식
• MNIST handwritten digits database
• http://yann.lecun.com/exdb/mnist/
13
15. 머신 러닝 라이브러리
• scikit-learn (http://scikit-learn.org)
• 파이썬이 머신러닝 분야의 표준 언어
• Weka
http://www.cs.waikato.ac.nz/ml/weka/
• Apache Mahout, Spark ML, ...
15
16. 딥 러닝 라이브러리
• TensorFlow(https://tensorflow.org)
• 2015년 11월 공개
• 구글이 개발, 지원
• 파이썬 API / 윈도우, 맥, 리눅스 지원
• 다양한 인공신경망 알고리즘 제공
• 계산 그래프 정의, 실행
• PyTorch(http://pytorch.org/)
• 페이스북 개발, 지원
• 다이나믹 계산 그래프
• MXNet, CNTK, Theano, Deeplearning4j, DyNet
• Torch, Caffe, Chainer
16
17. 텐서플로우 리소스
• 텐서플로우 웹사이트
http://tensorflow.org
• 텐서플로우 깃허브
https://github.com/tensorflow/tensorflow
• 텐서플로우 커뮤니티
https://www.facebook.com/groups/TensorFlowKR/
• 텐서플로우 한글 문서
https://tensorflowkorea.gitbooks.io/tensorflow-kr/
• 텐서플로우 블로그
https://tensorflow.blog/
17
19. History
• 네덜란드 개발자 귀도 반 로섬(Guido van Rossum)이 1989년에 개발 시작
• 1994년 1.0 Release, 2000년에 2.0 Release, 2010년에 2.7 Release
• 2008년에 3.0 Release, 2015년에 3.5 Release, 2016년에 3.6 Release
• Python 2.7 버전은 2020년 까지만 지원
19
Guido van Rossum,
Dropbox 2012~
출처: wikipedia
21. Advantage
• 오픈소스 범용 인터프리트 언어(no compile)
• 배우기 쉽고 코드를 읽기 좋다
• Battery Included
• 객체 지향 언어(Object Oriented Programming, OOP)
• 대화식 쉘(Interactive Shell): python, ipython
• 코드 + 결과 + 문서 : Jupyter notebook
• 풍부한 과학 기술 라이브러리(scikit-learn, scipy, numpy, pandas,...)
• 아나콘다(https://www.continuum.io/downloads)
• 캐노피(https://www.enthought.com/product/canopy)
• 더 풍부한 서드파티 라이브러리(pypi.python.org)
21
22. Python 2 vs 3
어떤 것을 써야할 지 잘 모르겠다면
파이썬 3이 적절합니다.
이미 파이썬 2로 작성한 코드가 많이 있거나
사용하고 있는 라이브러리가 파이썬 2 밖에
지원하지 않는 경우에는 파이썬 2를 사용합니다.
22
23. Comparison
23
Python Java Perl Ruby
dynamic type static type dynamic type dynamic type
interprete compile script interprete
less develop fast execute unreadable code less develop
battery included more modules
Flask, Django Spring, Struts Rails
strict indentation android text processing
more object-
oriented
more object-
oriented
less object-
oriented
more object-
oriented
24. Installer
• Mac & Linux
• come with python 2.7 out of the box
• python.org
• windows, mac
• linux 배포판 패키지 관리툴 (e.g. yum, apt-get)
• ActivaState, Canopy
• windows, mac, linux
• Anaconda
• continuum.io
• 720+ for stats, data mining, ml, dl, nlp, …
• include Numpy, SciPy, pandas, scikit-learn, Jupyter, NLTK, matplotlib, ...
24
47. 주의해야할 점
• 노트북 셀의 오브젝트(변수)는 노트북이 종료되면 모두 사라집니다.
• 다시 노트북을 열었을 때 출력 값은 보이지만 오브젝트는 메모리에 없습
니다.
• 오브젝트를 다시 만드려면 해당 셀을 다시 실행해야 합니다.
• 노트북 실행중에 상위 셀을 다시 실행하면 그로 인해 하위 오브젝트가 무
효화 될 수 있습니다.
47
50. 숫자(number)
In [1]: i = 10
In [2]: print(i)
10
In [3]: f = 10.0
In [4]: print(f)
10.0
50
51. 문자열(string)
In [1]: str1 = “Hello World”
In [2]: print(str1)
Hello World
In [3]: str2 = “안녕 파이썬”
In [4]: print(str2)
안녕 파이썬
51
52. 여러 변수 출력
In [1]: print(str1, str2)
Hello World 안녕 파이썬
In [2]: print(str1+str2)
Hello World안녕 파이썬
In [3]: print(str1+” ”+str2)
Hello World 안녕 파이썬
52
53. 다른 타입의 변수 출력
In [1]: print(i, str1)
10 Hello World
In [2]: print(i+str1)
TypeError: unsupported operand type(s) for +: 'int' and
'str’
In [3]: type(i)
int
53
54. 리스트(list)
In [1]: teams = []
In [2]: teams.append(1)
In [3]: print(teams)
[1]
In [4]: teams.append(2)
In [5]: print(teams)
[1, 2]
54
55. 리스트에 여러 종류의 값
In [1]: teams = []
In [2]: teams.append(”Tottenham”)
In [3]: print(teams)
[1, 2, “Tottenham”]
In [4]: len(teams)
3
55
56. 리스트 요소 선택
In [1]: print(teams[0])
1
In [2]: print(teams[2])
Tottenham
In [3]: print(teams[1:3])
[2, ‘Tottenham’]
In [4]: print(teams[0:2])
[0, 1]
56
57. 문자열 요소 선택
In [1]: print(str1[0])
H
In [2]: print(str1[0:6])
Hello
In [3]: print(str1[-1])
d
In [3]: print(str1[-5:])
World
57
58. 연산자
In [1]: sum = 1 + 2 + 3
In [2]: sum
6
In [3]: 4 * 5 / 2
10.0
In [4]: 4 * 5 // 2
10
58
59. 제곱, 나머지
In [1]: 3 ** 2
9
In [2]: 3 ** 3
27
In [3]: 10 % 3
1
59
60. if
In [1]: if i == 10:
print(‘test ok’)
test ok
In [2]: if str1 == ‘Hello World’:
print(‘test valid’)
test valid
60
61. else
In [1]: if i <= 10:
print(‘less than 10’)
else:
print(‘more than 10’)
less than 10
61
62. elif
In [1]: if i < 10:
print(‘less than 10’)
elif i == 10:
print(‘equal 10’)
else:
print(‘more than 10’)
equal 10
62
63. if ~ in list
In [1]: if ‘Tottenham’ in teams:
print(‘sonny’)
sonny
In [2]: if ‘Tottenham’ in teams:
idx = teams.index(‘Tottenham’)
print(teams[idx])
Tottenham
63
65. for ~ in range()
In [1]: for i in range(5):
print(i)
0
1
2
3
4
65
66. for ~ in range()
In [1]: for i in range(5):
if i % 2 == 0:
print(i)
continue
if i // 2 >= 1:
break
0
2
66
67. 딕셔너리(dictionary)
In [1]: stat = {‘sonny’: 11, ‘kane’: 19}
In [2]: stat[‘sonny’]
11
In [3]: type(stat)
dict
In [4]: stat[‘alli’] = 16
In [5]: stat
{‘alli’: 16, ‘kane’: 19, ‘sonny’: 11}
67
68. 복잡한 구조
In [1]: epl = {'team': teams, 'stat': stat}
In [2]: epl
{'stat': {'kane': 19, 'sonny': 11}, 'team': [1, 2]}
In [3]: epl = [teams, stat]
In [4]: epl
[[1, 2], {'kane': 19, 'sonny': 11}]
68
69. for ~ in dict
In [1]: for k in stat:
print(k, stat[k])
alli 16
sonny 11
kane 19
69
70. for ~ in dict.items()
In [1]: for k, v in stat.items():
print(k, v)
alli 16
sonny 11
kane 19
70
72. import
In [1]: import tensorflow
In [2]: tensorflow.constant(2)
<tf.Tensor 'Const:0' shape=() dtype=int32>
In [3]: import tensorflow as tf
In [4]: tf.constant(2)
<tf.Tensor 'Const:0' shape=() dtype=int32>
72
73. Python 온라인 문서
• 점프 투 파이썬:
https://wikidocs.net/book/1
• Think Python:
http://www.flowdas.com/thinkpython/
• A Byte of Python:
http://byteofpython-korean.sourceforge.net/byte_of_python.html
73
75. Graph 정의 + Session()
• 텐서플로우의 연산자를 사용하여 계산 구조를 정의합니다.
a = tf.constant(2)
b = tf.constant(3)
x = tf.add(a, b)
• 세션 객체를 만들어 만들어진 계산 그래프를 실행합니다.
x
<tf.Tensor 'Add:0' shape=() dtype=int32>
tf.Session().run(x)
5
75
76. 텐서(Tensor)
• 다차원 배열, 매트릭스, NumPy 배열
• 텐서플로우에서 사용하는 데이터 표현
a.shape
TensorShape([])
c = tf.constant([2, 2])
c.shape
TensorShape([Dimension(2)])
76
82. tf.add()
• 주어진 두 텐서를 더합니다.
a = tf.constant(10); b = tf.constant(20)
c = tf.add(a, b)
tf.Session().run(c)
30
d = tf.constant([10, 20]); e = tf.constant([20, 10])
f = d + e
tf.Session().run(f)
array([30, 30], dtype=int32)
82
83. tf.substrct()
• 주어진 두 텐서를 뺍니다.
a = tf.constant(10); b = tf.constant(20)
c = tf.subtract(a, b)
tf.Session().run(c)
-10
d = tf.constant([10, 20]); e = tf.constant([20, 10])
f = d - e
tf.Session().run(f)
array([-10, 10], dtype=int32)
83
84. tf.multiply()
• 주어진 두 텐서를 곱합니다.
a = tf.constant(10); b = tf.constant(20)
c = tf.multiply(a, b)
tf.Session().run(c)
200
d = tf.constant([10, 20]); e = tf.constant([20, 10])
f = d * e
tf.Session().run(f)
array([200, 200], dtype=int32)
84
85. tf.divide()
• 주어진 두 텐서를 곱합니다.
a = tf.constant(10); b = tf.constant(20)
c = tf.divide(a, b)
tf.Session().run(c)
0.5
d = tf.constant([10, 20]); e = tf.constant([20, 10])
f = d / e
tf.Session().run(f)
array([0.5, 2.], dtype=int32)
85
86. tf.Variable()
• 상수가 아니라 계산 그래프에서 변하는 값을 담는 도구입니다.
• 변수는 초깃값이 주어져야 하고 사용하기 전에 초기화 되어야 합니다.
a = tf.Variable(tf.constant(2))
a
<tensorflow.python.ops.variables.Variable at ...>
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
sess.run(a)
2
86
96. 플레이스홀더
• 상수를 바꾸려면 그래프를 다시 만들어야 합니다.
• 변수는 그래프 내부의 계산에 의해 변경되는 값을 저장합니다.
• 그래프는 보통 반복적으로 실행되므로 그래프를 실행할 때 프로그래머가
데이터를 주입할 방법이 필요합니다.
96
상수
상수
계산 변수데이터
계산 그래프
97. tf.placeholder() & feed_dict
• 값을 지정하지 않은 플레이스 홀더를 만든 후 실행시에 값을 주입합니다.
a = tf.Variable([[1, -2, 2], [3, -1, 1]])
b = tf.placeholder(tf.int32, [3, 2])
dot = tf.matmul(a, b)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(dot, feed_dict={b: [[2, -1], [4, 3], [1, 2]]})
array([[-4, -3],
[ 3, -4]], dtype=int32)
97