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
- ๊ทธ๋ํ(๋ ธ๋)๋ฅผ ์์ฑ
- ์ธ์ ์ ํตํด์ ๊ทธ๋ํ(๋ ธ๋)๋ฅผ ์คํ
- ๊ฐ์ ๋ฆฌํด
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]