mfajnberg / tensorevo

0 stars 1 forks source link

Implement activation registry (closes #17) #23

Closed daniil-berg closed 10 months ago

daniil-berg commented 10 months ago

As described in #17, this adds the ability for the user to register his own activation functions and have them (de-)serialized properly.

The interface is comprised of two associated functions for the Activation struct:

The implementation relies on the generic_singleton::get_or_init macro and the registry (for a specific type Activation<T>) is a static singleton. The ActivationRegistry<T> is simply a type alias for HashMap<String, Activation<T>>. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L67-L73

During initialization of the registry singleton, the pre-implemented common activation functions (currently only sigmoid and relu) are added automatically. Those are then available by default, but can be replaced by custom implementations via register at any point. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L79-L89

There is still a lot of unwrapping going on there with the RwLock, but since we have not cleaned that up anywhere else in the code yet, I did not bother to handle potential errors more cleanly yet.