Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Standardize Flower Dataset ด้วย TensorFlow

บทนำ

Data standardization: ปรับสเกลค่าพิกเซลให้อยู่ในช่วง [0,1] แทนที่จะเป็น [0,255] ช่วย neural network เรียนรู้ได้ดีขึ้น

โค้ดตัวอย่าง

from tensorflow.keras import layers
print("Standardizing the data using a rescaling layer")
normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)

print("This layer can be applied by calling the map function on the dataset")
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))

image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
print(np.min(first_image), np.max(first_image))
# Output: 0.0 0.96902645

คำอธิบาย

  • RGB values เดิม 0-255 → Rescaling(1./255) → 0-1
  • ใช้ map บน dataset: lambda x, y: (normalization_layer(x), y)
  • ตรวจสอบ: min=0.0, max≈0.97 (ไม่ถึง 1.0 เพราะภาพจริงไม่มีสีขาวสนิท)
  • วิธีอื่น: ใส่ Rescaling layer ในโมเดลโดยตรง (ดูบทความ 1004103458) → deploy ง่ายกว่า

ทำไมต้อง Standardize?

- Gradient descent converge เร็วขึ้นเมื่อ features มี scale คล้ายกัน
- หลีกเลี่ยงปัญหา vanishing/exploding gradients
- Numerical stability ดีขึ้น