JoyBeanRobber

导航

李沐动手学深度学习4——微积分

李沐动手学深度学习,有视频、有中文电子书(含配套代码),推荐!

视频(B站搜一大堆):https://www.bilibili.com/video/BV1fsmyYnEfw?spm_id_from=333.788.videopod.episodes&vd_source=0199f117e2bdbb60970034d4f33ff67d

官网电子书:https://zh.d2l.ai/index.html

画图直接用matplotlib库实现,没什么知识点,代码如下:

 

import matplotlib.pyplot as plt
import numpy as np


def f(x):
    return 3 * x**2 - 4 * x


def lim(f, x, h):
    return(f(x+h) - f(x)) / h


h = 0.1
for i in range(5):
    print(f'h={h:.5f}, limit={lim(f, 1, h):.5f}')
    h *= 0.1

fig, ax = plt.subplots(figsize=(12, 8))
x = np.linspace(0, 3, 20)
ax.plot(x, f(x), label='f(x)', color='blue', linestyle='-')
ax.plot(x, 2*x-3, label='Tangent line (x=1)', color='purple', linestyle='--')
ax.legend(fontsize=12)
ax.grid(True, linestyle=':')
ax.set_xlabel("x", fontsize=15)
ax.set_ylabel("f(x)", fontsize=15)
plt.show()

 

posted on 2025-04-16 15:52  欢乐豆掠夺者  阅读(21)  评论(0)    收藏  举报