在 Theano/lasagne 中训练神经网络时实时情节损失
Live plot losses while training neural net in Theano/lasagne
我正在 Theano 和千层面中训练神经网络,运行 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
但我也想看看live/dynamic两次损失的情节。有内置的方法吗?
我已经尝试创建一个自定义 class 并将其添加到我的网络 on_epoch_finished
,但要么我在每次迭代时得到一个新图(我想要一个,更新),要么我必须在每次迭代时删除以前的输出,因此看不到文本输出(我想保留)。
我终于设法按照我的意愿更新了损失图,使用以下 class:
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 和千层面中训练神经网络,运行 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
但我也想看看live/dynamic两次损失的情节。有内置的方法吗?
我已经尝试创建一个自定义 class 并将其添加到我的网络 on_epoch_finished
,但要么我在每次迭代时得到一个新图(我想要一个,更新),要么我必须在每次迭代时删除以前的输出,因此看不到文本输出(我想保留)。
我终于设法按照我的意愿更新了损失图,使用以下 class:
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