模型定义方式
TensorFlow 提供两种主要模型定义方式:
Sequential API
适合堆叠层的线性模型
📌 示例代码:model = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu', input_shape=(32,)), tf.keras.layers.Dense(10, activation='softmax') ])
Functional API
支持复杂拓扑结构(如多输入/输出)
📌 示例代码:inputs = tf.keras.Input(shape=(32,)) x = tf.keras.layers.Dense(64, activation='relu')(inputs) outputs = tf.keras.layers.Dense(10, activation='softmax')(x) model = tf.keras.Model(inputs=inputs, outputs=outputs)
核心组件
模型通常包含以下结构元素:
输入层
定义数据输入形状
📎 输入层设计指南隐藏层
- 全连接层 (
Dense
) - 卷积层 (
Conv2D
) - 循环层 (
LSTM
) - 注意力机制 (
Attention
)
- 全连接层 (
输出层
- 分类任务:使用
Softmax
激活函数 - 回归任务:使用
Linear
激活函数 - 二分类任务:使用
Sigmoid
激活函数
- 分类任务:使用
编译配置
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
模型可视化
使用TensorBoard可视化模型结构:
拓扑结构示例
复杂模型结构可包含:
- 多输入:
model.inputs = [input1, input2]
- 多输出:
model.outputs = [output1, output2]
- 路径共享:
Model
对象支持多路径设计
需要进一步了解模型结构优化技巧?点击此处查看进阶教程