TensorFlow Decision Forests และการให้บริการ TensorFlow

TensorFlow Serving (TF Serving) เป็นเครื่องมือในการรันโมเดล TensorFlow ทางออนไลน์ในการตั้งค่าการผลิตขนาดใหญ่โดยใช้ RPC หรือ REST API TensorFlow Decision Forests (TF-DF) ได้รับการสนับสนุนโดย TF Serving >=2.11

รุ่น TF-DF เข้ากันได้โดยตรงกับ TF Serving โมเดล Yggdrasil สามารถใช้กับ TF Serving ได้หลังจาก แปลงแล้ว ก่อน

ข้อจำกัด

TensorFlow เพิ่มค่าใช้จ่ายในการคำนวณจำนวนมาก สำหรับโมเดลขนาดเล็กและไวต่อความหน่วง (เช่น เวลาอนุมานโมเดล ~1µs) โอเวอร์เฮดนี้อาจเป็นลำดับความสำคัญที่มากกว่าเวลาที่ตัวโมเดลต้องการ ในกรณีนี้ ขอแนะนำให้รันโมเดล TF-DF ด้วย Yggdrasil Decision Forests

ตัวอย่างการใช้งาน

ตัวอย่างต่อไปนี้แสดงวิธีการเรียกใช้โมเดล TF-DF ใน TF Serving:

ขั้นแรก ให้ติดตั้ง TF Serving ในตัวอย่างนี้ เราจะใช้ TF-Serving + TF-DF เวอร์ชันที่คอมไพล์ไว้ล่วงหน้า

# Download TF Serving
wget https://github.com/tensorflow/decision-forests/releases/download/serving-1.0.1/tensorflow_model_server_linux.zip
unzip tensorflow_model_server_linux.zip

# Check that TF Serving works.
./tensorflow_model_server --version

ในตัวอย่างนี้ เราใช้โมเดล TF-DF ที่ผ่านการฝึกอบรมแล้ว

# Get a TF-DF model
git clone https://github.com/tensorflow/decision-forests.git
MODEL_PATH=$(pwd)/decision-forests/tensorflow_decision_forests/test_data/model/saved_model_adult_rf

echo "The TF-DF model is available at: ${MODEL_PATH}"

หมายเหตุ: TF-Serving ต้องใช้เส้นทางแบบเต็มของโมเดล นี่คือเหตุผลที่เราใช้ $(pwd)

TF-Serving รองรับการกำหนดเวอร์ชันโมเดล โมเดลควรอยู่ในไดเร็กทอรีที่มีชื่อเป็นเวอร์ชันของโมเดล เวอร์ชันของโมเดลเป็นจำนวนเต็ม เช่น "1" นี่คือไดเร็กทอรีทั่วไปสำหรับ TF-Serving

  • /path/to/model
    • 1 : เวอร์ชัน 1 ของโมเดล
    • 5 : เวอร์ชัน 5 ของโมเดล
    • 6 : เวอร์ชัน 6 ของโมเดล

สำหรับตัวอย่างนี้ เราเพียงแต่ต้องวางโมเดลไว้ในไดเร็กทอรีชื่อ "1"

mkdir -p /tmp/tf_serving_model
cp -R "${MODEL_PATH}" /tmp/tf_serving_model/1

ตอนนี้เราสามารถเริ่ม TF-Sering บนโมเดลได้แล้ว

./tensorflow_model_server \
    --rest_api_port=8502 \
    --model_name=my_model \
    --model_base_path=/tmp/tf_serving_model

สุดท้าย คุณสามารถส่งคำขอไปยัง TF Serving โดยใช้ Rest API ได้ มีให้เลือกสองรูปแบบ: Predict+Instances API และ Predict+Inputs API นี่คือตัวอย่างของแต่ละข้อ:

# Predictions with the predict+instances API.
curl http://localhost:8502/v1/models/my_model:predict -X POST \
    -d '{"instances": [{"age":39,"workclass":"State-gov","fnlwgt":77516,"education":"Bachelors","education_num":13,"marital_status":"Never-married","occupation":"Adm-clerical","relationship":"Not-in-family","race":"White","sex":"Male","capital_gain":2174,"capital_loss":0,"hours_per_week":40,"native_country":"United-States"}]}'
# Predictions with the predict+inputs API
curl http://localhost:8502/v1/models/my_model:predict -X POST \
    -d '{"inputs": {"age":[39],"workclass":["State-gov"],"fnlwgt":[77516],"education":["Bachelors"],"education_num":[13],"marital_status":["Never-married"],"occupation":["Adm-clerical"],"relationship":["Not-in-family"],"race":["White"],"sex":["Male"],"capital_gain":[2174],"capital_loss":[0],"hours_per_week":[40],"native_country":["United-States"]} }'