TensorFlow 高级 API - Keras

Keras 的核心概念与优势

Keras 是 TensorFlow 的高阶 API,专注于快速构建和训练深度学习模型。其设计理念强调模块化、可扩展性和用户友好性,适合从入门到生产级开发。主要优势包括:

  • 易用性:通过简洁的接口降低代码复杂度
  • 模块化:模型由可配置的模块组装而成
  • 跨平台:支持 CPU/GPU/TPU 训练,可导出多种格式

快速构建神经网络模型

以下示例展示如何用 Keras Sequential API 构建全连接网络:

from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=(784,)),
    layers.Dropout(0.2),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

功能式 API 构建复杂模型

对于多输入/输出或共享层的场景,Functional API 提供更灵活的构建方式:

inputs = tf.keras.Input(shape=(32,))
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
predictions = layers.Dense(10)(x)
model = tf.keras.Model(inputs=inputs, outputs=predictions)

模型训练与评估配置

典型的训练流程包含损失函数、优化器和指标配置:

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

history = model.fit(train_data, train_labels, 
                   epochs=10,
                   validation_split=0.2)

自定义层与损失函数

通过继承基础类实现自定义组件:

class CustomLayer(layers.Layer):
    def __init__(self, output_dim, **kwargs):
        super().__init__(**kwargs)
        self.output_dim = output_dim
    
    def build(self, input_shape):
        self.kernel = self.add_weight(
            shape=(input_shape[-1], self.output_dim),
            initializer='random_normal',
            trainable=True)
    
    def call(self, inputs):
        return tf.matmul(inputs, self.kernel)

预训练模型迁移学习

利用 Keras Applications 模块加载预训练模型:

base_model = tf.keras.applications.ResNet50(
    weights='imagenet',
    include_top=False,
    input_shape=(224, 224, 3))

# 冻结基础模型权重
base_model.trainable = False

# 添加自定义分类头
global_avg = layers.GlobalAveragePooling2D()(base_model.output)
output = layers.Dense(10, activation='softmax')(global_avg)
model = tf.keras.Model(base_model.input, output)

回调机制应用

常用回调实现训练过程控制:

callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=3),
    tf.keras.callbacks.ModelCheckpoint('model.h5'),
    tf.keras.callbacks.TensorBoard(log_dir='./logs')
]

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

模型部署与保存

多种导出格式满足不同部署需求:

# 保存完整模型
model.save('full_model.h5')

# 导出为TensorFlow Serving格式
tf.saved_model.save(model, 'saved_model')

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

性能优化技巧

提升训练效率的实用方法:

  • 使用 tf.data 构建高效数据管道
  • 启用混合精度训练
  • 利用分布式训练策略
# 混合精度配置
policy = tf.keras.mixed_precision.Policy('mixed_float16')
tf.keras.mixed_precision.set_global_policy(policy)

# 分布式训练
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
    model = build_model()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值