0Pricing
MLOps Academy · บทเรียน

เขียนตัวพยากรณ์แบบกำหนดเอง

เสียบโค้ดการเตรียมข้อมูลและโมเดลของคุณเอง

เขียนตัวพยากรณ์แบบกำหนดเอง เป็นบทเรียน MLOps Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MLOps Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

When Built-ins Fall Short

The built-in runtimes cover sklearn, PyTorch, and friends. But when your model needs special logic, you write your own. A custom predictor gives you full control. 🛠️

It Is Just a Container

A custom predictor is your own container image that speaks the KServe protocol. You package your code, model, and dependencies into one image and hand it to KServe.

The KServe Python SDK

The kserve Python package gives you a base class to extend. You subclass kserve.Model and fill in how your model loads and predicts.

import kserve

class MyModel(kserve.Model):
    def __init__(self, name):
        super().__init__(name)
        self.ready = False

Implement load()

The load method reads your model from disk into memory once at startup. When it finishes, you set ready to True so KServe knows it can serve.

def load(self):
    self.model = joblib.load("/mnt/models/model.joblib")
    self.ready = True

Implement predict()

The predict method takes the request payload, runs your model, and returns the result as a dict. This is where your real inference logic lives.

def predict(self, payload, headers=None):
    rows = payload["instances"]
    preds = self.model.predict(rows)
    return {"predictions": preds.tolist()}

Custom Preprocessing Fits Here

Need to reshape inputs or apply business rules before inference? Put that logic right inside predict or in a preprocess step. The runtime never had to know about it.

Start the Model Server

You wire your class into a ModelServer and start it. KServe's server handles HTTP, health, and the protocol so you only write model code. ModelServer runs the loop.

if __name__ == "__main__":
    model = MyModel("custom-model")
    model.load()
    kserve.ModelServer().start([model])

Package It in a Dockerfile

You build a container that installs kserve, copies your code, and runs the server. This Dockerfile produces the image KServe will launch.

FROM python:3.11-slim
RUN pip install kserve joblib scikit-learn
COPY model.py /app/model.py
CMD ["python", "/app/model.py"]

Point the Spec at Your Image

In the InferenceService, the predictor uses a plain containers block instead of a model format. You name your image and KServe runs it.

spec:
  predictor:
    containers:
      - name: kserve-container
        image: my-registry/custom-model:latest

Mounting Your Model

You can still set a storageUri, and KServe mounts the downloaded model at a known path your code reads. The container and the storageUri work together.

Same Protocol, Your Logic

Because you implement the standard endpoints, clients call your custom predictor exactly like a built-in one. The protocol stays identical, only the internals are yours.

Quick Check

You subclass kserve.Model for a custom predictor. Which method loads the model into memory?

Recap

You built a custom predictor by subclassing kserve.Model, filling in load and predict, and shipping it as a container. Same protocol, your own logic. Awesome work! 🎉

คำถามที่พบบ่อย

บทเรียน “เขียนตัวพยากรณ์แบบกำหนดเอง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เขียนตัวพยากรณ์แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MLOps Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เขียนตัวพยากรณ์แบบกำหนดเอง”

เสียบโค้ดการเตรียมข้อมูลและโมเดลของคุณเอง คุณปฏิบัติ MLOps Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MLOps Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MLOps Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “เขียนตัวพยากรณ์แบบกำหนดเอง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MLOps Academy นี้ได้ไหม

ได้ บทเรียน MLOps Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทรัพยากร InferenceService
  2. ลดขนาดเป็นศูนย์และเพิ่มกลับขึ้นมา
  3. เขียนตัวพยากรณ์แบบกำหนดเอง
  4. KServe เทียบกับ Seldon Core
← กลับไปที่ MLOps Academy