TensorFlow có thể được sử dụng với estimator để compile mô hình với sự giúp đỡ của phương thức 'train'.
Đọ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. Có 4 feature và một label.
- sepal length (chiều dài đài hoa)
- sepal width (chiều rộng đài hoa)
- petal length (chiều dài cánh hoa)
- petal width (chiều rộng cánh hoa)
Ví dụ
print("Mô hình đang được huấn luyện")
classifier.train(input_fn=lambda: input_fn(train, train_y, training=True), steps=5000)
Nguồn mã: https://www.tensorflow.org/tutorials/estimator/premade#first_things_first
Đầu ra
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:loss = 0.36297452, step = 5000 (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>
Giải thích
- Một khi đối tượng Estimator được tạo, các phương thức dưới đây có thể được gọi:
- Mô hình được huấn luyện.
- Mô hình đã huấn luyện được đánh giá.
- Mô hình này được dùng để làm dự đoán.
- Mô hình được huấn luyện lại.
- Điều này được thực hiện bằng cách gọi phương thức train của Estimator.