mirror of
https://github.com/helblazer811/ManimML.git
synced 2025-07-03 23:02:00 +08:00
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:
2
.gitignore
vendored
2
.gitignore
vendored
@ -9,4 +9,4 @@ setup.cfg
|
|||||||
!examples/media
|
!examples/media
|
||||||
examples/media/videos
|
examples/media/videos
|
||||||
examples/media/text
|
examples/media/text
|
||||||
examples/media/images
|
examples/media/images
|
||||||
|
38
Readme.md
38
Readme.md
@ -347,4 +347,40 @@ We can now render with:
|
|||||||
$ manim -pql example.py
|
$ 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">
|
||||||
|
BIN
assets/readme/dropout_seed_4.gif
Normal file
BIN
assets/readme/dropout_seed_4.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 212 KiB |
BIN
assets/readme/dropout_seed_4_first.gif
Normal file
BIN
assets/readme/dropout_seed_4_first.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 211 KiB |
BIN
assets/readme/dropout_seed_4_first_last.gif
Normal file
BIN
assets/readme/dropout_seed_4_first_last.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 213 KiB |
@ -192,7 +192,7 @@ def make_forward_pass_with_dropout_animation(
|
|||||||
|
|
||||||
|
|
||||||
def make_neural_network_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.
|
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
|
3. Revert network to pre-dropout appearance
|
||||||
"""
|
"""
|
||||||
# Go through the network and get the FeedForwardLayer instances
|
# Go through the network and get the FeedForwardLayer instances
|
||||||
|
if seed is not None:
|
||||||
|
random.seed(seed)
|
||||||
feed_forward_layers = neural_network.filter_layers(
|
feed_forward_layers = neural_network.filter_layers(
|
||||||
lambda layer: isinstance(layer, FeedForwardLayer)
|
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
|
# Get random nodes to drop out for each FeedForward Layer
|
||||||
layers_to_nodes_to_drop_out = {}
|
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
|
num_nodes = feed_forward_layer.num_nodes
|
||||||
nodes_to_drop_out = []
|
nodes_to_drop_out = []
|
||||||
# Compute random probability that each node is dropped out
|
# Compute random probability that each node is dropped out
|
||||||
for node_index in range(num_nodes):
|
for node_index in range(num_nodes):
|
||||||
dropout_prob = random.random()
|
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:
|
if dropout_prob < dropout_rate:
|
||||||
nodes_to_drop_out.append(node_index)
|
nodes_to_drop_out.append(node_index)
|
||||||
# Add the mapping to the dict
|
# Add the mapping to the dict
|
||||||
|
Reference in New Issue
Block a user