Tensorflow ๊ฐ์
Google ๋จธ์ ๋ฌ๋ ์ธํ ๋ฆฌ์ ์ค ์ฐ๊ตฌ์์ ๋ธ๋ ์ธ ํ ๊ฐ๋ฐ
๋จธ์ ๋ฌ๋๊ณผ ๋ฅ ๋ด๋ด ๋คํธ์ํฌ ์ฐ๊ตฌ ๋ชฉ์
์ฃผ์ ๊ธฐ๋ฅ
๋ค์ฐจ์ ๋ฐฐ์ด(Tensor) ์ ์, ์ต์ ํ, ํจ์จ์ ์ฐ์ ์ฐ์ฐ
๋ฅ ๋ด๋ด ๋คํธ์ํฌ์ ๋จธ์ ๋ฌ๋ ํ๋ก๊ทธ๋๋ฐ ์ง์
๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํฐ ์๋ ๊ด๋ฆฌ, GPU ๊ฐ์ ๊ธฐ๋ฅ ์ ๊ณต (ํ ์ํ๋ก ์๋ CPU, GPU ์์ ํ ๋น)
๋น ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ๋๊ท๋ชจ ๋ณ๋ ฌ ์ปดํจํ ์ง์
CPU vs GPU
CPU : ์ง๋ ฌ ์ฒ๋ฆฌ์ ์ต์ ํ๋ ๋ช ๊ฐ์ ์ฝ์ด๋ก ๊ตฌ์ฑ
GPU : ๋ณ๋ ฌ ์ฒ๋ฆฌ์ฉ์ผ๋ก ์ค๊ณ๋ ์ ์ฒ ๊ฐ์ ๋ณด๋ค ์ํ, ํจ์จ์ ์ธ ์ฝ์ด
* ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฐ์ฐ์ง์ฝ์ ์ธ ๋ถ๋ถ์ GPU๋ก ๋๊ธฐ๊ณ ๋๋จธ์ง ์ฝ๋๋ง CPU ์ฒ๋ฆฌ
Tensorflow ๊ฐ๋ฐ ํ๊ฒฝ
๊ฐ์ํ๊ฒฝ ์ฌ์ฉ ์ด์
* ๊ฐ์ํ๊ฒฝ : ๊ฐ์์ Python ๊ฐ๋ฐํ๊ฒฝ ์๋ฏธ
1) Python ๋ฒ์ ๊ด๋ฆฌ -> ํ๋์ ์์คํ ์ base, 2.7, 3.7 ๋ฒ์ ๋ฑ ์ค์น
2) Python ๋ฒ์ ์ ๋ฐ๋ฅธ ํจํค์ง ์์กด์ฑ ๋ฌธ์ ํด๊ฒฐ -> ex) ์ต์ ๋ฒ์ (base)์์ tensorflow ์ต์ ๋ฒ์ ์ค์น ์ค๋ฅ
3) Python ํ๋ก์ ํธ ์งํ ์ -> ํ๋ก์ ํธ์ ์ต์ ํ๋ ํ๊ฒฝ ๊ตฌ์ถ(ํ์ํ ํจํค์ง๋ง ์ค์น)
tensorflow์ค์น
๋จ๊ณ1 : Python ๋ฒ์ ํ์ธ
Anaconda Prompt> python
๋จ๊ณ2 : ๊ฐ์ํ๊ฒฝ ๋ง๋ค๊ธฐ Tensorflow ๊ฐ์ํ๊ฒฝ(python 3.8.8์ค์น)
ํ์) conda create –n ๊ฐ์ํ๊ฒฝ์ด๋ฆ python==๋ฒ์
conda create –n tensorflow python==3.8.8
* ๊ฐ์ํ๊ฒฝ ๋ฆฌ์คํธ ํ์ธ (base) > conda env list

๋จ๊ณ3 : ๊ฐ์ํ๊ฒฝ ํ์ฑํ
(base) > conda activate tensorflow

