我正在训练一些神经网络,并且我有一个脚本来搜索超参数。
"""Peform hyperparemeters search"""
import argparse
import os
from subprocess import check_call
import sys
import numpy as np
import utils
PYTHON = sys.executable
parser = argparse.ArgumentParser()
parser.add_argument('--parent_dir', default='experiments/learning_rate',
help='Directory containing params.json')
parser.add_argument('--data_dir', default='data', help="Directory containing the dataset")
def launch_training_job(parent_dir, data_dir, job_name, params):
"""Launch training of the model with a set of hyperparameters in parent_dir/job_name
Args:
model_dir: (string) directory containing config, weights and log
data_dir: (string) directory containing the dataset
params: (dict) containing hyperparameters
"""
# Create a new folder in parent_dir with unique_name "job_name"
model_dir = os.path.join(parent_dir, job_name)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# Write parameters in json file
json_path = os.path.join(model_dir, 'params.json')
params.save(json_path)
# Launch training with this config
cmd = "{python} -W ignore train.py --model_dir={model_dir} --data_dir {data_dir}".format(python=PYTHON,
model_dir=model_dir,
data_dir=data_dir)
print(cmd)
check_call(cmd, shell=True)
if __name__ == "__main__":
# Load the "reference" parameters from parent_dir json file
args = parser.parse_args()
json_path = os.path.join(args.parent_dir, 'params.json')
assert os.path.isfile(json_path), "No json configuration file found at {}".format(json_path)
params = utils.Params(json_path)
# Perform hypersearch over one parameter
learning_rates = [1e-5]
for learning_rate in learning_rates:
# Modify the relevant parameter in params
params.learning_rate = learning_rate
# Launch job (name has to be unique)
job_name = "learning_rate_{}".format(learning_rate)
launch_training_job(args.parent_dir, args.data_dir, job_name, params)
如果我尝试单独调用train.py,它不会耗尽内存-它仅使用GPU可用的12000MB中的大约2500MB。
但是,如果我调用此脚本来搜索超参数,即使我使用单个子进程调用它,也将耗尽内存,特别是仅测试一个学习率。
这是错误消息:RuntimeError: CUDA error: out of memory
。
请注意,对于我拥有的另一个较小的类似模型,不会发生这种情况。
为什么会这样?我该如何解决呢?
答案 0 :(得分:0)
我的评论没有足够的声誉,所以我会写一个答案。如果您使用的是Unix系统,则应尝试查看此链接: Python memory allocation error using subprocess.Popen 看来Unix的子进程有问题... 希望这会有所帮助。