fabiodimarco / tf-levenberg-marquardt

Tensorflow implementation of Levenberg-Marquardt training algorithm
MIT License
93 stars 18 forks source link

how can I use model_wrapper to test the model and get the predicted value? #10

Closed Fly-Playgroud closed 2 years ago

Fly-Playgroud commented 2 years ago

hellow ,how can I use model_wrapper to test the model and get the predicted value?

fabiodimarco commented 2 years ago

Hi, I am not sure I have understood the question. I'll try to answer.

import levenberg_marquardt as lm

model = code to create the model ...

model_wrapper = lm.ModelWrapper(model)

model_wrapper.compile(
    optimizer=tf.keras.optimizers.SGD(learning_rate=0.1),
    loss=lm.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'])

model_wrapper.fit(train_dataset, epochs=5)

After the training you can just use the model object you have created as a normal keras model and do all the tests you need. ModelWrapper it is used only to perform the training.

Let me know if I answered your question.

Fly-Playgroud commented 2 years ago

Thank you for your reply.! But after I trained the model using model_wrapper, I want to evaluate the performance of the model and when I use model.evaluate() to evaluate the model, it reports an error: "RuntimeError: You must compile your model before training/testing. usemodel.compile(optimizer, loss)``." How do I solve this problem?

fabiodimarco commented 2 years ago

Just compile the model with a random optimizer (ex. tf.keras.optimizers.Adam) and the loss you want to evaluate your model on (ex. tf.keras.losses.MeanSquaredError).

Or I think you can just call model_wrapper.evaluate(), do not remeber if it works you can try it.

Fly-Playgroud commented 2 years ago

I tried callingmodel_wrapper.evaluate() and it works fine, but the evaluation results are not very good. I guess that model_wrapper.evaluate() does not evaluate accurately, after all levenberg_marquardt.py does not provide a function interface similar to evaluate(). If it is convenient, I suggest you can add an evaluate function to levenberg_marquardt.py.

fabiodimarco commented 2 years ago

Actually ModelWrapper inherits from tf.keras.Sequential: class ModelWrapper(tf.keras.Sequential):

So model_wrapper.evaluate() must be correct. It you get bad results evaluating on test data il probably means you are overfitting train data. Try using a smaller model or use regularization techniques.

Fly-Playgroud commented 2 years ago

Thank you for your patience and answers. I see what you mean. Also, have you tried using theLM algorithm as a custom optimizer for a kreas, it would be much easier to do that!

fabiodimarco commented 2 years ago

The implementation I provided is made to use LM algorithm on keras models. It is not possible to do it by creating a custom keras optimizer because during the training loop keras provide to the optimizer the gradient vector, while LM needs the jacobian matrix. Creating a ModelWrapper that overrides the fit function is the easiest way I found to provide that behaviour.