Giter Site home page Giter Site logo

Comments (7)

fabiodimarco avatar fabiodimarco commented on July 2, 2024 1

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.

from tf-levenberg-marquardt.

fabiodimarco avatar fabiodimarco commented on July 2, 2024

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.

from tf-levenberg-marquardt.

Fly-Playgroud avatar Fly-Playgroud commented on July 2, 2024

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. use model.compile(optimizer, loss)``." How do I solve this problem?

from tf-levenberg-marquardt.

fabiodimarco avatar fabiodimarco commented on July 2, 2024

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.

from tf-levenberg-marquardt.

Fly-Playgroud avatar Fly-Playgroud commented on July 2, 2024

I tried calling model_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.

from tf-levenberg-marquardt.

fabiodimarco avatar fabiodimarco commented on July 2, 2024

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.

from tf-levenberg-marquardt.

Fly-Playgroud avatar Fly-Playgroud commented on July 2, 2024

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

from tf-levenberg-marquardt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.