helblazer811 / ManimML

ManimML is a project focused on providing animations and visualizations of common machine learning concepts with the Manim Community Library.
MIT License
2.35k stars 140 forks source link

Make Canonical Animations as Objects #27

Open helblazer811 opened 1 year ago

helblazer811 commented 1 year ago

Manim has the ability to represent animations as classes. These classes can take parameters and they can wrap around a passed Mobject (in our case this could be a neural network) and then a mobject specific animation is run. You can override such animations like Create (see neural_network.py). It would be interesting to make a generic TrainModel animation that is implemented by various models and emulates the training procedure.

For now, I could implement these animations as functions in the manim_ml/neural_network/animations directory.

Arbaaz-Mahmood commented 1 year ago

Here is some code I wrote for a generic model training class in manim:

`from manim import *

class ModelTraining(Scene): def construct(self): model_title = TextMobject("Model Training") self.play(Write(model_title)) self.wait()

    training_data = TextMobject("Training Data")
    self.play(Write(training_data))
    self.wait()

    model_training = TextMobject("Training the Model")
    self.play(Write(model_training))
    self.wait()

    accuracy = TextMobject("Accuracy:")
    accuracy_value = TextMobject("100%")
    accuracy_group = VGroup(accuracy, accuracy_value)
    accuracy_group.arrange(RIGHT)
    accuracy_group.to_edge(UP)
    self.play(Write(accuracy_group))
    self.wait()

    testing_data = TextMobject("Testing Data")
    self.play(Write(testing_data))
    self.wait()

    model_testing = TextMobject("Testing the Model")
    self.play(Write(model_testing))
    self.wait()

    test_accuracy = TextMobject("Test Accuracy:")
    test_accuracy_value = TextMobject("99%")
    test_accuracy_group = VGroup(test_accuracy, test_accuracy_value)
    test_accuracy_group.arrange(RIGHT)
    test_accuracy_group.to_edge(UP)
    self.play(Write(test_accuracy_group))
    self.wait()

`

Arbaaz-Mahmood commented 1 year ago

Here is an example of a simple transformer model implemented which inherits from this class: `from manim import * class TransformerModelScene(ModelTrainingScene): def init(self, kwargs): ModelTrainingScene.init(self, kwargs) def setup(self): self.prepare_data() self.add_data_points() self.add_transformer_model() def prepare_data(self):

Prepare the data for the Transformer model

    pass
def add_transformer_model(self):
    # Add the Transformer model to the scene
    pass

class TransformerModelTraining(Scene): def construct(self): self.add(TransformerModelScene())

` In this example, TransformerModelScene is a subclass of ModelTrainingScene and inherits all of its methods and attributes. The setup method is overridden to prepare the data for the Transformer model and add the model to the scene.

In the TransformerModelScene class, the prepare_data and add_transformer_model methods need to be implemented to prepare the data for the Transformer model and add the model to the scene, respectively.

Finally, TransformerModelTraining is a scene that adds the TransformerModelScene to the animation.

helblazer811 commented 1 year ago

Interesting ideas! I'm especially excited by the idea of creating more animations like ForwardPass with similar syntax to how the animations are done in the core library.