-
Notifications
You must be signed in to change notification settings - Fork 33
/
tictactoe.py
executable file
·173 lines (150 loc) · 5.37 KB
/
tictactoe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from manimlib.imports import *
class Intro(Scene):
def construct(self):
pass
class MinimaxTitle(Scene):
def construct(self):
title = TextMobject("Minimax", color=GREEN)
title.scale(2)
self.play(Write(title))
self.wait()
class Minimax(Scene):
def construct(self):
tree = TreeMobject((1, 2, 4))
tree.rotate(PI/2, axis=IN)
tree.add_labels(-1, ["9", "2", "5", "3"])
tree.scale(2)
tree.add_branch_labels(["R", "L"])
max1 = TextMobject("Maximizer")
max1.scale(1)
max1.shift(2 * LEFT + 1.5 * UP)
min1 = TextMobject("Minimizer")
min1.scale(1)
min1.shift(2 * LEFT + 1.5 * DOWN)
self.play(Write(tree))
self.wait()
self.play(Write(max1), Write(min1))
self.wait()
class TreeMobject(VGroup):
CONFIG = {
"neuron_radius": 0.15,
"neuron_to_neuron_buff": MED_SMALL_BUFF,
"layer_to_layer_buff": LARGE_BUFF,
"neuron_stroke_color": BLUE,
"neuron_stroke_width": 3,
"neuron_fill_color": GREEN,
"edge_color": LIGHT_GREY,
"edge_stroke_width": 2,
"edge_propogation_color": YELLOW,
"edge_propogation_time": 1,
"max_shown_neurons": 16,
"brace_for_large_layers": True,
"average_shown_activation_of_large_layer": True,
"include_output_labels": False,
}
def __init__(self, neural_network, size=0.15):
VGroup.__init__(self)
self.layer_sizes = neural_network
self.neuron_radius = size
self.add_neurons()
self.add_edges()
def add_neurons(self):
layers = VGroup(*[
self.get_layer(size)
for size in self.layer_sizes
])
layers.arrange_submobjects(RIGHT, buff=self.layer_to_layer_buff)
self.layers = layers
self.add(self.layers)
if self.include_output_labels:
self.add_output_labels()
def get_layer(self, size):
layer = VGroup()
n_neurons = size
if n_neurons > self.max_shown_neurons:
n_neurons = self.max_shown_neurons
neurons = VGroup(*[
Circle(
radius=self.neuron_radius,
stroke_color=self.neuron_stroke_color,
stroke_width=self.neuron_stroke_width,
fill_color=self.neuron_fill_color,
fill_opacity=0,
)
for x in range(n_neurons)
])
neurons.arrange_submobjects(
DOWN, buff=self.neuron_to_neuron_buff
)
for neuron in neurons:
neuron.edges_in = VGroup()
neuron.edges_out = VGroup()
layer.neurons = neurons
layer.add(neurons)
if size > n_neurons:
dots = TexMobject("\\vdots")
dots.move_to(neurons)
VGroup(*neurons[:len(neurons) // 2]).next_to(
dots, UP, MED_SMALL_BUFF
)
VGroup(*neurons[len(neurons) // 2:]).next_to(
dots, DOWN, MED_SMALL_BUFF
)
layer.dots = dots
layer.add(dots)
if self.brace_for_large_layers:
brace = Brace(layer, LEFT)
brace_label = brace.get_tex(str(size))
layer.brace = brace
layer.brace_label = brace_label
layer.add(brace, brace_label)
return layer
def add_edges(self):
self.edge_groups = VGroup()
for l1, l2 in zip(self.layers[:-1], self.layers[1:]):
edge_group = VGroup()
for i, n1 in enumerate(l1.neurons):
n2 = l2.neurons[2*i]
edge = self.get_edge(n1, n2)
edge_group.add(edge)
n1.edges_out.add(edge)
n2.edges_in.add(edge)
n2 = l2.neurons[2*i+1]
edge = self.get_edge(n1, n2)
edge_group.add(edge)
n1.edges_out.add(edge)
n2.edges_in.add(edge)
self.edge_groups.add(edge_group)
self.add_to_back(self.edge_groups)
def get_edge(self, neuron1, neuron2):
return Line(
neuron1.get_center(),
neuron2.get_center(),
buff=self.neuron_radius,
stroke_color=self.edge_color,
stroke_width=self.edge_stroke_width,
)
def add_input_labels(self):
self.output_labels = VGroup()
for n, neuron in enumerate(self.layers[0].neurons):
label = TexMobject(f"x_{n + 1}")
label.set_height(0.3 * neuron.get_height())
label.move_to(neuron)
self.output_labels.add(label)
self.add(self.output_labels)
def add_labels(self, layer, labels):
self.output_labels = VGroup()
for n, neuron in enumerate(self.layers[layer].neurons):
label = TexMobject(labels[n])
label.set_height(0.3 * neuron.get_height())
label.move_to(neuron)
self.output_labels.add(label)
self.add(self.output_labels)
def add_branch_labels(self, labels, move=(RIGHT, LEFT)):
weight_group = VGroup()
for n, i in enumerate(self.layers[1].neurons):
edge = self.get_edge(i, self.layers[0].neurons[0])
text = TextMobject(labels[n], color=RED)
text.move_to(edge, move[n] * 2)
weight_group.add(text)
self.add(weight_group)