【问题标题】:how can I save result for every loop in neural network如何保存神经网络中每个循环的结果
【发布时间】:2021-05-13 16:47:20
【问题描述】:

假设这是我的神经网络训练,我做了一个循环来选择每个隐藏层中的神经元数量。 如何保存每个循环的每个结果并在 (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)

【问题讨论】:

    标签: python tensorflow keras neural-network


    【解决方案1】:

    您可以使用名为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)
    

    【讨论】:

      【解决方案2】:

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

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

      要存储日志信息,请参考这里

      How to redirect TensorFlow logging to a file?

      【讨论】:

        猜你喜欢
        • 2019-10-17
        • 2017-02-19
        • 2023-03-14
        • 2018-10-05
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-23
        相关资源
        最近更新 更多