Implement activation registry (closes #17) #23
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.tensorevo/src/activation.rs
Lines 108 to 113 in 4895317
from_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.tensorevo/src/activation.rs
Lines 131 to 136 in 4895317
The 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>>
.tensorevo/src/activation.rs
Lines 67 to 73 in 4895317
During 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.tensorevo/src/activation.rs
Lines 79 to 89 in 4895317
There 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.