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:
register takes a name, a pointer to the actual activation function and a pointer to the derivative function (just like the new constructor), creates a new Activation with those and inserts it into the registry. If an instance with the same name was already present, it is replaced and the old one is returned (wrapped in Some(...)). Otherwise None is returned. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L108-L113
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.
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:register
takes a name, a pointer to the actual activation function and a pointer to the derivative function (just like thenew
constructor), creates a newActivation
with those and inserts it into the registry. If an instance with the same name was already present, it is replaced and the old one is returned (wrapped inSome(...)
). OtherwiseNone
is returned. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L108-L113from_name
now returns a clone of theActivation
instance registered under the provided name (wrapped inSome(...)
). If none was registered under that name,None
is returned. Sincefrom_name
used to return anActivation
proper, this is technically a breaking change. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L131-L136The implementation relies on the
generic_singleton::get_or_init
macro and the registry (for a specific typeActivation<T>
) is a static singleton. TheActivationRegistry<T>
is simply a type alias forHashMap<String, Activation<T>>
. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L67-L73During initialization of the registry singleton, the pre-implemented common activation functions (currently only
sigmoid
andrelu
) are added automatically. Those are then available by default, but can be replaced by custom implementations viaregister
at any point. https://github.com/mfajnberg/tensorevo/blob/4895317df5432894ff2b88b117c7a50144d31626/src/activation.rs#L79-L89There is still a lot of
unwrap
ping going on there with theRwLock
, 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.