Dropout Last Layer - option to remove node on the last layer and seed dropouts for reproducible animations (#30)

Option to remove node on the last layer and seed dropouts for reproducible animations
This commit is contained in:
Aman Priyanshu
2023-02-03 03:27:19 +03:00
committed by GitHub
parent 9698907cbf
commit 42defde750
6 changed files with 46 additions and 4 deletions

2
.gitignore vendored
View File

@ -9,4 +9,4 @@ setup.cfg
!examples/media
examples/media/videos
examples/media/text
examples/media/images
examples/media/images

View File

@ -347,4 +347,40 @@ We can now render with:
$ manim -pql example.py
```
<img src="assets/readme/dropout.gif">
<img src="assets/readme/dropout.gif">
#### Seed the Dropouts:
```py
self.play(
make_neural_network_dropout_animation(
nn, dropout_rate=0.25, do_forward_pass=True, seed=4
)
)
```
<img src="assets/readme/dropout_seed_4.gif">
#### 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
)
)
```
<img src="assets/readme/dropout_seed_4_first.gif">
#### 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
)
)
```
<img src="assets/readme/dropout_seed_4_first_last.gif">

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB

View File

@ -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