# How to create a tensor on GPU as default

**URL:** <https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128>\
**Category:** Uncategorized\
**Created:** [April 22, 2017, 5:46am UTC](https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128 "2017-04-22T05:46:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![b64406620](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/ecb155/32.png) [@b64406620](https://discuss.pytorch.org/u/b64406620)\
**Post date:** [April 22, 2017, 5:46am UTC](https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128/1 "2017-04-22T05:46:04Z")

</div>

Generally, we create a tensor by following code:  
t = torch.ones(4)  
t is a tensor on cpu, How can I create it on GPU as default??  
In other words , I want to create my tensors all on GPU as default.

---

<div class="post-metadata">

**Author:** ![chenyuntc](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chenyuntc/32/299_2.png) [@chenyuntc](https://discuss.pytorch.org/u/chenyuntc)\
**Post date:** [April 22, 2017, 11:13am UTC](https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128/2 "2017-04-22T11:13:49Z")

</div>

```auto
torch.set_default_tensor_type('torch.cuda.FloatTensor')

```

---

<div class="post-metadata">

**Author:** ![mderakhshani](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mderakhshani/32/1735_2.png) [@mderakhshani](https://discuss.pytorch.org/u/mderakhshani)\
**Post date:** [August 25, 2017, 6:51pm UTC](https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128/3 "2017-08-25T18:51:45Z")

</div>

Hi, here is my code:

> import torch  
> torch.set\_default\_tensor\_type(‘torch.cuda.FloatTensor’)  
> t = torch.rand(1,3,24,24)

and it caused bellow error:

> TypeError Traceback (most recent call last)  
> in ()  
> ----\> 1 t = torch.rand(1,3,24,24)
> 
> TypeError: Type torch.cuda.FloatTensor doesn’t implement stateless methods

Any idea for this type of error?

---

<div class="post-metadata">

**Author:** ![chenyuntc](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chenyuntc/32/299_2.png) [@chenyuntc](https://discuss.pytorch.org/u/chenyuntc)\
**Post date:** [August 26, 2017, 10:20am UTC](https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128/4 "2017-08-26T10:20:40Z")

</div>

`t = torch.Tensor(1,3,24,24).uniform_()`

---

<div class="post-metadata">

**Author:** ![QuantScientist](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/quantscientist/32/1393_2.png) [@QuantScientist](https://discuss.pytorch.org/u/QuantScientist)\
**Post date:** [August 26, 2017, 10:34am UTC](https://discuss.pytorch.org/t/how-to-create-a-tensor-on-gpu-as-default/2128/5 "2017-08-26T10:34:47Z")

</div>

```python
use_cuda = torch.cuda.is_available()
FloatTensor = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if use_cuda else torch.LongTensor
Tensor = FloatTensor

if use_cuda:
    lgr.info ("Using the GPU")    
    X = Variable(torch.from_numpy(x_data_np).cuda()) # Note the conversion for pytorch
    Y = Variable(torch.from_numpy(y_data_np).cuda())
else:
    lgr.info ("Using the CPU")
    X = Variable(torch.from_numpy(x_data_np)) # Note the conversion for pytorch
    Y = Variable(torch.from_numpy(y_data_np))

```
