Einen benutzerdefinierten Predictor schreiben
Binden Sie Ihren eigenen Preprocessing- und Modellcode ein.
Einen benutzerdefinierten Predictor schreiben ist eine kostenlose MLOps Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MLOps Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MLOps Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 = FalseImplement 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 = TrueImplement 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:latestMounting 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! 🎉
Häufig gestellte Fragen
Ist die Lektion „Einen benutzerdefinierten Predictor schreiben“ kostenlos?
Ja — der vollständige Text von „Einen benutzerdefinierten Predictor schreiben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MLOps Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MLOps Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Einen benutzerdefinierten Predictor schreiben“?
Binden Sie Ihren eigenen Preprocessing- und Modellcode ein. Du übst MLOps Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um MLOps Academy zu starten?
Keine Vorkenntnisse erforderlich. MLOps Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Einen benutzerdefinierten Predictor schreiben“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser MLOps Academy-Lektion Code schreiben und ausführen?
Ja. Jede MLOps Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Die InferenceService-Ressource
- Auf null skalieren und wieder hochskalieren
- Einen benutzerdefinierten Predictor schreiben
- KServe und Seldon Core