๊ธ€ ์ž‘์„ฑ์ž: ๋˜ฅํด๋ฒ .
๋ฐ˜์‘ํ˜•

๋ชฉ์ฐจ


  • ํ…์„œํ”Œ๋กœ์šฐ(TensorFlow)?

    • ๋ฐ์ดํ„ฐ ํ”Œ๋กœ์šฐ ๊ทธ๋ž˜ํ”„(Data Flow Graph)

  • ํ…์„œํ”Œ๋กœ์šฐ ์„ค์น˜(Install TensorFlow)

  • ๊ธฐ๋ณธ์ ์ธ ๋ช…๋ น์–ด ์—ฐ์Šต

  • Ranks, Shapes, Types

 

 

ํ…์„œํ”Œ๋กœ์šฐ(TensorFlow)?


๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  ๋จธ์‹ ๋Ÿฌ๋‹์„ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. 

๋ฐ์ดํ„ฐ ํ”Œ๋กœ์šฐ ๊ทธ๋ž˜ํ”„(data flow graph)๋ž€๊ฒƒ์„ ์‚ฌ์šฉํ•ด์„œ numerical ํ•œ ๊ณ„์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์„ ํ˜ธํ•˜๋Š” Python์ด๋ผ๋Š” ์–ธ์–ด๋ฅผ ๊ฐ€์ง€๊ณ  ํ…์„œํ”Œ๋กœ์šฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

https://towardsdatascience.com/deep-learning-framework-power-scores-2018-23607ddf297a

์œ„์˜ ๊ทธ๋ฆผ๋งŒ ๋ณด๋”๋ผ๋„ 2018๋…„ ๊ธฐ์ค€์ด๊ธด ํ•˜์ง€๋งŒ ํ…์„œํ”Œ๋กœ์šฐ๊ฐ€ ์••๋„์ ์œผ๋กœ 1์œ„๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

  • ๋ฐ์ดํ„ฐ ํ”Œ๋กœ์šฐ ๊ทธ๋ž˜ํ”„(Data Flow Graph)

Node(๋…ธ๋“œ)์™€ Edge(์—ฃ์ง€)๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ์šฐ๋ฆฌ๋Š” Graph(๊ทธ๋ž˜ํ”„)๋ผ ํ•œ๋‹ค.

Data Flow Graph์—์„œ๋Š” Node๋ฅผ ํ•˜๋‚˜์˜ Operation์ด๋ผ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๊ณ , Edge๋Š” Data(=Tensor)๋ผ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

https://codability.in/a-guide-to-tensorflow-part-1/

์œ„์˜ ๊ทธ๋ฆผ์—์„œ ๋ณด๋‹ค์‹œํ”ผ ๋™๊ทธ๋ผ๋ฏธ a, b, c, d, e๋Š” Node, ์ฆ‰ Operation์„ ๋‚˜ํƒ€๋‚ด๊ณ  Edge๋Š” Data๋ฅผ ๋‚˜ํƒ€๋‚ด์–ด ์›ํ•˜๋Š” ๊ฒฐ๊ด๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ  ์žˆ๋Š” ๊ณผ์ •์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ Data Flow Graph๋ผ ํ•œ๋‹ค.

 

ํ…์„œํ”Œ๋กœ์šฐ ์„ค์น˜(Install TensorFlow)


2019/06/28 - [Development/Machine Learning] - [๋จธ์‹ ๋Ÿฌ๋‹] ํŒŒ์ด์ฌ(Python), ํ…์„œํ”Œ๋กœ์šฐ(Tensor Flow) ์„ค์น˜

 

[๋จธ์‹ ๋Ÿฌ๋‹] ํŒŒ์ด์ฌ(Python), ํ…์„œํ”Œ๋กœ์šฐ(Tensor Flow) ์„ค์น˜

๋ณธ ํฌ์ŠคํŠธ๋Š” Windows ํ™˜๊ฒฝ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ถ๊ธˆํ•œ ์ ์€ ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ์นœ์ ˆํ•˜๊ฒŒ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค ^^! ํŒŒ์ด์ฌ 3 ์„ค์น˜(Install Python3) https://www.python.org/downloads/ Download Python The o..

cjwoov.tistory.com

 

๊ธฐ๋ณธ์ ์ธ ๋ช…๋ น์–ด ์—ฐ์Šต


์˜ˆ์ œ๋“ค์˜ ์ถœ์ฒ˜: https://github.com/hunkim/DeepLearningZeroToAll

ํ…์„œํ”Œ๋กœ์šฐ๋Š” ๊ธฐ์กด์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ

์ฒซ ๋ฒˆ์งธ๋กœ, operations๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋นŒ๋“œํ•œ๋‹ค.

