使用manim创建深度神经网络,并将代码文本放在其旁边。

huangapple go评论59阅读模式
英文:

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

问题

以下是您要翻译的代码部分:

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, 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):
    """神经网络的测试场景"""
    def construct(self):

        # 创建文本
        desc = Text(NN_text, font_size=7)
        desc = desc.next_to(ORIGIN)

        # 创建神经网络图层
        layers = [FeedForwardLayer(8), FeedForwardLayer(5), FeedForwardLayer(5), FeedForwardLayer(1)]
        nn = NeuralNetwork(layers)
        nn.scale(1)
        nn.move_to(LEFT)

        # 添加神经网络图层
        self.add(nn)

        first_layer = Text("输入层", font_size=7)
        first_layer = first_layer.next_to(layers[0].get_corner(DOWN), DOWN)
        self.add(first_layer)

        hidden_layer = Text("隐藏层", font_size=7)
        hidden_layer = hidden_layer.next_to(nn.get_corner(DOWN), DOWN)
        self.add(hidden_layer)

        output_layer = Text("输出层", 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)

        # 添加前向传播动画
        forward_propagation_animation = nn.make_forward_pass_animation(
            run_time=5, passing_flash=True
        )
        self.play(FadeIn(desc), forward_propagation_animation)

请注意,我已经修复了文本的格式,并用正常的引号替换了HTML实体。如果需要更多的修改,请告诉我。

英文:

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

答案1

得分: 1

我是一个manim的初学者,但我知道一些东西。使用Code()to_edge().set(width=)。我还将配置设置更改回了(更或多或少)标准配置设置。但通过对你的代码进行一些微小的修改,我得到了以下截图(并不完美 - 例如输入层的文本太小,可能比你最初的效果差,但希望能提供一些想法):

使用manim创建深度神经网络,并将代码文本放在其旁边。

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):
    """神经网络的测试场景"""
    def construct(self):
        
        # 创建文本
        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)

    
        # 创建Layer对象
        layers = [FeedForwardLayer(8), FeedForwardLayer(5), FeedForwardLayer(5), FeedForwardLayer(1)]
        nn = NeuralNetwork(layers)
        nn.to_edge(LEFT)
        nn.set(height=config.frame_height*0.7)
        # 创建动画
        self.add(nn)

        first_layer = Text("输入层", font_size=7)
        first_layer = first_layer.next_to(layers[0].get_corner(DOWN), DOWN)
        self.add(first_layer)

        hidden_layer = Text("隐藏层", font_size=7)
        hidden_layer = hidden_layer.next_to(nn.get_corner(DOWN), DOWN)
        self.add(hidden_layer)

        output_layer = Text("输出层", 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)
英文:

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):

使用manim创建深度神经网络,并将代码文本放在其旁边。

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)

huangapple
  • 本文由 发表于 2023年2月6日 08:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定