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)

+ Recent posts