本番環境 ML システム: デプロイテスト
コレクションでコンテンツを整理 必要に応じて、コンテンツの保存と分類を行います。
ユニコーンの出現を予測するユニコーン モデルをデプロイする準備が整いました。デプロイ時に、機械学習(ML)パイプラインが問題なく実行、更新、提供されるようにする必要があります。大きな [デプロイ] ボタンを押すだけでモデルをデプロイできたら、残念ながら、完全な ML システムでは、次のテストが必要です。
- 入力データの検証。
- 特徴量エンジニアリングの検証。
- 新しいモデル バージョンの品質を検証する。
- サービング インフラストラクチャの検証。
- パイプライン コンポーネント間の統合のテスト。
多くのソフトウェア エンジニアは、テスト駆動開発(TDD)を好みます。TDD では、ソフトウェア エンジニアは「実際の」ソースコードを記述する前にテストを記述します。ただし、機械学習では TDD が難しい場合があります。たとえば、モデルをトレーニングする前に、損失を検証するテストを作成することはできません。代わりに、まずモデル開発中に達成可能な損失を検出し、次に達成可能な損失に対して新しいモデル バージョンをテストする必要があります。
ユニコーン モデルについて
このセクションでは、ユニコーン モデルについて説明します。必知事項は次のとおりです。
機械学習を使用して、ユニコーンの出現を予測する分類モデルを構築しています。データセットには、ユニコーンの出現 10,000 件とユニコーンの出現なし 10,000 件の詳細が含まれています。このデータセットには、場所、時刻、高度、気温、湿度、樹木被覆、虹の有無など、さまざまな特徴が含まれています。
再現可能なトレーニングでモデルの更新をテストする
ユニコーン モデルの改善を継続したい場合もあります。たとえば、特定の特徴量に追加の特徴量エンジニアリングを行い、より良い(または少なくとも同じ)結果が得られるようにモデルを再トレーニングするとします。残念ながら、モデルのトレーニングを再現できないことがあります。再現性を高めるには、次の推奨事項に従ってください。
乱数ジェネレータを確定的にシードします。詳細については、データ生成のランダム化をご覧ください。
モデル コンポーネントを固定の順序で初期化して、コンポーネントが実行ごとに乱数生成ツールから同じ乱数を取得できるようにします。通常、ML ライブラリは、この要件を自動的に処理します。
モデルの複数の実行の平均値を取得します。
事前反復処理でもバージョン管理を使用すると、モデルやパイプラインを調査するときにコードとパラメータを特定できます。
これらのガイドラインに従ったとしても、非決定性の他の原因が存在する可能性があります。
ML API への呼び出しをテストする
API 呼び出しの更新をテストする方法モデルを再トレーニングすることもできますが、時間がかかります。代わりに、単体テストを作成してランダムな入力データを生成し、勾配降下法の 1 ステップを実行します。このステップがエラーなく完了した場合、API の更新によってモデルが破損していない可能性があります。
パイプライン コンポーネントの統合テストを作成する
ML パイプラインでは、1 つのコンポーネントの変更が他のコンポーネントでエラーを引き起こす可能性があります。パイプライン全体をエンドツーエンドで実行する統合テストを作成して、コンポーネントが連携して動作することを確認します。
インテグレーション テストを継続的に実行するだけでなく、新しいモデルと新しいソフトウェア バージョンをプッシュするときにインテグレーション テストを実行する必要があります。パイプライン全体の実行が遅いため、継続的インテグレーション テストが難しくなります。統合テストをより速く実行するには、データのサブセットまたはよりシンプルなモデルでトレーニングします。詳細はモデルとデータによって異なります。継続的なカバレッジを取得するには、高速テストを調整して、モデルまたはソフトウェアの新しいバージョンごとに実行するようにします。一方、時間のかかるテストはバックグラウンドで継続的に実行されます。
サービング前にモデルの品質を検証する
新しいモデル バージョンを本番環境に push する前に、次の 2 種類の品質低下をテストします。
突然の劣化。新しいバージョンのバグにより、品質が大幅に低下する可能性があります。新しいバージョンの品質を以前のバージョンと比較して検証します。
デグラデーションが遅い。突然の品質低下を検出するテストでは、複数のバージョンにわたるモデル品質の低下が検出されない場合があります。代わりに、検証データセットに対するモデルの予測が固定しきい値を満たしていることを確認します。検証データセットがライブデータから逸脱している場合は、検証データセットを更新し、モデルが引き続き同じ品質しきい値を満たしていることを確認します。
サービング前にモデルとインフラストラクチャの互換性を検証する
モデルがサーバーよりも速く更新されると、モデルとサーバーのソフトウェア依存関係が異なるため、互換性の問題が発生する可能性があります。モデルで使用されるオペレーションがサーバーに存在することを確認するには、サーバーのサンドボックス化バージョンにモデルをステージングします。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-27 UTC。
[null,null,["最終更新日 2025-07-27 UTC。"],[[["\u003cp\u003eDeploying a machine learning model involves validating data, features, model versions, serving infrastructure, and pipeline integration.\u003c/p\u003e\n"],["\u003cp\u003eReproducible model training involves deterministic seeding, fixed initialization order, averaging multiple runs, and using version control.\u003c/p\u003e\n"],["\u003cp\u003eIntegration tests ensure that different components of the ML pipeline work together seamlessly, and should be run continuously and for new model/software versions.\u003c/p\u003e\n"],["\u003cp\u003eBefore serving a new model, validate its quality by checking for sudden and slow degradations against previous versions and fixed thresholds.\u003c/p\u003e\n"],["\u003cp\u003eEnsure model-infrastructure compatibility by staging the model in a sandboxed server environment to avoid dependency conflicts.\u003c/p\u003e\n"]]],[],null,["# Production ML systems: Deployment testing\n\nYou're ready to deploy the unicorn model that predicts unicorn appearances!\nWhen deploying, your machine learning (ML) pipeline should run, update, and\nserve without a problem. If only deploying a model were as easy as pressing\na big **Deploy** button. Unfortunately, a full machine learning system\nrequires tests for:\n\n- Validating input data.\n- Validating feature engineering.\n- Validating the quality of new model versions.\n- Validating serving infrastructure.\n- Testing integration between pipeline components.\n\nMany software engineers favor test-driven development (TDD). In TDD, software\nengineers write tests *prior* to writing the \"real\" source code.\nHowever, TDD can be tricky in machine learning.\nFor example, before training your model, you can't write a test to validate\nthe loss. Instead, you must first discover the achievable loss during model\ndevelopment and *then* test new model versions against the achievable loss.\n\nAbout the unicorn model\n-----------------------\n\nThis section refers to the **unicorn model**. Here's what you need to know:\n\u003e You are using machine learning to build a classification model that predicts\n\u003e unicorn appearances. Your dataset details 10,000 unicorn appearances and\n\u003e 10,000 unicorn non-appearances. The dataset contains the location,\n\u003e time of day, elevation, temperature, humidity, tree cover, presence of a\n\u003e rainbow, and several other features.\n\nTest model updates with reproducible training\n---------------------------------------------\n\nPerhaps you want to continue improving your unicorn model. For example, suppose\nyou do some additional feature engineering on a certain feature and then\nretrain the model, hoping to get better (or at least the same) results.\nUnfortunately, it is sometimes difficult to reproduce model training.\nTo improve reproducibility, follow these recommendations:\n\n- Deterministically seed the random number generator.\n For details, see [randomization in data\n generation](/machine-learning/crash-course/production-ml-systems/monitoring#randomization)\n\n- Initialize model components in a fixed order to ensure the components get the\n same random number from the random number generator on every run.\n ML libraries typically handle this requirement automatically.\n\n- Take the average of several runs of the model.\n\n- Use version control, even for preliminary iterations, so that you can\n pinpoint code and parameters when investigating your model or pipeline.\n\nEven after following these guidelines, other sources of nondeterminism might\nstill exist.\n\nTest calls to machine learning API\n----------------------------------\n\nHow do you test updates to API calls? You could retrain your model, but\nthat's time intensive. Instead, write a unit test to generate random input data\nand run a single step of gradient descent. If this step completes without\nerrors, then any updates to the API probably haven't ruined your model.\n\nWrite integration tests for pipeline components\n-----------------------------------------------\n\nIn an ML pipeline, changes in one component can cause errors in other\ncomponents. Check that components work together by writing an\n**integration test** that runs the entire pipeline end-to-end.\n\nBesides running integration tests continuously, you should run integration tests\nwhen pushing new models and new software versions. The slowness of running the\nentire pipeline makes continuous integration testing harder. To run integration\ntests faster, train on a subset of the data or with a simpler model. The details\ndepend on your model and data. To get continuous coverage, you'd adjust your\nfaster tests so that they run with every new version of model or software.\nMeanwhile, your slow tests would run continuously in the background.\n\nValidate model quality before serving\n-------------------------------------\n\nBefore pushing a new model version to production, test for\nthe following two types of quality degradations:\n\n- **Sudden degradation.** A bug in the new version could cause significantly\n lower quality. Validate new versions by checking their quality\n against the previous version.\n\n- **Slow degradation.** Your test for sudden degradation might not detect a slow\n degradation in model quality over multiple versions. Instead, ensure your\n model's predictions on a validation dataset meet a fixed threshold. If your\n validation dataset deviates from live data, then update your validation\n dataset and ensure your model still meets the same quality threshold.\n\nValidate model-infrastructure compatibility before serving\n----------------------------------------------------------\n\nIf your model is updated faster than your server, then your model could have\ndifferent software dependencies from your server, potentially causing\nincompatibilities. Ensure that the operations used by the model are present in\nthe server by staging the model in a sandboxed version of the server.\n| **Key terms:**\n|\n| - [Training-serving skew](/machine-learning/glossary#training-serving-skew)\n- [Z-score normalization](/machine-learning/glossary#z-score-normalization) \n[Help Center](https://support.google.com/machinelearningeducation)"]]