TensorFlow基础详解
TensorFlow基础详解
TensorFlow是Google开发的深度学习框架,提供灵活的API和强大的分布式计算能力。
TensorFlow基础
张量操作
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 创建张量
x = tf.constant([1, 2, 3, 4, 5])
print(f"一维张量: {x}")
print(f"张量形状: {x.shape}")
print(f"数据类型: {x.dtype}")
# 创建二维张量
y = tf.constant([[1, 2, 3], [4, 5, 6]])
print(f"\n二维张量:\n{y}")
print(f"张量形状: {y.shape}")
# 创建特殊张量
zeros = tf.zeros((3, 3))
ones = tf.ones((2, 4))
rand_tensor = tf.random.normal((3, 3))
print(f"\n零张量:\n{zeros}")
print(f"\n随机张量:\n{rand_tensor}")
张量运算
# 基本运算
a = tf.constant([1, 2, 3, 4])
b = tf.constant([5, 6, 7, 8])
print(f"加法: {a + b}")
print(f"减法: {a - b}")
print(f"乘法: {a * b}")
print(f"除法: {a / b}")
print(f"幂运算: {a ** 2}")
# 数学函数
print(f"\n平方根: {tf.sqrt(tf.cast(a, tf.float32))}")
print(f"指数: {tf.exp(tf.cast(a, tf.float32))}")
print(f"对数: {tf.math.log(tf.cast(a, tf.float32))}")
print(f"正弦: {tf.sin(tf.cast(a, tf.float32))}")
# 聚合函数
print(f"\n求和: {tf.reduce_sum(a)}")
print(f"均值: {tf.reduce_mean(tf.cast(a, tf.float32))}")
print(f"最大值: {tf.reduce_max(a)}")
print(f"最小值: {tf.reduce_min(a)}")
自动微分
# 自动微分(GradientTape)
x = tf.Variable([2.0, 3.0])
with tf.GradientTape() as tape:
y = x ** 2 + 2 * x + 1
print(f"输入 x: {x.numpy()}")
print(f"输出 y: {y.numpy()}")
# 计算梯度
dy_dx = tape.gradient(y, x)
print(f"梯度 dy/dx: {dy_dx.numpy()}")
Keras API
使用Sequential API
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# 使用Sequential API构建模型
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(10,)),
layers.Dropout(0.2),
layers.Dense(32, activation='relu'),
layers.Dropout(0.2),
layers.Dense(2, activation='softmax')
])
# 模型摘要
model.summary()
使用Functional API
# 使用Functional API构建模型
inputs = keras.Input(shape=(10,))
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dropout(0.2)(x)
x = layers.Dense(32, activation='relu')(x)
x = layers.Dropout(0.2)(x)
outputs = layers.Dense(2, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.summary()
模型编译和训练
# 生成示例数据
np.random.seed(42)
X = np.random.randn(1000, 10).astype(np.float32)
y = np.random.randint(0, 2, 1000)
# 划分数据集
X_train, X_test = X[:800], X[800:]
y_train, y_test = y[:800], y[800:]
# 编译模型
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# 训练模型
print("训练模型:")
history = model.fit(
X_train, y_train,
epochs=10,
batch_size=32,
validation_split=0.2,
verbose=1
)
# 评估模型
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=0)
print(f"\n测试损失: {test_loss:.4f}")
print(f"测试准确率: {test_acc:.4f}")
模型构建
常用层
# 卷积层
conv_layer = layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1))
print(f"卷积层参数: {conv_layer.count_params()}")
# 池化层
pool_layer = layers.MaxPooling2D((2, 2))
# 循环层
lstm_layer = layers.LSTM(32, return_sequences=True)
print(f"LSTM层参数: {lstm_layer.count_params()}")
# 嵌入层
embedding_layer = layers.Embedding(input_dim=1000, output_dim=64)
print(f"嵌入层参数: {embedding_layer.count_params()}")
# 批归一化
bn_layer = layers.BatchNormalization()
print(f"批归一化层参数: {bn_layer.count_params()}")
自定义层
# 自定义层
class CustomDense(layers.Layer):
def __init__(self, units, **kwargs):
super(CustomDense, self).__init__(**kwargs)
self.units = units
def build(self, input_shape):
self.w = self.add_weight(
shape=(input_shape[-1], self.units),
initializer='glorot_uniform',
trainable=True
)
self.b = self.add_weight(
shape=(self.units,),
initializer='zeros',
trainable=True
)
def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
def get_config(self):
config = super(CustomDense, self).get_config()
config.update({'units': self.units})
return config
# 使用自定义层
custom_model = keras.Sequential([
CustomDense(64, input_shape=(10,)),
layers.Activation('relu'),
CustomDense(2)
])
custom_model.summary()
回调函数
常用回调函数
# 回调函数
callbacks = [
# 早停
keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True
),
# 学习率调度
keras.callbacks.ReduceLROnPlateau(
monitor='val_loss',
factor=0.2,
patience=2,
min_lr=1e-6
),
# 模型检查点
keras.callbacks.ModelCheckpoint(
filepath='best_model.keras',
monitor='val_loss',
save_best_only=True
),
# TensorBoard日志
keras.callbacks.TensorBoard(
log_dir='./logs',
histogram_freq=1
)
]
# 使用回调函数训练
print("使用回调函数训练:")
history = model.fit(
X_train, y_train,
epochs=20,
batch_size=32,
validation_split=0.2,
callbacks=callbacks,
verbose=1
)
可视化训练过程
# 可视化训练历史
def plot_history(history):
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
# 损失曲线
ax1.plot(history.history['loss'], label='训练损失')
ax1.plot(history.history['val_loss'], label='验证损失')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('损失')
ax1.set_title('损失曲线')
ax1.legend()
ax1.grid(True, alpha=0.3)
# 准确率曲线
ax2.plot(history.history['accuracy'], label='训练准确率')
ax2.plot(history.history['val_accuracy'], label='验证准确率')
ax2.set_xlabel('Epoch')
ax2.set_ylabel('准确率')
ax2.set_title('准确率曲线')
ax2.legend()
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
plot_history(history)
实际应用
图像分类
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
# 构建CNN模型
model = keras.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
# 编译模型
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.summary()
# 训练模型
print("\n训练MNIST分类器:")
history = model.fit(
x_train, y_train,
epochs=10,
batch_size=64,
validation_split=0.1,
verbose=1
)
# 评估模型
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
print(f"\n测试准确率: {test_acc:.4f}")
# 可视化
plot_history(history)
文本分类
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
# 加载IMDB数据集
vocab_size = 10000
max_length = 200
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
# 填充序列
x_train = pad_sequences(x_train, maxlen=max_length)
x_test = pad_sequences(x_test, maxlen=max_length)
# 构建文本分类模型
model = keras.Sequential([
layers.Embedding(vocab_size, 128, input_length=max_length),
layers.LSTM(64, return_sequences=True),
layers.LSTM(32),
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(1, activation='sigmoid')
])
# 编译模型
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
model.summary()
# 训练模型
print("\n训练文本分类器:")
history = model.fit(
x_train, y_train,
epochs=5,
batch_size=64,
validation_split=0.2,
verbose=1
)
# 评估模型
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
print(f"\n测试准确率: {test_acc:.4f}")
# 可视化
plot_history(history)
保存和加载模型
# 保存模型
model.save('my_model.keras')
print("模型已保存")
# 加载模型
loaded_model = keras.models.load_model('my_model.keras')
print("模型已加载")
# 保存权重
model.save_weights('model_weights.weights.h5')
print("权重已保存")
# 加载权重
model.load_weights('model_weights.weights.h5')
print("权重已加载")
TensorFlow最佳实践
- 使用Keras API:高级API简化开发
- 数据管道:使用tf.data构建高效数据管道
- 分布式训练:使用MultiWorkerMirroredStrategy进行分布式训练
- 模型优化:使用TensorFlow Lite和TensorFlow.js部署模型
- 监控工具:使用TensorBoard监控训练过程
TensorFlow是工业级深度学习框架,掌握TensorFlow对于构建和部署大规模深度学习应用至关重要。