李沐动手学深度学习 pytorch 在pycharm中无法显示动图

学习李沐动手学深度学习过程中,发现无法显示动图,仅仅在输出栏显示以下内容:

解决方案:

1.“Ctrl+鼠标左键” 点击打开训练函数。以李沐老师的“softmax简洁实现”为例,先“Ctrl+鼠标左键”打开

d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)

中的train_ch3

2.找到“Animator”函数,

def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater):
    """Train a model (defined in Chapter 3)."""
    animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
                        legend=['train loss', 'train acc', 'test acc'])

“Ctrl+鼠标左键”点击打开“Animator”类,将def add 改为:

    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()
        display.display(self.fig)
        plt.draw()
        plt.pause(0.001)
        display.clear_output(wait=True)

其实只是改了最后几行而已。修改的时候会问你是否修改,点击修改即可。

3.修改之后会一直显示动图,不想要这个修改的话把倒数第二、第三行注释掉就行:

        self.config_axes()
        display.display(self.fig)
        # plt.draw()
        # plt.pause(0.001)
        display.clear_output(wait=True)

4.最后在训练代码末尾加上d2l.plt.show()

num_epochs = 10
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
d2l.plt.show()

你可能感兴趣的:(pycharm,python,ide,pytorch)