DeepTrackAI / deeplay

Other
4 stars 5 forks source link

Bm/refactor/block-methods #94

Closed BenjaminMidtvedt closed 6 months ago

BenjaminMidtvedt commented 6 months ago

Adds block methods and improves Selector flexibility

Block methods

Adds block methods for adding and removing modules from a sequential block:

block.append(Layer(Linear, 1, 1))
block.append(Layer(Linear, 1, 1), name="fc1")
block.prepend(Layer(Linear, 1, 1))
block.insert(Layer(Linear, 1, 1), after="activation")
block.remove("normalization")
block.append_dropout(0.5)
block.preprend_dropout(0.5)
block.insert_dropout(0.5, after="activation")
block.remove_dropout()
block.set_dropout(0.5, on_missing="append")

Selector methods

Adds filter methods: filter, isinstance, hasattr, and the attributes "all" and "first".

Selection.all.some_method() #will apply `some_method` on all selected modules
Selection.first.some_method() #will apply `some_method` on the first selected module

some methods, like configure and `set_input_map exist on the Selection object, and will apply either all of first depending on the method. However, this syntax allows you to execute any method as necessary.

filter take name, module as arguments. isinstance and hasattr will check Layer.classtype if necessary and include_layer_classtype is True. So

isinstance(Layer(Linear), Linear) # True
module = ...
module["encoder", ...].isinstance(Layer).configure(...)
module[..., "activation"].isinstance(nn.ReLU).configure(nn.CELU)
module["encoder", ...].hasattr("set_dropout").all.set_dropout(0.1)
module["encoder", ...].filter(lambda name, module: "layer" in name)