跳转至

TensorFlow 入门教程

概述

TensorFlow是Google开发的深度学习框架,以静态计算图和生产部署能力著称,广泛应用于工业界。

安装配置

# 安装TensorFlow
pip install tensorflow

# GPU版本
pip install tensorflow[and-cuda]

# 验证安装
python -c "import tensorflow as tf; print(tf.__version__); print(tf.config.list_physical_devices('GPU'))"

张量基础

创建张量

import tensorflow as tf

# 从Python对象创建
t1 = tf.constant([[1, 2, 3], [4, 5, 6]])
t2 = tf.zeros([3, 3])
t3 = tf.ones([2, 4])
t4 = tf.random.normal([3, 3], mean=0.0, stddev=1.0)

# 从NumPy创建
import numpy as np
a = np.array([1, 2, 3])
t5 = tf.convert_to_tensor(a)

张量操作

# 基本运算
a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])

c = a + b           # 加法
d = a * b           # 乘法
e = tf.reduce_sum(a)  # 求和

# 矩阵运算
mat1 = tf.random.normal([3, 4])
mat2 = tf.random.normal([4, 5])
result = tf.matmul(mat1, mat2)  # 矩阵乘法

# 形状操作
x = tf.random.normal([2, 3, 4])
y = tf.reshape(x, [2, 12])  # 重塑
z = tf.transpose(x)          # 转置

GPU加速

# 检查GPU
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

# 放置在GPU
with tf.device('/GPU:0'):
    a = tf.random.normal([1000, 1000])
    b = tf.random.normal([1000, 1000])
    c = tf.matmul(a, b)

自动微分

GradientTape

# 基本梯度计算
x = tf.Variable(3.0)

with tf.GradientTape() as tape:
    y = x ** 2 + 2 * x + 1

dy_dx = tape.gradient(y, x)
print(dy_dx)  # tf.Tensor(8.0, shape=(), dtype=float32)

多变量梯度

x = tf.Variable([1.0, 2.0, 3.0])
w = tf.Variable([0.5, 0.5, 0.5])

with tf.GradientTape() as tape:
    y = tf.reduce_sum(x * w) + 2

dy_dx, dy_dw = tape.gradient(y, [x, w])
print(dy_dx)  # [0.5, 0.5, 0.5]
print(dy_dw)  # [1., 2., 3.]

Keras API

Sequential模型

import tensorflow as tf
from tensorflow import keras

# 创建模型
model = keras.Sequential([
    keras.layers.Dense(128, activation='relu', input_shape=(784,)),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation='softmax')
])

# 查看模型
model.summary()

Functional API

# 函数式API,支持复杂结构
inputs = keras.Input(shape=(784,))
x = keras.layers.Dense(128, activation='relu')(inputs)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Dense(64, activation='relu')(x)
outputs = keras.layers.Dense(10, activation='softmax')(x)

model = keras.Model(inputs=inputs, outputs=outputs)

常用层

# 全连接层
dense = keras.layers.Dense(64, activation='relu')

# 卷积层
conv2d = keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')

# 池化层
pool = keras.layers.MaxPooling2D((2, 2))

# Dropout
dropout = keras.layers.Dropout(0.5)

# BatchNorm
batchnorm = keras.layers.BatchNormalization()

# Flatten
flatten = keras.layers.Flatten()

CNN示例

def create_cnn():
    model = keras.Sequential([
        # 卷积块1
        keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        keras.layers.MaxPooling2D((2, 2)),

        # 卷积块2
        keras.layers.Conv2D(64, (3, 3), activation='relu'),
        keras.layers.MaxPooling2D((2, 2)),

        # 全连接层
        keras.layers.Flatten(),
        keras.layers.Dense(64, activation='relu'),
        keras.layers.Dropout(0.5),
        keras.layers.Dense(10, activation='softmax')
    ])
    return model

RNN/LSTM示例

# LSTM模型
model = keras.Sequential([
    keras.layers.Embedding(10000, 128),
    keras.layers.LSTM(64, return_sequences=True),
    keras.layers.LSTM(32),
    keras.layers.Dense(10, activation='softmax')
])

# 双向LSTM
model = keras.Sequential([
    keras.layers.Embedding(10000, 128),
    keras.layers.Bidirectional(keras.layers.LSTM(64)),
    keras.layers.Dense(10, activation='softmax')
])

