บทนำ
TensorFlow เฟรมเวิร์ก ML จาก Google โค้ดเปิด ใช้กับ Python ใช้ในงานวิจัยและการผลิต มีเทคนิคเพิ่มประสิทธิภาพ ใช้ NumPy และเทนเซอร์ รองรับ Deep Neural Networks สเกลได้ดี ใช้ GPU computation จัดการทรัพยากรอัตโนมัติ
การติดตั้ง
pip install tensorflow
Tensor
เทนเซอร์ = อาร์เรย์หลายมิติ สำหรับ Data flow graph
ตัวอย่าง: Reduce Sum บน Matrix
import tensorflow as tf
import numpy as np
matrix_1 = tf.Variable([[1,2,3],[4,5,8],[9,10,0]])
print("Matrix:")
print(matrix_1)
print("ผลรวมทุกอิลิเมนต์:")
result = tf.reduce_sum(matrix_1)
print(result)
print("ผลรวมแต่ละแถว (axis=1):")
result = tf.reduce_sum(matrix_1, 1)
print(result)
ผลลัพธ์
Matrix:ผลรวมทุกอิลิเมนต์: tf.Tensor(42, shape=(), dtype=int32) ผลรวมแต่ละแถว: tf.Tensor([6 17 19], shape=(3,), dtype=int32)
คำอธิบาย
- นำเข้าแพ็กเกจและตั้งชื่อย่อ
- สร้าง Matrix ด้วย NumPy
- ใช้
tf.reduce_sumหาผลรวมทั้งหมด - ระบุ axis=1 เพื่อหาผลรวมแต่ละแถว (axis=0 จะเป็นแต่ละคอลัมน์)
- แสดงผลบนคอนโซล