Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

TensorFlow có thể được sử dụng với Estimators để đánh giá mô hình bằng Python như thế nào?


TensorFlow có thể được sử dụng với estimator để đánh giá mô hình với sự giúp đỡ của phương thức 'evaluate' có trong module 'classifier'.

Đọc thêm: TensorFlow là gì và Keras hoạt động như thế nào với TensorFlow để tạo mạng nơ-ron?

Chúng ta sẽ sử dụng Keras Sequential API, giúp xây dựng mô hình tuần tự dùng cho stack các layer đơn giản, nơi mỗi layer có chính xác một input tensor và một output tensor.

Một mạng nơ-ron chứa ít nhất một layer được gọi là layer tích chập. Chúng ta có thể sử dụng Mạng nơ-ron tích chập (CNN) để xây dựng mô hình học.

TensorFlow Text chứa tập hợp các lớp và ops liên quan đến văn bản có thể được sử dụng với TensorFlow 2.0. TensorFlow Text có thể được dùng để tiền xử lý sequence modelling.

Chúng ta đang sử dụng Google Colaboratory để chạy mã dưới đây. Google Colab giúp chạy mã Python trên trình duyệt và yêu cầu cấu hình zero và truy cập miễn phí GPU. Colaboratory được xây dựng dựa trên Jupyter Notebook.

Một Estimator là biểu diễn cấp cao của TensorFlow cho một mô hình hoàn chỉnh. Nó được thiết kế để dễ dàng mở rộng và huấn luyện bất đồng bộ.

Mô hình được huấn luyện sử dụng bộ dữ liệu iris.

Ví dụ

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))

Nguồn mã: https://www.tensorflow.org/tutorials/estimator/premade#first_things_first

Đầu ra

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

Giải thích

  • Một khi mô hình được huấn luyện, một số thông tin về hiệu suất có thể được thu được.
  • Không có tham số nào được truyền cho hàm 'evaluate'.
  • input_fn cho eval chỉ yield một epoch dữ liệu.
  • eval_result dictionary chứa average_loss (mean loss per sample), loss (mean loss per mini-batch) và giá trị global_step của estimator (số lần lặp huấn luyện).