我希望能够在模型训练时获取每个实例的损失值。
history = model.fit(..)
例如以上的代码返回了每个epoch的损失值而不是mini batch或instance的值。
如何最好地解决这个问题?有什么建议吗?
我希望能够在模型训练时获取每个实例的损失值。
history = model.fit(..)
例如以上的代码返回了每个epoch的损失值而不是mini batch或instance的值。
如何最好地解决这个问题?有什么建议吗?
在官方 Keras 文档页面的末尾,您会找到正是您所寻找的内容:https://keras.io/callbacks/#callback
以下是创建自定义回调的代码:
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
model = Sequential()
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
history = LossHistory()
model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
print(history.losses)
# outputs
'''
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
'''
如果您想获取每个批次的损失值,您可能需要在生成器内调用model.train_on_batch
。不知道您的数据集是什么样子,很难提供完整的示例,但您需要将数据集分成批次,并逐个馈送。
def make_batches(...):
...
batches = make_batches(...)
batch_losses = [model.train_on_batch(x, y) for x, y in batches]
def make_batches(batchsize, x, y):
...
batchsize = n
batches = make_batches(n, ...)
batch_instances = [make_batches(1, x, y) for x, y in batches]
losses = [
(model.train_on_batch(x, y), [model.test_on_batch(*inst) for inst in instances])
for batch, instances in zip(batches, batch_instances)
]
一种解决方案是计算训练期望和来自训练输入的预测之间的损失函数。在损失=均方误差且输出为三维(即图像宽度x高度x通道)的情况下:
model.fit(train_in,train_out,...)
pred = model.predict(train_in)
loss = np.add.reduce(np.square(test_out-pred),axis=(1,2,3)) # this computes the total squared error for each sample
loss = loss / ( pred.shape[1]*pred.shape[2]*pred.shape[3]) # this computes the mean over the sample entry
np.savetxt("loss.txt",loss) # This line saves the data to file
从这里和这里汇集资源后,我得到了以下代码。也许它会对你有所帮助。这个想法是你可以覆盖keras的Callbacks
类,然后使用on_batch_end
方法来检查keras自动提供给该方法的logs
中的损失值。
这里有一个具有特定功能的NN的工作代码。也许你可以从这里开始 -
import numpy as np
import pandas as pd
import seaborn as sns
import os
import matplotlib.pyplot as plt
import time
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import Callback
# fix random seed for reproducibility
seed = 155
np.random.seed(seed)
# load pima indians dataset
# download directly from website
dataset = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data",
header=None).values
X_train, X_test, Y_train, Y_test = train_test_split(dataset[:,0:8], dataset[:,8], test_size=0.25, random_state=87)
class NBatchLogger(Callback):
def __init__(self,display=100):
'''
display: Number of batches to wait before outputting loss
'''
self.seen = 0
self.display = display
def on_batch_end(self,batch,logs={}):
self.seen += logs.get('size', 0)
if self.seen % self.display == 0:
print('\n{0}/{1} - Batch Loss: {2}'.format(self.seen,self.params['samples'],
logs.get('loss')))
out_batch = NBatchLogger(display=1000)
np.random.seed(seed)
my_first_nn = Sequential() # create model
my_first_nn.add(Dense(5, input_dim=8, activation='relu')) # hidden layer
my_first_nn.add(Dense(1, activation='sigmoid')) # output layer
my_first_nn.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
my_first_nn_fitted = my_first_nn.fit(X_train, Y_train, epochs=1000, verbose=0, batch_size=128,
callbacks=[out_batch], initial_epoch=0)
如果您需要类似这样的东西,请告诉我。
on_batch_end
仅在每个小批量结束时返回损失。我想要做的是更深入地了解并在每个训练实例之后获取损失。有什么建议吗? - e.hunnigton