Rookie Tutorial -- Learning not only technology, but also dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
PyTorch Tutorial
(https://example.com/pytorch/pytorch-tutorial.html "PyTorch Tutorial")
(https://example.com/pytorch/pytorch-intro.html "PyTorch Introduction")
(https://example.com/pytorch/pytorch-install.html "PyTorch Installation")
(https://example.com/pytorch/pytorch-basic.html "PyTorch Basics")
(https://example.com/pytorch/pytorch-tensor.html "PyTorch Tensor")
(https://example.com/pytorch/pytorch-neural-network.html "PyTorch Neural Network Basics")
(https://example.com/pytorch/pytorch-first-neural-network.html "PyTorch First Neural Network")
(https://example.com/pytorch/pytorch-dataset-dataloader.html "PyTorch Data Processing and Loading")
(https://example.com/pytorch/pytorch-linear-regression.html "PyTorch Linear Regression")
(https://example.com/pytorch/pytorch-cnn.html "PyTorch Convolutional Neural Network")
(https://example.com/pytorch/pytorch-recurrent-neural-network.html "PyTorch Recurrent Neural Network")
(https://example.com/pytorch/pytorch-datasets.html "PyTorch Datasets")
(https://example.com/pytorch/pytorch-transforms.html "PyTorch Data Transforms")
(https://example.com/pytorch/pytorch-torch-ref.html "PyTorch torch Reference Manual")
[PyTorch torch.nn Reference Manual](https://example.com/pytorch/pytorch-torch-nn-ref.html "PyTorch torch.nn Reference Manual")
(https://example.com/pytorch/transformer-model.html "Transformer Model")
(https://example.com/pytorch/pytorch-transformer-model.html "PyTorch Building Transformer Model")
[PyTorch torch.optim Optimizer Module](https://example.com/pytorch/pytorch-torch-optim.html "PyTorch torch.optim Optimizer Module")
(https://example.com/pytorch/pytorch-torchvision.html "PyTorch torchvision Computer Vision Module")
(https://example.com/pytorch/pytorch-model-deployment.html "PyTorch Model Deployment")
(https://example.com/pytorch/pytorch-model-save.html "PyTorch Model Saving and Loading")
(https://example.com/pytorch/pytorch-image-classification.html "PyTorch Example - Image Classification Project")
(https://example.com/pytorch/pytorch-text-classification.html "PyTorch Example - Text Sentiment Analysis Project")
(https://example.com/pytorch/pytorch-autograd.html "PyTorch Autograd Automatic Differentiation")
[PyTorch GPU / CUDA Acceleration](https://example.com/pytorch/pytorch-gpu-cuda.html "PyTorch GPU / CUDA Acceleration")
(https://example.com/pytorch/pytorch-loss-function.html "PyTorch Loss Function")
(https://example.com/pytorch/pytorch-lr-scheduler.html "PyTorch Learning Rate Scheduler")
(https://example.com/pytorch/pytorch-transfer-learning.html "PyTorch Transfer Learning")
(https://example.com/pytorch/pytorch-batchnorm-dropout.html "PyTorch Batch Normalization and Dropout")
[PyTorch LSTM / GRU](https://example.com/pytorch/pytorch-lstm-gru.html "PyTorch LSTM / GRU")
(https://example.com/pytorch/pytorch-embedding.html "PyTorch Word Embedding (Embedding)")
(https://example.com/pytorch/pytorch-gan.html "PyTorch Generative Adversarial Network (GAN)")
(https://example.com/pytorch/pytorch-autoencoder.html "PyTorch Autoencoder (Autoencoder)")
(https://example.com/pytorch/pytorch-evaluation-debugging.html "PyTorch Model Evaluation and Debugging")
(https://example.com/pytorch/pytorch-torchtext.html "PyTorch torchtext")
(https://example.com/pytorch/pytorch-amp.html "PyTorch Mixed Precision Training (AMP)")
[PyTorch TorchScript/ONNX Export](https://example.com/pytorch/pytorch-torchscript-onnx-export.html "PyTorch TorchScript/ONNX Export")
(https://example.com/pytorch/pytorch-distributed.html "PyTorch Distributed Training")
(https://example.com/pytorch/pytorch-attention.html "PyTorch Attention Mechanism")
PyTorch torch.diagonal Function
This tutorial explains the usage of the torch.diagonal function in PyTorch. The torch.diagonal function is used to extract the diagonal elements from a tensor.
Syntax
torch.diagonal(input, offset=0, dim1=0, dim2=1)
Parameters
- input: The input tensor from which the diagonal is to be extracted.
- offset: The offset of the diagonal. Default is 0, which means the main diagonal. A positive value moves the diagonal up, and a negative value moves it down.
- dim1: The first dimension of the diagonal. Default is 0.
- dim2: The second dimension of the diagonal. Default is 1.
Example
import torch
# Create a 2D tensor
tensor = torch.tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Extract the main diagonal
diagonal = torch.diagonal(tensor)
print(diagonal) # Output: tensor([1, 5, 9])
# Extract the diagonal above the main diagonal
diagonal_above = torch.diagonal(tensor, offset=1)
print(diagonal_above) # Output: tensor([2, 6])
# Extract the diagonal below the main diagonal
diagonal_below = torch.diagonal(tensor, offset=-1)
print(diagonal_below) # Output: tensor([4, 8])
The torch.diagonal function is useful for extracting specific diagonals from tensors, which can be particularly helpful in various linear algebra operations and neural network architectures.
YouTip