YouTip LogoYouTip

Pytorch Torch Get_Default_Device

```html PyTorch torch.get_default_device Function | Tutorial [Tutorial -- Learning not just technology, but dreams!]

PyTorch Tutorial

PyTorch Tutorial | PyTorch Introduction | PyTorch Installation | PyTorch Basics | PyTorch Tensors | PyTorch Neural Network Basics | PyTorch First Neural Network | PyTorch Data Processing and Loading | PyTorch Linear Regression | PyTorch Convolutional Neural Network | PyTorch Recurrent Neural Network | PyTorch Datasets | PyTorch Data Transforms | Pytorch torch | PyTorch torch.nn | Transformer Model | PyTorch Transformer | PyTorch torch.optim | PyTorch torchvision | PyTorch Model Deployment | PyTorch Model Save and Load | PyTorch Image Classification | PyTorch Text Sentiment Analysis | PyTorch Autograd | PyTorch GPU / CUDA Acceleration | PyTorch Loss Functions | PyTorch Learning Rate Scheduler | PyTorch Transfer Learning | PyTorch Batch Normalization | PyTorch LSTM / GRU | PyTorch Word Embedding | PyTorch GAN | PyTorch Autoencoder | PyTorch Model Evaluation and Debugging | PyTorch torchtext | PyTorch Mixed Precision Training | PyTorch TorchScript/ONNX Export | PyTorch Distributed Training | PyTorch Attention Mechanism

torch.get_default_device

The torch.get_default_device() function returns the default device currently set in PyTorch. This device is used as the default target for tensor operations when no explicit device is specified.

Syntax

torch.get_default_device()

Parameters

This function does not accept any parameters.

Return Value

Returns a torch.device object representing the current default device. If no default device has been set, it returns None.

Example

import torch

# Set default device to CUDA if available
if torch.cuda.is_available():
    torch.set_default_device('cuda')

# Get the current default device
default_device = torch.get_default_device()
print(default_device)  # Output: device(type='cuda') or None if not set

Notes

  • The default device can be set using torch.set_default_device().
  • If no default device is explicitly set, PyTorch does not assign a default device by default, and torch.get_default_device() will return None.
  • This function is useful for writing device-agnostic code that adapts to the environment's default device setting.

Related Functions

  • torch.set_default_device() β€” Sets the default device.
  • torch.device() β€” Constructs a device object.
```
← Pytorch Torch IgammaPytorch Torch Geqrf β†’