如何在Keras中返回验证丢失的历史记录 [英] How to return history of validation loss in Keras

查看:45
本文介绍了如何在Keras中返回验证丢失的历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Anaconda Python 2.7 Windows 10.

Using Anaconda Python 2.7 Windows 10.

我正在使用Keras范例训练语言模型:

I am training a language model using the Keras exmaple:

print('Build model...')
model = Sequential()
model.add(GRU(512, return_sequences=True, input_shape=(maxlen, len(chars))))
model.add(Dropout(0.2))
model.add(GRU(512, return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(len(chars)))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

def sample(a, temperature=1.0):
    # helper function to sample an index from a probability array
    a = np.log(a) / temperature
    a = np.exp(a) / np.sum(np.exp(a))
    return np.argmax(np.random.multinomial(1, a, 1))


# train the model, output generated text after each iteration
for iteration in range(1, 3):
    print()
    print('-' * 50)
    print('Iteration', iteration)
    model.fit(X, y, batch_size=128, nb_epoch=1)
    start_index = random.randint(0, len(text) - maxlen - 1)

    for diversity in [0.2, 0.5, 1.0, 1.2]:
        print()
        print('----- diversity:', diversity)

        generated = ''
        sentence = text[start_index: start_index + maxlen]
        generated += sentence
        print('----- Generating with seed: "' + sentence + '"')
        sys.stdout.write(generated)

        for i in range(400):
            x = np.zeros((1, maxlen, len(chars)))
            for t, char in enumerate(sentence):
                x[0, t, char_indices[char]] = 1.

            preds = model.predict(x, verbose=0)[0]
            next_index = sample(preds, diversity)
            next_char = indices_char[next_index]

            generated += next_char
            sentence = sentence[1:] + next_char

            sys.stdout.write(next_char)
            sys.stdout.flush()
        print()

根据Keras文档,model.fit方法返回一个History回调,该回调具有一个history属性,其中包含连续损失列表和其他度量.

According to Keras documentation, the model.fit method returns a History callback, which has a history attribute containing the lists of successive losses and other metrics.

hist = model.fit(X, y, validation_split=0.2)
print(hist.history)

训练完模型后,如果运行print(model.history),我会收到错误消息:

After training my model, if I run print(model.history) I get the error:

 AttributeError: 'Sequential' object has no attribute 'history'

使用上述代码训练模型后,如何返回模型历史记录?

How do I return my model history after training my model with the above code?

更新

问题在于:

必须首先定义以下内容:

The following had to first be defined:

from keras.callbacks import History 
history = History()

必须调用callbacks选项

The callbacks option had to be called

model.fit(X_train, Y_train, nb_epoch=5, batch_size=16, callbacks=[history])

但是现在如果我打印

print(history.History)

它返回

{}

即使我运行了一次迭代.

even though I ran an iteration.

推荐答案

已解决.

这些损失只会在各个时期保存到历史记录中.我正在运行迭代,而不是使用Keras内置的epochs选项.

The losses only save to the History over the epochs. I was running iterations instead of using the Keras built in epochs option.

所以我现在没有进行4次迭代

so instead of doing 4 iterations I now have

model.fit(......, nb_epoch = 4)

现在,它返回每次运行的损失:

Now it returns the loss for each epoch run:

print(hist.history)
{'loss': [1.4358016599558268, 1.399221191623641, 1.381293383180471, h1.3758836857303727]}

这篇关于如何在Keras中返回验证丢失的历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