Merge pull request #547 from 3b1b/diffyq

Diffyq
This commit is contained in:
Grant Sanderson 2019-05-24 15:13:40 -07:00 committed by GitHub
commit cec7872f48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 140 additions and 15 deletions

View file

@ -0,0 +1,40 @@
#!/usr/bin/env python
from manimlib.imports import *
from active_projects.ode.part2.fourier_series import FourierOfName
name_color_pairs = [
]
circle_counts = [
# 10,
# 25,
100,
]
if __name__ == "__main__":
for name, color in name_color_pairs:
for n_circles in circle_counts:
try:
first_name = name.split(" ")[0]
scene = FourierOfName(
name_text=name,
name_color=color,
n_circles=n_circles,
file_writer_config={
"write_to_movie": True,
"output_directory": os.path.join(
"patron_fourier_names",
first_name,
),
"file_name": "{}_Fouierified_{}_Separate_paths".format(
first_name,
n_circles
),
},
camera_config={
"frame_rate": 24,
},
)
except:
pass

View file

@ -313,12 +313,7 @@ class FourierOfPiSymbol(FourierCirclesScene):
coefs = self.get_coefficients_of_path(path)
circles = self.get_circles(coefficients=coefs)
for k, circle in zip(it.count(1), circles):
circle.set_stroke(width=max(
1 / np.sqrt(k),
1,
))
self.set_decreasing_stroke_widths(circles)
# approx_path = self.get_circle_end_path(circles)
drawn_path = self.get_drawn_path(circles)
if self.start_drawn:
@ -329,6 +324,14 @@ class FourierOfPiSymbol(FourierCirclesScene):
self.add(drawn_path)
self.wait(self.run_time)
def set_decreasing_stroke_widths(self, circles):
for k, circle in zip(it.count(1), circles):
circle.set_stroke(width=max(
1 / np.sqrt(k),
1,
))
return circles
def get_path(self):
tex_mob = TexMobject(self.tex)
tex_mob.set_height(6)
@ -338,6 +341,51 @@ class FourierOfPiSymbol(FourierCirclesScene):
return path
class FourierOfName(FourierOfPiSymbol):
CONFIG = {
"n_circles": 100,
"name_color": WHITE,
"name_text": "Abc",
"time_per_symbol": 5,
"slow_factor": 1 / 5,
}
def construct(self):
name = TextMobject(self.name_text)
max_width = FRAME_WIDTH - 2
max_height = FRAME_HEIGHT - 2
name.set_width(max_width)
if name.get_height() > max_height:
name.set_height(max_height)
circles = VGroup(VectorizedPoint())
for path in name.family_members_with_points():
for subpath in path.get_subpaths():
sp_mob = VMobject()
sp_mob.set_points(subpath)
coefs = self.get_coefficients_of_path(sp_mob)
new_circles = self.get_circles(
coefficients=coefs
)
self.set_decreasing_stroke_widths(new_circles)
drawn_path = self.get_drawn_path(new_circles)
drawn_path.clear_updaters()
drawn_path.set_stroke(self.name_color, 3)
new_circles.suspend_updating()
self.play(ReplacementTransform(circles, new_circles))
new_circles.resume_updating()
circles = new_circles
self.play(
ShowCreation(drawn_path),
rate_func=linear,
run_time=self.time_per_symbol
)
circles.suspend_updating()
self.play(FadeOut(circles))
self.wait(3)
class FourierOfPiSymbol5(FourierOfPiSymbol):
CONFIG = {
"n_circles": 5,

View file

@ -34,6 +34,7 @@ class SceneFileWriter(object):
# TODO, address this in extract_scene et. al.
"file_name": None,
"output_directory": None,
"file_name": None,
}
def __init__(self, scene, **kwargs):
@ -81,7 +82,10 @@ class SceneFileWriter(object):
return root if root else ext[1:]
def get_default_file_name(self):
return self.scene.__class__.__name__
if self.file_name is None:
return self.scene.__class__.__name__
else:
return self.file_name
def get_movie_directory(self):
pixel_height = self.scene.camera.pixel_height

View file

@ -20,7 +20,7 @@ import random
import numpy as np
from PIL import Image
from nn.mnist_loader import load_data_wrapper
from utils.space_ops import get_norm
# from utils.space_ops import get_norm
NN_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
# PRETRAINED_DATA_FILE = os.path.join(NN_DIRECTORY, "pretrained_weights_and_biases_80")

View file

@ -2964,7 +2964,7 @@ class BiasForInactiviyWords(Scene):
self.play(Write(words))
self.wait(3)
class ContinualEdgeUpdate(ContinualAnimation):
class ContinualEdgeUpdate(VGroup):
CONFIG = {
"max_stroke_width" : 3,
"stroke_width_exp" : 7,
@ -2972,7 +2972,8 @@ class ContinualEdgeUpdate(ContinualAnimation):
"colors" : [GREEN, GREEN, GREEN, RED],
}
def __init__(self, network_mob, **kwargs):
digest_config(self, kwargs)
VGroup.__init__(self, **kwargs)
self.internal_time = 0
n_cycles = self.n_cycles
edges = VGroup(*it.chain(*network_mob.edge_groups))
self.move_to_targets = []
@ -2990,11 +2991,14 @@ class ContinualEdgeUpdate(ContinualAnimation):
edge.generate_target()
edge.target.set_stroke(edge.colors[0], edge.widths[0])
self.move_to_targets.append(MoveToTarget(edge))
edge.become(edge.target)
self.move_to_targets.append(edge)
self.edges = edges
ContinualAnimation.__init__(self, edges, **kwargs)
self.add(edges)
self.add_updater(lambda m, dt: self.update_edges(dt))
def update_mobject(self, dt):
def update_edges(self, dt):
self.internal_time += dt
if self.internal_time < 1:
alpha = smooth(self.internal_time)
for move_to_target in self.move_to_targets:
@ -4559,8 +4563,9 @@ class ShowAmplify(PiCreatureScene):
class Thumbnail(NetworkScene):
CONFIG = {
"network_mob_config" : {
'neuron_stroke_color' : WHITE
}
'neuron_stroke_color' : WHITE,
'layer_to_layer_buff': 1.25,
},
}
def construct(self):
network_mob = self.network_mob
@ -4568,6 +4573,23 @@ class Thumbnail(NetworkScene):
for layer in network_mob.layers:
layer.neurons.set_stroke(width = 5)
network_mob.set_height(5)
network_mob.to_edge(DOWN)
network_mob.to_edge(LEFT, buff=1)
subtitle = TextMobject(
"From the\\\\",
"ground up\\\\",
)
# subtitle.arrange(
# DOWN,
# buff=0.25,
# aligned_edge=LEFT,
# )
subtitle.set_color(YELLOW)
subtitle.set_height(2.75)
subtitle.next_to(network_mob, RIGHT, buff=MED_LARGE_BUFF)
edge_update = ContinualEdgeUpdate(
network_mob,
max_stroke_width = 10,
@ -4576,7 +4598,18 @@ class Thumbnail(NetworkScene):
edge_update.internal_time = 3
edge_update.update(0)
for mob in network_mob.family_members_with_points():
if mob.get_stroke_width() < 2:
mob.set_stroke(width=2)
title = TextMobject("Neural Networks")
title.scale(3)
title.to_edge(UP)
self.add(network_mob)
self.add(subtitle)
self.add(title)