张量基础 #
什么是张量? #
张量(Tensor)是 TensorFlow 中的核心数据结构,是多维数组的泛化形式。张量可以表示标量(0维)、向量(1维)、矩阵(2维)以及更高维度的数据。
张量的维度 #
text
┌─────────────────────────────────────────────────────────────┐
│ 张量维度示意 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 标量 (0-D) 向量 (1-D) 矩阵 (2-D) 3-D 张量 │
│ │
│ [5] [1, 2, 3] [1, 2, 3] [[[1, 2], │
│ [4, 5, 6] [3, 4]], │
│ [[5, 6], │
│ [7, 8]]] │
│ │
│ shape: [] shape: [3] shape: [2,3] shape: [2,2,2] │
│ dim: 0 dim: 1 dim: 2 dim: 3 │
│ │
└─────────────────────────────────────────────────────────────┘
创建张量 #
使用 tf.constant #
python
import tensorflow as tf
# 标量
scalar = tf.constant(5)
print(f"标量: {scalar}, 形状: {scalar.shape}")
# 向量
vector = tf.constant([1, 2, 3, 4])
print(f"向量: {vector}, 形状: {vector.shape}")
# 矩阵
matrix = tf.constant([[1, 2], [3, 4]])
print(f"矩阵:\n{matrix.numpy()}")
# 3D 张量
tensor_3d = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(f"3D张量形状: {tensor_3d.shape}")
# 指定数据类型
float_tensor = tf.constant([1, 2, 3], dtype=tf.float32)
int_tensor = tf.constant([1.5, 2.5, 3.5], dtype=tf.int32)
print(f"float32: {float_tensor.dtype}, int32: {int_tensor.dtype}")
使用 tf.zeros 和 tf.ones #
python
import tensorflow as tf
# 全零张量
zeros = tf.zeros([3, 4])
print(f"zeros 形状: {zeros.shape}")
# 全一张量
ones = tf.ones([2, 3, 4])
print(f"ones 形状: {ones.shape}")
# 与已有张量相同形状
like_zeros = tf.zeros_like(zeros)
like_ones = tf.ones_like(ones)
# 指定数据类型
zeros_float = tf.zeros([2, 2], dtype=tf.float64)
ones_int = tf.ones([2, 2], dtype=tf.int32)
使用 tf.fill #
python
import tensorflow as tf
# 填充特定值
filled = tf.fill([3, 3], 7)
print(f"填充张量:\n{filled.numpy()}")
# 创建对角矩阵
diagonal = tf.linalg.diag([1, 2, 3, 4])
print(f"对角矩阵:\n{diagonal.numpy()}")
使用 tf.random #
python
import tensorflow as tf
# 设置随机种子
tf.random.set_seed(42)
# 均匀分布
uniform = tf.random.uniform([3, 3], minval=0, maxval=10)
print(f"均匀分布:\n{uniform.numpy()}")
# 正态分布
normal = tf.random.normal([3, 3], mean=0, stddev=1)
print(f"正态分布:\n{normal.numpy()}")
# 截断正态分布(排除过大过小的值)
truncated = tf.random.truncated_normal([3, 3], mean=0, stddev=1)
# 随机打乱
values = tf.constant([1, 2, 3, 4, 5])
shuffled = tf.random.shuffle(values)
print(f"打乱后: {shuffled.numpy()}")
从 NumPy 创建 #
python
import tensorflow as tf
import numpy as np
# 从 NumPy 数组创建
np_array = np.array([[1, 2, 3], [4, 5, 6]])
tensor = tf.constant(np_array)
print(f"从 NumPy 创建: {tensor.shape}")
# 使用 convert_to_tensor
tensor2 = tf.convert_to_tensor(np_array, dtype=tf.float32)
print(f"数据类型: {tensor2.dtype}")
# 转换回 NumPy
back_to_numpy = tensor.numpy()
print(f"类型: {type(back_to_numpy)}")
数据类型 #
支持的数据类型 #
python
import tensorflow as tf
# 整数类型
int8 = tf.constant([1, 2], dtype=tf.int8)
int16 = tf.constant([1, 2], dtype=tf.int16)
int32 = tf.constant([1, 2], dtype=tf.int32)
int64 = tf.constant([1, 2], dtype=tf.int64)
# 无符号整数
uint8 = tf.constant([1, 2], dtype=tf.uint8)
uint16 = tf.constant([1, 2], dtype=tf.uint16)
uint32 = tf.constant([1, 2], dtype=tf.uint32)
uint64 = tf.constant([1, 2], dtype=tf.uint64)
# 浮点类型
float16 = tf.constant([1.0], dtype=tf.float16)
float32 = tf.constant([1.0], dtype=tf.float32)
float64 = tf.constant([1.0], dtype=tf.float64)
bfloat16 = tf.constant([1.0], dtype=tf.bfloat16)
# 复数类型
complex64 = tf.constant([1+2j], dtype=tf.complex64)
complex128 = tf.constant([1+2j], dtype=tf.complex128)
# 布尔类型
bool_tensor = tf.constant([True, False], dtype=tf.bool)
# 字符串类型
string_tensor = tf.constant(["hello", "world"], dtype=tf.string)
类型转换 #
python
import tensorflow as tf
# 创建张量
x = tf.constant([1.5, 2.7, 3.9])
# 类型转换
x_int = tf.cast(x, tf.int32)
x_float16 = tf.cast(x, tf.float16)
x_float64 = tf.cast(x, tf.float64)
print(f"原始: {x.dtype}")
print(f"int32: {x_int.numpy()}")
print(f"float16: {x_float16.dtype}")
# 布尔转换
y = tf.constant([0, 1, 2, 0])
y_bool = tf.cast(y, tf.bool)
print(f"布尔: {y_bool.numpy()}")
张量属性 #
python
import tensorflow as tf
tensor = tf.constant([
[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]
])
# 形状
print(f"形状: {tensor.shape}")
# 维度数
print(f"维度数: {tensor.ndim}")
# 数据类型
print(f"数据类型: {tensor.dtype}")
# 设备
print(f"设备: {tensor.device}")
# 元素数量
print(f"元素数量: {tf.size(tensor).numpy()}")
# 各维度大小
print(f"第0维大小: {tensor.shape[0]}")
print(f"第1维大小: {tensor.shape[1]}")
print(f"第2维大小: {tensor.shape[2]}")
形状操作 #
reshape #
python
import tensorflow as tf
x = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
# 改变形状
reshaped = tf.reshape(x, [3, 4])
print(f"reshape [3,4]:\n{reshaped.numpy()}")
reshaped2 = tf.reshape(x, [2, 6])
print(f"reshape [2,6]:\n{reshaped2.numpy()}")
# 使用 -1 自动推断
reshaped3 = tf.reshape(x, [-1, 4])
print(f"reshape [-1,4]:\n{reshaped3.numpy()}")
# 展平
flattened = tf.reshape(x, [-1])
print(f"展平: {flattened.numpy()}")
squeeze 和 expand_dims #
python
import tensorflow as tf
# squeeze - 移除大小为1的维度
x = tf.constant([[[1, 2, 3]]])
print(f"原始形状: {x.shape}")
squeezed = tf.squeeze(x)
print(f"squeeze后: {squeezed.shape}")
# 指定移除的维度
squeezed2 = tf.squeeze(x, axis=0)
print(f"squeeze axis=0: {squeezed2.shape}")
# expand_dims - 增加维度
y = tf.constant([1, 2, 3])
print(f"\n原始形状: {y.shape}")
expanded0 = tf.expand_dims(y, axis=0)
print(f"expand axis=0: {expanded0.shape}")
expanded1 = tf.expand_dims(y, axis=1)
print(f"expand axis=1: {expanded1.shape}")
expanded_neg1 = tf.expand_dims(y, axis=-1)
print(f"expand axis=-1: {expanded_neg1.shape}")
transpose #
python
import tensorflow as tf
x = tf.constant([[1, 2, 3], [4, 5, 6]])
print(f"原始:\n{x.numpy()}")
# 转置
transposed = tf.transpose(x)
print(f"转置:\n{transposed.numpy()}")
# 指定维度顺序
y = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(f"\n3D张量形状: {y.shape}")
transposed_3d = tf.transpose(y, perm=[0, 2, 1])
print(f"perm=[0,2,1] 形状: {transposed_3d.shape}")
索引与切片 #
基本索引 #
python
import tensorflow as tf
x = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 单元素索引
print(f"x[0, 0] = {x[0, 0].numpy()}")
print(f"x[1, 2] = {x[1, 2].numpy()}")
# 行索引
print(f"x[0] = {x[0].numpy()}")
print(f"x[-1] = {x[-1].numpy()}")
# 列索引
print(f"x[:, 0] = {x[:, 0].numpy()}")
print(f"x[:, -1] = {x[:, -1].numpy()}")
切片操作 #
python
import tensorflow as tf
x = tf.constant([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 基本切片
print(f"x[2:5] = {x[2:5].numpy()}")
print(f"x[:5] = {x[:5].numpy()}")
print(f"x[5:] = {x[5:].numpy()}")
print(f"x[::2] = {x[::2].numpy()}") # 步长
print(f"x[::-1] = {x[::-1].numpy()}") # 反转
# 2D 切片
matrix = tf.constant([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
print(f"\n前两行: {matrix[:2].numpy()}")
print(f"中间两列: {matrix[:, 1:3].numpy()}")
print(f"右下角 2x2: {matrix[1:, 2:].numpy()}")
高级索引 #
python
import tensorflow as tf
x = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 使用 gather
indices = tf.constant([0, 2])
gathered = tf.gather(x, indices, axis=0)
print(f"gather 行 [0, 2]:\n{gathered.numpy()}")
gathered_cols = tf.gather(x, indices, axis=1)
print(f"gather 列 [0, 2]:\n{gathered_cols.numpy()}")
# 使用 gather_nd
indices_nd = tf.constant([[0, 0], [1, 1], [2, 2]])
gathered_nd = tf.gather_nd(x, indices_nd)
print(f"gather_nd 对角线: {gathered_nd.numpy()}")
# 布尔索引
mask = x > 5
print(f"大于5的元素: {tf.boolean_mask(x, mask).numpy()}")
设备管理 #
python
import tensorflow as tf
# 查看可用设备
print("CPU 设备:", tf.config.list_physical_devices('CPU'))
print("GPU 设备:", tf.config.list_physical_devices('GPU'))
# 指定设备创建张量
with tf.device('/CPU:0'):
cpu_tensor = tf.constant([1, 2, 3])
print(f"CPU 张量设备: {cpu_tensor.device}")
if tf.config.list_physical_devices('GPU'):
with tf.device('/GPU:0'):
gpu_tensor = tf.constant([1, 2, 3])
print(f"GPU 张量设备: {gpu_tensor.device}")
# 张量复制
if tf.config.list_physical_devices('GPU'):
cpu_tensor = tf.constant([1, 2, 3])
gpu_tensor = cpu_tensor.gpu() # 复制到 GPU
back_to_cpu = gpu_tensor.cpu() # 复制回 CPU
张量与 NumPy 互操作 #
python
import tensorflow as tf
import numpy as np
# TensorFlow 张量 → NumPy 数组
tensor = tf.constant([[1, 2], [3, 4]])
np_array = tensor.numpy()
print(f"NumPy 数组:\n{np_array}")
# NumPy 数组 → TensorFlow 张量
np_array = np.array([[5, 6], [7, 8]])
tensor = tf.convert_to_tensor(np_array)
print(f"TensorFlow 张量:\n{tensor}")
# 混合运算
result = tensor + np_array # NumPy 数组自动转换为张量
print(f"混合运算结果:\n{result.numpy()}")
下一步 #
现在你已经掌握了张量的基础知识,接下来学习 张量操作,了解各种数学运算和操作!
最后更新:2026-04-04