python - 如何在使用 GBM 算法期间估计 ram 使用情况
问题描述
我想估计我的算法 XGBOOST 和 LightGBM 如何使用 python 消耗内存
我的意思是这样的:
我用这段代码来估计速度,我问是否有类似的记忆方法:
start_time = time.time()
xg_cl.fit(X_train, y_train, eval_metric="auc", early_stopping_rounds=300, eval_set=eval_set, verbose=True)
XgBoost_time_training = (time.time() - start_time)
print("--- %s seconds ---" % (time.time() - start_time))
PS:我需要一个可以在 WINDOWS 而不是 linux 中执行的代码
解决方案
你可以使用memory_profiler.profile
,这里是一个例子
import numpy as np
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
from memory_profiler import profile
precision = 10
fp = open('memory.log', 'w+')
@profile(precision=precision, stream=fp)
def xgb_test():
X1 = np.linspace(0, np.radians(360), 1000).reshape(-1, 1)
X2 = np.linspace(0, np.radians(360), 1000).reshape(-1, 1)
Y = np.sin(X1) + 0.0001 * X2
X = np.concatenate((X1, X2), axis = 1)
x_train, x_val, y_train, y_val = train_test_split(X, Y, test_size = 0.2, random_state = 69)
eval_set = [(x_train, y_train), (x_val, y_val)]
XGBModel = XGBRegressor(max_depth = 10)
XGBModel.fit(x_train,y_train,eval_set=eval_set,early_stopping_rounds=100,eval_metric="mae",verbose=2)
if __name__ == '__main__':
xgb_test()
通过运行脚本,每行(步骤)的内存消耗将被写入memory.log
文件,如下所示:
Filename: test.py
Line # Mem usage Increment Line Contents
================================================
12 104.6992187500 MiB 104.6992187500 MiB @profile(precision=precision, stream=fp)
13 def xgb_test():
14
15 104.6992187500 MiB 0.0000000000 MiB X1 = np.linspace(0, np.radians(360), 1000).reshape(-1, 1)
16 104.6992187500 MiB 0.0000000000 MiB X2 = np.linspace(0, np.radians(360), 1000).reshape(-1, 1)
17 104.7226562500 MiB 0.0234375000 MiB Y = np.sin(X1) + 0.0001 * X2
18 104.7382812500 MiB 0.0156250000 MiB X = np.concatenate((X1, X2), axis = 1)
19
20 104.8320312500 MiB 0.0937500000 MiB x_train, x_val, y_train, y_val = train_test_split(X, Y, test_size = 0.2, random_state = 69)
21 104.8320312500 MiB 0.0000000000 MiB eval_set = [(x_train, y_train), (x_val, y_val)]
22 104.8320312500 MiB 0.0000000000 MiB XGBModel = XGBRegressor(max_depth = 10)
23 105.6718750000 MiB 0.8398437500 MiB XGBModel.fit(x_train,y_train,eval_set=eval_set,early_stopping_rounds=100,eval_metric="mae",verbose=2)
其中Increment
列显示每个步骤占用的内存量。
推荐阅读
- spring - 在不同时区使用 Quartz 调度程序运行应用程序
- java - bash脚本中keytool命令中的匹配模式
- javascript - 如何将参数添加到 pm2 文件执行
- javascript - Webos/Tizen Firebase 电话验证
- redirect - 没有主机名的IIS中的url重写
- r - How can I define if statements depending on the type of the separation using strsplit in R?
- node.js - Error while Loading image from form into MongoDB using Node/Expressjs
- octave - T(1.2):下标必须是整数 1 到 (2^63)-1 或逻辑
- c# - I'm having problems with async
- dolphindb - DolphinDB 中的哪个函数类似于 lag 操作?