# Jupyter Notebook魔法命令,用于在Notebook中内联显示图表
%matplotlib inline
# 导入NumPy库,用于高效的数值计算
import numpy as np
# 从matplotlib_inline库导入backend_inline模块,用于设置图表显示格式
from matplotlib_inline import backend_inline
# 从d2l库导入PyTorch相关工具(d2l是《动手学深度学习》配套库)
from d2l import torch as d2l
# 定义目标函数f(x) = 3x² - 4x
def f(x):
return 3 * x ** 2 - 4 * x
# 定义数值导数计算函数,使用前向差分公式
def numerical_lim(f, x, h):
return (f(x + h) - f(x)) / h
# 初始化步长h为0.1
h = 0.1
# 进行5次迭代计算
for i in range(5):
# 格式化输出当前步长和数值导数结果(保留5位小数)
print(f'h={
h:.5f}, numerical limit={
numerical_lim(f, 1, h):.5f}')
# 每次迭代将步长缩小10倍
h *= 0.1
# 定义函数:
深度学习deeplearn3
于 2025-04-03 21:20:31 首次发布

最低0.47元/天 解锁文章


被折叠的 条评论
为什么被折叠?



