【问题标题】:How to save and load a NeuralFit model or weights?如何保存和加载 NeuralFit 模型或权重?
【发布时间】:2023-02-07 01:21:55
【问题描述】:

我已经进化了一个神经网络来使用 neuralfit 库学习 y=x^2,但我想保存模型以便稍后进行预测。我目前有:

import neuralfit
import numpy as np

# y(x) = x^2
x = np.arange(10).reshape(-1,1)
y = x**2

# Evolve model
model = neuralfit.Model(1,1)
model.compile('alpha', loss='mse', monitors=['size'])
model.evolve(x,y,epochs=1000)

# Save model
...

我将如何保存和加载model

【问题讨论】:

    标签: python parameters neural-network save neuralfit


    【解决方案1】:

    有两种方法可以做到这一点:(1) 使用 Neuralfit 和 (2) 使用 Keras。最好使用 NeuralFit,因为生成的保存文件要小得多(在本例中为 50 倍)。

    方法一:用NeuralFit

    基于documentation

    # Save a model
    model.save('model.nf')
    
    # Load a saved model
    model = neuralfit.load('model.nf')
    

    方法 2:使用 Keras

    由于 NeuralFit 允许转换为 Keras,我们可以将模型转换为 Keras,然后使用 their functionality 保存它。换句话说:

    # Save a model
    keras_model = model.to_keras()
    keras_model.save('model.h5')
    
    # Load a saved model
    keras_model = keras.models.load_model('model.h5')
    model = neuralfit.from_keras(keras_model)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2021-12-15
      • 2019-09-26
      相关资源
      最近更新 更多