Matplotlib 在Theano/lasagne中训练神经网络时的活图损失

Matplotlib 在Theano/lasagne中训练神经网络时的活图损失,matplotlib,ipython-notebook,theano,lasagne,Matplotlib,Ipython Notebook,Theano,Lasagne,我在Theano和lasagne中训练神经网络,在iPython笔记本上运行代码。我喜欢在每次迭代中显示列车和有效损耗,如下所示: epoch train loss valid loss train/val valid acc dur ------- ------------ ------------ ----------- ----------- ----- 1 0.53927 0.22774 2.36794

我在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
但我也希望看到两次损失的真实/动态情节。有没有一种内在的方法可以做到这一点


我曾尝试创建一个自定义类,并将其添加到我的网络的
(在_epoch_finished
)中,但要么在每次迭代时得到一个新的绘图(我想要一个,正在更新),要么在每次迭代时必须删除以前的输出,因此无法看到文本输出(我希望保留).

我终于按照自己的意愿,使用以下类更新了损失图:

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

我不熟悉theano,但在这个答案中可以找到一个实时更新matplotlib绘图的简单示例->@BasJansen是的,我在一段独立的代码中获得了我想要的,但是没有成功地用Theano/Lasagneca实现它你能复制/粘贴一段最小的代码让人们可以玩吗?@BasJansen我清理了我的代码,把它粘贴到这里,现在它工作了!