From 535d55f6d785a11ff67f00ba9c0beb1e753733f3 Mon Sep 17 00:00:00 2001 From: futurebigman <89tomoyatomoya@gmail.com> Date: Mon, 19 May 2025 13:12:11 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=A1=E3=83=A2=E3=83=AA=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E6=A4=9C=E8=A8=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day5/requirements.txt | 3 ++- .../tests/test_model.py" | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/day5/requirements.txt b/day5/requirements.txt index d37edb49b..b3c12277c 100644 --- a/day5/requirements.txt +++ b/day5/requirements.txt @@ -4,4 +4,5 @@ mlflow pandas pytest great_expectations -black \ No newline at end of file +black +psutil \ No newline at end of file diff --git "a/day5/\346\274\224\347\277\2223/tests/test_model.py" "b/day5/\346\274\224\347\277\2223/tests/test_model.py" index e11a19a5c..48f4ea819 100644 --- "a/day5/\346\274\224\347\277\2223/tests/test_model.py" +++ "b/day5/\346\274\224\347\277\2223/tests/test_model.py" @@ -11,6 +11,7 @@ from sklearn.preprocessing import OneHotEncoder, StandardScaler from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline +import psutil # テスト用データとモデルパスを定義 DATA_PATH = os.path.join(os.path.dirname(__file__), "../data/Titanic.csv") @@ -157,7 +158,7 @@ def test_model_reproducibility(sample_data, preprocessor): steps=[ ("preprocessor", preprocessor), ("classifier", RandomForestClassifier(n_estimators=100, random_state=42)), - ] + ] ) # 学習 @@ -171,3 +172,19 @@ def test_model_reproducibility(sample_data, preprocessor): assert np.array_equal( predictions1, predictions2 ), "モデルの予測結果に再現性がありません" + +def memory_usage_amount(train_model): + model, X_test, y_test = train_model + # プロセス取得 + process = psutil.Process(os.getpid()) + + # predict前のメモリ + mem_before = process.memory_info().rss / 1024 ** 2 # MB単位 + + # 実行 + model.predict(X_test) + + # predict後のメモリ + mem_after = process.memory_info().rss / 1024 ** 2 + + assert mem_after - mem_before < 10, "推論にメモリを使いすぎです" From 0e746861c18ead5005d6a1da3d5caf2fd7d3beb3 Mon Sep 17 00:00:00 2001 From: futurebigman <89tomoyatomoya@gmail.com> Date: Mon, 19 May 2025 13:33:24 +0900 Subject: [PATCH 2/2] black modified --- .../tests/test_model.py" | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git "a/day5/\346\274\224\347\277\2223/tests/test_model.py" "b/day5/\346\274\224\347\277\2223/tests/test_model.py" index 48f4ea819..1676f29b7 100644 --- "a/day5/\346\274\224\347\277\2223/tests/test_model.py" +++ "b/day5/\346\274\224\347\277\2223/tests/test_model.py" @@ -158,7 +158,7 @@ def test_model_reproducibility(sample_data, preprocessor): steps=[ ("preprocessor", preprocessor), ("classifier", RandomForestClassifier(n_estimators=100, random_state=42)), - ] + ] ) # 学習 @@ -172,19 +172,23 @@ def test_model_reproducibility(sample_data, preprocessor): assert np.array_equal( predictions1, predictions2 ), "モデルの予測結果に再現性がありません" - + + def memory_usage_amount(train_model): + """メモリ使用量を検証""" model, X_test, y_test = train_model # プロセス取得 process = psutil.Process(os.getpid()) # predict前のメモリ - mem_before = process.memory_info().rss / 1024 ** 2 # MB単位 + mem_before = process.memory_info().rss / 1024**2 # MB単位 # 実行 model.predict(X_test) # predict後のメモリ - mem_after = process.memory_info().rss / 1024 ** 2 - - assert mem_after - mem_before < 10, "推論にメモリを使いすぎです" + mem_after = process.memory_info().rss / 1024**2 + + assert ( + mem_after - mem_before < 10 + ), f"推論にメモリを使いすぎです. {mem_after - mem_before}MB"