运行 Python 代码的一部分在两个不同的 GPU 上并行
Running a portion of Python code in parallel on two different GPUs
我有一个类似于以下内容的 PyTorch 脚本:
# Loading data
train_loader, test_loader = someDataLoaderFunction()
# Define the architecture
model = ResNet18()
model = model.cuda()
# Get method from program argument
method = args.method
# Training
train(method, model, train_loader, test_loader)
为了 运行 使用两种不同方法(method1
和 method2
)的脚本,在两个不同的终端中 运行 以下命令就足够了:
CUDA_VISIBLE_DEVICES=0 python program.py --method method1
CUDA_VISIBLE_DEVICES=1 python program.py --method method2
问题是,上面的数据加载器函数中包含了一些随机性,这意味着这两种方法应用于两组不同的训练数据。我希望他们训练完全相同的数据集,所以我修改了脚本如下:
# Loading data
train_loader, test_loader = someDataLoaderFunction()
# Define the architecture
model = ResNet18()
model = model.cuda()
## Run for the first method
method = 'method1'
# Training
train(method, model, train_loader, test_loader)
## Run for the second method
method = 'method2'
# Must re-initialize the network first
model = ResNet18()
model = model.cuda()
# Training
train(method, model, train_loader, test_loader)
是否可以让每个方法并行运行?
非常感谢您的帮助!
我想最简单的方法是如下修复种子。
myseed=args.seed
np.random.seed(myseed)
torch.manual_seed(myseed)
torch.cuda.manual_seed(myseed)
这应该会强制数据加载器每次都获取相同的样本。并行方式是使用多线程,但我几乎不认为它值得为您发布的问题带来麻烦。
我有一个类似于以下内容的 PyTorch 脚本:
# Loading data
train_loader, test_loader = someDataLoaderFunction()
# Define the architecture
model = ResNet18()
model = model.cuda()
# Get method from program argument
method = args.method
# Training
train(method, model, train_loader, test_loader)
为了 运行 使用两种不同方法(method1
和 method2
)的脚本,在两个不同的终端中 运行 以下命令就足够了:
CUDA_VISIBLE_DEVICES=0 python program.py --method method1
CUDA_VISIBLE_DEVICES=1 python program.py --method method2
问题是,上面的数据加载器函数中包含了一些随机性,这意味着这两种方法应用于两组不同的训练数据。我希望他们训练完全相同的数据集,所以我修改了脚本如下:
# Loading data
train_loader, test_loader = someDataLoaderFunction()
# Define the architecture
model = ResNet18()
model = model.cuda()
## Run for the first method
method = 'method1'
# Training
train(method, model, train_loader, test_loader)
## Run for the second method
method = 'method2'
# Must re-initialize the network first
model = ResNet18()
model = model.cuda()
# Training
train(method, model, train_loader, test_loader)
是否可以让每个方法并行运行? 非常感谢您的帮助!
我想最简单的方法是如下修复种子。
myseed=args.seed
np.random.seed(myseed)
torch.manual_seed(myseed)
torch.cuda.manual_seed(myseed)
这应该会强制数据加载器每次都获取相同的样本。并行方式是使用多线程,但我几乎不认为它值得为您发布的问题带来麻烦。