Skip to content

Instantly share code, notes, and snippets.

@arif9799
Last active August 8, 2023 02:10
Show Gist options
  • Save arif9799/3534b9165c599e4dd9090b278958007e to your computer and use it in GitHub Desktop.
Save arif9799/3534b9165c599e4dd9090b278958007e to your computer and use it in GitHub Desktop.
Attention is not enough! Mathematical Representation of Words -embedding vectors
from manim import *
from manim.utils.unit import Percent, Pixels
from colour import Color
import gensim
from gensim.models import Word2Vec
import numpy as np
import Util_Functions
from Util_Functions import *
config.frame_width = 32
config.frame_height = 18
HIGH = True
if HIGH:
print("Its high")
config.pixel_height = 2160
config.pixel_width = 3840
else:
print("Its low")
config.pixel_height = 1080
config.pixel_width = 1920
data = [['Attention', 'is', 'not', 'enough'], ['Towards', 'Data', 'Science'], ['It', 'was', 'a', 'really', 'good', 'article']]
sentences_Embedded = construct_Word_Embeddings(data, 6)
sentence_split = data[0]
sentence_embed = np.round(sentences_Embedded[0],2)
class Figure3(Scene):
def construct(self):
################################################################################
# INTRO ANIMATION SAME ACROSS ALL VIDEOS #
################################################################################
def myAnimation(wordsForIntro: str):
fontHeight = config.frame_height//8
fontColor = WHITE
timePerChar = 0.1
C = MathTex(r"\mathbb{C}", color = fontColor).scale(config.frame_height//3)
self.play(Broadcast(C), run_time=1)
self.add(C)
# Building text objects of individual characters.
wordsMath = VGroup()
for word in wordsForIntro:
charTex = VGroup()
for i,ch in enumerate(word):
chTex = MathTex("\mathbb{%s}"%ch, color = fontColor).scale(fontHeight)
if i != 0:
chTex.next_to(charTex[-1], RIGHT, buff=0.05).align_to(C, DOWN)
else:
chTex.next_to(C, RIGHT, buff=0.05).align_to(C, DOWN)
charTex.add(chTex)
wordsMath.add(charTex)
# Succesion or AnimationGroup--- Both are messed up ----HENCE INEFFECIENT ANIMATION
for wInd in range(len(wordsMath)):
for chInd in range(len(wordsMath[wInd])):
self.play(Write(wordsMath[wInd][chInd], run_time = timePerChar))
self.wait(0.5)
for chInd in reversed(range(len(wordsMath[wInd]))):
self.play(Unwrite(wordsMath[wInd][chInd], run_time = timePerChar))
self.play(Circumscribe(C, color=MAROON_E, fade_out=False, time_width=2, shape=Circle, buff=1, stroke_width=config.frame_height//3, run_time=1.5))
self.play(ShrinkToCenter(C, run_time=0.25))
self.wait(0.5)
# myAnimation(wordsForIntro= ['OMPLEX', 'ONCEPTS', 'OMPREHENSIBLE'])
################################################################################
# MAIN CODE OF THE FIGURE STARTS HERE #
# NO FUNCTION OR ANYTHING OF THAT SORT #
################################################################################
# Building Mobjects of Word Vectors and arranging them horizontally
Word_Vectors = VGroup(
*[ Matrix(word.reshape(-1,1)).set_column_colors([GREEN_A,GREEN_E]) for word in sentence_embed]
)
Word_Vectors.arrange(RIGHT, buff=1)
# Building Text Mobjects of words corresponding to Word Vectors (May replace it in future if I find how to caption Mobjects)
Words = VGroup(
*[Text(word,
slant = ITALIC,
color = BLUE_C,
font_size = 64) for word in sentence_split]
)
for i in range(len(Word_Vectors)):
Words[i].next_to(Word_Vectors[i], DOWN*2)
# Caption = Text("Embeddings are high dimensional-vectors that represent words \n and its semantics mathematically since models cannot interpret words directly.",
# slant = ITALIC,
# line_spacing=1,
# font_size = 32).next_to(Word_Vectors, UP*2)
# Creating and triggering the animation
self.play(Create(Word_Vectors, run_time = 4))
self.play(
AnimationGroup(
*[ DrawBorderThenFill(word) for word in Words],
lag_ratio = 1
), run_time = 4
)
self.play(
AnimationGroup(
*[ ApplyWave(word, ripples=4) for word in Words],
lag_ratio = 1
), run_time = 4
)
self.wait(8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment