TensorFlow 张量操作
TensorFlow 张量操作
张量(Tensor)是 TensorFlow 的核心数据结构,类似于多维数组,用于表示数据和执行计算。TensorFlow 提供了丰富的张量操作,涵盖创建、变换、数学运算等。本文将介绍 TensorFlow 中常用的张量操作,附带简洁的代码示例,适合初学者和需要快速参考的用户。如果需要更深入的特定操作,请告诉我!
1. 张量的基本概念
- 张量:多维数组,类似 NumPy 的
ndarray
,有形状(shape)和数据类型(dtype)。 - 属性:
- 形状(shape):如
(2, 3)
表示 2 行 3 列。 - 数据类型:如
tf.float32
,tf.int32
,tf.string
。 - 秩(rank):张量的维度(0 维为标量,1 维为向量,2 维为矩阵,等等)。
- TensorFlow 2.x:默认使用 Eager Execution,张量操作即时执行,类似 Python。
2. 创建张量
以下是常用的张量创建方法:
常量张量(tf.constant
)
不可修改的张量。
import tensorflow as tf
# 创建常量张量
tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
print(tensor)
# 输出:tf.Tensor([[1. 2.] [3. 4.]], shape=(2, 2), dtype=float32)
变量张量(tf.Variable
)
可修改的张量,常用于模型参数(如权重)。
var = tf.Variable([[1.0, 2.0], [3.0, 4.0]])
var.assign(var + 1.0) # 更新值
print(var)
# 输出:tf.Tensor([[2. 3.] [4. 5.]], shape=(2, 2), dtype=float32)
其他创建方法
- 全零/全一张量:
zeros = tf.zeros([2, 3]) # 2x3 全零张量
ones = tf.ones([2, 3]) # 2x3 全一张量
- 随机张量:
random = tf.random.normal([2, 2], mean=0, stddev=1) # 正态分布
uniform = tf.random.uniform([2, 2], minval=0, maxval=1) # 均匀分布
- 从 NumPy 转换:
import numpy as np
np_array = np.array([[1, 2], [3, 4]])
tensor = tf.convert_to_tensor(np_array, dtype=tf.float32)
3. 基本张量操作
形状操作
- 查看形状:
print(tensor.shape) # 输出:(2, 2)
- 改变形状(
tf.reshape
):
tensor = tf.constant([[1, 2], [3, 4], [5, 6]])
reshaped = tf.reshape(tensor, [2, 3]) # 变成 2x3
print(reshaped) # 输出:tf.Tensor([[1 2 3] [4 5 6]], shape=(2, 3), dtype=int32)
- 扩展/压缩维度(
tf.expand_dims
,tf.squeeze
):
expanded = tf.expand_dims(tensor, axis=0) # 在 axis=0 增加维度
squeezed = tf.squeeze(expanded, axis=0) # 移除维度
切片和索引
- 切片:
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
sliced = tensor[:, 1:3] # 取所有行,第 1 到 2 列
print(sliced) # 输出:tf.Tensor([[2 3] [5 6]], shape=(2, 2), dtype=int32)
- 索引:
element = tensor[0, 1] # 第 0 行,第 1 列
print(element) # 输出:tf.Tensor(2, shape=(), dtype=int32)
连接和拆分
- 连接(
tf.concat
,tf.stack
):
t1 = tf.constant([[1, 2]])
t2 = tf.constant([[3, 4]])
concat = tf.concat([t1, t2], axis=0) # 沿 axis=0 连接
print(concat) # 输出:tf.Tensor([[1 2] [3 4]], shape=(2, 2), dtype=int32)
stack = tf.stack([t1, t2], axis=0) # 创建新维度
print(stack) # 输出:tf.Tensor([[[1 2]] [[3 4]]], shape=(2, 1, 2), dtype=int32)
- 拆分(
tf.split
):
split = tf.split(tensor, num_or_size_splits=2, axis=0) # 按行拆分
print(split) # 输出:[<tf.Tensor: shape=(1, 3), ...>, <tf.Tensor: shape=(1, 3), ...>]
4. 数学运算
TensorFlow 支持逐元素运算、矩阵运算等,类似 NumPy。
逐元素运算
- 加、减、乘、除等:
a = tf.constant([1, 2, 3], dtype=tf.float32)
b = tf.constant([4, 5, 6], dtype=tf.float32)
add = tf.add(a, b) # 或 a + b
print(add) # 输出:tf.Tensor([5. 7. 9.], shape=(3,), dtype=float32)
- 其他:
tf.square
,tf.sqrt
,tf.exp
,tf.log
。
矩阵运算
- 矩阵乘法(
tf.matmul
):
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(matmul) # 输出:tf.Tensor([[19. 22.] [43. 50.]], shape=(2, 2), dtype=float32)
- 转置(
tf.transpose
):
transposed = tf.transpose(a)
print(transposed) # 输出:tf.Tensor([[1. 3.] [2. 4.]], shape=(2, 2), dtype=float32)
统计操作
- 求和、均值、最大/最小值:
tensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
sum = tf.reduce_sum(tensor) # 所有元素求和
mean = tf.reduce_mean(tensor, axis=0) # 按列求均值
max = tf.reduce_max(tensor) # 最大值
print(sum, mean, max)
# 输出:tf.Tensor(10.0, ...), tf.Tensor([2. 3.], ...), tf.Tensor(4.0, ...)
5. 广播(Broadcasting)
TensorFlow 支持广播,允许不同形状的张量进行运算。
a = tf.constant([1, 2, 3])
b = tf.constant([[1], [2], [3]])
result = a + b # a 广播到 [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
print(result) # 输出:tf.Tensor([[2 3 4] [3 4 5] [4 5 6]], shape=(3, 3), dtype=int32)
6. 高级操作
张量填充(tf.pad
)
tensor = tf.constant([[1, 2], [3, 4]])
padded = tf.pad(tensor, [[1, 1], [1, 1]], mode='CONSTANT') # 四周填充 0
print(padded)
# 输出:tf.Tensor([[0 0 0 0] [0 1 2 0] [0 3 4 0] [0 0 0 0]], shape=(4, 4), dtype=int32)
张量排序(tf.sort
, tf.argsort
)
tensor = tf.constant([3, 1, 2])
sorted_tensor = tf.sort(tensor) # 排序
indices = tf.argsort(tensor) # 排序索引
print(sorted_tensor, indices)
# 输出:tf.Tensor([1 2 3], ...), tf.Tensor([1 2 0], ...)
条件选择(tf.where
)
tensor = tf.constant([1, -2, 3, -4])
positive = tf.where(tensor > 0, tensor, 0) # 大于 0 保留,否则置 0
print(positive) # 输出:tf.Tensor([1 0 3 0], shape=(4,), dtype=int32)
7. 与 NumPy 的互操作
TensorFlow 张量可以与 NumPy 数组无缝转换:
import numpy as np
tensor = tf.constant([[1, 2], [3, 4]])
np_array = tensor.numpy() # 转为 NumPy 数组
tensor_from_np = tf.convert_to_tensor(np_array) # 从 NumPy 转回张量
8. 性能优化
- 批量操作:避免逐元素操作,使用张量运算以利用 GPU/TPU 加速。
- 数据类型:优先使用
float32
或int32
,避免float64
节省内存。 - Graph 模式:对于高性能场景,使用
tf.function
装饰器将 Python 函数转为静态图:
@tf.function
def my_func(x):
return x * x
总结
TensorFlow 的张量操作涵盖创建、形状变换、数学运算、切片、连接等,功能强大且灵活。结合 Eager Execution 和 NumPy 兼容性,操作直观且高效。常用场景包括数据预处理、模型输入准备和自定义计算。
如果你需要更详细的某类操作(如矩阵分解、稀疏张量处理)或具体示例,请告诉我!需要生成图表展示张量操作结果吗?