1. 张量 concepts
张量 (Tensor) is TensorFlowin最basic datastructure, class似于NumPyin array, 但具 has 更强 big functions, such asGPU加速, 自动微分etc.. 张量可以看作 is many 维array, 根据维度不同, 可以分 for 以 under 几种class型:
- 标量 (0阶张量) : 单个数值, such as
5or3.14 - 向量 (1阶张量) : 一维array, such as
[1, 2, 3] - 矩阵 (2阶张量) : 二维array, such as
[[1, 2], [3, 4]] - high 阶张量: 三维及以 on array, such as三维张量可以表示彩色graph像 ( high 度×宽度×通道)
提示
in TensorFlowin, 张量 is 不可变 (Immutable) , 这意味着一旦creation, 就不能直接modify其值. 所 has operation都会返回一个 new 张量.
2. creation张量
TensorFlowproviding了 many 种creation张量 method, 以 under is 常用 几种:
2.1 using常量creation张量
using tf.constant() function可以creation常量张量:
import tensorflow as tf
# creation标量
a = tf.constant(5)
print("标量:", a)
print("形状:", a.shape)
print("dataclass型:", a.dtype)
# creation向量
b = tf.constant([1, 2, 3])
print("\n向量:", b)
print("形状:", b.shape)
# creation矩阵
c = tf.constant([[1, 2], [3, 4]])
print("\n矩阵:", c)
print("形状:", c.shape)
# creation三维张量
d = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("\n三维张量:", d)
print("形状:", d.shape)
2.2 using特殊值creation张量
TensorFlowproviding了creation特殊值张量 function:
# creation全零张量
zeros = tf.zeros([2, 3])
print("全零张量:", zeros)
# creation全一张量
ones = tf.ones([3, 2])
print("\n全一张量:", ones)
# creation填充张量
filled = tf.fill([2, 2], 5)
print("\n填充张量:", filled)
# creation随机张量
# 均匀分布随机张量
uniform = tf.random.uniform([2, 3], minval=0, maxval=1)
print("\n均匀分布随机张量:", uniform)
# 正态分布随机张量
normal = tf.random.normal([2, 3], mean=0, stddev=1)
print("\n正态分布随机张量:", normal)
2.3 from NumPyarraycreation张量
可以using tf.convert_to_tensor() function将NumPyarray转换 for TensorFlow张量:
import numpy as np
# creationNumPyarray
np_array = np.array([[1, 2], [3, 4]])
# 转换 for TensorFlow张量
tf_tensor = tf.convert_to_tensor(np_array)
print("NumPyarray:", np_array)
print("TensorFlow张量:", tf_tensor)
print("class型转换:", type(np_array), "->", type(tf_tensor))
3. 张量 property
每个张量都 has 以 under important property:
- 形状 (shape) : 张量 维度 and 每个维度 big small
- dataclass型 (dtype) : 张量in元素 dataclass型, such as
tf.float32,tf.int32etc. - 设备 (device) : 张量where 设备, such as CPU or GPU
tensor = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
print("张量:", tensor)
print("形状:", tensor.shape)
print("dataclass型:", tensor.dtype)
print("设备:", tensor.device)
4. 张量 basicoperation
TensorFlowproviding了丰富 张量operation, including数学运算, 形状变换, index and 切片etc..
4.1 数学运算
TensorFlowsupport所 has basic 数学运算:
a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])
# 加法
add = tf.add(a, b)
# or 直接using + 运算符
add_op = a + b
print("加法:", add, add_op)
# 减法
sub = tf.subtract(a, b)
# or 直接using - 运算符
sub_op = a - b
print("\n减法:", sub, sub_op)
# 乘法 (元素级)
mul = tf.multiply(a, b)
# or 直接using * 运算符
mul_op = a * b
print("\n乘法 (元素级) :", mul, mul_op)
# 除法
div = tf.divide(a, b)
# or 直接using / 运算符
div_op = a / b
print("\n除法:", div, div_op)
# 矩阵乘法
matrix_a = tf.constant([[1, 2], [3, 4]])
matrix_b = tf.constant([[5, 6], [7, 8]])
matmul = tf.matmul(matrix_a, matrix_b)
# or using @ 运算符
matmul_op = matrix_a @ matrix_b
print("\n矩阵乘法:", matmul, matmul_op)
4.2 形状变换
形状变换 is 张量operationin常用 operation, including重塑, 转置, scale维度etc.:
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
print("原张量:", tensor)
print("原形状:", tensor.shape)
# 重塑张量
reshaped = tf.reshape(tensor, [3, 2])
print("\n重塑 after :", reshaped)
print("形状:", reshaped.shape)
# 转置张量
transposed = tf.transpose(tensor)
print("\n转置 after :", transposed)
print("形状:", transposed.shape)
# scale维度
expanded = tf.expand_dims(tensor, axis=0)
print("\nscale维度 after :", expanded)
print("形状:", expanded.shape)
# 压缩维度
compressed = tf.squeeze(expanded, axis=0)
print("\n压缩维度 after :", compressed)
print("形状:", compressed.shape)
4.3 index and 切片
张量 index and 切片 and NumPyarrayclass似:
tensor = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("原张量:", tensor)
# 获取单个元素
element = tensor[0, 1]
print("\n获取单个元素 [0, 1]:", element)
# 切片operation
# 获取第一行
row = tensor[0, :]
print("\n获取第一行:", row)
# 获取第一列
col = tensor[:, 0]
print("\n获取第一列:", col)
# 获取子张量
sub_tensor = tensor[0:2, 1:3]
print("\n获取子张量 [0:2, 1:3]:", sub_tensor)
# using步 long
step = tensor[::2, ::2]
print("\nusing步 long [::2, ::2]:", step)
4.4 aggregateoperation
TensorFlowproviding了各种aggregateoperation:
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
print("原张量:", tensor)
# 求 and
sum_all = tf.reduce_sum(tensor)
print("\n求 and (所 has 元素) :", sum_all)
# 按行求 and
sum_rows = tf.reduce_sum(tensor, axis=0)
print("按行求 and :", sum_rows)
# 按列求 and
sum_cols = tf.reduce_sum(tensor, axis=1)
print("按列求 and :", sum_cols)
# 求平均值
mean_all = tf.reduce_mean(tensor)
print("\n平均值 (所 has 元素) :", mean_all)
# 求最 big 值
max_all = tf.reduce_max(tensor)
print("\n最 big 值 (所 has 元素) :", max_all)
# 求最 small 值
min_all = tf.reduce_min(tensor)
print("\n最 small 值 (所 has 元素) :", min_all)
# 求标准差
std = tf.math.reduce_std(tf.cast(tensor, tf.float32))
print("\n标准差:", std)
5. 张量 dataclass型转换
TensorFlowsupport不同dataclass型之间 转换:
tensor = tf.constant([1, 2, 3], dtype=tf.int32)
print("原张量:", tensor)
print("原dataclass型:", tensor.dtype)
# 转换 for float32
tensor_float = tf.cast(tensor, tf.float32)
print("\n转换 for float32:", tensor_float)
print("dataclass型:", tensor_float.dtype)
# 转换 for bool
tensor_bool = tf.cast(tensor, tf.bool)
print("\n转换 for bool:", tensor_bool)
print("dataclass型:", tensor_bool.dtype)
6. variable and 常量
in TensorFlowin, has 两种主要 张量class型:
- 常量 (Constant) : 值不可变 张量, using
tf.constant()creation - variable (Variable) : 值可变 张量, using
tf.Variable()creation, 常用于modelparameter
# creation常量
const = tf.constant([1, 2, 3])
print("常量:", const)
# creationvariable
var = tf.Variable([1, 2, 3])
print("\nvariable:", var)
# modifyvariable值
var.assign([4, 5, 6])
print("\nmodifyvariable after :", var)
# 增加variable值
var.assign_add([1, 1, 1])
print("\n增加variable after :", var)
# reducingvariable值
var.assign_sub([2, 2, 2])
print("\nreducingvariable after :", var)
提示
variable is model训练 core, 因 for model 权重 and 偏置需要 in 训练过程incontinuouslyupdate.
7. 张量 and NumPy互operation
TensorFlow张量 and NumPyarray可以方便地for互operation:
# TensorFlow张量转NumPyarray
tf_tensor = tf.constant([[1, 2], [3, 4]])
np_array = tf_tensor.numpy()
print("TensorFlow张量:", tf_tensor)
print("转换 for NumPyarray:", np_array)
print("class型:", type(np_array))
# NumPyarray转TensorFlow张量
np_array = np.array([[5, 6], [7, 8]])
tf_tensor = tf.convert_to_tensor(np_array)
print("\nNumPyarray:", np_array)
print("转换 for TensorFlow张量:", tf_tensor)
print("class型:", type(tf_tensor))
8. 自动微分mechanism
TensorFlow 自动微分mechanism is 其corefeatures之一, 用于计算张量operation 梯度:
# creationvariable
tx = tf.Variable(3.0)
tf.GradientTape() as tape:
y = tx ** 2
# 计算梯度
grad = tape.gradient(y, tx)
print("y = tx ** 2, tx = 3.0")
print("dy/dtx =", grad)
提示
自动微分 is 深度Learningmodel训练 Basics, 它允许TensorFlow自动计算损失function相 for 于modelparameter 梯度, from 而implementation梯度 under 降optimization.
9. 练习
练习 1: creation张量
- creation一个形状 for (3, 4) 全零张量
- creation一个形状 for (2, 3) 全一张量
- creation一个形状 for (2, 2, 2) 随机张量, 元素值 in 0 to 10 之间 整数
- 将 NumPy array
np.array([[1.0, 2.0], [3.0, 4.0]])转换 for TensorFlow 张量
练习 2: 张量operation
- creation两个形状 for (2, 3) and (3, 2) 随机张量, for矩阵乘法
- creation一个形状 for (4, 4) 随机张量, 计算其每行 and , 每列 平均值
- creation一个形状 for (3, 3) 随机张量, 获取其 for 角线元素
- creation一个形状 for (1, 4, 4) 张量, using squeeze operation去除维度 for 1 维度
练习 3: variableoperation
- creation一个形状 for (2, 2) variable, 初始值 for 全 1
- 将variable 所 has 元素增加 2
- 将variable 第一行设置 for [5, 5]
- 计算variable 行列式