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