简体   繁体   English

在 cpu 上并行运行多个神经网络

[英]Running multiple neural networks in parallel on cpu

I am trying to run multiple instances of same neural network training in pytorch but with different hyperparameters.我正在尝试在 pytorch 中运行相同神经网络训练的多个实例,但使用不同的超参数。 For example different learning rate, optimizer etc. I tried to create a multiprocessing.Pool but I am getting an error:例如不同的学习率、优化器等。我试图创建一个 multiprocessing.Pool 但我收到一个错误:

Runtime error: Unable to handle autograd's threading in combination with fork-based multiprocessing. See https://github.com/pytorch/pytorch/wiki/Autograd-and-Fork

Here is the sample code to give an idea of what I am doing:这是示例代码,可让您了解我在做什么:

def run_one_instance(idx=0):
    lr = choose_random([0.001, 0.01, 0.1, 1])
    optimizer = choose_random(["SGD", "Adam"])
    num_hidden_layers = choose_random([2,3,4,5])
    model = create_model(num_hidden_layers, lr, optimizer)
    model.train()
    return model.evaluate()

def run_multiple_instances(num_instances=40):
    pool_obj = multiprocessing.Pool()
    result = pool_obj.map(run_one_instance, range(0, num_instances))
    return result

So is there something I am doing wrong?那么我做错了什么吗? Is there any other way to run multiple neural networks in parallel (on cpu or gpu)?有没有其他方法可以并行运行多个神经网络(在 cpu 或 gpu 上)?

As the link in the error message explains , use Python's multiprocessing's spawn context instead of the fork context.正如错误消息中的链接所解释的那样,使用 Python 的多处理的spawn上下文而不是fork上下文。 Like so:像这样:

import multiprocessing as mp


ctx = mp.get_context('spawn')


def run_one_instance(idx=0):
    lr = choose_random([0.001, 0.01, 0.1, 1])
    optimizer = choose_random(["SGD", "Adam"])
    num_hidden_layers = choose_random([2,3,4,5])
    model = create_model(num_hidden_layers, lr, optimizer)
    model.train()
    return model.evaluate()

def run_multiple_instances(num_instances=40):
    pool_obj = ctx.Pool()
    result = pool_obj.map(run_one_instance, range(0, num_instances))
    return result

The ctx object has the same API as the multiprocessing module. ctx object 与多处理模块具有相同的 API。 See the Python multiprocessing docs for more info about multiprocessing contexts (eg spawn and fork ).有关多处理上下文(例如spawnfork )的更多信息,请参阅Python 多处理文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM