张量操作 #

数学运算 #

基本算术运算 #

python
import tensorflow as tf

a = tf.constant([1, 2, 3], dtype=tf.float32)
b = tf.constant([4, 5, 6], dtype=tf.float32)

# 四则运算
print(f"加法: {tf.add(a, b).numpy()}")        # 或 a + b
print(f"减法: {tf.subtract(a, b).numpy()}")   # 或 a - b
print(f"乘法: {tf.multiply(a, b).numpy()}")   # 或 a * b
print(f"除法: {tf.divide(a, b).numpy()}")     # 或 a / b

# 整除和取余
print(f"整除: {tf.floordiv(b, a).numpy()}")   # 或 b // a
print(f"取余: {tf.math.floormod(b, a).numpy()}")  # 或 b % a

# 负数
print(f"负数: {tf.negative(a).numpy()}")      # 或 -a

幂运算与开方 #

python
import tensorflow as tf

x = tf.constant([1, 2, 3, 4], dtype=tf.float32)

# 幂运算
print(f"平方: {tf.square(x).numpy()}")
print(f"立方: {tf.pow(x, 3).numpy()}")
print(f"x^x: {tf.pow(x, x).numpy()}")

# 开方
print(f"平方根: {tf.sqrt(x).numpy()}")
print(f"立方根: {tf.pow(x, 1/3).numpy()}")

# 指数与对数
print(f"e^x: {tf.exp(x).numpy()}")
print(f"ln(x): {tf.math.log(x).numpy()}")
print(f"log2(x): {tf.math.log(x) / tf.math.log(2.0).numpy()}")

# 指数函数
print(f"2^x: {tf.math.pow(2.0, x).numpy()}")
print(f"10^x: {tf.math.pow(10.0, x).numpy()}")

三角函数 #

python
import tensorflow as tf
import numpy as np

x = tf.constant([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2], dtype=tf.float32)

# 三角函数
print(f"sin(x): {tf.sin(x).numpy()}")
print(f"cos(x): {tf.cos(x).numpy()}")
print(f"tan(x): {tf.tan(x).numpy()}")

# 反三角函数
y = tf.constant([0, 0.5, 0.707, 0.866, 1.0], dtype=tf.float32)
print(f"arcsin(y): {tf.asin(y).numpy()}")
print(f"arccos(y): {tf.acos(y).numpy()}")
print(f"arctan(y): {tf.atan(y).numpy()}")

# 双曲函数
print(f"sinh(x): {tf.sinh(x).numpy()}")
print(f"cosh(x): {tf.cosh(x).numpy()}")
print(f"tanh(x): {tf.tanh(x).numpy()}")

取整与符号 #

python
import tensorflow as tf

x = tf.constant([-2.5, -1.5, -0.5, 0.5, 1.5, 2.5])

# 取整
print(f"四舍五入: {tf.round(x).numpy()}")
print(f"向下取整: {tf.floor(x).numpy()}")
print(f"向上取整: {tf.math.ceil(x).numpy()}")

# 符号
print(f"绝对值: {tf.abs(x).numpy()}")
print(f"符号: {tf.sign(x).numpy()}")
print(f"负值: {tf.negative(x).numpy()}")

限制与裁剪 #

python
import tensorflow as tf

x = tf.constant([1, 5, 10, 15, 20], dtype=tf.float32)

# 裁剪到范围
clipped = tf.clip_by_value(x, 5, 15)
print(f"clip [5, 15]: {clipped.numpy()}")

# 按范数裁剪
y = tf.constant([[1.0, 2.0], [3.0, 4.0]])
clipped_norm = tf.clip_by_norm(y, 2.0)
print(f"按范数裁剪:\n{clipped_norm.numpy()}")

# 按全局范数裁剪
clipped_global = tf.clip_by_global_norm([y], 5.0)

# ReLU 效果
relu = tf.maximum(x, 0)
print(f"ReLU: {relu.numpy()}")

矩阵运算 #

矩阵乘法 #

python
import tensorflow as tf

# 2D 矩阵乘法
a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
b = tf.constant([[5, 6], [7, 8]], dtype=tf.float32)

matmul = tf.matmul(a, b)
print(f"矩阵乘法:\n{matmul.numpy()}")

# 使用 @ 运算符
matmul2 = a @ b
print(f"@ 运算符:\n{matmul2.numpy()}")

# 批量矩阵乘法
batch_a = tf.random.normal([3, 2, 4])  # 3个 2x4 矩阵
batch_b = tf.random.normal([3, 4, 5])  # 3个 4x5 矩阵
batch_result = tf.matmul(batch_a, batch_b)
print(f"批量矩阵乘法形状: {batch_result.shape}")

矩阵属性 #

python
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)

# 转置
print(f"转置:\n{tf.transpose(a).numpy()}")

# 行列式
det = tf.linalg.det(a)
print(f"行列式: {det.numpy()}")

# 迹(对角线元素和)
trace = tf.linalg.trace(a)
print(f"迹: {trace.numpy()}")

# 范数
norm_fro = tf.norm(a)  # Frobenius 范数
norm_1 = tf.norm(a, ord=1)  # L1 范数
norm_2 = tf.norm(a, ord=2)  # L2 范数
print(f"Frobenius范数: {norm_fro.numpy()}")

矩阵分解 #

python
import tensorflow as tf

a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=tf.float32)

# SVD 分解
s, u, v = tf.linalg.svd(a)
print(f"奇异值: {s.numpy()}")
print(f"U 形状: {u.shape}")
print(f"V 形状: {v.shape}")

# QR 分解
q, r = tf.linalg.qr(a)
print(f"\nQR分解:")
print(f"Q 形状: {q.shape}")
print(f"R 形状: {r.shape}")

# Cholesky 分解(需要正定矩阵)
pos_def = tf.constant([[4, 2, 1], [2, 5, 3], [1, 3, 6]], dtype=tf.float32)
chol = tf.linalg.cholesky(pos_def)
print(f"\nCholesky分解:\n{chol.numpy()}")

矩阵求逆 #

python
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)

# 矩阵求逆
inv = tf.linalg.inv(a)
print(f"逆矩阵:\n{inv.numpy()}")

# 验证
identity = a @ inv
print(f"A @ A^-1:\n{identity.numpy()}")

# 伪逆(Moore-Penrose)
b = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
pseudo_inv = tf.linalg.pinv(b)
print(f"\n伪逆形状: {pseudo_inv.shape}")

广播机制 #

广播规则 #

text
┌─────────────────────────────────────────────────────────────┐
│                    广播规则                                  │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 从右向左比较各维度                                       │
│  2. 维度大小相等 或 其中一个为1 或 其中一个不存在             │
│  3. 满足条件的维度可以广播                                   │
│                                                             │
│  示例:                                                      │
│  [3, 4]     + [4]      → [3, 4] + [1, 4] → [3, 4]          │
│  [3, 1]     + [1, 4]   → [3, 4]                            │
│  [2, 3, 4]  + [3, 4]   → [2, 3, 4]                         │
│  [2, 1, 4]  + [2, 3, 1] → [2, 3, 4]                        │
│                                                             │
└─────────────────────────────────────────────────────────────┘

广播示例 #

python
import tensorflow as tf

# 标量广播
x = tf.constant([1, 2, 3])
y = tf.constant(10)
print(f"标量广播: {(x + y).numpy()}")

# 向量广播
a = tf.constant([[1, 2, 3], [4, 5, 6]])  # shape: [2, 3]
b = tf.constant([10, 20, 30])             # shape: [3]
print(f"向量广播:\n{(a + b).numpy()}")

# 列向量广播
c = tf.constant([[10], [20]])  # shape: [2, 1]
print(f"列向量广播:\n{(a + c).numpy()}")

# 显式广播
d = tf.constant([1, 2, 3])
broadcasted = tf.broadcast_to(d, [2, 3])
print(f"显式广播:\n{broadcasted.numpy()}")

广播失败案例 #

python
import tensorflow as tf

try:
    a = tf.constant([1, 2, 3])      # shape: [3]
    b = tf.constant([1, 2])         # shape: [2]
    result = a + b  # 错误!形状不兼容
except Exception as e:
    print(f"广播失败: {e}")

# 解决方案
a = tf.constant([[1, 2, 3]])        # shape: [1, 3]
b = tf.constant([[1], [2]])         # shape: [2, 1]
result = a + b                       # shape: [2, 3]
print(f"正确广播:\n{result.numpy()}")

归约操作 #

求和与均值 #

python
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)

# 全局归约
print(f"总和: {tf.reduce_sum(x).numpy()}")
print(f"均值: {tf.reduce_mean(x).numpy()}")
print(f"最大值: {tf.reduce_max(x).numpy()}")
print(f"最小值: {tf.reduce_min(x).numpy()}")
print(f"乘积: {tf.reduce_prod(x).numpy()}")

# 按轴归约
print(f"\n按行求和 (axis=0): {tf.reduce_sum(x, axis=0).numpy()}")
print(f"按列求和 (axis=1): {tf.reduce_sum(x, axis=1).numpy()}")

# 保持维度
print(f"保持维度: {tf.reduce_sum(x, axis=1, keepdims=True).shape}")

其他归约操作 #

python
import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)

# 逻辑归约
bool_x = tf.constant([[True, False], [True, True]])
print(f"全部为True: {tf.reduce_all(bool_x).numpy()}")
print(f"任一为True: {tf.reduce_any(bool_x).numpy()}")

# 累积和
print(f"累积和:\n{tf.cumsum(x, axis=1).numpy()}")

# 累积积
print(f"累积积:\n{tf.cumprod(x, axis=1).numpy()}")

# 方差和标准差
print(f"方差: {tf.math.reduce_variance(x).numpy()}")
print(f"标准差: {tf.math.reduce_std(x).numpy()}")

# LogSumExp
print(f"LogSumExp: {tf.reduce_logsumexp(x).numpy()}")

比较操作 #

元素比较 #

python
import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([1, 2, 0, 4, 6])

# 比较运算
print(f"相等: {tf.equal(a, b).numpy()}")
print(f"不等: {tf.not_equal(a, b).numpy()}")
print(f"大于: {tf.greater(a, b).numpy()}")
print(f"小于: {tf.less(a, b).numpy()}")
print(f"大于等于: {tf.greater_equal(a, b).numpy()}")
print(f"小于等于: {tf.less_equal(a, b).numpy()}")

条件选择 #

python
import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([10, 20, 30, 40, 50])
condition = tf.constant([True, False, True, False, True])

# 条件选择
result = tf.where(condition, a, b)
print(f"条件选择: {result.numpy()}")

# 根据条件选择元素
x = tf.constant([-2, -1, 0, 1, 2])
positive = tf.maximum(x, 0)
print(f"正值: {positive.numpy()}")

# 查找非零元素索引
nonzero = tf.where(tf.not_equal(x, 0))
print(f"非零索引: {nonzero.numpy()}")

排序与查找 #

python
import tensorflow as tf

x = tf.constant([3, 1, 4, 1, 5, 9, 2, 6])

# 排序
sorted_x = tf.sort(x)
print(f"升序: {sorted_x.numpy()}")
print(f"降序: {tf.sort(x, direction='DESCENDING').numpy()}")

# 排序索引
indices = tf.argsort(x)
print(f"排序索引: {indices.numpy()}")

# 查找最大/最小值索引
print(f"最大值索引: {tf.argmax(x).numpy()}")
print(f"最小值索引: {tf.argmin(x).numpy()}")

# Top-K
values, indices = tf.math.top_k(x, k=3)
print(f"Top-3 值: {values.numpy()}")
print(f"Top-3 索引: {indices.numpy()}")

# 查找元素
x_2d = tf.constant([[1, 2], [3, 4]])
print(f"全局最大值位置: {tf.argmax(x_2d).numpy()}")
print(f"按行最大值位置: {tf.argmax(x_2d, axis=1).numpy()}")

拼接与分割 #

拼接操作 #

python
import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6], [7, 8]])

# 沿轴拼接
concat_0 = tf.concat([a, b], axis=0)
print(f"axis=0 拼接:\n{concat_0.numpy()}")

concat_1 = tf.concat([a, b], axis=1)
print(f"axis=1 拼接:\n{concat_1.numpy()}")

# 堆叠(增加新维度)
stack_0 = tf.stack([a, b], axis=0)
print(f"stack axis=0 形状: {stack_0.shape}")

stack_neg1 = tf.stack([a, b], axis=-1)
print(f"stack axis=-1 形状: {stack_neg1.shape}")

分割操作 #

python
import tensorflow as tf

x = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]])

# 沿轴分割
split_2 = tf.split(x, num_or_size_splits=2, axis=1)
print(f"分成2份:")
for i, part in enumerate(split_2):
    print(f"  部分{i}: {part.numpy()}")

# 不等分
split_sizes = [1, 2, 1]
split_custom = tf.split(x, num_or_size_splits=split_sizes, axis=1)
print(f"\n按大小 [1,2,1] 分割:")
for i, part in enumerate(split_custom):
    print(f"  部分{i}: {part.numpy()}")

# Unstack(沿轴拆分)
unstacked = tf.unstack(x, axis=0)
print(f"\nunstack 结果数量: {len(unstacked)}")

下一步 #

现在你已经掌握了张量操作,接下来学习 变量与状态,了解如何在模型中管理可变状态!

最后更新:2026-04-04