0
# Tensor Operations
1
2
Core tensor creation, manipulation, and mathematical operations that form the foundation of PyTorch's computational capabilities. These operations support automatic differentiation and GPU acceleration.
3
4
## Capabilities
5
6
### Tensor Creation
7
8
Create tensors from data, with specific values, or using random initialization patterns.
9
10
```python { .api }
11
def tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) -> Tensor:
12
"""
13
Construct a tensor with data.
14
15
Parameters:
16
- data: Initial data (list, tuple, ndarray, scalar, tensor)
17
- dtype: Data type (torch.float32, torch.int64, etc.)
18
- device: Device placement (torch.device or string)
19
- requires_grad: Enable automatic differentiation
20
- pin_memory: Use pinned memory for faster GPU transfer
21
22
Returns:
23
Tensor with specified data and properties
24
"""
25
26
def zeros(*size, dtype=None, device=None, requires_grad=False) -> Tensor:
27
"""Create tensor filled with zeros."""
28
29
def ones(*size, dtype=None, device=None, requires_grad=False) -> Tensor:
30
"""Create tensor filled with ones."""
31
32
def empty(*size, dtype=None, device=None, requires_grad=False) -> Tensor:
33
"""Create uninitialized tensor."""
34
35
def full(size, fill_value, *, dtype=None, device=None, requires_grad=False) -> Tensor:
36
"""Create tensor filled with specific value."""
37
38
def eye(n, m=None, *, dtype=None, device=None, requires_grad=False) -> Tensor:
39
"""Create identity matrix."""
40
```
41
42
### Random Tensor Creation
43
44
Generate tensors with random values from various distributions.
45
46
```python { .api }
47
def rand(*size, dtype=None, device=None, requires_grad=False) -> Tensor:
48
"""Random values from uniform distribution [0, 1)."""
49
50
def randn(*size, dtype=None, device=None, requires_grad=False) -> Tensor:
51
"""Random values from standard normal distribution."""
52
53
def randint(low=0, high, size, *, dtype=None, device=None, requires_grad=False) -> Tensor:
54
"""Random integers from [low, high)."""
55
56
def randperm(n, *, dtype=torch.int64, device=None, requires_grad=False) -> Tensor:
57
"""Random permutation of integers 0 to n-1."""
58
59
def multinomial(input, num_samples, replacement=False, *, generator=None) -> Tensor:
60
"""Sample from multinomial distribution."""
61
```
62
63
### Range and Sequence Creation
64
65
Create tensors with sequential or linearly spaced values.
66
67
```python { .api }
68
def arange(start=0, end, step=1, *, dtype=None, device=None, requires_grad=False) -> Tensor:
69
"""Values from start to end with step."""
70
71
def linspace(start, end, steps, *, dtype=None, device=None, requires_grad=False) -> Tensor:
72
"""Linearly spaced values from start to end."""
73
74
def logspace(start, end, steps, base=10.0, *, dtype=None, device=None, requires_grad=False) -> Tensor:
75
"""Logarithmically spaced values."""
76
```
77
78
### Tensor Conversion and Creation from Existing Data
79
80
Convert between PyTorch tensors and other data structures.
81
82
```python { .api }
83
def from_numpy(ndarray) -> Tensor:
84
"""Create tensor from NumPy array (shares memory)."""
85
86
def as_tensor(data, dtype=None, device=None) -> Tensor:
87
"""Convert data to tensor, avoiding copy if possible."""
88
89
def stack(tensors, dim=0) -> Tensor:
90
"""Stack tensors along new dimension."""
91
92
def cat(tensors, dim=0) -> Tensor:
93
"""Concatenate tensors along existing dimension."""
94
95
def hstack(tensors) -> Tensor:
96
"""Stack tensors horizontally (column-wise)."""
97
98
def vstack(tensors) -> Tensor:
99
"""Stack tensors vertically (row-wise)."""
100
101
def dstack(tensors) -> Tensor:
102
"""Stack tensors depth-wise (along third dimension)."""
103
```
104
105
### Shape Manipulation
106
107
Reshape, transpose, and manipulate tensor dimensions.
108
109
```python { .api }
110
def reshape(input, shape) -> Tensor:
111
"""Return tensor with new shape."""
112
113
def view(input, *shape) -> Tensor:
114
"""Return tensor with new shape (shares memory)."""
115
116
def squeeze(input, dim=None) -> Tensor:
117
"""Remove dimensions of size 1."""
118
119
def unsqueeze(input, dim) -> Tensor:
120
"""Add dimension of size 1."""
121
122
def transpose(input, dim0, dim1) -> Tensor:
123
"""Swap two dimensions."""
124
125
def permute(input, dims) -> Tensor:
126
"""Permute dimensions."""
127
128
def flatten(input, start_dim=0, end_dim=-1) -> Tensor:
129
"""Flatten dimensions."""
130
131
def flip(input, dims) -> Tensor:
132
"""Reverse tensor along specified dimensions."""
133
```
134
135
### Tensor Splitting and Joining
136
137
Split tensors into chunks or join multiple tensors.
138
139
```python { .api }
140
def split(tensor, split_size_or_sections, dim=0) -> List[Tensor]:
141
"""Split tensor into chunks."""
142
143
def chunk(input, chunks, dim=0) -> List[Tensor]:
144
"""Split tensor into specific number of chunks."""
145
146
def unbind(input, dim=0) -> List[Tensor]:
147
"""Remove dimension and return sequence of tensors."""
148
149
def meshgrid(*tensors, indexing='ij') -> List[Tensor]:
150
"""Create coordinate grids."""
151
```
152
153
### Indexing and Selection
154
155
Advanced indexing operations for selecting and manipulating tensor elements.
156
157
```python { .api }
158
def gather(input, dim, index) -> Tensor:
159
"""Gather values along axis specified by index."""
160
161
def scatter(input, dim, index, src) -> Tensor:
162
"""Scatter values along axis specified by index."""
163
164
def scatter_add(input, dim, index, src) -> Tensor:
165
"""Scatter and add values."""
166
167
def index_select(input, dim, index) -> Tensor:
168
"""Select elements along dimension."""
169
170
def masked_select(input, mask) -> Tensor:
171
"""Select elements where mask is True."""
172
173
def nonzero(input, *, as_tuple=False) -> Tensor:
174
"""Return indices of non-zero elements."""
175
176
def where(condition, x, y) -> Tensor:
177
"""Select elements from x or y based on condition."""
178
```
179
180
### Element-wise Mathematical Operations
181
182
Basic arithmetic and mathematical functions applied element-wise.
183
184
```python { .api }
185
def add(input, other, *, alpha=1) -> Tensor:
186
"""Add tensors element-wise."""
187
188
def sub(input, other, *, alpha=1) -> Tensor:
189
"""Subtract tensors element-wise."""
190
191
def mul(input, other) -> Tensor:
192
"""Multiply tensors element-wise."""
193
194
def div(input, other, *, rounding_mode=None) -> Tensor:
195
"""Divide tensors element-wise."""
196
197
def pow(input, exponent) -> Tensor:
198
"""Raise to power element-wise."""
199
200
def abs(input) -> Tensor:
201
"""Absolute value element-wise."""
202
203
def neg(input) -> Tensor:
204
"""Negate elements."""
205
206
def sign(input) -> Tensor:
207
"""Sign of elements (-1, 0, 1)."""
208
209
def sqrt(input) -> Tensor:
210
"""Square root element-wise."""
211
212
def square(input) -> Tensor:
213
"""Square element-wise."""
214
215
def exp(input) -> Tensor:
216
"""Exponential function element-wise."""
217
218
def log(input) -> Tensor:
219
"""Natural logarithm element-wise."""
220
221
def log10(input) -> Tensor:
222
"""Base-10 logarithm element-wise."""
223
224
def log2(input) -> Tensor:
225
"""Base-2 logarithm element-wise."""
226
```
227
228
### Trigonometric Functions
229
230
Trigonometric and hyperbolic functions.
231
232
```python { .api }
233
def sin(input) -> Tensor:
234
"""Sine element-wise."""
235
236
def cos(input) -> Tensor:
237
"""Cosine element-wise."""
238
239
def tan(input) -> Tensor:
240
"""Tangent element-wise."""
241
242
def asin(input) -> Tensor:
243
"""Arcsine element-wise."""
244
245
def acos(input) -> Tensor:
246
"""Arccosine element-wise."""
247
248
def atan(input) -> Tensor:
249
"""Arctangent element-wise."""
250
251
def atan2(input, other) -> Tensor:
252
"""Two-argument arctangent."""
253
254
def sinh(input) -> Tensor:
255
"""Hyperbolic sine element-wise."""
256
257
def cosh(input) -> Tensor:
258
"""Hyperbolic cosine element-wise."""
259
260
def tanh(input) -> Tensor:
261
"""Hyperbolic tangent element-wise."""
262
```
263
264
### Comparison Operations
265
266
Element-wise comparison operations returning boolean tensors.
267
268
```python { .api }
269
def eq(input, other) -> Tensor:
270
"""Element-wise equality."""
271
272
def ne(input, other) -> Tensor:
273
"""Element-wise inequality."""
274
275
def lt(input, other) -> Tensor:
276
"""Element-wise less than."""
277
278
def le(input, other) -> Tensor:
279
"""Element-wise less than or equal."""
280
281
def gt(input, other) -> Tensor:
282
"""Element-wise greater than."""
283
284
def ge(input, other) -> Tensor:
285
"""Element-wise greater than or equal."""
286
287
def equal(input, other) -> bool:
288
"""True if tensors are element-wise equal."""
289
290
def allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) -> bool:
291
"""True if tensors are approximately equal."""
292
```
293
294
### Reduction Operations
295
296
Operations that reduce tensor dimensions by aggregating values.
297
298
```python { .api }
299
def sum(input, dim=None, keepdim=False, *, dtype=None) -> Tensor:
300
"""Sum of tensor elements."""
301
302
def mean(input, dim=None, keepdim=False, *, dtype=None) -> Tensor:
303
"""Mean of tensor elements."""
304
305
def median(input, dim=None, keepdim=False) -> Tensor:
306
"""Median of tensor elements."""
307
308
def mode(input, dim=None, keepdim=False) -> Tensor:
309
"""Mode of tensor elements."""
310
311
def std(input, dim=None, keepdim=False, *, dtype=None) -> Tensor:
312
"""Standard deviation."""
313
314
def var(input, dim=None, keepdim=False, *, dtype=None) -> Tensor:
315
"""Variance."""
316
317
def max(input, dim=None, keepdim=False) -> Tensor:
318
"""Maximum values."""
319
320
def min(input, dim=None, keepdim=False) -> Tensor:
321
"""Minimum values."""
322
323
def argmax(input, dim=None, keepdim=False) -> Tensor:
324
"""Indices of maximum values."""
325
326
def argmin(input, dim=None, keepdim=False) -> Tensor:
327
"""Indices of minimum values."""
328
329
def prod(input, dim=None, keepdim=False, *, dtype=None) -> Tensor:
330
"""Product of tensor elements."""
331
332
def all(input, dim=None, keepdim=False) -> Tensor:
333
"""True if all elements are True."""
334
335
def any(input, dim=None, keepdim=False) -> Tensor:
336
"""True if any elements are True."""
337
```
338
339
### Linear Algebra Operations
340
341
Core linear algebra operations for matrices and vectors.
342
343
```python { .api }
344
def matmul(input, other) -> Tensor:
345
"""Matrix multiplication."""
346
347
def mm(input, mat2) -> Tensor:
348
"""Matrix multiplication (2D tensors only)."""
349
350
def bmm(input, mat2) -> Tensor:
351
"""Batch matrix multiplication."""
352
353
def dot(input, other) -> Tensor:
354
"""Dot product of vectors."""
355
356
def mv(input, vec) -> Tensor:
357
"""Matrix-vector multiplication."""
358
359
def outer(input, vec2) -> Tensor:
360
"""Outer product of vectors."""
361
362
def cross(input, other, dim=None) -> Tensor:
363
"""Cross product."""
364
365
def norm(input, p='fro', dim=None, keepdim=False, *, dtype=None) -> Tensor:
366
"""Matrix or vector norm."""
367
```
368
369
### Tensor Properties and Utilities
370
371
Functions for inspecting and manipulating tensor properties.
372
373
```python { .api }
374
def is_tensor(obj) -> bool:
375
"""Check if object is a tensor."""
376
377
def numel(input) -> int:
378
"""Number of elements in tensor."""
379
380
def typename(o) -> str:
381
"""Type name of tensor."""
382
383
def is_floating_point(input) -> bool:
384
"""Check if tensor has floating point data type."""
385
386
def is_complex(input) -> bool:
387
"""Check if tensor has complex data type."""
388
389
def is_signed(input) -> bool:
390
"""Check if tensor has signed data type."""
391
392
def clone(input) -> Tensor:
393
"""Create copy of tensor."""
394
395
def detach(input) -> Tensor:
396
"""Detach tensor from computation graph."""
397
```
398
399
## Usage Examples
400
401
### Basic Tensor Operations
402
403
```python
404
import torch
405
406
# Create tensors
407
x = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
408
y = torch.rand(2, 2)
409
410
# Basic operations
411
z = torch.add(x, y)
412
product = torch.matmul(x, y)
413
mean_val = torch.mean(x)
414
415
# Shape manipulation
416
reshaped = torch.reshape(x, (4,))
417
transposed = torch.transpose(x, 0, 1)
418
419
# Indexing
420
selected = torch.index_select(x, 0, torch.tensor([0]))
421
mask = x > 2
422
masked = torch.masked_select(x, mask)
423
424
print(f"Original: {x}")
425
print(f"Sum: {z}")
426
print(f"Mean: {mean_val}")
427
print(f"Reshaped: {reshaped}")
428
print(f"Masked: {masked}")
429
```
430
431
### GPU Operations
432
433
```python
434
import torch
435
436
# Check CUDA availability
437
if torch.cuda.is_available():
438
device = torch.device('cuda')
439
440
# Create tensors on GPU
441
x = torch.tensor([[1, 2], [3, 4]], device=device, dtype=torch.float32)
442
y = torch.rand(2, 2, device=device)
443
444
# Operations are performed on GPU
445
z = torch.matmul(x, y)
446
447
# Move back to CPU if needed
448
z_cpu = z.cpu()
449
450
print(f"GPU result: {z}")
451
print(f"CPU result: {z_cpu}")
452
```