====================== 代码与 API 文档示例 ====================== 本节演示如何在 RST 中展示代码块和 API 文档。 代码块 ====== Python 示例: .. code-block:: python :linenos: :caption: 训练模型示例 import torch import torch.nn as nn class SimpleNet(nn.Module): def __init__(self, num_classes=10): super().__init__() self.fc = nn.Linear(784, num_classes) def forward(self, x): return self.fc(x.view(x.size(0), -1)) Shell 示例: .. code-block:: bash $ pip install sphinx rst2pdf $ sphinx-quickstart docs/ JSON 示例: .. code-block:: json { "name": "yield-smart-app", "version": "0.1.0", "dependencies": { "streamlit": "^1.28", "pandas": "^2.0" } } API 文档 ======== 函数文档 -------- .. function:: compute_yield(data, method="linear", verbose=False) :noindex: 计算给定数据集的产量预测值。 :param data: 输入数据集,形状为 ``(N, D)`` 的数组或 DataFrame :type data: numpy.ndarray or pandas.DataFrame :param str method: 插值方法,可选 ``"linear"``、``"cubic"`` 或 ``"spline"`` :param bool verbose: 是否打印调试信息 :return: 产量预测结果 :rtype: numpy.ndarray :raises ValueError: 当 ``data`` 为空或包含非法值时抛出 类文档 ------ .. class:: DataLoader(source, batch_size=32, shuffle=True) :noindex: 数据加载器类。 .. attribute:: source :type: str 数据源路径或 URL。 .. attribute:: batch_size :type: int 每批加载的样本数量。 .. method:: __iter__() :noindex: 返回批次迭代器。 .. method:: reset() :noindex: 重置内部状态,重新打乱数据顺序(如果 ``shuffle=True``)。