![]() | ![]() | ![]() | ![]() |
นี่คือบทแนะนำ TensorFlow เบื้องต้นที่แสดงวิธีการ:
- นำเข้าแพ็คเกจที่จำเป็น
- สร้างและใช้เทนเซอร์
- ใช้การเร่งความเร็ว GPU
- สาธิต
tf.data.Dataset
นำเข้า TensorFlow
ในการเริ่มต้น ให้นำเข้าโมดูล tensorflow
ลว์ สำหรับ TensorFlow 2 การดำเนินการอย่างกระตือรือร้นจะเปิดไว้โดยค่าเริ่มต้น สิ่งนี้ทำให้ส่วนหน้าโต้ตอบกับ TensorFlow ได้มากขึ้น ซึ่งเราจะพูดถึงรายละเอียดในภายหลัง
import tensorflow as tf
เทนเซอร์
เทนเซอร์เป็นอาร์เรย์หลายมิติ คล้ายกับวัตถุ NumPy ndarray
วัตถุ tf.Tensor
มีชนิดข้อมูลและรูปร่าง นอกจากนี้ tf.Tensor
สามารถอยู่ในหน่วยความจำคันเร่ง (เช่น GPU) TensorFlow มีไลบรารีปฏิบัติการมากมาย ( tf.add , tf.matmul , tf.linalg.inv เป็นต้น) ที่ใช้และผลิต tf.Tensor
s การดำเนินการเหล่านี้จะแปลงประเภท Python ดั้งเดิมโดยอัตโนมัติ เช่น:
print(tf.add(1, 2)) print(tf.add([1, 2], [3, 4])) print(tf.square(5)) print(tf.reduce_sum([1, 2, 3])) # Operator overloading is also supported print(tf.square(2) + tf.square(3))
tf.Tensor(3, shape=(), dtype=int32) tf.Tensor([4 6], shape=(2,), dtype=int32) tf.Tensor(25, shape=(), dtype=int32) tf.Tensor(6, shape=(), dtype=int32) tf.Tensor(13, shape=(), dtype=int32)
tf.Tensor
แต่ละตัวมีรูปร่างและประเภทข้อมูล:
x = tf.matmul([[1]], [[2, 3]]) print(x) print(x.shape) print(x.dtype)
tf.Tensor([[2 3]], shape=(1, 2), dtype=int32) (1, 2) <dtype: 'int32'>
ความแตกต่างที่ชัดเจนที่สุดระหว่างอาร์เรย์ NumPy และ tf.Tensor
คือ:
- รองรับเทนเซอร์ด้วยหน่วยความจำคันเร่ง (เช่น GPU, TPU)
- เทนเซอร์ไม่เปลี่ยนรูป
ความเข้ากันได้ของ NumPy
การแปลงระหว่าง TensorFlow tf.Tensor
s และ NumPy ndarray
นั้นง่ายมาก:
- การทำงานของ TensorFlow จะแปลง NumPy ndarrays เป็น Tensors โดยอัตโนมัติ
- การดำเนินการ NumPy จะแปลง Tensors เป็น NumPy ndarrays โดยอัตโนมัติ
เทนเซอร์จะถูกแปลงเป็น NumPy ndarrays อย่างชัดเจนโดยใช้เมธอด . .numpy()
การแปลงเหล่านี้โดยทั่วไปจะมีราคาถูกเนื่องจากอาร์เรย์และ tf.Tensor
แบ่งปันการแสดงหน่วยความจำพื้นฐาน ถ้าเป็นไปได้ อย่างไรก็ตาม การแชร์การแสดงข้อมูลพื้นฐานอาจไม่สามารถทำได้เสมอไป เนื่องจาก tf.Tensor
อาจโฮสต์อยู่ในหน่วยความจำ GPU ในขณะที่อาร์เรย์ NumPy จะได้รับการสนับสนุนโดยหน่วยความจำของโฮสต์เสมอ และการแปลงเกี่ยวข้องกับการคัดลอกจาก GPU ไปยังหน่วยความจำโฮสต์
import numpy as np ndarray = np.ones([3, 3]) print("TensorFlow operations convert numpy arrays to Tensors automatically") tensor = tf.multiply(ndarray, 42) print(tensor) print("And NumPy operations convert Tensors to numpy arrays automatically") print(np.add(tensor, 1)) print("The .numpy() method explicitly converts a Tensor to a numpy array") print(tensor.numpy())
TensorFlow operations convert numpy arrays to Tensors automatically tf.Tensor( [[42. 42. 42.] [42. 42. 42.] [42. 42. 42.]], shape=(3, 3), dtype=float64) And NumPy operations convert Tensors to numpy arrays automatically [[43. 43. 43.] [43. 43. 43.] [43. 43. 43.]] The .numpy() method explicitly converts a Tensor to a numpy array [[42. 42. 42.] [42. 42. 42.] [42. 42. 42.]]
การเร่งความเร็ว GPU
การทำงานของ TensorFlow หลายอย่างถูกเร่งความเร็วโดยใช้ GPU สำหรับการคำนวณ หากไม่มีคำอธิบายประกอบ TensorFlow จะตัดสินใจโดยอัตโนมัติว่าจะใช้ GPU หรือ CPU สำหรับการทำงานหรือไม่ โดยจะคัดลอกเทนเซอร์ระหว่างหน่วยความจำ CPU และ GPU หากจำเป็น เทนเซอร์ที่เกิดจากการดำเนินการมักจะได้รับการสนับสนุนโดยหน่วยความจำของอุปกรณ์ที่การดำเนินการดำเนินการเช่น:
x = tf.random.uniform([3, 3]) print("Is there a GPU available: "), print(tf.config.list_physical_devices("GPU")) print("Is the Tensor on GPU #0: "), print(x.device.endswith('GPU:0'))
Is there a GPU available: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] Is the Tensor on GPU #0: True
ชื่ออุปกรณ์
คุณสมบัติ Tensor.device
จัดเตรียมชื่อสตริงแบบเต็มของอุปกรณ์ที่โฮสต์เนื้อหาของเทนเซอร์ ชื่อนี้เข้ารหัสรายละเอียดมากมาย เช่น ตัวระบุที่อยู่เครือข่ายของโฮสต์ที่โปรแกรมนี้กำลังทำงานและอุปกรณ์ภายในโฮสต์นั้น สิ่งนี้จำเป็นสำหรับการดำเนินการแบบกระจายของโปรแกรม TensorFlow สตริงลงท้ายด้วย GPU:<N>
ถ้าเทนเซอร์ถูกวางบน GPU ลำดับที่ N
บนโฮสต์
ตำแหน่งอุปกรณ์ที่ชัดเจน
ใน TensorFlow การ จัดวาง หมายถึงวิธีการกำหนด (วางบน) อุปกรณ์สำหรับการดำเนินการแต่ละรายการ ดังที่กล่าวไว้ เมื่อไม่มีคำแนะนำที่ชัดเจน TensorFlow จะตัดสินใจโดยอัตโนมัติว่าอุปกรณ์ใดที่จะดำเนินการและคัดลอกเทนเซอร์ไปยังอุปกรณ์นั้น หากจำเป็น อย่างไรก็ตาม การดำเนินการของ TensorFlow สามารถวางไว้บนอุปกรณ์เฉพาะได้อย่างชัดเจนโดยใช้ตัวจัดการบริบท tf.device
ตัวอย่างเช่น:
import time def time_matmul(x): start = time.time() for loop in range(10): tf.matmul(x, x) result = time.time()-start print("10 loops: {:0.2f}ms".format(1000*result)) # Force execution on CPU print("On CPU:") with tf.device("CPU:0"): x = tf.random.uniform([1000, 1000]) assert x.device.endswith("CPU:0") time_matmul(x) # Force execution on GPU #0 if available if tf.config.list_physical_devices("GPU"): print("On GPU:") with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc. x = tf.random.uniform([1000, 1000]) assert x.device.endswith("GPU:0") time_matmul(x)
On CPU: 10 loops: 91.47ms On GPU: 10 loops: 388.16ms
ชุดข้อมูล
ส่วนนี้ใช้ tf.data.Dataset
API เพื่อสร้างไปป์ไลน์สำหรับการป้อนข้อมูลไปยังโมเดลของคุณ tf.data.Dataset
API ใช้เพื่อสร้างไพพ์ไลน์อินพุตที่มีประสิทธิภาพและซับซ้อนจากชิ้นส่วนที่เรียบง่ายและนำกลับมาใช้ใหม่ได้ ซึ่งจะป้อนการฝึกอบรมของโมเดลหรือลูปการประเมิน
สร้าง Dataset
สร้างชุดข้อมูล ต้นทาง โดยใช้หนึ่งในฟังก์ชันของโรงงาน เช่น Dataset.from_tensors
, Dataset.from_tensor_slices
หรือใช้อ็อบเจ็กต์ที่อ่านจากไฟล์ เช่น TextLineDataset
หรือ TFRecordDataset
ดู คำแนะนำชุดข้อมูล TensorFlow สำหรับข้อมูลเพิ่มเติม
ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6]) # Create a CSV file import tempfile _, filename = tempfile.mkstemp() with open(filename, 'w') as f: f.write("""Line 1 Line 2 Line 3 """) ds_file = tf.data.TextLineDataset(filename)
ใช้การแปลงร่าง
ใช้ฟังก์ชันการแปลง เช่น map
, batch
และ shuffle
เพื่อใช้การแปลงกับเร็กคอร์ดชุดข้อมูล
ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2) ds_file = ds_file.batch(2)
ย้ำ
tf.data.Dataset
อบเจ็กต์สนับสนุนการวนซ้ำเพื่อวนซ้ำเร็กคอร์ด:
print('Elements of ds_tensors:') for x in ds_tensors: print(x) print('\nElements in ds_file:') for x in ds_file: print(x)
Elements of ds_tensors: tf.Tensor([1 9], shape=(2,), dtype=int32) tf.Tensor([16 4], shape=(2,), dtype=int32) tf.Tensor([25 36], shape=(2,), dtype=int32) Elements in ds_file: tf.Tensor([b'Line 1' b'Line 2'], shape=(2,), dtype=string) tf.Tensor([b'Line 3' b' '], shape=(2,), dtype=string)