1.Introduction to Python and TensorFlow

Post on 21-Jan-2018

1.221 views 0 download

Transcript of 1.Introduction to Python and TensorFlow

Introduction toPython and TensorFlow

Contacts

• Haesun Park

• email : haesunrpark@gmail.com

• facebook : facebook.com/haesunrpark

• blog : tensorflow.blog

2

Book

• 텐서플로 첫걸음

• First Contact with TensorFlow, Jordie Torres.

• 온라인 : tensorflow.blog/2016/04/28/first-contact-with-tensorflow/

• Yes24 : yes24.com/24/goods/30547754

3

Outlook

• Part 1: 파이썬과 텐서플로우 소개

• Part 2: 회귀 분석과 로지스틱 회귀

• Part 3: 뉴럴 네트워크 알고리즘

• Part 4: 콘볼루션 뉴럴 네트워크

4

머신 러닝 소개

5

머신 러닝이란

“머신 러닝 또는 기계 학습은 인공 지능의 한 분야로, 컴퓨터가 학습할 수있도록 하는 알고리즘과 기술을 개발하는 분야를 말한다.” - 위키피디아

“Field of study that gives computers the ability to learn without being

explicitly programmed.” - Arthur Samuel, 1959

6

AI ⊃ 머신러닝 ⊃ 딥러닝

• 딥러닝이란 인공신경망(Artificial Neural Network) 알고리즘을 사용한 머신러닝의 한 분야이다.

7

머신 러닝 역사

8

ImageNet Challenge

9

2016년 2.99%

연구 분야

10

머신 러닝 분류

• 감독 여부나 주어지는 정보에 따라

• 지도 학습(supervised learning)• 알고있는 샘플과 정답(레이블)을 이용한 학습• 스팸필터, 붓꽃 품종, 암진단

• 비지도 학습(unsupervised learning)• 정답이 없는 샘플을 이용하여 데이터를 재표현• 차원축소, 군집

• 강화 학습(reinforcement learning)• 환경으로부터 받은 보상을 이용하여 학습• 게임, 로봇

11

딥 러닝의 분야

• 음성인식(Speech Recognition)• Google Assistant• Amazon Echo• SKT NUGU

• 이미지인식(Image Recognition)• Google Photo• 자율주행 자동차

• 자연어처리(Natural Language Processing, NLP)• 기계번역(Machine Translation)• 챗봇(chatbot)

• 게임, 그림, 음악, 로봇, ...

12

손글씨 숫자 인식

• MNIST handwritten digits database

• http://yann.lecun.com/exdb/mnist/

13

텐서플로우 소개

14

머신 러닝 라이브러리

