我在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
,但要么我在每次迭代时得到一个新的情节(我喜欢单一的,更新),或者我必须在每次迭代时擦除先前的输出,因此无法看到文本输出(我想保留)。
答案 0 :(得分: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