数据处理

tf.data API

# 创建数据集
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))

# 数据处理管道
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(32)
dataset = dataset.prefetch(tf.data.AUTOTUNE)

# 使用预构建数据集
mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

数据增强

data_augmentation = keras.Sequential([
    keras.layers.RandomFlip("horizontal"),
    keras.layers.RandomRotation(0.1),
    keras.layers.RandomZoom(0.1),
    keras.layers.RandomContrast(0.1),
])

# 在模型中使用
model = keras.Sequential([
    data_augmentation,
    keras.layers.Conv2D(32, 3, activation='relu'),
    # ...
])

模型训练

编译模型

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# 自定义优化器
optimizer = keras.optimizers.Adam(learning_rate=0.001)
model.compile(
    optimizer=optimizer,
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

训练模型

# 基本训练
history = model.fit(
    x_train, y_train,
    epochs=10,
    batch_size=32,
    validation_split=0.2
)

# 使用数据集
history = model.fit(
    train_dataset,
    epochs=10,
    validation_data=test_dataset
)

回调函数

callbacks = [
    # 保存最佳模型
    keras.callbacks.ModelCheckpoint(
        'best_model.keras',
        save_best_only=True,
        monitor='val_accuracy'
    ),
    # 早停
    keras.callbacks.EarlyStopping(
        monitor='val_loss',
        patience=5,
        restore_best_weights=True
    ),
    # 学习率调度
    keras.callbacks.ReduceLROnPlateau(
        monitor='val_loss',
        factor=0.2,
        patience=2
    ),
    # TensorBoard
    keras.callbacks.TensorBoard(log_dir='./logs')
]

history = model.fit(
    x_train, y_train,
    epochs=50,
    validation_split=0.2,
    callbacks=callbacks
)

模型评估与预测

# 评估模型
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc:.4f}')

# 预测
predictions = model.predict(x_test)
predicted_classes = tf.argmax(predictions, axis=1)

# 混淆矩阵
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, predicted_classes)

模型保存与加载

# 保存整个模型
model.save('my_model.keras')

# 加载模型
loaded_model = keras.models.load_model('my_model.keras')

# 保存权重
model.save_weights('model_weights.weights.h5')

# 加载权重
model.load_weights('model_weights.weights.h5')

# 保存为SavedModel格式(TensorFlow Serving)
tf.saved_model.save(model, 'saved_model/1')

TensorFlow Lite(移动端部署)

# 转换为TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# 保存
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

# 加载和推理
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])

实战示例:MNIST手写数字识别

import tensorflow as tf
from tensorflow import keras

# 加载数据
(x_train, y_train), (x_test, y_test) = keras.datasets.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

# 创建模型
model = keras.Sequential([
    keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)),
    keras.layers.MaxPooling2D(2),
    keras.layers.Conv2D(64, 3, activation='relu'),
    keras.layers.MaxPooling2D(2),
    keras.layers.Flatten(),
    keras.layers.Dropout(0.5),
    keras.layers.Dense(10, activation='softmax')
])

# 编译和训练
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

history = model.fit(
    x_train, y_train,
    epochs=10,
    validation_split=0.1,
    batch_size=32
)

# 评估
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc:.4f}')

TensorFlow vs PyTorch

特性 TensorFlow PyTorch
计算图 静态(2.x动态) 动态
API风格 Keras(高级) nn.Module
部署 TF Serving, TFLite TorchServe
移动端 TFLite PyTorch Mobile
学习曲线 较平缓 略陡
工业应用 广泛 增长中
学术研究 多 更多
# 选择建议

选择TensorFlow:
- 需要生产部署
- 移动端/嵌入式应用
- 已有TF生态项目

选择PyTorch:
- 学术研究
- 快速原型开发
- 动态图需求

学习资源

# 官方资源
- TensorFlow官网:https://www.tensorflow.org
- TensorFlow教程:https://www.tensorflow.org/tutorials
- TensorFlow Hub:https://tfhub.dev

# 推荐课程
- DeepLearning.AI TensorFlow Developer Certificate
- Stanford CS20 TensorFlow

下一步学习

  1. PyTorch入门教程 - 对比学习另一个框架
  2. 深度学习教程 - 深入理论知识
  3. AI Agent智能体教程 - 构建智能应用