diff --git a/.gitignore b/.gitignore index f032d79..50fe2d9 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ setup.cfg !examples/media examples/media/videos examples/media/text -examples/media/images \ No newline at end of file +examples/media/images diff --git a/Readme.md b/Readme.md index 87625d7..fcbc288 100644 --- a/Readme.md +++ b/Readme.md @@ -347,4 +347,40 @@ We can now render with: $ manim -pql example.py ``` - \ No newline at end of file + + +#### Seed the Dropouts: + +```py +self.play( + make_neural_network_dropout_animation( + nn, dropout_rate=0.25, do_forward_pass=True, seed=4 + ) +) +``` + + + +#### Seed the Dropouts with First layer static: + +```py +self.play( + make_neural_network_dropout_animation( + nn, dropout_rate=0.25, do_forward_pass=True, seed=4, first_layer_stable=True + ) +) +``` + + + +#### Seed the Dropouts with First and Last layers static: + +```py +self.play( + make_neural_network_dropout_animation( + nn, dropout_rate=0.25, do_forward_pass=True, seed=4, first_layer_stable=True, last_layer_stable=True + ) +) +``` + + diff --git a/assets/readme/dropout_seed_4.gif b/assets/readme/dropout_seed_4.gif new file mode 100644 index 0000000..6643215 Binary files /dev/null and b/assets/readme/dropout_seed_4.gif differ diff --git a/assets/readme/dropout_seed_4_first.gif b/assets/readme/dropout_seed_4_first.gif new file mode 100644 index 0000000..20a1767 Binary files /dev/null and b/assets/readme/dropout_seed_4_first.gif differ diff --git a/assets/readme/dropout_seed_4_first_last.gif b/assets/readme/dropout_seed_4_first_last.gif new file mode 100644 index 0000000..c1fb7b2 Binary files /dev/null and b/assets/readme/dropout_seed_4_first_last.gif differ diff --git a/manim_ml/neural_network/animations/dropout.py b/manim_ml/neural_network/animations/dropout.py index 31ec935..11ae788 100644 --- a/manim_ml/neural_network/animations/dropout.py +++ b/manim_ml/neural_network/animations/dropout.py @@ -192,7 +192,7 @@ def make_forward_pass_with_dropout_animation( def make_neural_network_dropout_animation( - neural_network, dropout_rate=0.5, do_forward_pass=True + neural_network, dropout_rate=0.5, do_forward_pass=True, last_layer_stable=False, first_layer_stable=False, seed=None ): """ Makes a dropout animation for a given neural network. @@ -204,6 +204,8 @@ def make_neural_network_dropout_animation( 3. Revert network to pre-dropout appearance """ # Go through the network and get the FeedForwardLayer instances + if seed is not None: + random.seed(seed) feed_forward_layers = neural_network.filter_layers( lambda layer: isinstance(layer, FeedForwardLayer) ) @@ -213,12 +215,16 @@ def make_neural_network_dropout_animation( ) # Get random nodes to drop out for each FeedForward Layer layers_to_nodes_to_drop_out = {} - for feed_forward_layer in feed_forward_layers: + for idx, feed_forward_layer in enumerate(feed_forward_layers): num_nodes = feed_forward_layer.num_nodes nodes_to_drop_out = [] # Compute random probability that each node is dropped out for node_index in range(num_nodes): dropout_prob = random.random() + if last_layer_stable and idx==len(feed_forward_layers)-1: + continue + if first_layer_stable and idx==0: + continue if dropout_prob < dropout_rate: nodes_to_drop_out.append(node_index) # Add the mapping to the dict