# Train model by using a specific GPU

**URL:** <https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923>\
**Category:** Uncategorized\
**Created:** [April 25, 2018, 6:30am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923 "2018-04-25T06:30:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![LJ\_Mason](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/2acd7d/32.png) [@LJ\_Mason](https://discuss.pytorch.org/u/LJ_Mason)\
**Post date:** [April 25, 2018, 6:30am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/1 "2018-04-25T06:30:34Z")

</div>

I have two GPUs, and GPU 0 is in using. So I want to train my model on GPU 1. However, I’ve tried to use

`os.environ["CUDA_VISIBLE_DEVICES"]="1"` in my script,

and

```auto
CUDA_VISIBLE_DEVICES=1

```

both not work.

---

<div class="post-metadata">

**Author:** ![LJ\_Mason](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/2acd7d/32.png) [@LJ\_Mason](https://discuss.pytorch.org/u/LJ_Mason)\
**Post date:** [April 25, 2018, 8:11am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/2 "2018-04-25T08:11:50Z")

</div>

The program just keeps running on GPU 0……  
anyone has solution???

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [April 25, 2018, 9:25am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/3 "2018-04-25T09:25:07Z")

</div>

Hi,

Setting the env variable within python won’t work, it needs to be set in the shell before you run your script.  
Also, [this](https://discuss.pytorch.org/t/gpu-devices-nvidia-smi-and-cuda-get-device-name-output-appear-inconsistent/13150/2) might help as well.

---

<div class="post-metadata">

**Author:** ![LJ\_Mason](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/2acd7d/32.png) [@LJ\_Mason](https://discuss.pytorch.org/u/LJ_Mason)\
**Post date:** [May 3, 2018, 6:12am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/4 "2018-05-03T06:12:04Z")

</div>

I’ve already tried `CUDA_VISIBLE_DEVICES=1 python main.py`  
not work.

---

<div class="post-metadata">

**Author:** ![thadar](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@thadar](https://discuss.pytorch.org/u/thadar)\
**Post date:** [May 3, 2018, 6:22am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/5 "2018-05-03T06:22:06Z")

</div>

Hey,  
I’m not sure if this will be helpful or not but if you use pytorch 0.3.1 you can direct your model to run on a specific gpu by using `model.cuda(_GPU_ID) #_GPU_ID should be 0, 1, 2 etc`

if you are using pytorch 0.4 you can direct your model to run on a specific gpu by using

```auto
device = torch.device("cuda:1")
model.to(device)

```

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [May 3, 2018, 12:35pm UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/6 "2018-05-03T12:35:07Z")

</div>

Does `CUDA_VISIBLE_DEVICES=0 python main.py` with a 0 makes it run on GPU 1?

---

<div class="post-metadata">

**Author:** ![justusschock](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/justusschock/32/4637_2.png) [@justusschock](https://discuss.pytorch.org/u/justusschock)\
**Post date:** [May 4, 2018, 7:56am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/7 "2018-05-04T07:56:32Z")

</div>

That’s not completely true. It works setting the variable inside the python script. But it has to be set before the first import of pytorch or other modules using pytorch (and other kinds of GPU-processing as in other DL\_libraries like keras or tensorflow).

At least this is what I experienced on a GPU-Cluster running Linux. On Windows however this seems to work only in some cases

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [May 4, 2018, 8:55am UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/8 "2018-05-04T08:55:58Z")

</div>

Yes it is possible to set it within python but it has to be visible and be done before the cuda init.  
The thing is that it can be tricky for a user to know when the cuda init is going to happen so for my scripts, I assume that it does not work and set it outside, that way I’m sure it always works, even if an obscure dependency changed and now initialize cuda before I set the variable.

---

<div class="post-metadata">

**Author:** ![LMA](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@LMA](https://discuss.pytorch.org/u/LMA)\
**Post date:** [March 1, 2019, 5:33pm UTC](https://discuss.pytorch.org/t/train-model-by-using-a-specific-gpu/16923/9 "2019-03-01T17:33:54Z")

</div>

I ran into the same problem as LJ\_Mason. Any update (preferably with an example) on how to choose a specific GPU for running a particular python/pytorch script in Jupyter Notebook? Thanks.
