在Visdom中具有多个绘制线的一个窗口

时间:2019-03-03 15:13:09

标签: data-visualization visualization visdom

我试图在Visdom的单个窗口中绘制训练和验证损失。我原本希望这样做会更加顺利,但是我无法弄清楚。

下面的代码为每一行生成两个单独的窗口。我想将它们绘制在同一张图上,最好以两种单独的颜色绘制,并带有说明哪个是图例的图例。我已经浏览了文档,但是即使我认为这是一个非常常见的情况,也找不到解决方案?

class Visualizations:
    def __init__(self, env_name=None):
        if env_name is None:
            env_name = time.strftime("%d-%b %H:%M")
        self.env_name = env_name
        self.vis = visdom.Visdom(env=self.env_name)
        self.train_loss = None
        self.valid_loss = None

    def plot_loss(self, train_loss, epoch, valid_loss=None):
        self.train_loss = self.vis.line(
            [train_loss],
            [epoch],
            win=self.train_loss,
            update='append' if self.train_loss else None,
            opts=dict(
                xlabel='Epoch',
                ylabel='Loss',
                title='Mean training loss per epoch',
            )
        )
        self.valid_loss = self.vis.line(
            [valid_loss],
            [epoch],
            win=self.valid_loss,
            update='append' if self.valid_loss else None,
            opts=dict(
                xlabel='Epoch',
                ylabel='Loss',
                title='Mean validation loss per epoch',
            )
        )

您可以像这样测试它:

import random
vis = Visualizations()
for i in range(10):
    vis.plot_loss(random.uniform(0, 1), i, random.uniform(0, 1))

0 个答案:

没有答案