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

วิธีใช้ TensorFlow เพื่อแสดงผลการฝึกอบรมด้วย Python


ผลลัพธ์การฝึกอบรมสามารถแสดงภาพได้ด้วย TensorFlow โดยใช้ Python ร่วมกับไลบรารี 'matplotlib' เมธอด 'plot' ถูกใช้เพื่อพล็อตข้อมูลบนคอนโซล

อ่านเพิ่มเติม: TensorFlow คืออะไร และ Keras ทำงานกับ TensorFlow เพื่อสร้าง Neural Networks อย่างไร?

เราจะใช้ Keras Sequential API ซึ่งช่วยในการสร้างโมเดล sequential ที่ใช้ทำงานกับชั้นเลเยอร์แบบเรียงลำดับ โดยแต่ละเลเยอร์มี input tensor และ output tensor เพียงหนึ่งเดียว

Neural network ที่มีอย่างน้อยหนึ่งเลเยอร์แบบ Convolutional เรียกว่า Convolutional Neural Network เราสามารถใช้ CNN เพื่อสร้างโมเดลการเรียนรู้

Image classifier ถูกสร้างโดยใช้ keras.Sequential model และโหลดข้อมูลด้วย preprocessing.image_dataset_from_directory ข้อมูลถูกโหลดจากดิสก์อย่างมีประสิทธิภาพ Overfitting ถูกตรวจจับและมีการใช้เทคนิคเพื่อลดทอน ได้แก่ data augmentation และ dropout มีรูปภาพดอกไม้ 3700 รูป ชุดข้อมูลนี้มี 5 โฟลเดอร์ย่อย โดยมีหนึ่งโฟลเดอร์ต่อคลาส ได้แก่: daisy, dandelion, roses, sunflowers, และ tulips

เราใช้ Google Colaboratory เพื่อรันโค้ดด้านล่าง Google Colab ช่วยให้รันโค้ด Python ผ่านเบราว์เซอร์ ไม่ต้องตั้งค่า และเข้าถึง GPU ฟรี Colaboratory สร้างขึ้นบน Jupyter Notebook

ตัวอย่าง

print("Calculating the accuracy")
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
print("Calculating the loss")
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
print("The results are being visualized")
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

Code credit - https://www.tensorflow.org/tutorials/images/classification

ผลลัพธ์

Calculating the accuracy
Calculating the loss
The results are being visualized

วิธีใช้ TensorFlow เพื่อแสดงผลการฝึกอบรมด้วย Python

คำอธิบาย

  • กราฟด้านบนแสดงว่า training accuracy และ validation accuracy ไม่สอดคล้องกัน

  • โมเดลบรรลุ accuracy ประมาณ 60 เปอร์เซ็นต์บน validation dataset

  • นี่คือ overfitting

  • Training accuracy เพิ่มขึ้นแบบเส้นตรงตามเวลา แต่ validation accuracy คงที่ที่ประมาณ 60 เปอร์เซ็นต์ระหว่างการฝึก

  • เมื่อจำนวนตัวอย่างฝึกอบรมน้อย โมเดลจะเรียนรู้จาก noise หรือรายละเอียดที่ไม่ต้องการ

  • สิ่งนี้ส่งผลต่อประสิทธิภาพของโมเดลกับตัวอย่างใหม่

  • เนื่องจาก overfitting โมเดลจะไม่สามารถ generalize ได้ดีกับชุดข้อมูลใหม่

  • มีหลายวิธีป้องกัน overfitting เราจะใช้ data augmentation เพื่อแก้ปัญหา