[英]How to get the loss for each epoch in custom batch training in Keras?
我正在用 AI 进行一些试验,我有一个 model,我想在其中分批训练它,这些批次是先前选择的并且具有不规则的形状。
这是我所拥有的:
model = create_model(units_vector, (inp_shape,), lr)
for i in range(epochs):
for batch in batches:
model.fit(batch[0], batch[1], epochs=1, verbose=0)
function create_model
定义了一个简单的 model (仅密集层)并使用mse
和Adam
编译它。
batches
是存储输入和输出的元组列表(在 numpy 数组中)。
我想要一种在每个时代结束时打印 model 实验的损失的方法。 或者通过某种方式来检查 model 是否正在学习某些东西(不必在每批中打印一条消息,因为垃圾邮件太多)。
这是一种在每个时期结束时打印损失的简单方法。 您可以通过model.history.history
访问损失。 也可以根据需要修改消息
epochs = 5
n_batches = 20
n_features = 10
batches = [[np.random.uniform(0,1, (1,n_features)), np.random.uniform(0,1, (1,1))] for _ in range(n_batches)]
inp = Input((n_features,))
x = Dense(32)(inp)
out = Dense(1)(x)
model = Model(inp, out)
model.compile('adam', 'mse')
for i in range(epochs):
for batch in batches:
model.fit(batch[0], batch[1], epochs=1, verbose=0)
print(f"EPOCH {i}", model.history.history)
output 示例:
EPOCH 0 {'loss': [0.9013449549674988]}
EPOCH 1 {'loss': [0.7315107583999634]}
EPOCH 2 {'loss': [0.5937882661819458]}
EPOCH 3 {'loss': [0.5331881046295166]}
EPOCH 4 {'loss': [0.47262871265411377]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.