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

วิธีใช้ TensorFlow Export โมเดลที่สร้างขึ้นด้วย Python


TensorFlow เฟรมเวิร์ก ML จาก Google Open Source ใช้กับ Python อัลกอริทึม Deep Learning งานวิจัยและ Production มีเทคนิคเพิ่มประสิทธิภาพคำนวณ ใช้ NumPy และ Multi-dimensional Arrays (Tensors) รองรับ Deep Neural Network สเกลได้ดี มี Dataset ยอดนิยม ใช้ GPU Computation จัดการ Resource อัตโนมัติ มี Library ML มากมาย รองรับเอกสารดี รัน โมเดล Deep Neural Network ฝึกและสร้าง Application ทำนายลักษณะข้อมูลได้

ติดตั้ง tensorflow บน Windows:

pip install tensorflow

Tensor โครงสร้างข้อมูลใน TensorFlow (Data Flow Graph) เป็นอาร์เรย์หลายมิติ/ลิสต์

ใช้ Google Colab รันโค้ด (Python บนเบราว์เซอร์ ฟรี GPU บน Jupyter Notebook)

ตัวอย่างโค้ด

print("The model is being exported")
export_model = tf.keras.Sequential(
    [binary_vectorize_layer, binary_model,
    layers.Activation('sigmoid')])
print("The model is being compiled")
export_model.compile(
    loss=losses.SparseCategoricalCrossentropy(from_logits=False),
    optimizer='adam',
    metrics=['accuracy'])
print("The model is being tested with `raw_test_ds`, which resuls in raw strings")
loss, accuracy = export_model.evaluate(raw_test_ds)
print("The accuracy of the model is : {:2.2%}".format(binary_accuracy))

Credit: TensorFlow Tutorials

ผลลัพธ์

The model is being exported
The model is being compiled
The model is being tested with `raw_test_ds`, which resuls in raw strings
250/250 [==============================] - 4s 13ms/step - loss: 0.5296 - accuracy: 0.8078
The accuracy of the model is : 81.10%

คำอธิบาย

  • Layer TextVectorization ใช้กับ Dataset ก่อนเข้าโมเดล

  • ถ้าต้องการให้โมเดลรับ Raw Strings ให้ใส่ TextVectorization Layer เข้าในโมเดล

  • สร้างโมเดลใหม่ด้วย Weights ที่ใช้ตอน Train