如何在神经网络中保存每个循环的结果



假设这是我的神经网络训练,我在每个隐藏层中为选择数量的神经元做了一个循环。我如何保存每个循环的每个结果,并在(I)循环结束时打印所有循环的所有结果:

import tensorflow as tf
import numpy as np
from tensorflow import keras
for i in range(1,9):
model=tf.keras.Sequential([keras.layers.Dense(units =i, input_shape=[1]),
(keras.layers.Dense(units =i, input_shape=[1)])
model.compile(optimizer='sgd',loss='mean_squared_error')
xs=np.array([2,3,4], dtype=float)
ys=np.array([100,200,300],dtype=float)
model.fit(xs,ys,epochs=4000)
result= (model.predict([1]))
print(result)

您可以使用名为ModelCheckpoint的回调函数

import tensorflow as tf
from tf.keras.callbacks import ModelCheckpoint
EPOCHS = 10
checkpoint_filepath = '/tmp/checkpoint'
model_checkpoint_callback = ModelCheckpoint(
filepath=checkpoint_filepath,
save_weights_only=True,
monitor='val_acc',
mode='max',
save_best_only=True)
# Model weights are saved at the end of every epoch, if it's the best seen
# so far.
model.fit(epochs=EPOCHS, callbacks=[model_checkpoint_callback])
# The model weights (that are considered the best) are loaded into the model.
model.load_weights(checkpoint_filepath)

通常,我们不会使用print来打印日志。我们需要在培训期间使用记录器。

https://www.tensorflow.org/api_docs/python/tf/compat/v1/logging

存储日志信息,请参考

如何重定向TensorFlow日志到一个文件?

最新更新