Keras,从回调附加到日志 [英] Keras, append to logs from callback
问题描述
我有一个回调函数,它在 on_epoch_end
中为验证数据和每 10 个时期为测试数据计算几个额外的指标.
I have a callback that computes a couple of additional metrics in on_epoch_end
for validation data and every 10 epochs for test data.
我还有一个 CSVLogger
回调,可以将正常指标保存到日志文件中.
I also have a CSVLogger
callback that saves normal metrics to a log file.
是否有一种简单的方法可以从我的回调中将一两列添加到 CSVLogger
正确写入的日志中?
Is there an easy way from my callback to add a column or two to the logs that gets properly written by CSVLogger
?
推荐答案
您可以将其他指标插入字典 logs
.
You can insert your additional metrics into the dictionary logs
.
from keras.callbacks import Callback
class ComputeMetrics(Callback):
def on_epoch_end(self, epoch, logs):
logs['val_metric'] = epoch ** 2 # replace it with your metrics
if (epoch + 1) % 10 == 0:
logs['test_metric'] = epoch ** 3 # same
else:
logs['test_metric'] = np.nan
只要记住在 fit
调用中将这个回调放在 CSVLogger
之前.稍后出现在列表中的回调将收到 logs
的修改版本.例如,
Just remember to place this callback before CSVLogger
in your fit
call. Callbacks that appear later in the list would receive a modified version of logs
. For example,
model = Sequential([Dense(1, input_shape=(10,))])
model.compile(loss='mse', optimizer='adam')
model.fit(np.random.rand(100, 10),
np.random.rand(100),
epochs=30,
validation_data=(np.random.rand(100, 10), np.random.rand(100)),
callbacks=[ComputeMetrics(), CSVLogger('1.log')])
现在,如果您查看输出日志文件,您将看到另外两列 test_metric
和 val_metric
:
Now if you take a look at the output log file, you'll see two additional columns test_metric
and val_metric
:
epoch,loss,test_metric,val_loss,val_metric
0,0.547923130989,nan,0.370979120433,0
1,0.525437340736,nan,0.35585285902,1
2,0.501358469725,nan,0.341958616376,4
3,0.479624577463,nan,0.329370084703,9
4,0.460121934414,nan,0.317930338383,16
5,0.440655426979,nan,0.307486981452,25
6,0.422990380526,nan,0.298160370588,36
7,0.406809270382,nan,0.289906248748,49
8,0.3912438941,nan,0.282540213466,64
9,0.377326357365,729,0.276457450986,81
10,0.364721306562,nan,0.271435074806,100
11,0.353612961769,nan,0.266939682364,121
12,0.343238875866,nan,0.263228923082,144
13,0.333940329552,nan,0.260326927304,169
14,0.325931007862,nan,0.25773427248,196
15,0.317790198028,nan,0.255648627281,225
16,0.310636150837,nan,0.25411529541,256
17,0.304091459513,nan,0.252928718328,289
18,0.298703012466,nan,0.252127869725,324
19,0.292693507671,6859,0.251701972485,361
20,0.287824733257,nan,0.251610517502,400
21,0.283586999774,nan,0.251790778637,441
22,0.27927801609,nan,0.252100949883,484
23,0.276239238977,nan,0.252632959485,529
24,0.273072380424,nan,0.253150621653,576
25,0.270296501517,nan,0.253555388451,625
26,0.268056542277,nan,0.254015884399,676
27,0.266158599854,nan,0.254496408701,729
28,0.264166412354,nan,0.254723013639,784
29,0.262506003976,24389,0.255338237286,841
这篇关于Keras,从回调附加到日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!