๋จ๊ณ4 : Tensorflow ์ต์ ๋ฒ์ ์ค์น
ํ์) conda install ํจํค์ง์ด๋ฆ
conda install tensorflow
* Tensorflow 1.6 ๋ถํฐ conda ๋ฐฉ์์ผ๋ก ์ค์น ๊ถ์ฅ
๋จ๊ณ5 : Spyder ์ค์น(Tensorflow ์คํ์ฉ)
(tensorflow) > conda install spyder
[์๋ฌ] ERROR conda.core.link:_execute(698)
์์คํ ํ๊ฒฝ๋ณ์ > ์์คํ ๋ณ์ > Path, C:\Windows\System32 ์ถ๊ฐํ๊ธฐ -> ์๋จ..^^
[์๋ฌ] ERROR conda.core.link:_execute(699): An error occurred while installing package 'defaults::bcrypt-3.2.0-py38he774522_0'
๊ฐ์ํ๊ฒฝ ์ญ์ : conda env remove --n tensorflow
๋จ๊ณ 1~5๊น์ง ์ฌ์ค์น
[์๋ฌ]WARNING: Target Directory <directory> already exists." when I pip install a module?
๋ฌด์ํ๊ณ y ํ์ -> ์ค์น ์๋ฃ!
๋จ๊ณ6 : Spyder ์คํ(Tensorflow ์คํ์ฉ)
๊ฐ์ํ๊ฒฝ Tensorflow์ฉ Spyder ์คํ
๋จ๊ณ7 : Spyder import ํ ์คํธ
๋จ๊ณ8 : Spyder ํ๊ฒฝ์ค์
์ํน๋๋ ํ ๋ฆฌ ๋ณ๊ฒฝ
๋จ๊ณ9 : ๊ฐ์ํ๊ฒฝ ์ข ๋ฃ
(tensorflow) > conda deactivate
* (base)๋ก ํ๋กฌํํธ ๋ณ๊ฒฝ
๊ฐ์ํ๊ฒฝ ์ ๊ฑฐ (๋ฐฉ๋ฒ1) : conda remove --name ๊ฐ์ํ๊ฒฝ์ด๋ฆ --all
(tensorflow) conda deactivate
(base) > conda remove --name tensorflow -- all
* ์ฃผ์ : ๊ฐ์ํ๊ฒฝ ์ ๊ฑฐ๋ ๊ฐ์ํ๊ฒฝ์์ ๋น ์ ธ๋์จ ํ ์คํํ๋ค.
tf basic
Python code vs Tensorflow code
์์์ ์ ์ ์
Python code
x = 10 #๊ฐ ํ ๋น
y = 30
print('x=',x) #x= 10
z = x + y #์ฐ์ฐ
print('z=', z) #์ถ๋ ฅ : z= 40
Tensorflow ver1.x code
import tensorflow.compat.v1 as tf #ver1.x -> ver2.x ๋ง์ด๊ทธ๋ ์ด์
tf.disable_v2_behavior() #ver2.x ์ฌ์ฉ ์ํจ
[ํ๋ก๊ทธ๋จ ์ ์ ์์ญ]
์์ ์ ์
x = tf.constant(10) #์ซ์ ์์
y = tf.constant(20) #์ซ์ ์์
print('x =', x)
print('y =', y)
x = Tensor("Const:0", shape=(), dtype=int32)
y = Tensor("Const_1:0", shape=(), dtype=int32)
์ ์ ์
z = tf.add(x, y) #z = x + y
print('z=', z) #z = Tensor("Add:0", shape=(), dtype=int32)
sess = tf.Session() #์ธ์
๊ฐ์ฒด
[ํ๋ก๊ทธ๋จ ์คํ ์์ญ]
print('x =', sess.run(x)) #์ฒ๋ฆฌ๊ธฐ ํ ๋น -> ๊ฐ ํ ๋น(x=10)
#x = 10
print('y =', sess.run(y)) #์ฒ๋ฆฌ๊ธฐ ํ ๋น -> ๊ฐ ํ ๋น(y=20)
#y = 20
print('z =', sess.run(z)) #์ฒ๋ฆฌ๊ธฐ ํ ๋น -> ๋ง์
์ฐ์ฐ
#z = 30
sess.close() #์ธ์
๊ฐ์ฒด ๋ซ๊ธฐ
tensorboard ver1.x
Tensorboard
๊ทธ๋ํ ์๊ฐํ ๋๊ตฌ
Node์ ๊ณ์ธต์ ๋ถ๋ฅ์ ์์ธํ ์ ๊ณต
ํน์ ๋ถ๋ถ ํ๋ ๋ฐ ์ถ์ → ๋ณ์์ ํต๊ณ์ ๋ณด ์ ๊ณต
๋ฐ์ดํฐ์ ํ๋ฆ ์๊ฐํ ๊ธฐ๋ฅ
Tensorflow ver1.x code
import tensorflow.compat.v1 as tf #ver1.x -> ver2.x ๋ง์ด๊ทธ๋ ์ด์
tf.disable_v2_behavior() #ver2.x ์ฌ์ฉ ์ํจ
tensorboard ์ด๊ธฐํ
tf.reset_default_graph()
[์ ์ ์์ญ]
์์ ์ ์
x = tf.constant(1, name='x')
y = tf.constant(2, name='y')
์ฐ์ ์ฐ์ฐ์ ์ ์ ์
a = tf.add(x, y, name='a') #a = x + y
b = tf.multiply(a, 6, name='b') #b = a * 6
c = tf.subtract(20, 10, name='c') #c = 20 - 10
d = tf.div(c, 2, name='d') #d = c / 2
g = tf.add(b, d, name='g')
h = tf.multiply(g, d, name='h')
with tf.Session() as sess : #session ๊ฐ์ฒด
''' ์คํ ์์ญ '''
print('d =', sess.run(d)) #device ํ ๋น -> ์ ์ฐ์ฐ
#d = 5
print('h =', sess.run(h)) #device ํ ๋น -> ์ ์ฐ์ฐ
#h = 115
#tensorboard graph ๋ก๊ทธ ์์ฑ
tf.summary.merge_all() #์์, ์ ๋ชจ์ผ๋ ์ญํ
write = tf.summary.FileWriter(r'C:\ITWILL\5_Tensorflow\graph',sess.graph) # log file ๊ฒฝ๋ก
write.close()
Tensorboard ๋ก๊ทธํ์ผ ์คํ(๊ฐ์ํ๊ฒฝ์์)

Tensorboard URL (http://localhost:6006/) ์ ์

h : ์ต์์ ๋ ธ๋
d : ์์ ๋ ธ๋
x, y, Edge : ์ ์ ๋ ฅ๊ฐ
C Node : ํ์ ์ฐ์ฐ
tensorflow logic
if, while
import tensorflow.compat.v1 as tf #ver1.x
tf.disable_v2_behavior() #ver2.0 ์ฌ์ฉ์ํจ
if ๋ฌธ
x = tf.constant(10) #x = 10
def true_fn() :
return tf.multiply(x , 10)
def false_fn():
return tf.add(x, 10)
y = tf.cond(x > 100, true_fn, false_fn)
x > 100 : ์กฐ๊ฑด์
true_fn() : true
false_fn() : false
while ๋ฌธ
i = tf.constant(0) #i = 0 : ๋ฐ๋ณต๋ณ์
def cond(i) : #i = ๋ฐ๋ณต๋ณ์
return tf.less(i, 100) #i < 100
def body(i) : #i = ๋ฐ๋ณต๋ณ์
return tf.add(i, 1) #i += 1
loop = tf.while_loop(cond, body, (i,)) #[i]
cond : ์กฐ๊ฑดํจ์
body : ๋ฐ๋ณต๋ฌธ
(i,) : ๋ฐ๋ณต๋ณ์
with tf.Session() as sess :
print("if =", sess.run(y)) #if = 20
print("loop =", sess.run(loop)) #loop = 100
eager execution ver2.x
* ์ฃผ์ : spyder ์ฌ์คํ ํ์
Tensorflow 2.x ํน์ง
๋จ์์ฑ๊ณผ ํธ์์ฑ์ ์ด์ ์ ๋๊ณ ์
๊ทธ๋ ์ด๋
python์ฒ๋ผ ์ฆ์ ์คํ(eager execution) -> session ์์ด ์ฝ๋ ์คํ
@tf.function ๊ธฐ๋ฅ : Python ์ฝ๋ ์ง์
Keras ๋ชจ๋ : tensorflow ๋จธ์ ๋ฌ๋ & ๋ฅ๋ฌ๋ ๋ชจ๋ธ ์ ๊ณต
import tensorflow as tf #ver2.x
print(tf.__version__) #2.3.0
์ฆ์ ์คํ ๋ชจ๋
tf.executing_eagerly() #default ํ์ฑํ
์์ ์ ์ + ์์ฑ
a = tf.constant(value=[[1,2], [3,4]], dtype = tf.float32) #[1, 2]๋ 1ํ [3,4]๋ 2ํ / ๋ฐ์ดํฐ ํ์
์ 32bit ์ค์ํ -> 2์ฐจ์ ํ๋ ฌ๊ตฌ์กฐ
print(a)
tf.Tensor( : ๋ค์ฐจ์ ๋ฐ์ดํฐ, ๋ชจ์, ์๋ฃํ
[[1. 2.]
[3. 4.]], shape=(2, 2), dtype=float32)
print(a.numpy()) #๊ฐ์ฒด a ์์ ์์น ๋ฐ์ดํฐ๋ง ์ถ๋ ฅ
์ ์ ์ + ์คํ
b = tf.add(x = a, y = 0.5) #b = a + 0.5 -> ์ฐจ์์ด ๋ฌ๋ผ๋ braodcast์ฐ์ฐ์ ์ํด ๊ฐ๋ฅ
print(b)
tf.Tensor(
[[1.5 2.5]
[3.5 4.5]], shape=(2, 2), dtype=float32)
print(b.numpy()) #๊ฐ์ฒด b ์์ ์์ ๋ฐ์ดํฐ๋ง ์ถ๋ ฅ๊ฐ๋ฅ
tf.Tensor(
[[1.5 2.5]
[3.5 4.5]], shape=(2, 2), dtype=float32)
ํ๋ ฌ๊ณฑ(ํ๋ ฌ๋ด์ )
X = [[2.0, 3.0]] #(1, 2)
w = [[1.0], [1.5]] #(2, 1)
mat = tf.linalg.matmul(a = X, b = w) #data = python
print(mat) #[[6.5]]
print(mat.numpy()) #[[6, 5]]
@tf.function ver2.x
@tf.function ํจ์์ฅ์์
- ๋์ ํจ์์์์ python code ์์ฑ ์ง์
- python code -> tensorflow code ์๋๋ณํ
-> ๊ฐ๋ฐ์์๊ฒ ํธ์์ฑ์ ์ ๊ณต.
-> tensorflow๋ ์ ์ฉ ๋ฅ๋ฌ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ ๊ณตํ๋ค. ์ผ๋ฐ์ ์ธ ํ๋ก๊ทธ๋จ์์๋ python์ฝ๋๋ฅผ ์ ๊ณตํ๋ ๊ฒ์ด 2.0์ ๋ชฉ์
import tensorflow as tf #ver2.x
if ์ฒ๋ฆฌ
@tf.function #ํจ์์ฅ์์ : python์ฝ๋๋ก ์ฒ๋ฆฌํ ์ ์๊ฒ ๋์์ค
def if_fn(x) :
#python code -> tensorflow code
if x > 100 :
y = x * 10
else :
y = x + 10
return y
x = tf.constant(value=10) #tensorflow ๊ฐ์ฒด
ํจ์ ํธ์ถ
print('y = ', if_fn(x)) #y = tf.Tensor(20, shape=(), dtype=int32)
print('y = ', if_fn(x).numpy()) #y = 20
while ์ฒ๋ฆฌ
@tf.function #ํจ์์ฅ์์
def while_fn(i) :
#python code๋ก ์์ฑ -> ๋ด๋ถ์์๋ tensorflow code๋ก ๋ฐ์ ์ฒ๋ฆฌ
while i < 100 :
i += 1 # i = i + 1
return i #๋ฐ๋ณต๋ณ์
i = tf.constant(value = 0) #i = 0 (์์ ์ด๊ธฐ๊ฐ)
print('i =', while_fn(i)) #i = tf.Tensor(100, shape=(), dtype=int32)'๋ฐ์ดํฐ๋ถ์๊ฐ ๊ณผ์ > Tensorflow' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| DAY65. Tensorflow Keras model (1)dnn model (0) | 2021.12.22 |
|---|---|
| DAY64. Tensorflow Classification (Sigmoid, Softmax) (0) | 2021.12.21 |
| DAY63. Tensorflow LinearRegression (3)keras dnn (0) | 2021.12.20 |
| DAY62. Tensorflow LinearRegression (2)ํ๊ท๋ชจ๋ธ (0) | 2021.12.17 |
| DAY61. Tensorflow LinearRegression (1)function basic (๊ธฐ๋ณธํจ์) (0) | 2021.12.16 |