PyTorch和TensorFlow是深度学习两大主流框架。 ## PyTorch ```python import torch.nn as nn class SimpleNet(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 256) self.fc2 = nn.Linear(256, 10) def forward(self, x): return self.fc2(self.fc1(x)) ``` ## TensorFlow/Keras ```python import tensorflow as tf model = tf.keras.Sequential([ tf.keras.layers.Dense(256, activation='relu'), tf.keras.layers.Dense(10, activation='softmax'), ]) ``` ## 对比 | 特性 | PyTorch | TensorFlow | |------|---------|------------| | 易用性 | Pythonic | Keras简化 | | 调试 | 原生Python | 较难 | | 部署 | TorchServe | TF Serving | | 社区 | 研究界主流 | 工业界主流 | 两个框架都在不断进化,选择取决于团队背景和项目需求。