1、概述
manim作为非常炫酷的动画系统可以非常清晰生动表述数学原理的相关动画。但是在原生系统中并没有神经网络结构绘制的模块,需要自行开发。本文展示了最基本的dnn网络的绘制方式,供大家参考。
2、完整代码
class NeuralNetworkMobject(VGroup):
# Constructor with parameters of the neurons in a list
def __init__(self, neural_network, *args, **kwargs):
VGroup.__init__(self, *args,**kwargs)
self.neuron_radius = 0.15
self.neuron_to_neuron_buff= MED_SMALL_BUFF
self.layer_to_layer_buff= LARGE_BUFF
self.output_neuron_color= WHITE
self.input_neuron_color= WHITE
self.hidden_layer_neuron_color= WHITE
self.neuron_stroke_width= 2
self.neuron_fill_color= GREEN
self.edge_color= LIGHT_GREY
self.edge_stroke_width= 2
self.edge_propogation_color= YELLOW
self.edge_propogation_time= 1
self.max_shown_neurons= 16
self.brace_for_large_layers= True
self.average_shown_activation_of_large_layer= True
self.include_output_labels= False
self.arrow= False
self.arrow_tip_size= 0.1
self.left_size= 1
self.neuron_fill_opacity= 1
for name, value in kwargs.items():
setattr(self, name, value)
self.layer_sizes = neural_network
self.add_neurons()
self.add_edges()
self.add_to_back(self.layers)
# Helper method for constructor
def add_neurons(self):
layers = VGroup(*[
self.get_layer(size, index)
for index, size in enumerate(self.layer_sizes)
])
layers.arrange_submobjects(RIGHT, buff=self.layer_to_layer_buff)
self.layers = layers
if self.include_output_labels:
self.label_outputs_text()
# Helper method for constructor
def get_nn_fill_color(self, index):
if index == -1 or index =