๊ฐ์ธ๊ณต๋ถ/Tensorflow
81. Tensorflow Basic ์ฐ์ต๋ฌธ์
LEE_BOMB
2021. 12. 15. 23:32
๋ฌธ1) ๋ ์์๋ฅผ ์ ์ํ๊ณ , ์ฌ์น์ฐ์ฐ ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ์์ค.
์กฐ๊ฑด1> ๋ ์์ ์ด๋ฆ : a, b
์กฐ๊ฑด2> ๋ณ์ ์ด๋ฆ : add,subtract,multiply,div
<<์ถ๋ ฅ๊ฒฐ๊ณผ>>
a= 100
b= 20
===============
๋ง์ = 120
๋บ์ = 80
๊ณฑ์ = 2000
๋๋์ = 5.0
import tensorflow.compat.v1 as tf # ver1.x
tf.disable_v2_behavior() #ver2.0 ์ฌ์ฉ์ํจ
[ํ๋ก๊ทธ๋จ ์ ์ ์์ญ]
์์ ์ ์
a = tf.constant(100)
b = tf.constant(20)
์ ์ ์
add = tf.add(a, b)
sub = tf.subtract(a, b)
mul = tf.multiply(a, b)
div = tf.div(a, b)
sess = tf.Session() #session ์์ฑ
[ํ๋ก๊ทธ๋จ ์คํ ์์ญ]
print('a =', sess.run(a))
print('b =', sess.run(b))
print('='*20)
print('๋ง์
=', sess.run(add))
print('๋บ์
=', sess.run(sub))
print('๊ณฑ์
=', sess.run(mul))
print('๋๋์
=', sess.run(div))
sess.close()
๋ฌธ2) ๋ค์๊ณผ ๊ฐ์ ์์์ ์ฌ์น์ฐ์ฐ ํจ์๋ฅผ ์ด์ฉํ์ฌ dataflow์ graph๋ฅผ ์์ฑํ์ฌ tensorboard๋ก ์ถ๋ ฅํ์์ค.
์กฐ๊ฑด1> ์์ : x = 100, y = 50
์กฐ๊ฑด2> ๊ณ์ฐ์ : result = ((x - 5) * y) / (y + 20)
-> ๊ณ์ฐ์ ์ฐ์ ์์์ ๋ง๊ฒ ์ฌ์น์ฐ์ฐ ํจ์ ์ด์ฉ ์ ์์ฑ
1. sub = x - 5
2. mul = sub * y
3. add = y + 20
4. div = mul / add
์กฐ๊ฑด3> ์ถ๋ ฅ graph : ๊ฐ์์๋ฃ ์ฐธ๊ณ
import tensorflow.compat.v1 as tf #ver 1.x
tf.disable_v2_behavior() #ver 2.x ์ฌ์ฉ์ํจ
tensorboard ์ด๊ธฐํ
tf.reset_default_graph()
์์ ์ ์
x = tf.constant(100, name = 'x')
y = tf.constant(50, name = 'y')
๊ณ์ฐ์ ์ ์ : result = ((x - 5) * y) / (y + 20)
sub = tf.subtract(x, 5, name='subtract')
mul = tf.multiply(sub, y, name='multiply')
add = tf.add(y, 20, name='add')
div = tf.div(mul, add, name='div')
with tf.Session() as sess :
print('div =', sess.run(div)) #div = 67
#tensorboard graph ๋ก๊ทธ ์์ฑ
tf.summary.merge_all() #์์, ์ ๋ชจ์ผ๋ ์ญํ
write = tf.summary.FileWriter(r'C:\ITWILL\5_Tensorflow\graph',sess.graph) #log file ๊ฒฝ๋ก
write.close()