๋‘ ๋ฒˆ์งธ๋กœ, ์„ธ์…˜์„ ๋งŒ๋“ค๊ณ  run์„ ํ†ตํ•ด ๊ทธ๋ž˜ํ”„๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ, run์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ์‹คํ–‰์‹œํ‚จ ๊ฐ’์„ ํ™œ์šฉํ•œ๋‹ค.

๋ผ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

 

  • Hello Tensor Flow!

'Hello, Tensor Flow'๋ผ๋Š” Node๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋‹ค.

import tensorflow as tf

hello = tf.constant("Hello, TensorFlow!")

sess = tf.Session()

print(sess.run(hello))
b'Hello, TensorFlow!'

 

  • Computational Graph
import tensorflow as tf

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)

print("node1:", node1, "node2", node2)
print("node3:", node3)

sess = tf.Session()
print("sess.run(node1, node2): ", sess.run([node1, node2]))
print("sess.run(node3): ", sess.run(node3))
node1: Tensor("Const:0", shape=(), dtype=float32) node2 Tensor("Const_1:0", shape=(), dtype=float32)
node3: Tensor("Add:0", shape=(), dtype=float32)
sess.run(node1, node2):  [3.0, 4.0]
sess.run(node3):  7.0

node๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ถœ๋ ฅํ•˜๋ฉด node์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์ด ์ถœ๋ ฅ๋˜๊ณ ,

์„ธ์…˜์„ ๋งŒ๋“ค์–ด์„œ run์„ ์‹œ์ผœ์•ผ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

  • Placeholder

์œ„์˜ ์˜ˆ์ œ๋“ค์€ node๋“ค์„ ์ƒ์ˆ˜๋กœ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ์ด๋ฒˆ์—๋Š” ์ƒ์ˆ˜๊ฐ€ ์•„๋‹Œ, ์‹คํ–‰์‹œํ‚ค๋Š” ๋‹จ๊ณ„์—์„œ ๊ฐ’์„ ๋˜์ ธ์ฃผ๋Š” ์˜ˆ์ œ๋‹ค.

์ด๋•Œ Placeholder๋ผ๋Š” ํŠน๋ณ„ํ•œ node๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค.

import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # tf.add(a, b)์™€ ๊ฐ™์€ ์˜๋ฏธ

sess = tf.Session()

print(sess.run(adder_node, feed_dict={a:3, b:4.5}))
print(sess.run(adder_node, feed_dict={a: [1,3], b: [2,4]}))
7.5
[3. 7.]

์ž…๋ ฅ ๊ฐ’์œผ๋กœ [1, 3], [2, 4]์™€ ๊ฐ™์ด ๊ฐ’์ด ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ž…๋ ฅ ๊ฐ’์œผ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

 

Ranks, Shapes, Types


  • Rank
Rank Math entity Python example
0 Scalar (magnitude only) s = 483
1 Vector (magnitude and direction) v = [1.1, 2.2, 3.3]
2 Matrix (table of numbers) m = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
3 3-Tensor (cube of numbers) t = [ [ [2], [4], [6] ], [ [8], [10], [12] ], [ [14], [16], [18] ] ] 
n n-Tensor (you get the idea) ....

Rank๋Š” ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ๋ช‡ ์ฐจ์› Array๋ƒ๋ฅผ ๋œปํ•œ๋‹ค.

 

  • Shape
Rank Shape Dimension number Example
0 [] 0-D A 0-D tensor. A scalar.
1 [D0] 1-D A 1-D tensor with shape [5].
2 [D0, D1] 2-D A 2-D tensor with shape [3, 4].
3 [D0, D1, D2] 3-D A 3-D tensor with shape [1, 4, 3].
n [D0, D1, ... Dn-1] n-D A tensor with shape [D0, D1, ... Dn-1].

Shape๋Š” ๊ฐ ์ฐจ์›์—์„œ์˜ Element์˜ ์ˆซ์ž๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

t = [ [1, 2, 3], [4, 5, 6] ]

์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ t๋Š” 2์ฐจ์› ๋ฐฐ์—ด์— ์›์†Œ๋ฅผ 4, 5, 6 -> 3๊ฐœ, [1,2,3], [4,5,6] -> 2๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ

[2, 3]๋ผ๊ณ  Shape๋ฅผ ํ‘œ๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • Types

Type๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

Data type Python type
DT_FLOAT tf.float32
DT_DOUBLE tf.float64
DT_INT8 tf.int8
DT_INT16 tf.int16
DT_INT32 tf.int32
... ...

 

์ฐธ๊ณ 


Sung Kim๋‹˜ - ML lab 01. TensorFlow์˜ ์„ค์น˜ ๋ฐ ๊ธฐ๋ณธ์ ์ธ operations
https://www.youtube.com/watch?v=-57Ne86Ia8w

 

๋ฐ˜์‘ํ˜•