Miraclelucy / dive_into_deep_learning

✔️李沐 【动手学深度学习】课程学习笔记:使用pycharm编程,基于pytorch框架实现。
2.4k stars 451 forks source link

提出另一种实现pycharm的方法,可直接使用原课程代码 #2

Closed Li-4 closed 2 years ago

Li-4 commented 2 years ago

尝试更改了 Animator 累加器的 add 方法实现使用 Matplotlib 输出图表 代码如下

    def add(self, x, y):
        # Add multiple data points into the figure
        if not hasattr(y, "__len__"):
            y = [y]
        n = len(y)
        if not hasattr(x, "__len__"):
            x = [x] * n
        if not self.X:
            self.X = [[] for _ in range(n)]
        if not self.Y:
            self.Y = [[] for _ in range(n)]
        for i, (a, b) in enumerate(zip(x, y)):
            if a is not None and b is not None:
                self.X[i].append(a)
                self.Y[i].append(b)
        self.axes[0].cla()
        for x, y, fmt in zip(self.X, self.Y, self.fmts):
            self.axes[0].plot(x, y, fmt)
        self.config_axes()
        d2l.plt.pause(0.01)

主要添加了 d2l.plt.pause(0.01) 来显示图片,我在权重衰减从零实现中测试正常

Li-4 commented 2 years ago

要在具体的程序(不是d2l包)中多加一句

d2l.plt.show()

这样可以保证画图结束后,不会自动关闭 Matplotlib 窗口,若是 Pycharm 的 SciView 显示就不用了