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

การทำนาย Fashion MNIST ด้วย TensorFlow


บทนำ

TensorFlow เฟรมเวิร์ก ML จาก Google โค้ดเปิด ใช้กับ Python ใช้ในงานวิจัยและการผลิต มีเทคนิคเพิ่มประสิทธิภาพ ใช้ NumPy และเทนเซอร์

การติดตั้ง

pip install tensorflow

Tensor และชุดข้อมูล Fashion MNIST

เทนเซอร์ = อาร์เรย์หลายมิติ สำหรับ Data flow graph Fashion MNIST: ภาพเสื้อผ้า 70,000+ ภาพ 10 หมวดหมู่ 28x28 pixels

ตัวอย่าง: สร้าง Probability Model และ ทำนาย

probability_model = tf.keras.Sequential([
    model,
    tf.keras.layers.Softmax()
])

predictions = probability_model.predict(test_images)
print("กำลังทำนาย")
print(predictions[0])
np.argmax(predictions[0])

print("ป้ายกำกับทดสอบ:")
print(test_labels[0])

def plot_image(i, predictions_array, true_label, img):
    true_label, img = true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(img, cmap=plt.cm.binary)
    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'
    plt.xlabel("{} {:2.0f}% ({})".format(
        class_names[predicted_label],
        100*np.max(predictions_array),
        class_names[true_label]), color=color)

def plot_value_array(i, predictions_array, true_label):
    true_label = true_label[i]
    plt.grid(False)
    plt.xticks(range(10))
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)
    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('green')

ที่มาโค้ด: TensorFlow Classification Tutorial

ผลลัพธ์

กำลังทำนาย
[1.3008227e-07 9.4930819e-10 2.0181861e-09 5.4944155e-10 3.8257373e-11 1.3896286e-04 1.4776078e-08 3.1724274e-03 9.4210514e-11 9.9668854e-01]
ป้ายกำกับทดสอบ: 9

คำอธิบาย

  • หลังฝึกโมเดล ต้องทดสอบโดยทำนายภาพ
  • ต่อ Softmax layer เข้ากับ logits เพื่อแปลงเป็นความน่าจะเป็น
  • Softmax ช่วยแปลง logits เป็น probabilities ง่ายต่อการตีความ
  • กำหนด plot_value_array แสดงค่าจริงและทำนาย