【问题标题】:Live plot losses while training neural net in Theano/lasagne在 Theano/lasagne 中训练神经网络时的实时图损失
【发布时间】:2015-08-18 09:34:34
【问题描述】:

我正在 Theano 和 lasagne 中训练神经网络,并在 iPython 笔记本中运行代码。我喜欢在每次迭代时显示训练和有效损失,如下所示:

epoch    train loss    valid loss    train/val    valid acc  dur
-------  ------------  ------------  -----------  -----------  -----
  1       0.53927       0.22774      2.36794      0.93296  5.45s
  2       0.28789       0.16561      1.73840      0.95033  5.40s

但我也想看看这两个损失的实时/动态图。有内置的方法吗?

我已经尝试创建一个自定义类并将其添加到我的网络on_epoch_finished,但要么我在每次迭代中得到一个新图(我想要一个,更新),要么我必须删除以前的输出在每次迭代中,因此看不到文本输出(我想保留)。

【问题讨论】:

  • 我不熟悉 theano,但可以在这个答案中找到实时更新 matplotlib 图的简单示例 -> stackoverflow.com/a/16446688/1093485
  • @BasJansen 是的,我设法在一段孤立的代码中获得了我想要的东西,但没有成功用 Theano/lasagne 实现它
  • 你能复制/粘贴一小段代码,以便人们可以玩弄它吗?
  • @BasJansen 我清理了我的代码并粘贴到这里,现在它可以工作了!

标签: matplotlib ipython-notebook theano lasagne


【解决方案1】:

我终于设法使用以下类更新了损失图:

from IPython import display
from matplotlib import pyplot as plt
import numpy as np
from lasagne import layers
from lasagne.updates import nesterov_momentum
from nolearn.lasagne import NeuralNet
from lasagne import nonlinearities

class PlotLosses(object):
    def __init__(self, figsize=(8,6)):
        plt.plot([], []) 

    def __call__(self, nn, train_history):
        train_loss = np.array([i["train_loss"] for i in nn.train_history_])
        valid_loss = np.array([i["valid_loss"] for i in nn.train_history_])

        plt.gca().cla()
        plt.plot(train_loss, label="train") 
        plt.plot(valid_loss, label="test")

        plt.legend()
        plt.draw()

以及要重现的代码示例:

net_SO = NeuralNet(
    layers=[(layers.InputLayer, {"name": 'input', 'shape': (None, 1, 28, 28)}),
            (layers.Conv2DLayer, {"name": 'conv1', 'filter_size': (3,3,), 'num_filters': 5}),
            (layers.DropoutLayer, {'name': 'dropout1', 'p': 0.2}),
            (layers.DenseLayer, {"name": 'hidden1', 'num_units': 50}),
            (layers.DropoutLayer, {'name': 'dropout2', 'p': 0.2}),
            (layers.DenseLayer, {"name": 'output', 'nonlinearity': nonlinearities.softmax, 'num_units': 10})],
    # optimization method:
    update=nesterov_momentum,
    update_learning_rate=10**(-2),
    update_momentum=0.9,

    regression=False,  
    max_epochs=200, 
    verbose=1,

    on_epoch_finished=[PlotLosses(figsize=(8,6))], #this is the important line
    )

net_SO.fit(X, y) #X and y from the MNIST dataset

【讨论】:

    猜你喜欢
    • 2016-07-21
    • 2021-12-12
    • 2020-10-17
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多