DeepLearning_Lab01 - 8BitsCoding/RobotMentor GitHub Wiki

์œ„ ๋ฐฉ๋ฒ•๋„ ์ข‹์ง€๋งŒ ์—ญ์‹œ ๊ฐ€์žฅ ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์€ ํ™ˆํŽ˜์ด์ง€์—์„œ ์ง์ ‘ ์„ค์น˜๋ฐฉ๋ฒ•์„ ๋ณด๋Š” ๊ฒƒ!


Installing Tensorflow

python์ด ์„ค์น˜๋˜์—ˆ๋‹ค๋Š” ๊ฐ€์ •์•„๋ž˜ ์ง„ํ–‰

ํ•„์ˆ˜ ํŒจํ‚ค์ง€ ์„ค์น˜ํ™•์ธ

$ sudo apt-get update
$ sudo apt-get upgrade
$ python3 --version
# 3.4, 3.5, 3.6 over

$ pip3 --version
$ sudo apt-get install python3-pip python3-dev

install

$ pip3 install --user --upgrade tensorflow

Verify

$ python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
$ python3
>>> import tensorlfow as tf
>>> tf.__version__
'1.0.0'
>>>

TensorFlow Hello World!

์ฐธ๊ณ ์‚ฌ์ดํŠธ

import tensorflow as tf

# Create a constant op
# This op is added as a node to the default graph
hello = tf.constant("Hello, TensorFlow!")

# start a TF session
sess = tf.Session()

# run the op and get result
print(sess.run(hello))

๊ฒฐ๊ณผ : b'Hello, TensorFlow!'

hello๋ผ๋Š” ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ ํ›„ session์„ ํ†ตํ•˜์—ฌ run

์ฐธ๊ณ ๋กœ ์•ž์˜ b''๋Š” bytes literal(stream)์„ ์˜๋ฏธํ•œ๋‹ค.


Computational Graph

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
node3 = tf.add(node1, node2)
print("node1:", node1, "node2:", node2)
print("node3: ", node3)

๊ฒฐ๊ณผ : node1: Tensor("Const_1:0", shape=(), dtype=float32) node2: Tensor("Const_2:0", shape=(), dtype=float32) node3: Tensor("Add:0", shape=(), dtype=float32)

๊ฐ ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ tensor์ž„์„ ์•Œ๋ ค์ค€๋‹ค.

๊ฒฐ๊ณผ๋Š” ์–ด๋–ป๊ฒŒ ๋ณด๋‚˜??

sess = tf.Session()
print("sess.run(node1, node2): ", sess.run([node1, node2]))
print("sess.run(node3): ", sess.run(node3))

๊ฒฐ๊ณผ : sess.run(node1, node2): [3.0, 4.0] sess.run(node3): 7.0

์„ธ์…˜์„ ์ƒ์„ฑ ํ›„ ๋…ธ๋“œ๋ฅผ ์„ธ์…˜์„ ํ†ตํ•ด์„œ ๋Ÿฐํ•œ๋‹ค.


TensorFlow Mechnics

์ด๋ฏธ์ง€

  1. ๊ทธ๋ž˜ํ”„(๋…ธ๋“œ)๋ฅผ ์ƒ์„ฑ
  2. ์„ธ์…˜์„ ํ†ตํ•ด์„œ ๊ทธ๋ž˜ํ”„(๋…ธ๋“œ)๋ฅผ ์‹คํ–‰
  3. ๊ฐ’์„ ๋ฆฌํ„ด

Placeholder

์ •ํ•ด์ง„ constant value๊ฐ€ ์•„๋‹ˆ๋ผ runtime์— ๊ฐ’์„ ๋„ฃ๊ณ ์‹ถ๋‹ค๋ฉด(feed_dict) -> Placeholder

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b  # + provides a shortcut for tf.add(a, b)

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.]


tensor์˜ rank์˜ ๊ฐœ๋…?

๋ช‡ ์ฐจ์› array์ธ์ง€๋ฅผ ์˜๋ฏธ

# a rank 0 tensor
# this is a scalar with shape(์Šค์นผ๋ผ ๊ฐ’์„ ์˜๋ฏธ)
>>> 3

# a rank 1 tensor
# ๋ฒกํ„ฐ ๊ฐ’์„ ์˜๋ฏธ
>>> [1. ,2. ,3.]

# a rank 2 tensor
# ๋ฉ”ํŠธ๋ฆญ์Šค ๊ฐ’์„ ์˜๋ฏธ
>>> [1., 2., 3.], [4., 5., 6.](/8BitsCoding/RobotMentor/wiki/1.,-2.,-3.],-[4.,-5.,-6.)

์ด๋ฏธ์ง€


tensor์˜ shapes ๊ฐœ๋…?

ํ–‰, ์—ด์„ ์˜๋ฏธ

>>> t = [1, 2, 3], [4, 5, 6], [7, 8, 9](/8BitsCoding/RobotMentor/wiki/1,-2,-3],-[4,-5,-6],-[7,-8,-9)
# shape = [3, 3]

์ด๋ฏธ์ง€


tensor์˜ types

์ด๋ฏธ์ง€