9951 explained code solutions for 126 technologies

# python-pytorchHow can I use Python PyTorch without CUDA?

You can use Python PyTorch without CUDA by setting the `device` to `cpu` when creating the Tensors. For example:

``````import torch

x = torch.rand(5, 3)
print(x.device)

device = torch.device("cpu")
x = torch.rand(5, 3, device=device)
print(x.device)``````

## Output example

``````cpu
cpu``````
1. `import torch` imports the PyTorch library.
2. `x = torch.rand(5, 3)` creates a tensor of size (5, 3) on the default device, which is usually the GPU.
3. `print(x.device)` prints the device on which the tensor is located.
4. `device = torch.device("cpu")` creates a CPU device object.
5. `x = torch.rand(5, 3, device=device)` creates a tensor of size (5, 3) on the CPU device.
6. `print(x.device)` prints the device on which the tensor is located.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7507912,"math_prob":0.8013169,"size":1497,"snap":"2023-40-2023-50","text_gpt3_token_len":420,"char_repetition_ratio":0.20696583,"word_repetition_ratio":0.2,"special_character_ratio":0.251169,"punctuation_ratio":0.16088328,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9902603,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-03T04:49:35Z\",\"WARC-Record-ID\":\"<urn:uuid:d33f8724-9488-418f-933d-bb29a82832e3>\",\"Content-Length\":\"7419\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5b161aaf-6bf3-4a25-8c3a-21f031196729>\",\"WARC-Concurrent-To\":\"<urn:uuid:2561615b-213a-4156-bcfa-3682c2bdb03d>\",\"WARC-IP-Address\":\"104.21.65.233\",\"WARC-Target-URI\":\"https://onelinerhub.com/python-pytorch/how-can-i-use-python-pytorch-without-cuda\",\"WARC-Payload-Digest\":\"sha1:BI6WYNY7IDHUBPRWHZRIYK4PJU2AE7J4\",\"WARC-Block-Digest\":\"sha1:NKMNILIO3GVXKCFOPVN2NXHAKEZZC25V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100484.76_warc_CC-MAIN-20231203030948-20231203060948-00481.warc.gz\"}"}