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

Tensorflow สามารถใช้กับ Estimators เพื่อประเมินโมเดลโดยใช้ Python ได้อย่างไร


สามารถใช้ Tensorflow กับตัวประมาณเพื่อประเมินแบบจำลองโดยใช้วิธี 'ประเมิน' ที่มีอยู่ในโมดูล 'ตัวแยกประเภท'

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

เราจะใช้ Keras Sequential API ซึ่งมีประโยชน์ในการสร้างแบบจำลองตามลำดับที่ใช้ในการทำงานกับสแต็กเลเยอร์ธรรมดา โดยที่ทุกเลเยอร์มีอินพุตเทนเซอร์หนึ่งตัวและเอาต์พุตเทนเซอร์หนึ่งรายการ

โครงข่ายประสาทเทียมที่มีอย่างน้อยหนึ่งชั้นเรียกว่าชั้น Convolutional เราสามารถใช้ Convolutional Neural Network เพื่อสร้างแบบจำลองการเรียนรู้

TensorFlow Text มีคอลเลกชั่นของคลาสที่เกี่ยวข้องกับข้อความและ ops ที่สามารถใช้ได้กับ TensorFlow 2.0 สามารถใช้ข้อความ TensorFlow เพื่อประมวลผลการสร้างแบบจำลองลำดับล่วงหน้าได้

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

Estimator คือการแสดงโมเดลที่สมบูรณ์ของ TensorFlow ในระดับสูง ออกแบบมาเพื่อการปรับขนาดและการฝึกแบบอะซิงโครนัสที่ง่ายดาย

โมเดลนี้ฝึกโดยใช้ชุดข้อมูลไอริส

ตัวอย่าง

eval_result = classifier.evaluate(input_fn=lambda: input_fn(test, test_y, training=False))
print('\nTest dataset accuracy is: {accuracy:0.3f}\n'.format(**eval_result))

เครดิตโค้ด −https://www.tensorflow.org/tutorials/estimator/premade#first_things_first

ผลลัพธ์

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 dataset accuracy is: 0.967

คำอธิบาย

  • เมื่อฝึก Moel แล้ว สามารถรับข้อมูลบางอย่างเกี่ยวกับประสิทธิภาพได้

  • ไม่มีการส่งพารามิเตอร์ไปยังฟังก์ชัน 'ประเมิน'

  • input_fn สำหรับ eval จะให้ข้อมูลเพียงยุคเดียวเท่านั้น

  • พจนานุกรม eval_result ประกอบด้วย average_loss (การสูญเสียเฉลี่ยต่อตัวอย่าง) การสูญเสีย (การสูญเสียเฉลี่ยต่อ mini-batch) และมูลค่าของ global_step ของตัวประมาณ (จำนวนการทำซ้ำการฝึกอบรมที่ได้รับ)