Search code examples
pythonanimationmanim

creating deep neural network with text of code next to it using manim


I would like to create a cool animation for my deep neural network with code implementation next to it using manim library (the reference image is attacked). But, my output is distorted and the text is off the screen and not good in general.

Can you edit it in a way the both DPP and text code looks good? Text need to be colored as written in python.

please use the community manim library and not other versions like manimlib.

from manim import *
from manim_ml.neural_network.layers import FeedForwardLayer
from manim_ml.neural_network.neural_network import NeuralNetwork

config.pixel_height = 900
config.pixel_width = 1400
config.frame_height = 7.0
config.frame_width = 7.0

NN_text ="""
import keras
from keras.models import Sequential
from keras. layers import Dense
model = Sequential ()
n_cols = concrete_data. shape [1]
model. add (Dense (5, activation='relu',
model. add (Dense(5, activations' reluj, input_shape=(n_ (cols, )))
model.add(Dense (1))
model. compile (optimizer='adam', loss='mean_squared_error')
model.fit (predictors, target)
predictions = model.predict(test_data)
"""

class NeuralNetworkScene(Scene):
    """Test Scene for the Neural Network"""
    def construct(self):

        # Make the text
        desc = Text(NN_text,font_size=7)
        desc=desc.next_to(ORIGIN)
    
        # Make the Layer object
        layers = [FeedForwardLayer(8), FeedForwardLayer(5), FeedForwardLayer(5),FeedForwardLayer(1)]
        nn = NeuralNetwork(layers)
        nn.scale(1)
        nn.move_to(LEFT)
        # Make Animation
        self.add(nn)

        first_layer = Text("Input Layer", font_size=7)
        first_layer=first_layer.next_to(layers[0].get_corner(DOWN),DOWN)
        self.add(first_layer)

        hidden_layer = Text("Hidden Layer", font_size=7)
        hidden_layer=hidden_layer.next_to(nn.get_corner(DOWN),DOWN)
        self.add(hidden_layer)

        output_layer = Text("Output Layer", font_size=7)
        location_output = layers[3].get_corner(DOWN)
        location_output[1] = layers[0].get_corner(DOWN)[1]
        output_layer=output_layer.next_to(location_output,DOWN)
        self.add(output_layer)

        # self.play(Create(nn))
        forward_propagation_animation = nn.make_forward_pass_animation(
            run_time=5, passing_flash=True
        )
        self.play(FadeIn(desc),forward_propagation_animation)


In fact implement this image with manim.

DPP image


Solution

  • I'm a manim beginner, but here is what I know. Use Code() and to_edge() and .set(width=). Also I changed back to (more or less) the standard config settings. But with those minor modifications to your code here is a screenshot of what I can get (it's not perfect - the input layer text is too small for example, and I don't know maybe it's worse than what you originally had, but I hope it gives some ideas):

    enter image description here

    from manim import *
    from manim_ml.neural_network.layers import FeedForwardLayer
    from manim_ml.neural_network.neural_network import NeuralNetwork
    
    config.pixel_height = 900
    config.pixel_width = 1400
    config.frame_height = 8.0
    config.frame_width = 14.0
    
    NN_text ="""
    import keras
    from keras.models import Sequential
    from keras.layers import Dense
    
    model = Sequential ()
    n_cols = concrete_data.shape[1]
    
    model.add(Dense(5, activation='relu',
    model.add(Dense(5, activation= 'relu', input_shape=(n_cols, )))
    model.add(Dense(1))
    
    model.compile(optimizer='adam', loss='mean_squared_error')
    
    model.fit (predictors, target)
    
    predictions = model.predict(test_data)
    """
    
    class NeuralNetworkScene(Scene):
        """Test Scene for the Neural Network"""
        def construct(self):
            
            # Make the text
            desc = Code(code=NN_text, insert_line_no=False, font="Monospace", style='Monokai', language='python')
            desc=desc.next_to(ORIGIN).set(height=config.frame_height*0.5).to_edge(RIGHT)
    
        
            # Make the Layer object
            layers = [FeedForwardLayer(8), FeedForwardLayer(5), FeedForwardLayer(5),FeedForwardLayer(1)]
            nn = NeuralNetwork(layers)
            nn.to_edge(LEFT)
            nn.set(height=config.frame_height*0.7)
            # Make Animation
            self.add(nn)
    
            first_layer = Text("Input Layer", font_size=7)
            first_layer=first_layer.next_to(layers[0].get_corner(DOWN),DOWN)
            self.add(first_layer)
    
            hidden_layer = Text("Hidden Layer", font_size=7)
            hidden_layer=hidden_layer.next_to(nn.get_corner(DOWN),DOWN)
            self.add(hidden_layer)
    
            output_layer = Text("Output Layer", font_size=7)
            location_output = layers[3].get_corner(DOWN)
            location_output[1] = layers[0].get_corner(DOWN)[1]
            output_layer=output_layer.next_to(location_output,DOWN)
            self.add(output_layer)
    
            # self.play(Create(nn))
            forward_propagation_animation = nn.make_forward_pass_animation(
                run_time=5, passing_flash=True
            )
            self.play(FadeIn(desc),forward_propagation_animation)