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

การทำนายจากโมเดลที่เทรนแล้วด้วย TensorFlow Estimator


แนวคิด

ใช้ predict method ใน classifier ทำนายข้อมูลใหม่

ใช้ Keras Sequential API, CNN, TensorFlow Text, Google Colab, Estimator เหมือนเดิม

โมเดลเทรนด้วย Iris dataset มี 4 features 1 label: sepal length, sepal width, petal length, petal width

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

print("Generating predictions from model")
expected = ['Setosa', 'Versicolor', 'Virginica']
predict_x = {
    'SepalLength': [5.1, 5.9, 6.9],
    'SepalWidth': [3.3, 3.0, 3.1],
    'PetalLength': [1.7, 4.2, 5.4],
    'PetalWidth': [0.5, 1.5, 2.1],
}
print("Defining input function for prediction")
print("It converts inputs to dataset without labels")
def input_fn(features, batch_size=256):
    return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)
predictions = classifier.predict(
    input_fn=lambda: input_fn(predict_x))

Credit: TensorFlow Tutorials

ผลลัพธ์

Generating predictions from model
Defining input function for prediction
It converts inputs to dataset without labels

คำอธิบาย

  • โมเดลที่เทรนแล้วให้ผลดี
  • ใช้ทำนายสปีชีส์ดอกไม้ Iris จากขนาดที่ไม่มี label
  • ทำนายได้ด้วยการเรียกฟังก์ชันเดียว