# PyTorch torch.tril_indices
The `torch.tril_indices` function in PyTorch is used to generate the indices of the lower triangular part of a 2D matrix (including the diagonal by default). It returns a 2-tuple of tensors containing the row and column indices of these elements.
This function is highly useful for masking operations, extracting lower triangular matrices, or performing operations on symmetric/triangular matrices without allocating memory for unnecessary elements.
---
## Syntax
```python
torch.tril_indices(row, column, offset=0, dtype=torch.long, device='cpu', layout=torch.strided)
```
### Parameter Descriptions
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `row` | `int` | The number of rows in the 2D matrix. |
| `column` | `int` | The number of columns in the 2D matrix. |
| `offset` | `int` (optional) | The diagonal offset. Default is `0` (main diagonal).
β’ `offset = 0`: Includes the main diagonal and below.
β’ `offset > 0`: Includes diagonals above the main diagonal.
β’ `offset < 0`: Excludes the main diagonal and elements below it up to the offset. |
| `dtype` | `torch.dtype` (optional) | The desired data type of returned tensors. Default is `torch.long`. |
| `device` | `torch.device` (optional) | The desired device of returned tensors. Default is `'cpu'`. |
| `layout` | `torch.layout` (optional) | The desired layout of returned tensors. Default is `torch.strided`. |
### Return Value
* Returns a 2-tuple of tensors of shape `(2, N)`, where `N` is the number of elements in the lower triangular part.
* The first tensor contains the **row indices**, and the second tensor contains the **column indices**.
---
## Code Examples
### Example 1: Basic Usage (Square Matrix)
Generate the lower triangular indices for a $3 \times 3$ matrix.
```python
import torch
# Generate lower triangular indices for a 3x3 matrix
row, col = torch.tril_indices(3, 3)
print("row indices:", row)
print("col indices:", col)
```
**Output:**
```text
row indices: tensor([0, 1, 1, 2, 2, 2])
col indices: tensor([0, 0, 1, 0, 1, 2])
```
---
### Example 2: Using Indices for Masking and Assignment
You can use the generated indices to directly index into a tensor and modify its values.
```python
import torch
# Generate indices with an offset of 1 (includes one diagonal above the main diagonal)
row, col = torch.tril_indices(3, 3, offset=1)
# Create a 3x3 matrix of ones
a = torch.ones(3, 3)
# Set the elements outside the specified lower triangular area to 0
# Note: To zero out the upper triangular part, we invert the selection logic
mask = torch.ones(3, 3)
mask[row, col] = 0
print("Mask matrix:")
print(mask)
```
**Output:**
```text
Mask matrix:
tensor([[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
```
---
### Example 3: Non-Square Matrix
`torch.tril_indices` also works seamlessly with non-square matrices.
```python
import torch
# Generate lower triangular indices for a 3x4 rectangular matrix
row, col = torch.tril_indices(3, 4)
print("row indices:", row)
print("col indices:", col)
```
**Output:**
```text
row indices: tensor([0, 1, 1, 2, 2, 2])
col indices: tensor([0, 0, 1, 0, 1, 2])
```
---
### Example 4: Using Negative Offset
Using a negative offset excludes the main diagonal and elements immediately below it.
```python
import torch
# Generate lower triangular indices with offset = -1 (excludes the main diagonal)
row, col = torch.tril_indices(3, 3, offset=-1)
print("row indices:", row)
print("col indices:", col)
```
**Output:**
```text
row indices: tensor([1, 2, 2])
col indices: tensor([0, 0, 1])
```
---
## Considerations
1. **Memory Efficiency**: `torch.tril_indices` returns only the coordinate indices rather than a full dense mask matrix. This is highly memory-efficient when working with large sparse matrices.
2. **Device Allocation**: If you are performing operations on a GPU, make sure to set `device='cuda'` inside `torch.tril_indices` to avoid unnecessary CPU-to-GPU data transfer overhead.
3. **Relationship with `torch.tril`**: While `torch.tril` zeroes out elements of an existing tensor, `torch.tril_indices` provides the raw coordinate indices, which is useful for advanced indexing, sparse tensor creation, and custom loss functions (e.g., calculating pairwise distances without duplicates).
π Categories
- β‘ JavaScript (1589)
- π PHP (872)
- π Python3 (810)
- π HTML (691)
- βοΈ C# (650)
- π Python (594)
- β Java (552)
- βοΈ PyTorch (534)
- π§ Linux (472)
- βοΈ C (432)
- π¦ jQuery (406)
- π¨ CSS (377)
- π XML (259)
- π¦ jQuery UI (231)
- π― Bootstrap (220)
- βοΈ C++ (215)
- π °οΈ Angular (205)
- π HTML DOM (201)
- π΄ Redis (188)
- π Web Building (142)
- π Vue.js (141)
- π R (131)
- πΌ Pandas (124)
- ποΈ SQL (105)
- βοΈ Docker (86)
- βοΈ TypeScript (73)
- βοΈ Highcharts (70)
- π AI Agent (70)
- βοΈ React (68)
- π Node.js (65)
- βοΈ Machine Learning (60)
- π Git (59)
- π΅ Go (58)
- π Markdown (58)
- π’ NumPy (55)
- π§ͺ Flask (54)
- βοΈ Scala (53)
- ποΈ SQLite (52)
- π JSTL (52)
- βοΈ VS Code (51)
- π MongoDB (49)
- π Perl (48)
- π Ruby (47)
- π Matplotlib (47)
- βοΈ Uncategorized (46)
- π Swift (46)
- ποΈ PostgreSQL (46)
- βοΈ Data Structures (46)
- π Playwright (46)
- π iOS (45)
- ποΈ MySQL (44)
- βοΈ LangChain (43)
- π FastAPI (40)
- βοΈ Ionic (38)
- π Design Patterns (37)
- βοΈ Eclipse (37)
- π¨ CSS3 (34)
- π Lua (34)
- βοΈ Codex (34)
- πΈ Django (32)
- βοΈ OpenCV (32)
- π Rust (31)
- π JSP (31)
- βοΈ Claude Code (31)
- π Pillow (30)
- βοΈ OpenCode (28)
- π AI Skills (27)
- π Flutter (26)
- π Maven (26)
- π¨ Tailwind CSS (25)
- π§ TensorFlow (25)
- π Servlet (24)
- π Dart (23)
- π Assembly (23)
- βοΈ Memcached (22)
- βοΈ SVG (22)
- βοΈ Electron (22)
- π NLP (22)
- π Regex (21)
- π Android (20)
- π£ Kotlin (19)
- π Julia (19)
- π SOAP (17)
- π Selenium (17)
- π PowerShell (17)
- π Sass (16)
- π HTTP (16)
- π Zig (15)
- π AI (15)
- π AJAX (14)
- π Swagger (14)
- βοΈ Scikit-learn (13)
- βοΈ ECharts (13)
- βοΈ Chart.js (13)
- βοΈ Cursor (13)
- βοΈ SciPy (12)
- π RDF (12)
- π Ollama (12)
- π Next.js (12)
- π Plotly Dash (12)
- π JSON (11)
- π RESTful API (11)
- π WSDL (9)
- βοΈ CMake (8)
- π Firebug (7)
- π Nginx (6)
- βΈοΈ Kubernetes (6)
- π Jupyter (6)
- π LaTeX (4)
- π UniApp (4)
- ποΈ SQL Server (1)
YouTip