975 words
5 minutes
Tensor Creation & Basic Operations

I. Tensor Creation & Basic Operations (张量创建与基础操作)#

1. torch.tensor()#

Creates a Tensor (张量) directly from a Python list or NumPy array. You can specify the Data Type (数据类型) and Device (设备) at creation time.
import torch
x = torch.tensor(
[[1.0, 2.0], [3.0, 4.0]],
dtype=torch.float32
)
print(x.shape) # torch.Size([2, 2])
import numpy as np
arr = np.array([1, 2, 3], dtype=np.float32)
x = torch.as_tensor(arr)
arr[0] = 100 # share the memory with array
print(x)
Note: Every call copies the data. To share memory with the source array, use torch.as_tensor() instead.

2. torch.zeros() / torch.ones()#

Creates all-zero or all-one Tensors (全零/全一张量). Commonly used for bias initialization (偏置初始化) and mask generation (掩码生成).
z = torch.zeros(3, 4) # 3×4 all zeros
o = torch.ones(2, 3, dtype=torch.int32) # 2×3 all ones, int type
out = torch.empty(2, 3, dtype=torch.float16)
print(out)
out = torch.zeros(2, 3, dtype=torch.float16, out=out)
print(out)
# tensor([[ 5.5680e+03, -9.3126e-04, nan],
# [ 0.0000e+00, 7.8906e-01, 1.1133e-01]], dtype=torch.float16)
# tensor([[0., 0., 0.],
# [0., 0., 0.]], dtype=torch.float16)
Note: The out= parameter writes results into an existing Tensor, avoiding extra memory allocation (内存分配).

3.torch.arange()#

Generates an Arithmetic Sequence Tensor (等差数列张量), analogous to Python's range(). Supports float step sizes.
t = torch.arange(0, 10, 2) # tensor([0, 2, 4, 6, 8])
f = torch.arange(0.0, 1.0, step = 0.25) # tensor([0.00, 0.25, 0.50, 0.75])
print(t)
print(f)
Note: Floating-point steps may cause Boundary Precision Issues (边界精度问题). For exact equal-interval sampling, use torch.linspace().

4. torch.linspace()#

Uniformly generates steps points in the interval [start, end]. More precise than arange for float ranges.
t = torch.linspace(0, 1, steps=5)
# tensor([0.00, 0.25, 0.50, 0.75, 1.00])
Note: Commonly used for plotting function curves (函数曲线) and generating uniformly sampled frequency axes (均匀采样频率轴).

5. torch.rand() / torch.randn()/ torch.randint()#

rand: Uniform Distribution (均匀分布) U[0,1). randn: Standard Normal Distribution (标准正态分布) N(-∞, +∞).
u = torch.rand(2, 3) # Uniform distribution
n = torch.randn(2, 3) # Normal distribution
i = torch.randint(low=0, high=10, size=(2, 3))
# Fix seed for reproducibility (可复现性)
torch.manual_seed(42)
x = torch.rand(3)
print(x)
print(u)
print(n)
print(i)
# tensor([0.8823, 0.9150, 0.3829])
# tensor([[0.9593, 0.3904, 0.6009],
# [0.2566, 0.7936, 0.9408]])
# tensor([[ 1.5231, 0.6647, -1.0324],
# [-0.2770, -0.1671, -0.1079]])
# tensor([[6, 3, 1],
# [9, 3, 1]])
Note: Neural network Weight Initialization (权重初始化) typically uses randn; Dropout Mask (Dropout掩码) generation uses rand.

6. torch.eye()#

Creates an Identity Matrix (单位矩阵) with ones on the diagonal and zeros elsewhere. Commonly used in Linear Algebra (线性代数) and regularization (正则化).
I = torch.eye(3)
# tensor([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]])
Note: Pass n, m to create a Non-square Identity Matrix (非方阵单位矩阵), e.g. torch.eye(3, 4).

7. torch.full()#

Creates a Tensor of specified shape where all elements equal a given Fill Value (填充值).
t = torch.full((2, 3), fill_value=7.0)
# tensor([[7., 7., 7.],
# [7., 7., 7.]])
Note: More efficient than torch.zeros() * 7. Ideal for creating padding masks (填充掩码).

8. torch.from_numpy()#

Converts a NumPy ndarray to a Tensor. The two share memory (共享内存) — modifying one affects the other.
import numpy as np
arr = np.array([1.0, 2.0, 3.0])
t = torch.from_numpy(arr) # as_tensor(arr)
arr[0] = 99
print(t) # tensor([99., 2., 3.])
Note: Shared memory is a double-edged sword: it saves memory but can cause unintended modifications to the source array.

9. Tensor.numpy()#

Converts a CPU Tensor back to a NumPy ndarray. Also shares the underlying memory (底层内存).
t = torch.tensor([1.0, 2.0, 3.0])
arr = t.numpy()
t[0] = 100
print(arr) # [100. 2. 3.]
Note: GPU Tensors must call .cpu() first, and Tensors with requires_grad=True must call .detach() first.

10.torch.empty()#

Allocates Uninitialized Memory (未初始化内存) for a Tensor — the fastest allocation method. Values are whatever remains in memory.
t = torch.empty(3, 3) # Values are undefined
t.fill_(0.5) # Must fill before reading
Note: Never read values before filling. Best for performance-sensitive scenarios where you immediately overwrite the buffer.
💡 One-line Takeaway
Use torch.tensor() for known data, torch.zeros/ones/rand/randn() for initialized buffers, and torch.empty() only when you'll immediately overwrite every element.

Tensor Creation & Basic Operations
https://lxy-alexander.github.io/blog/posts/pytorch/api/01tensor-creation--basic-operations/
Author
Alexander Lee
Published at
2026-03-12
License
CC BY-NC-SA 4.0