0

suppose this is my neural network training and i made a loop for choose number of neuron in each hidden layer. how can I save each result for every loop and print all the result for all loop in the end of (i) loop:

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)
desertnaut
  • 57,590
  • 26
  • 140
  • 166

2 Answers2

0

you can use the callback function called 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)
nipun
  • 672
  • 5
  • 11
-1

Typically, we won't use print to print the log. We need to use the logger during training.

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

To store the log information, please refer to this

How to redirect TensorFlow logging to a file?

Frank
  • 1,151
  • 10
  • 22