• scikit-learn (http://scikit-learn.org)

• 파이썬이 머신러닝 분야의 표준 언어

• Wekahttp://www.cs.waikato.ac.nz/ml/weka/

• Apache Mahout, Spark ML, ...15

딥 러닝 라이브러리

• TensorFlow(https://tensorflow.org)• 2015년 11월 공개• 구글이 개발, 지원• 파이썬 API / 윈도우, 맥, 리눅스 지원• 다양한 인공신경망 알고리즘 제공• 계산 그래프 정의, 실행

• PyTorch(http://pytorch.org/)• 페이스북 개발, 지원• 다이나믹 계산 그래프

• MXNet, CNTK, Theano, Deeplearning4j, DyNet

• Torch, Caffe, Chainer16

텐서플로우 리소스

• 텐서플로우 웹사이트http://tensorflow.org

• 텐서플로우 깃허브https://github.com/tensorflow/tensorflow

• 텐서플로우 커뮤니티https://www.facebook.com/groups/TensorFlowKR/

• 텐서플로우 한글 문서https://tensorflowkorea.gitbooks.io/tensorflow-kr/

• 텐서플로우 블로그https://tensorflow.blog/

17

파이썬 소개

18

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

Trend

20

US Worldwide

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

Python 2 vs 3

어떤 것을 써야할 지 잘 모르겠다면

파이썬 3이 적절합니다.

이미 파이썬 2로 작성한 코드가 많이 있거나

사용하고 있는 라이브러리가 파이썬 2 밖에

지원하지 않는 경우에는 파이썬 2를 사용합니다.

22

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

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

Package

• 서드파티 라이브러리

• pypi.python.org

$ pip install package_name

$ conda install package_name

25

Script, Interactive shell

$ python script_name.py

$ python

$ ipython

26

~~~~~~~~

Jupyter notebook

27

• 코드 + 문서 + 결과

• aka Ipython notebook

• julia, R, ruby

$ jupyter notebook

Notebook

28

Notebook Example

29

Github + notebook

30

파이썬과 텐서플로우 설치

31

Anaconda

• https://www.continuum.io/downloads

32

Anaconda Download

33

Anaconda Install

34

Anaconda Prompt

35

Python 3.5 Environment

>conda create -n tensorflow python=3.5 anaconda

36

Activate Environment

>activate tensorflow

37

Install TensorFlow

(tensorflow)>pip install tensorflow

38

Check TensorFlow

>ipython…In [1]: import tensorflow as tf

39

Notebook 연결

• 아마존 웹서비스 EC2에 있는 주피터 노트북에 접속하여 실습합니다.

• 브라우저로 아래 페이지에 있는 주피터 노트북 서버 주소로 연결하세요.

• 서버가 재 시작되면 주피터 노트북 주소가 매번 달라집니다.

40

폴더 생성

41

Untitled Folder 하나를 선택해서각자 생일의 월일로 폴더를 만들어 주세요.

노트북 생성

42

자신의 폴더로 들어가Python3 노트북을 하나 만듭니다.

노트북 이름 바꾸기

43

노트북의 이름을적절한 이름으로 바꾸어 줍니다.

텐서플로우 테스트

44

노트북의 첫번째 셀에 ‘import tensorflow as tf’ 란 명령을 기입하고Shift + Enter 를 눌러서 셀을 실행합니다.

아무런 메세지가 나오지 않으면 성공입니다.

셀(cell) 타입

45

기본적인 사용법

46

주의해야할 점

• 노트북 셀의 오브젝트(변수)는 노트북이 종료되면 모두 사라집니다.

• 다시 노트북을 열었을 때 출력 값은 보이지만 오브젝트는 메모리에 없습

니다.

• 오브젝트를 다시 만드려면 해당 셀을 다시 실행해야 합니다.

• 노트북 실행중에 상위 셀을 다시 실행하면 그로 인해 하위 오브젝트가 무

효화 될 수 있습니다.

47

파이썬 기초

48

print()

In [1]: print(“Hello World”)

Hello World

In [2]: print(‘Hello World’)

Hello World

In [3]: # comment

49

숫자(number)

In [1]: i = 10

In [2]: print(i)

10

In [3]: f = 10.0

In [4]: print(f)

10.0

50

문자열(string)

In [1]: str1 = “Hello World”

In [2]: print(str1)

Hello World

In [3]: str2 = “안녕 파이썬”

In [4]: print(str2)

안녕 파이썬

51

여러 변수 출력

In [1]: print(str1, str2)

Hello World 안녕 파이썬

In [2]: print(str1+str2)

Hello World안녕 파이썬

In [3]: print(str1+” ”+str2)

Hello World 안녕 파이썬

52

다른 타입의 변수 출력

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

리스트(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

리스트에 여러 종류의 값

In [1]: teams = []

In [2]: teams.append(”Tottenham”)

In [3]: print(teams)

[1, 2, “Tottenham”]

In [4]: len(teams)

3

55

리스트 요소 선택

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

문자열 요소 선택

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

연산자

In [1]: sum = 1 + 2 + 3

In [2]: sum

6

In [3]: 4 * 5 / 2

10.0

In [4]: 4 * 5 // 2

10

58

제곱, 나머지

In [1]: 3 ** 2

9

In [2]: 3 ** 3

27

In [3]: 10 % 3

1

59

if

In [1]: if i == 10:

print(‘test ok’)

test ok

In [2]: if str1 == ‘Hello World’:

print(‘test valid’)

test valid

60

else

In [1]: if i <= 10:

print(‘less than 10’)

else:

print(‘more than 10’)

less than 10

61

elif

In [1]: if i < 10:

print(‘less than 10’)

elif i == 10:

print(‘equal 10’)

else:

print(‘more than 10’)

equal 10

62

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

for

In [1]: for name in teams:

print(name)

1

2

Tottenham

64

for ~ in range()

In [1]: for i in range(5):

print(i)

0

1

2

3

4

65

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

딕셔너리(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

복잡한 구조

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

for ~ in dict

In [1]: for k in stat:

print(k, stat[k])

alli 16

sonny 11

kane 19

69

for ~ in dict.items()

In [1]: for k, v in stat.items():

print(k, v)

alli 16

sonny 11

kane 19

70

del()

In [1]: del(teams[2])

In [2]: teams

[1, 2]

In [3]: del(stat[‘alli’])

In [4]: stat

{‘kane’: 19, ‘sonny’: 11}

71

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

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

텐서플로우 기초

74

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)

575

텐서(Tensor)

• 다차원 배열, 매트릭스, NumPy 배열

• 텐서플로우에서 사용하는 데이터 표현

a.shape

TensorShape([])

c = tf.constant([2, 2])

c.shape

TensorShape([Dimension(2)])

76

상수(Constant)

d = tf.constant([[0, 1, 2], [3, 4, 5]])

d.shape

TensorShape([Dimension(2), Dimension(3)])

sess = tf.Session()

sess.run(d)

array([[0, 1, 2], [3, 4, 5]], dtype=int32)

77

행렬(matrix) 𝑑 = 0 1 23 4 5

zeros(), ones()

• 0으로 채워진 텐서를 만듭니다.

e = tf.zeros([2, 3])

tf.Session().run(e)

array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32)

• 1로 채워진 텐서를 만듭니다.

f = tf.ones([2, 3], dtype=tf.int32)

tf.Session().run(f)

array([[1, 1, 1], [1, 1, 1]], dtype=int32)

78

random_normal()

• 정규 분포(𝜇 = 0, 𝜎 = 1)의 난수를 발생시킵니다

g = tf.random_normal([2, 3])

tf.Session().run(g)

array([[-0.42949361, 2.13715839, -0.73751086],

[-0.56948185, -0.92247707, 0.62523431]], dtype=float32)

79

truncated_normal()

• 정규 분포의 난수 중 표준편차의 두배 밖의 값은 제외합니다

h = tf.truncated_normal([2, 3])

tf.Session().run(h)

array([[ 1.09942925, -0.31089929, 0.39926037],

[-0.55724454, -0.41932136, -1.33153999]], dtype=float32)

80

random_uniform()

• 균등 분포(0~1)의 난수를 발생시킵니다

p = tf.random_uniform([2, 3])

tf.Session().run(p)

array([[ 0.53323591, 0.19622314, 0.86946297],

[ 0.04974151, 0.95073402, 0.90066719]], dtype=float32)

81

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

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

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

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

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)

286

assign()

• 변수의 값을 바꿉니다

a = tf.Variable(tf.constant(2))

upd = a.assign_add(tf.constant(3))

init = tf.global_variables_initializer()

sess = tf.Session()

sess.run(init)

sess.run(upd)

5

sess.run(upd)

887

tf.Variable(tf.random_normal())

• 변수를 생성할 때 랜덤한 값으로 초기화를 많이 합니다.

• random_normal(), truncated_normal(), random_uniform()

a = tf.Variable(tf.random_normal([2,3]))

sess = tf.Session()

sess.run(tf.global_variables_initializer())

sess.run(a)

array([[-1.4886378 , 0.28386045, 0.48322374],

[-1.92303669, -0.40773764, -0.11247366]], dtype=float32)

88

행렬(matrix)

• 2×3 행렬

1 −2 23 −1 1

a = tf.Variable([[1, -2, 2], [3, -1, 1]])

sess = tf.Session()

sess.run(tf.global_variables_initializer())

sess.run(a[1][2])

189

행(row)

열(column)

열(column)행(row)

tf.reduce_sum(a, 0)

• 행렬의 행방향으로 합을 계산합니다.

a = tf.Variable([[1, -2, 2], [3, -1, 1]])

asum = tf.reduce_sum(a, 0)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

sess.run(asum)

array([ 4, -3, 3], dtype=int32)

90

1 −2 23 −1 1

4 −3 3

tf.reduce_sum(a, 1)

• 행렬의 열방향으로 합을 계산합니다.

a = tf.Variable([[1, -2, 2], [3, -1, 1]])

asum = tf.reduce_sum(a, 1)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

sess.run(asum)

array([ 1, 3], dtype=int32)

asum = tf.reduce_sum(a)

sess.run(asum)

491

1 −2 23 −1 1

1 3

1 −2 23 −1 1

2. −1.5 1.5

tf.reduce_mean()

• 행렬의 행, 열방향으로 합을 계산합니다.

a = tf.Variable([[1., -2., 2.], [3., -1., 1.]])

amean = tf.reduce_mean(a, 0)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

sess.run(amean)

array([ 2. , -1.5, 1.5], dtype=float32)

92

행렬 내적

• 행렬의덧셈

• 2×3 + 2×3 = [2×3]

1 −2 23 −1 1 + −1 3 2

2 4 1 = 0 1 45 3 2

• 행렬의곱셈:내적,점곱(dotproduct)• 2×3 ⋅ 3×2 = [2×2]

1 −2 23 −1 1

2 −14 31 2

= −4 −33 −4

93

행(row)

열(column)

tf.matmul()

• 두개의 텐서를 입력 받아 행렬 내적을 계산합니다.

a = tf.Variable([[1, -2, 2], [3, -1, 1]])

b = tf.Variable([[2, -1], [4, 3], [1, 2]])

dot = tf.matmul(a, b)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

sess.run(dot)

array([[-4, -3],

[ 3, -4]], dtype=int32)

94

1 −2 23 −1 12 −14 31 2

−4 −33 −4

신경망 학습은 반복된 과정

95

플레이스홀더

• 상수를 바꾸려면 그래프를 다시 만들어야 합니다.

• 변수는 그래프 내부의 계산에 의해 변경되는 값을 저장합니다.

• 그래프는 보통 반복적으로 실행되므로 그래프를 실행할 때 프로그래머가데이터를 주입할 방법이 필요합니다.

96

상수

상수

계산 변수데이터

계산 그래프

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

Materials

• Github : https://github.com/rickiepark/tfk-notebooks/tree/master/tensorflow_for_beginners

• Slideshare :

https://www.slideshare.net/RickyPark3/1introduction-to-python-and-tensorflow

98

감사합니다.

99