2017-09-27 13 views
1

に並列にPythonコードの部分を実行する2つの異なる方法(method1method2)でスクリプトを実行するために私は次のようなPyTorchスクリプト有する二つの異なるGPUを

# 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) 

を、それが十分で二つの異なる端末で次のコマンドを実行する:問題がある

CUDA_VISIBLE_DEVICES=0 python program.py --method method1 
CUDA_VISIBLE_DEVICES=1 python program.py --method method2 

、上記データローダ関数は、2つの方法が2つに適用されたことを意味し、その中のいくつかのランダムを含有します異なるトレーニングデータセット。私は、データの正確な同じセットを訓練するためにそれらをしたいと思いますので、以下のように、私は、スクリプトを修正:

# 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) 

それはそれぞれの方法について並列に実行させることは可能ですか? あらかじめご協力いただきありがとうございます!

+0

Umm、並列コンピューティングは、まったく異なるコーディングアーキテクチャを必要とします。私ができることは、Python 3の 'queue' inbuilt libを指し示すことです。それを使ってパラレル実行のオーケストレーションを行う必要があります。また、競合状態とスレッドロックについて読んでください。そうしないと、コードフラストレーションが発生する可能性があります。 – aim100k

+0

@ aim100kありがとう。私はC++やMatlabのループのようないくつかの基本的なものしか出していませんでした。( – Khue

+0

あなたのウェブサイトを見て、あなたがしていることが本当に素晴らしいと思います。あなたはここで答えを見つけます – aim100k

答えて

1

私は、以下のようにシードを修正するのが最も簡単な方法だと思います。

myseed=args.seed 
np.random.seed(myseed) 
torch.manual_seed(myseed) 
torch.cuda.manual_seed(myseed) 

これにより、データローダーは毎回同じサンプルを取得する必要があります。並行して、マルチスレッドを使用する方法がありますが、投稿した問題の面倒を見る価値はほとんどありません。

+1

ありがとう、私はここで同じ答えを持っています:https://discuss.pytorch.org/t/how-to-run-two-training-methods-in-parallel-on-exactly-the-same-data/7796/ 2 – Khue

関連する問題