Package: manim-ml
ManimMLManimML is a project focused on providing animations and visualizations of common machine learning concepts with theManim Community Library. We want this project to be a compilation of primitive visualizations that can be easily combined to create videos about complex machine learning concepts. Additionally, we want to provide a set of abstractions which allow users to focus on explanations instead of software engineering.A sneak peak ...Table of ContentsManimMLTable of ContentsGetting StartedInstallationFirst Neural NetworkGuideSetting Up a SceneA Simple Feed Forward NetworkAnimating the Forward PassConvolutional Neural NetworksConvolutional Neural Network with an ImageMax PoolingActivation FunctionsMore Complex Animations: Neural Network DropoutCitationGetting StartedInstallationFirst you will want toinstall manim. Make sure it is the Manim Community edition, and not the original 3Blue1Brown Manim version.Then install the package form source orpip install manim_ml. Note: some recent features may only available if you install from source.First Neural NetworkThis is a visualization of a Convolutional Neural Network. The code needed to generate this visualization is shown below.frommanimimport*frommanim_ml.neural_networkimportConvolutional2DLayer,FeedForwardLayer,NeuralNetwork# This changes the resolution of our rendered videosconfig.pixel_height=700config.pixel_width=1900config.frame_height=7.0config.frame_width=7.0# Here we define our basic sceneclassBasicScene(ThreeDScene):# The code for generating our scene goes heredefconstruct(self):# Make the neural networknn=NeuralNetwork([Convolutional2DLayer(1,7,3,filter_spacing=0.32),Convolutional2DLayer(3,5,3,filter_spacing=0.32),Convolutional2DLayer(5,3,3,filter_spacing=0.18),FeedForwardLayer(3),FeedForwardLayer(3),],layer_spacing=0.25,)# Center the neural networknn.move_to(ORIGIN)self.add(nn)# Make a forward pass animationforward_pass=nn.make_forward_pass_animation()# Play animationself.play(forward_pass)You can generate the above video by copying the above code into a file calledexample.pyand running the following in your command line (assuming everything is installed properly):$manim-pqlexample.pyThe above generates a low resolution rendering, you can improve the resolution (at the cost of slowing down rendering speed) by running:$manim-pqhexample.pyGuideThis is a more in depth guide showing how to use various features of ManimML (Note: ManimML is still under development so some features may change, and documentation is lacking).Setting Up a SceneIn Manim all of your visualizations and animations belong inside of aScene. You can make a scene by extending theSceneclass or theThreeDSceneclass if your animation has 3D content (as does our example). Add the following code to a python module calledexample.py.frommanimimport*# Import modules hereclassBasicScene(ThreeDScene):defconstruct(self):# Your code goes heretext=Text("Your first scene!")self.add(text)In order to render the scene we will run the following in the command line:$manim-pq-lexample.pyThis will generate an image file in low quality (use-hfor high quality).For the rest of the tutorial the code snippets will need to be copied into the body of theconstructfunction.A Simple Feed Forward NetworkWith ManimML we can easily visualize a simple feed forward neural network.frommanim_ml.neural_networkimportNeuralNetwork,FeedForwardLayernn=NeuralNetwork([FeedForwardLayer(num_nodes=3),FeedForwardLayer(num_nodes=5),FeedForwardLayer(num_nodes=3)])self.add(nn)In the above code we create aNeuralNetworkobject and pass a list of layers to it. For each feed forward layer we specify the number of nodes. ManimML will automatically piece together the individual layers into a single neural network. We callself.add(nn)in the body of the scene'sconstructmethod in order to add the neural network to the scene.The majority of ManimML neural network objects and functions can be imported directly frommanim_ml.neural_network.We can now render a still frame image of the scene by running:$manim-pqlexample.pyAnimating the Forward PassWe can automatically render the forward pass of a neural network by creating the animation with theneural_network.make_forward_pass_animationmethod and play the animation in our scene withself.play(animation).frommanim_ml.neural_networkimportNeuralNetwork,FeedForwardLayer# Make the neural networknn=NeuralNetwork([FeedForwardLayer(num_nodes=3),FeedForwardLayer(num_nodes=5),FeedForwardLayer(num_nodes=3)])self.add(nn)# Make the animationforward_pass_animation=nn.make_forward_pass_animation()# Play the animationself.play(forward_pass_animation)We can now render with:$manim-pqlexample.pyConvolutional Neural NetworksManimML supports visualizations of Convolutional Neural Networks. You can specify the number of feature maps, feature map size, and filter size as followsConvolutional2DLayer(num_feature_maps, feature_map_size, filter_size). There are a number of other style parameters that we can change as well(documentation coming soon).Here is a multi-layer convolutional neural network. If you are unfamiliar with convolutional networksthis overviewis a great resource. You need to be careful that the feature map sizes and filter dimensions of adjacent layers match up.frommanim_ml.neural_networkimportNeuralNetwork,FeedForwardLayer,Convolutional2DLayernn=NeuralNetwork([Convolutional2DLayer(1,7,3,filter_spacing=0.32),# Note the default stride is 1.Convolutional2DLayer(3,5,3,filter_spacing=0.32),Convolutional2DLayer(5,3,3,filter_spacing=0.18),FeedForwardLayer(3),FeedForwardLayer(3),],layer_spacing=0.25,)# Center the neural networknn.move_to(ORIGIN)self.add(nn)# Make a forward pass animationforward_pass=nn.make_forward_pass_animation()We can now render with:$manim-pqlexample.pyAnd there we have it, a convolutional neural network.Convolutional Neural Network with an ImageWe can also animate an image being fed into a convolutional neural network by specifiying anImageLayerbefore the first convolutional layer.importnumpyasnpfromPILimportImagefrommanim_ml.neural_networkimportNeuralNetwork,FeedForwardLayer,Convolutional2DLayer,ImageLayerimage=Image.open("digit.jpeg")# You will need to download an image of a digit.numpy_image=np.asarray(image)nn=NeuralNetwork([ImageLayer(numpy_image,height=1.5),Convolutional2DLayer(1,7,3,filter_spacing=0.32),# Note the default stride is 1.Convolutional2DLayer(3,5,3,filter_spacing=0.32),Convolutional2DLayer(5,3,3,filter_spacing=0.18),FeedForwardLayer(3),FeedForwardLayer(3),],layer_spacing=0.25,)# Center the neural networknn.move_to(ORIGIN)self.add(nn)# Make a forward pass animationforward_pass=nn.make_forward_pass_animation()We can now render with:$manim-pqlexample.pyMax PoolingA common operation in deep learning is the 2D Max Pooling operation, which reduces the size of convolutional feature maps. We can visualize max pooling with theMaxPooling2DLayer.frommanim_ml.neural_networkimportNeuralNetwork,Convolutional2DLayer,MaxPooling2DLayer# Make neural networknn=NeuralNetwork([Convolutional2DLayer(1,8),Convolutional2DLayer(3,6,3),MaxPooling2DLayer(kernel_size=2),Convolutional2DLayer(5,2,2),],layer_spacing=0.25,)# Center the nnnn.move_to(ORIGIN)self.add(nn)# Play animationforward_pass=nn.make_forward_pass_animation()self.wait(1)self.play(forward_pass)We can now render with:$manim-pqlexample.pyActivation FunctionsActivation functions apply non-linarities to the outputs of neural networks. They have different shapes, and it is useful to be able to visualize the functions. I added the ability to visualize activation functions overFeedForwardLayerandConvolutional2DLayerby passing an argument as follows:layer=FeedForwardLayer(num_nodes=3,activation_function="ReLU")We can add these to larger neural network as follows:frommanim_ml.neural_networkimportNeuralNetwork,Convolutional2DLayer,FeedForwardLayer# Make nnnn=NeuralNetwork([Convolutional2DLayer(1,7,filter_spacing=0.32),Convolutional2DLayer(3,5,3,filter_spacing=0.32,activation_function="ReLU"),FeedForwardLayer(3,activation_function="Sigmoid"),],layer_spacing=0.25,)self.add(nn)# Play animationforward_pass=nn.make_forward_pass_animation()self.play(forward_pass)We can now render with:$manim-pqlexample.pyMore Complex Animations: Neural Network Dropoutfrommanim_ml.neural_networkimportNeuralNetwork,FeedForwardLayerfrommanim_ml.neural_network.animations.dropoutimportmake_neural_network_dropout_animation# Make nnnn=NeuralNetwork([FeedForwardLayer(3),FeedForwardLayer(5),FeedForwardLayer(3),FeedForwardLayer(5),FeedForwardLayer(4),],layer_spacing=0.4,)# Center the nnnn.move_to(ORIGIN)self.add(nn)# Play animationself.play(make_neural_network_dropout_animation(nn,dropout_rate=0.25,do_forward_pass=True))self.wait(1)We can now render with:$manim-pqlexample.pySeed the Dropouts: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: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: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))CitationIf you found ManimML useful please cite it below!@software{alec_helbling_2023_7760911,
  author       = {Alec Helbling},
  title        = {{ManimML: A Python Animation Engine for Machine 
                   Learning Architectures}},
  month        = mar,
  year         = 2023,
  publisher    = {Zenodo},
  version      = {v0.0.20},
  doi          = {10.5281/zenodo.7760911},
  url          = {https://doi.org/10.5281/zenodo.7760911}
}