Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras Callback #13

Closed
delphixx opened this issue Jul 29, 2019 · 10 comments
Closed

Keras Callback #13

delphixx opened this issue Jul 29, 2019 · 10 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@delphixx
Copy link

Hi!
Your project is very useful and I appreciate your contribution! I started with the example and wanted to customize the Keras Callback mechanism to show the model.fit progress to the user.

But I don't really know how to do it:
Declaring a new class
private class KerasCallback : Keras.Callbacks.Callback{}

Creating the object:
KerasCallback x1 = new KerasCallback();

And then send to fit:
model.Fit(x, y, batch_size: 2, epochs: 1000, verbose: 1, callbacks: new Callback[] { x1 });

And all I get is:
Python.Runtime.PythonException: AttributeError - Callback object has no attribute Invoke.

It seems as I missed to declare a method or property to the class. Any ideas?
Maybe you could extend one of the examples with the solution.

Thanks in advance!

@Oceania2018 Oceania2018 added the help wanted Extra attention is needed label Jul 29, 2019
@deepakkumar1984
Copy link
Member

Thanks for the appreciation. Are you trying to build a custom callback? Could you please let me know what the callback function will do so that I can try to implement it or provide you solution?

@delphixx
Copy link
Author

Yes I try to build a custom callback! I want to get the step, batch, training and validation score value - let’s say in a Console.WriteLine output. How can I do it?

@delphixx
Copy link
Author

delphixx commented Aug 5, 2019

Any Ideas on how to make a delegate for example on_batch_end or on_train_begin?

@deepakkumar1984
Copy link
Member

Didn't get much time last few weeks, will try to come up with an example for your requirement by this week

@deepakkumar1984
Copy link
Member

I am able to come up with a solution to implement custom callback. Since the library is a wrapper for the Keras python, the custom callback need to be a python code only. Here it the example: https://github.com/SciSharp/Keras.NET/blob/master/Examples/BasicSamples/ImplementCallback.cs

Steps1: Create a python file with your callback implementaion.Took an example from keras documentation itself:

import keras

class LossHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.losses = []

    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
        print("Custom Callback: " + str(logs.get('loss')))

Step2: In your .NET project use the Calback.Custom method which can take the file or code to give you instance of callback object. Pass the callback instance to fit method. You can access the custom properties defined in the python implementation:

var lossHistory = Callback.Custom("LossHistory", "LossHistory.py");

//Compile and train
model.Compile(optimizer: new Adam(), loss: "binary_crossentropy", metrics: new string[] { "accuracy" });
var history = model.Fit(x, y, batch_size: 2, epochs: 10, verbose: 1, callbacks: new Callback[] { lossHistory });

var customLosses = lossHistory.Get<double[]>("losses");

@deepakkumar1984
Copy link
Member

image

See the result the custom callback in invoked by printing the message.

@deepakkumar1984
Copy link
Member

Please reopen if still need help

@delphixx
Copy link
Author

Thanks for the example! This helps me a lot!

Is there a way to create a delegate method that fires on every training step in C# ?

@deepakkumar1984
Copy link
Member

Since most of the core functionality is written in python and Keras.NET invoke those functions, so its not possible to hook .NET delegates within Python.

@delphixx
Copy link
Author

Ok! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants