1. สร้าง Graph หรือ flow ของการทำงาน
2. การสั่งให้ทำงานจะเรียกว่า SESSION
้เริ่มต้นของการใช้งาน จะต้อง import tensorflow เข้ามาใช้งานกันก่อน ด้วยคำสั่งตั้งอย่าง
Code: Select all
import tensorflow as tf
ตัวอย่างเเรกในการหัดเขียน
Code: Select all
import tensorflow as tf
a = 20
b = 30
c = tf.add(a, b, name='Add')
print(c)
Code: Select all
Tensor("Add:0", shape=(), dtype=int32)
Code: Select all
sess = tf.Session()
print(sess.run(c))
sess.close()
Code: Select all
50
Code: Select all
sess.close()
ภาพตัวอย่างในการรันแสดงผล
เเต่ถ้าหากเราใช้ with ตามตัวอย่าง เราไม่ต้องทำการปิด Session ก็ได้ สามารถเลือกใช้งานได้ 2 วิธี
Code: Select all
with tf.Session() as sess:
print(sess.run(c))
Code: Select all
50
Code: Select all
import tensorflow as tf
# create graph
a = tf.constant("hello world")
# launch the graph in a session
with tf.Session() as sess:
print(sess.run(a))
Code: Select all
hello world
อ้างอิง
-https://www.easy-tensorflow.com/tf-tuto ... nd-session