![]() | ![]() | ![]() | ![]() |
บทช่วยสอนนี้แสดงวิธีแก้ปัญหาการจัดประเภทม่านตาใน TensorFlow โดยใช้เครื่องมือประมาณการ เครื่องมือประมาณค่าเป็นตัวแทนระดับสูงของแบบจำลองที่สมบูรณ์ของ TensorFlow และได้รับการออกแบบมาเพื่อให้ง่ายต่อการปรับขนาดและการฝึกอบรมแบบอะซิงโครนัส สำหรับรายละเอียดเพิ่มเติมโปรดดู เครื่องมือประมาณการ
โปรดทราบว่าใน TensorFlow 2.0 Keras API สามารถทำงานเหล่านี้ให้สำเร็จได้หลายอย่างและเชื่อว่าเป็น API ที่เรียนรู้ได้ง่ายกว่า หากคุณกำลังเริ่มต้นใหม่เราขอแนะนำให้คุณเริ่มต้นด้วย Keras สำหรับข้อมูลเพิ่มเติมเกี่ยวกับ API ระดับสูงที่มีอยู่ใน TensorFlow 2.0 โปรดดูการ กำหนดมาตรฐานบน Keras
สิ่งแรกก่อน
ในการเริ่มต้นคุณต้องนำเข้า TensorFlow ก่อนและไลบรารีจำนวนหนึ่งที่คุณต้องการ
import tensorflow as tf
import pandas as pd
ชุดข้อมูล
โปรแกรมตัวอย่างในเอกสารนี้สร้างและทดสอบแบบจำลองที่จำแนกดอกไอริสออกเป็นสามชนิดโดยพิจารณาจากขนาดของ กลีบเลี้ยง และ กลีบดอก
คุณจะฝึกโมเดลโดยใช้ชุดข้อมูล Iris ชุดข้อมูล Iris ประกอบด้วยคุณสมบัติสี่ประการและหนึ่ง ป้ายกำกับ คุณสมบัติทั้งสี่นี้ระบุลักษณะทางพฤกษศาสตร์ของดอกไอริสแต่ละดอก:
- ความยาวของกลีบเลี้ยง
- ความกว้างของกลีบเลี้ยง
- ความยาวกลีบดอก
- ความกว้างของกลีบดอก
จากข้อมูลนี้คุณสามารถกำหนดค่าคงที่ที่เป็นประโยชน์สำหรับการแยกวิเคราะห์ข้อมูลได้ดังนี้
CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
SPECIES = ['Setosa', 'Versicolor', 'Virginica']
จากนั้นดาวน์โหลดและแยกวิเคราะห์ชุดข้อมูล Iris โดยใช้ Keras และ Pandas โปรดทราบว่าคุณเก็บชุดข้อมูลที่แตกต่างกันสำหรับการฝึกอบรมและการทดสอบ
train_path = tf.keras.utils.get_file(
"iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv")
test_path = tf.keras.utils.get_file(
"iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv")
train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
คุณสามารถตรวจสอบข้อมูลของคุณเพื่อดูว่าคุณมีคอลัมน์คุณลักษณะโฟลตสี่คอลัมน์และป้ายกำกับ int32 หนึ่งรายการ
train.head()
สำหรับชุดข้อมูลแต่ละชุดให้แยกป้ายกำกับซึ่งโมเดลจะได้รับการฝึกฝนให้คาดการณ์
train_y = train.pop('Species')
test_y = test.pop('Species')
# The label column has now been removed from the features.
train.head()
ภาพรวมของการเขียนโปรแกรมด้วย Estimators
เมื่อคุณได้ตั้งค่าข้อมูลแล้วคุณสามารถกำหนดแบบจำลองโดยใช้เครื่องมือประมาณการ TensorFlow Estimator คือคลาสใด ๆ ที่ได้มาจาก tf.estimator.Estimator
TensorFlow จัดเตรียมชุดของ tf.estimator
(ตัวอย่างเช่น LinearRegressor
) เพื่อใช้อัลกอริทึม ML ทั่วไป นอกเหนือจากนั้นคุณสามารถเขียน เครื่องมือประมาณการ ของคุณ เอง ได้ เราขอแนะนำให้ใช้เครื่องมือประมาณการที่สร้างไว้ล่วงหน้าเมื่อเพิ่งเริ่มต้น
ในการเขียนโปรแกรม TensorFlow โดยใช้เครื่องมือประมาณการที่สร้างไว้ล่วงหน้าคุณต้องดำเนินการดังต่อไปนี้:
- สร้างฟังก์ชันการป้อนข้อมูลอย่างน้อยหนึ่งฟังก์ชัน
- กำหนดคอลัมน์คุณลักษณะของโมเดล
- เริ่มต้นเครื่องมือประมาณการโดยระบุคอลัมน์คุณลักษณะและพารามิเตอร์หลายพารามิเตอร์ต่างๆ
- เรียกใช้วิธีการอย่างน้อยหนึ่งวิธีบนออบเจ็กต์ Estimator โดยส่งผ่านฟังก์ชันอินพุตที่เหมาะสมเป็นแหล่งที่มาของข้อมูล
มาดูกันว่างานเหล่านั้นถูกนำไปใช้งานอย่างไรสำหรับการจำแนกไอริส
สร้างฟังก์ชันการป้อนข้อมูล
คุณต้องสร้างฟังก์ชันอินพุตเพื่อจัดหาข้อมูลสำหรับการฝึกอบรมการประเมินและการคาดคะเน
ฟังก์ชันอินพุต คือฟังก์ชันที่ส่งคืนอ็อบเจ็กต์tf.data.Dataset
ซึ่งแสดงผลลัพธ์ทูเพิลสององค์ประกอบต่อไปนี้:
-
features
- พจนานุกรม Python ซึ่ง:- แต่ละคีย์คือชื่อของคุณสมบัติ
- แต่ละค่าคืออาร์เรย์ที่มีค่าทั้งหมดของคุณลักษณะนั้น
-
label
- อาร์เรย์ที่มีค่าของ ป้ายกำกับ สำหรับทุกตัวอย่าง
เพียงเพื่อแสดงให้เห็นถึงรูปแบบของฟังก์ชั่นการป้อนข้อมูลนี่คือการใช้งานง่ายๆ:
def input_evaluation_set():
features = {'SepalLength': np.array([6.4, 5.0]),
'SepalWidth': np.array([2.8, 2.3]),
'PetalLength': np.array([5.6, 3.3]),
'PetalWidth': np.array([2.2, 1.0])}
labels = np.array([2, 1])
return features, labels
ฟังก์ชันการป้อนข้อมูลของคุณอาจสร้างพจนานุกรม features
และรายการ label
ในแบบที่คุณต้องการ อย่างไรก็ตามเราขอแนะนำให้ใช้ Dataset API ของ TensorFlow ซึ่งสามารถแยกวิเคราะห์ข้อมูลได้ทุกประเภท
Dataset API สามารถจัดการกรณีทั่วไปจำนวนมากให้คุณได้ ตัวอย่างเช่นเมื่อใช้ Dataset API คุณสามารถอ่านในเรกคอร์ดจากคอลเล็กชันไฟล์ขนาดใหญ่พร้อมกันและรวมเข้าเป็นสตรีมเดียวได้อย่างง่ายดาย
เพื่อให้สิ่งต่างๆง่ายขึ้นในตัวอย่างนี้คุณจะต้องโหลดข้อมูลด้วย แพนด้า และสร้างไปป์ไลน์อินพุตจากข้อมูลในหน่วยความจำนี้:
def input_fn(features, labels, training=True, batch_size=256):
"""An input function for training or evaluating"""
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
# Shuffle and repeat if you are in training mode.
if training:
dataset = dataset.shuffle(1000).repeat()
return dataset.batch(batch_size)
กำหนดคอลัมน์คุณลักษณะ
คอลัมน์คุณลักษณะ คือวัตถุที่อธิบายว่าโมเดลควรใช้ข้อมูลอินพุตดิบจากพจนานุกรมคุณลักษณะอย่างไร เมื่อคุณสร้างแบบจำลองเครื่องมือประมาณการคุณจะส่งผ่านรายการคอลัมน์คุณลักษณะที่อธิบายคุณลักษณะแต่ละอย่างที่คุณต้องการให้โมเดลใช้ โมดูล tf.feature_column
มีตัวเลือกมากมายสำหรับการแสดงข้อมูลไปยังโมเดล
สำหรับ Iris คุณลักษณะดิบ 4 รายการเป็นค่าตัวเลขดังนั้นเราจะสร้างรายการคอลัมน์คุณลักษณะเพื่อบอกให้โมเดลเครื่องมือประมาณการแสดงคุณลักษณะทั้งสี่เป็นค่าทศนิยม 32 บิต ดังนั้นรหัสในการสร้างคอลัมน์คุณลักษณะคือ:
# Feature columns describe how to use the input.
my_feature_columns = []
for key in train.keys():
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
คอลัมน์คุณลักษณะอาจซับซ้อนกว่าคอลัมน์ที่เราแสดงไว้ที่นี่ คุณสามารถอ่านเพิ่มเติมเกี่ยวกับคอลัมน์คุณลักษณะได้ใน คู่มือ นี้
ตอนนี้คุณมีคำอธิบายว่าคุณต้องการให้โมเดลแสดงคุณสมบัติดิบอย่างไรคุณสามารถสร้างตัวประมาณค่าได้
สร้างเครื่องมือประมาณค่าทันที
ปัญหาม่านตาเป็นปัญหาการจำแนกคลาสสิก โชคดีที่ TensorFlow มีเครื่องมือประมาณการลักษณนามที่สร้างไว้ล่วงหน้าหลายตัว ได้แก่ :
-
tf.estimator.DNNClassifier
สำหรับโมเดลระดับลึกที่ดำเนินการจำแนกหลายคลาส -
tf.estimator.DNNLinearCombinedClassifier
สำหรับรุ่นกว้างและลึก -
tf.estimator.LinearClassifier
สำหรับลักษณนามตามแบบจำลองเชิงเส้น
สำหรับปัญหาม่านตา tf.estimator.DNNClassifier
ดูเหมือนจะเป็นทางเลือกที่ดีที่สุด นี่คือวิธีที่คุณสร้างอินสแตนซ์เครื่องมือประมาณการนี้:
# Build a DNN with 2 hidden layers with 30 and 10 hidden nodes each.
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
# Two hidden layers of 30 and 10 nodes respectively.
hidden_units=[30, 10],
# The model must choose between 3 classes.
n_classes=3)
INFO:tensorflow:Using default config. WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpbhg2uvbr INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpbhg2uvbr', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true graph_options { rewrite_options { meta_optimizer_iterations: ONE } } , '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
ฝึกประเมินและคาดการณ์
ตอนนี้คุณมีออบเจ็กต์ Estimator แล้วคุณสามารถเรียกใช้เมธอดเพื่อทำสิ่งต่อไปนี้:
- ฝึกโมเดล
- ประเมินรูปแบบการฝึกอบรม
- ใช้แบบจำลองที่ได้รับการฝึกฝนเพื่อทำการคาดการณ์
ฝึกโมเดล
ฝึกโมเดลโดยเรียกใช้เมธอด train
ของ Estimator ดังนี้:
# Train the Model.
classifier.train(
input_fn=lambda: input_fn(train, train_y, training=True),
steps=5000)
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version. Instructions for updating: Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts. INFO:tensorflow:Calling model_fn. WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx. If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2. To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor. Warning:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/keras/optimizer_v2/adagrad.py:83: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version. Instructions for updating: Call initializer instance with the dtype argument instead of passing it to the constructor INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Create CheckpointSaverHook. INFO:tensorflow:Graph was finalized. INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 0... INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmpbhg2uvbr/model.ckpt. INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 0... INFO:tensorflow:loss = 1.1140382, step = 0 INFO:tensorflow:global_step/sec: 312.415 INFO:tensorflow:loss = 0.8781501, step = 100 (0.321 sec) INFO:tensorflow:global_step/sec: 375.535 INFO:tensorflow:loss = 0.80712265, step = 200 (0.266 sec) INFO:tensorflow:global_step/sec: 372.712 INFO:tensorflow:loss = 0.7615077, step = 300 (0.268 sec) INFO:tensorflow:global_step/sec: 368.782 INFO:tensorflow:loss = 0.733555, step = 400 (0.271 sec) INFO:tensorflow:global_step/sec: 372.689 INFO:tensorflow:loss = 0.6983943, step = 500 (0.268 sec) INFO:tensorflow:global_step/sec: 370.308 INFO:tensorflow:loss = 0.67940104, step = 600 (0.270 sec) INFO:tensorflow:global_step/sec: 373.374 INFO:tensorflow:loss = 0.65386146, step = 700 (0.268 sec) INFO:tensorflow:global_step/sec: 368.335 INFO:tensorflow:loss = 0.63730353, step = 800 (0.272 sec) INFO:tensorflow:global_step/sec: 371.575 INFO:tensorflow:loss = 0.61313766, step = 900 (0.269 sec) INFO:tensorflow:global_step/sec: 371.975 INFO:tensorflow:loss = 0.6123625, step = 1000 (0.269 sec) INFO:tensorflow:global_step/sec: 369.615 INFO:tensorflow:loss = 0.5957534, step = 1100 (0.270 sec) INFO:tensorflow:global_step/sec: 374.054 INFO:tensorflow:loss = 0.57203, step = 1200 (0.267 sec) INFO:tensorflow:global_step/sec: 369.713 INFO:tensorflow:loss = 0.56556034, step = 1300 (0.270 sec) INFO:tensorflow:global_step/sec: 366.202 INFO:tensorflow:loss = 0.547443, step = 1400 (0.273 sec) INFO:tensorflow:global_step/sec: 361.407 INFO:tensorflow:loss = 0.53326523, step = 1500 (0.277 sec) INFO:tensorflow:global_step/sec: 367.461 INFO:tensorflow:loss = 0.51837724, step = 1600 (0.272 sec) INFO:tensorflow:global_step/sec: 364.181 INFO:tensorflow:loss = 0.5281174, step = 1700 (0.275 sec) INFO:tensorflow:global_step/sec: 368.139 INFO:tensorflow:loss = 0.5139683, step = 1800 (0.271 sec) INFO:tensorflow:global_step/sec: 366.277 INFO:tensorflow:loss = 0.51073176, step = 1900 (0.273 sec) INFO:tensorflow:global_step/sec: 366.634 INFO:tensorflow:loss = 0.4949246, step = 2000 (0.273 sec) INFO:tensorflow:global_step/sec: 364.732 INFO:tensorflow:loss = 0.49381495, step = 2100 (0.274 sec) INFO:tensorflow:global_step/sec: 365.006 INFO:tensorflow:loss = 0.48916715, step = 2200 (0.274 sec) INFO:tensorflow:global_step/sec: 366.902 INFO:tensorflow:loss = 0.48790723, step = 2300 (0.273 sec) INFO:tensorflow:global_step/sec: 362.232 INFO:tensorflow:loss = 0.47671652, step = 2400 (0.276 sec) INFO:tensorflow:global_step/sec: 368.592 INFO:tensorflow:loss = 0.47324088, step = 2500 (0.271 sec) INFO:tensorflow:global_step/sec: 371.611 INFO:tensorflow:loss = 0.46822113, step = 2600 (0.269 sec) INFO:tensorflow:global_step/sec: 362.345 INFO:tensorflow:loss = 0.4621966, step = 2700 (0.276 sec) INFO:tensorflow:global_step/sec: 362.788 INFO:tensorflow:loss = 0.47817266, step = 2800 (0.275 sec) INFO:tensorflow:global_step/sec: 368.473 INFO:tensorflow:loss = 0.45853442, step = 2900 (0.271 sec) INFO:tensorflow:global_step/sec: 360.944 INFO:tensorflow:loss = 0.44062576, step = 3000 (0.277 sec) INFO:tensorflow:global_step/sec: 370.982 INFO:tensorflow:loss = 0.4331399, step = 3100 (0.269 sec) INFO:tensorflow:global_step/sec: 366.248 INFO:tensorflow:loss = 0.45120597, step = 3200 (0.273 sec) INFO:tensorflow:global_step/sec: 371.703 INFO:tensorflow:loss = 0.4403404, step = 3300 (0.269 sec) INFO:tensorflow:global_step/sec: 362.176 INFO:tensorflow:loss = 0.42405623, step = 3400 (0.276 sec) INFO:tensorflow:global_step/sec: 363.283 INFO:tensorflow:loss = 0.41672814, step = 3500 (0.275 sec) INFO:tensorflow:global_step/sec: 363.529 INFO:tensorflow:loss = 0.42626005, step = 3600 (0.275 sec) INFO:tensorflow:global_step/sec: 367.348 INFO:tensorflow:loss = 0.4089098, step = 3700 (0.272 sec) INFO:tensorflow:global_step/sec: 363.067 INFO:tensorflow:loss = 0.41276374, step = 3800 (0.275 sec) INFO:tensorflow:global_step/sec: 364.771 INFO:tensorflow:loss = 0.4112524, step = 3900 (0.274 sec) INFO:tensorflow:global_step/sec: 363.167 INFO:tensorflow:loss = 0.39261794, step = 4000 (0.275 sec) INFO:tensorflow:global_step/sec: 362.082 INFO:tensorflow:loss = 0.41160905, step = 4100 (0.276 sec) INFO:tensorflow:global_step/sec: 364.979 INFO:tensorflow:loss = 0.39620766, step = 4200 (0.274 sec) INFO:tensorflow:global_step/sec: 363.323 INFO:tensorflow:loss = 0.39696264, step = 4300 (0.275 sec) INFO:tensorflow:global_step/sec: 361.25 INFO:tensorflow:loss = 0.38196522, step = 4400 (0.277 sec) INFO:tensorflow:global_step/sec: 365.666 INFO:tensorflow:loss = 0.38667366, step = 4500 (0.274 sec) INFO:tensorflow:global_step/sec: 361.202 INFO:tensorflow:loss = 0.38149032, step = 4600 (0.277 sec) INFO:tensorflow:global_step/sec: 365.038 INFO:tensorflow:loss = 0.37832782, step = 4700 (0.274 sec) INFO:tensorflow:global_step/sec: 366.375 INFO:tensorflow:loss = 0.3726803, step = 4800 (0.273 sec) INFO:tensorflow:global_step/sec: 366.474 INFO:tensorflow:loss = 0.37167495, step = 4900 (0.273 sec) INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 5000... INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpbhg2uvbr/model.ckpt. INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 5000... INFO:tensorflow:Loss for final step: 0.36297452. <tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7fc9983ed470>
โปรดทราบว่าคุณสรุปการเรียก input_fn
ของคุณใน lambda
เพื่อจับอาร์กิวเมนต์ในขณะที่จัดเตรียมฟังก์ชันอินพุตที่ไม่มีอาร์กิวเมนต์ตามที่ Estimator คาดไว้ อาร์กิวเมนต์ steps
จะบอกวิธีการหยุดการฝึกหลังจากขั้นตอนการฝึกอบรมหลายขั้นตอน
ประเมินรูปแบบการฝึกอบรม
ตอนนี้โมเดลได้รับการฝึกฝนแล้วคุณจะได้รับสถิติบางอย่างเกี่ยวกับประสิทธิภาพ บล็อกโค้ดต่อไปนี้จะประเมินความถูกต้องของแบบจำลองที่ได้รับการฝึกฝนบนข้อมูลทดสอบ:
eval_result = classifier.evaluate(
input_fn=lambda: input_fn(test, test_y, training=False))
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
INFO:tensorflow:Calling model_fn. WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx. If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2. To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor. INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Starting evaluation at 2020-09-10T01:40:47Z INFO:tensorflow:Graph was finalized. INFO:tensorflow:Restoring parameters from /tmp/tmpbhg2uvbr/model.ckpt-5000 INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. INFO:tensorflow:Inference Time : 0.21153s INFO:tensorflow:Finished evaluation at 2020-09-10-01:40:47 INFO:tensorflow:Saving dict for global step 5000: accuracy = 0.96666664, average_loss = 0.42594802, global_step = 5000, loss = 0.42594802 INFO:tensorflow:Saving 'checkpoint_path' summary for global step 5000: /tmp/tmpbhg2uvbr/model.ckpt-5000 Test set accuracy: 0.967
ไม่เหมือนกับการโทรไปยังวิธีการ train
คุณไม่ได้ผ่าน steps
เพื่อประเมิน input_fn
สำหรับ eval ให้ข้อมูลเพียง ช่วง เดียวเท่านั้น
นอกจากนี้พจนานุกรม eval_result
ยังประกอบด้วย average_loss
(การสูญเสียค่าเฉลี่ยต่อตัวอย่าง) การ loss
( loss
เฉลี่ยต่อชุดย่อย) และค่า global_step
ของตัวประมาณค่า (จำนวนการทำซ้ำการฝึกอบรมที่ได้รับ)
ทำการคาดการณ์ (อนุมาน) จากแบบจำลองที่ได้รับการฝึกฝน
ขณะนี้คุณมีรูปแบบการฝึกอบรมที่ให้ผลการประเมินที่ดี ตอนนี้คุณสามารถใช้แบบจำลองที่ได้รับการฝึกฝนเพื่อทำนายสายพันธุ์ของดอกไอริสตามขนาดที่ไม่มีป้าย เช่นเดียวกับการฝึกอบรมและการประเมินผลคุณสามารถคาดเดาได้โดยใช้การเรียกใช้ฟังก์ชันเดียว:
# Generate predictions from the 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],
}
def input_fn(features, batch_size=256):
"""An input function for prediction."""
# Convert the inputs to a Dataset without labels.
return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)
predictions = classifier.predict(
input_fn=lambda: input_fn(predict_x))
วิธีการ predict
จะส่งคืน Python ที่ทำซ้ำได้โดยให้พจนานุกรมผลลัพธ์การทำนายสำหรับแต่ละตัวอย่าง รหัสต่อไปนี้จะพิมพ์การคาดการณ์และความน่าจะเป็น:
for pred_dict, expec in zip(predictions, expected):
class_id = pred_dict['class_ids'][0]
probability = pred_dict['probabilities'][class_id]
print('Prediction is "{}" ({:.1f}%), expected "{}"'.format(
SPECIES[class_id], 100 * probability, expec))
INFO:tensorflow:Calling model_fn. INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Graph was finalized. INFO:tensorflow:Restoring parameters from /tmp/tmpbhg2uvbr/model.ckpt-5000 INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. Prediction is "Setosa" (91.3%), expected "Setosa" Prediction is "Versicolor" (52.0%), expected "Versicolor" Prediction is "Virginica" (63.5%), expected "Virginica"