mirror of
https://github.com/helblazer811/ManimML.git
synced 2025-08-06 17:29:45 +08:00
Working neural network test with refactor
This commit is contained in:
@ -5,8 +5,9 @@ from manim_ml.neural_network.neural_network import NeuralNetwork
|
|||||||
class FeedForwardNeuralNetwork(NeuralNetwork):
|
class FeedForwardNeuralNetwork(NeuralNetwork):
|
||||||
"""NeuralNetwork with just feed forward layers"""
|
"""NeuralNetwork with just feed forward layers"""
|
||||||
|
|
||||||
def __init__(self, layer_node_count, node_radius=1.0,
|
def __init__(self, layer_node_count, node_radius=0.08,
|
||||||
node_color=BLUE, **kwargs):
|
node_color=BLUE, **kwargs):
|
||||||
|
|
||||||
# construct layers
|
# construct layers
|
||||||
layers = []
|
layers = []
|
||||||
for num_nodes in layer_node_count:
|
for num_nodes in layer_node_count:
|
||||||
|
@ -24,16 +24,17 @@ class ConnectiveLayer(NeuralNetworkLayer):
|
|||||||
class FeedForwardToFeedForward(ConnectiveLayer):
|
class FeedForwardToFeedForward(ConnectiveLayer):
|
||||||
|
|
||||||
def __init__(self, input_layer, output_layer, passing_flash=True,
|
def __init__(self, input_layer, output_layer, passing_flash=True,
|
||||||
dot_radius=0.05, animation_dot_count=RED, edge_color=WHITE,
|
dot_radius=0.05, animation_dot_color=RED, edge_color=WHITE,
|
||||||
edge_width=0.5):
|
edge_width=0.5):
|
||||||
super(FeedForwardToFeedForward, self).__init__(input_layer, output_layer)
|
super().__init__(input_layer, output_layer)
|
||||||
self.passing_flash = passing_flash
|
self.passing_flash = passing_flash
|
||||||
self.edge_color = edge_color
|
self.edge_color = edge_color
|
||||||
self.dot_radius = dot_radius
|
self.dot_radius = dot_radius
|
||||||
self.animation_dot_count = animation_dot_count
|
self.animation_dot_color = animation_dot_color
|
||||||
self.edge_width = edge_width
|
self.edge_width = edge_width
|
||||||
|
|
||||||
self.construct_edges()
|
self.edges = self.construct_edges()
|
||||||
|
self.add(self.edges)
|
||||||
|
|
||||||
def construct_edges(self):
|
def construct_edges(self):
|
||||||
# Go through each node in the two layers and make a connecting line
|
# Go through each node in the two layers and make a connecting line
|
||||||
@ -42,26 +43,33 @@ class FeedForwardToFeedForward(ConnectiveLayer):
|
|||||||
for node_j in self.output_layer.node_group:
|
for node_j in self.output_layer.node_group:
|
||||||
line = Line(node_i.get_center(), node_j.get_center(),
|
line = Line(node_i.get_center(), node_j.get_center(),
|
||||||
color=self.edge_color, stroke_width=self.edge_width)
|
color=self.edge_color, stroke_width=self.edge_width)
|
||||||
self.add(line)
|
edges.append(line)
|
||||||
|
|
||||||
self.edges = VGroup(*edges)
|
edges = VGroup(*edges)
|
||||||
|
return edges
|
||||||
|
|
||||||
def make_forward_pass_animation(self, run_time=1):
|
def make_forward_pass_animation(self, run_time=1):
|
||||||
"""Animation for passing information from one FeedForwardLayer to the next"""
|
"""Animation for passing information from one FeedForwardLayer to the next"""
|
||||||
path_animations = []
|
path_animations = []
|
||||||
|
dots = []
|
||||||
for edge in self.edges:
|
for edge in self.edges:
|
||||||
dot = Dot(color=self.animation_dot_color, fill_opacity=1.0, radius=self.dot_radius)
|
dot = Dot(color=self.animation_dot_color, fill_opacity=1.0, radius=self.dot_radius)
|
||||||
# Handle layering
|
# Handle layering
|
||||||
dot.set_z_index(1)
|
dot.set_z_index(1)
|
||||||
# Add to dots group
|
# Add to dots group
|
||||||
self.dots.add(dot)
|
dots.append(dot)
|
||||||
# Make the animation
|
# Make the animation
|
||||||
if self.passing_flash:
|
if self.passing_flash:
|
||||||
|
print("passing flash")
|
||||||
anim = ShowPassingFlash(edge.copy().set_color(self.animation_dot_color), time_width=0.2, run_time=3)
|
anim = ShowPassingFlash(edge.copy().set_color(self.animation_dot_color), time_width=0.2, run_time=3)
|
||||||
else:
|
else:
|
||||||
anim = MoveAlongPath(dot, edge, run_time=run_time, rate_function=sigmoid)
|
anim = MoveAlongPath(dot, edge, run_time=run_time, rate_function=sigmoid)
|
||||||
path_animations.append(anim)
|
path_animations.append(anim)
|
||||||
|
|
||||||
|
if not self.passing_flash:
|
||||||
|
dots = VGroup(*dots)
|
||||||
|
self.add(dots)
|
||||||
|
|
||||||
path_animations = AnimationGroup(*path_animations)
|
path_animations = AnimationGroup(*path_animations)
|
||||||
|
|
||||||
return path_animations
|
return path_animations
|
||||||
|
@ -16,7 +16,7 @@ from manim_ml.neural_network.layers import FeedForwardToFeedForward, FeedForward
|
|||||||
class NeuralNetwork(VGroup):
|
class NeuralNetwork(VGroup):
|
||||||
|
|
||||||
def __init__(self, layers, edge_color=WHITE, layer_spacing=0.8,
|
def __init__(self, layers, edge_color=WHITE, layer_spacing=0.8,
|
||||||
animation_dot_color=RED, edge_width=2.0, dot_radius=0.05):
|
animation_dot_color=RED, edge_width=1.5, dot_radius=0.05):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.layers = layers
|
self.layers = layers
|
||||||
self.edge_width = edge_width
|
self.edge_width = edge_width
|
||||||
@ -55,7 +55,9 @@ class NeuralNetwork(VGroup):
|
|||||||
|
|
||||||
if isinstance(current_layer, FeedForwardLayer) \
|
if isinstance(current_layer, FeedForwardLayer) \
|
||||||
and isinstance(next_layer, FeedForwardLayer):
|
and isinstance(next_layer, FeedForwardLayer):
|
||||||
edge_layer = FeedForwardToFeedForward(current_layer, next_layer)
|
edge_layer = FeedForwardToFeedForward(current_layer, next_layer,
|
||||||
|
edge_width=self.edge_width)
|
||||||
|
|
||||||
connective_layers.add(edge_layer)
|
connective_layers.add(edge_layer)
|
||||||
else:
|
else:
|
||||||
raise Exception(f"Unimplemented connection for layer types: {type(current_layer)} and {type(next_layer)}")
|
raise Exception(f"Unimplemented connection for layer types: {type(current_layer)} and {type(next_layer)}")
|
||||||
@ -64,14 +66,15 @@ class NeuralNetwork(VGroup):
|
|||||||
connective_layers.set_z_index(0)
|
connective_layers.set_z_index(0)
|
||||||
return connective_layers
|
return connective_layers
|
||||||
|
|
||||||
def make_forward_propagation_animation(self, run_time=2, passing_flash=True):
|
def make_forward_pass_animation(self, run_time=2, passing_flash=True):
|
||||||
"""Generates an animation for feed forward propogation"""
|
"""Generates an animation for feed forward propogation"""
|
||||||
all_animations = []
|
all_animations = []
|
||||||
|
|
||||||
for layer_index, layer in enumerate(self.layers[:-1]):
|
for layer_index, layer in enumerate(self.layers[:-1]):
|
||||||
connective_layer = self.connective_layers[layer_index]
|
|
||||||
layer_forward_pass = layer.make_forward_pass_animation()
|
layer_forward_pass = layer.make_forward_pass_animation()
|
||||||
all_animations.append(layer_forward_pass)
|
all_animations.append(layer_forward_pass)
|
||||||
|
|
||||||
|
connective_layer = self.connective_layers[layer_index]
|
||||||
connective_forward_pass = connective_layer.make_forward_pass_animation()
|
connective_forward_pass = connective_layer.make_forward_pass_animation()
|
||||||
all_animations.append(connective_forward_pass)
|
all_animations.append(connective_forward_pass)
|
||||||
|
|
||||||
|
39
tests/test_neural_network.py
Normal file
39
tests/test_neural_network.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from manim import *
|
||||||
|
from manim_ml.neural_network.layers import FeedForwardLayer
|
||||||
|
from manim_ml.neural_network.neural_network import NeuralNetwork
|
||||||
|
from manim_ml.neural_network.feed_forward import FeedForwardNeuralNetwork
|
||||||
|
|
||||||
|
config.pixel_height = 720
|
||||||
|
config.pixel_width = 1280
|
||||||
|
config.frame_height = 6.0
|
||||||
|
config.frame_width = 6.0
|
||||||
|
|
||||||
|
class FeedForwardNeuralNetworkScene(Scene):
|
||||||
|
|
||||||
|
def construct(self):
|
||||||
|
nn = FeedForwardNeuralNetwork([3, 5, 3])
|
||||||
|
self.play(Create(nn))
|
||||||
|
self.play(Wait(3))
|
||||||
|
|
||||||
|
class NeuralNetworkScene(Scene):
|
||||||
|
"""Test Scene for the Neural Network"""
|
||||||
|
|
||||||
|
def construct(self):
|
||||||
|
# Make the Layer object
|
||||||
|
layers = [FeedForwardLayer(3), FeedForwardLayer(5), FeedForwardLayer(3)]
|
||||||
|
nn = NeuralNetwork(layers)
|
||||||
|
nn.move_to(ORIGIN)
|
||||||
|
# Make Animation
|
||||||
|
self.add(nn)
|
||||||
|
forward_propagation_animation = nn.make_forward_pass_animation(run_time=5, passing_flash=True)
|
||||||
|
|
||||||
|
self.play(forward_propagation_animation)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
"""Render all scenes"""
|
||||||
|
# Feed Forward Neural Network
|
||||||
|
ffnn_scene = FeedForwardNeuralNetworkScene()
|
||||||
|
ffnn_scene.render()
|
||||||
|
# Neural Network
|
||||||
|
nn_scene = NeuralNetworkScene()
|
||||||
|
nn_scene.render()
|
Reference in New Issue
Block a user