fun little pytorch thing i came across today (yes it's super basic, but felt compelled to post it anyway): when adding two tensors together if there's no singleton tensor (e.g. (3,) and (3, 1)) then it'll fail if the two tensors don't share a dimension.
for matmul the inner dimensions need to be the same, but the rules are different for tensor addition
bigram model can hit the same NLL floor which the data file calculates. in this case, we hit 2.4557, which gets within 0.002 in 1000 steps with a learning rate of 50. (the floor is about 2.454)
data file is used to mathematically calculate the bigram character distributions directly--we get the best possible distributions based on our dataset. so we find the NLL minimum directly by counting.
bigram file allows us to search for it with gradient descent. we're able to learn the optimal distribution in W because if a row of W is ln(p), softmax returns p; which means W *can* hold the exact best distribution, and gradient descent helps us find it.
https://t.co/b19vsNxnQP learns the values. we first get the dataset by collecting the character pairs, build a dict of the tokens, and two dicts for str -> ID and ID -> str (stoi, itos).
we create xs and ys, two tensors, and append the first and second character of each pair into their tensors respectively. shoutout sliding window
from there, we one-hot encode the inputs. we do this because we don't want the model to assume a hierarchical/distance-type relationship from the numeric IDs. using one-hot encoding to create a simplex allows us to have numeric IDs for each character while keeping their vectors equidistant.
we instantiate the weights matrix W with torch.randn, and ensure that requires_grad=True for the backward pass, and then run the training loop.
1) logits by xenc.float() @ W
2) make the scores positive via e^x
3) normalizing the scores: dividing the e^x'd scores by the per-row sums. each row is now a probability distribution over the next character, which adds up to 1
with our outputs (based on ys), we calculate our NLL loss, and then perform our backward pass. shoutout .backward()
then at the end, we update the weights via the gradients, reset the gradients, and loop this over 1000 iterations. learning rate 50 seemed to do the trick to get 2.4557--100 kept circling around 2.49 and 2.51.
the weights are saved, and names can be generated!
https://t.co/VsKTCaqUQv is one method to get to the loss floor (2.454). we take each character pair, count the frequencies, and create a dict of the per-pair conditional probabilities (given a character, what's the chance we see the second one). we get it by taking a pair's count, and dividing by the total count of every pair that starts with that same first character. we then calculate NLL by taking -ln(p) of each, weighted average of all -ln(p) scores (the "surprise score" of each character pair, weighted by the number of occurrences in the actual names.txt dataset), and voila, 2.454.
very cool that for one-hot encoding (for characters), there's already a mathematical concept which underlines what we need. a simplex!
it allows us to have vectors which are equidistant from each other, but allows us to still note the numeric ID of that letter in the vector
okay. NOW i get how NLL (negative log-likelihood) works as a loss function.
we take the negated natural log (-ln(p) for a probability p of the true outcome) of how much the model is confident that the actual outcome happened, then add them up together and take the average of the total number of occurrences.
we want to push the probabilities through some function because if you multiply decimals over and over again, it'll eventually become so small that python will write the number as 0.0. so we need to normalize (is that the right term?) the numbers.
with ln, if p <= 1, ln(p) <= 0. so that's why we do -ln(p), to flip the sign.
aside from the derivative being simple, why ln?
2) now trying to build a basic network to learn the distributions. it's still gonna be gibberish because there's nothing that gets you full character context (e.g. attention) just yet.
so currently working on understanding a loss score then can make a basic network. very interesting stuff
it's hard to tell just what is interesting so far because the scale is so minute, but the more complex part here is the means by which this model learns (loss score based on the data); so right now that's the most interesting part. aiming to implement and train a GPT by friday, so we'll see how far i can get
making a neural network which predicts the next best character. so yes, the karpathy lecture (will post repo when done)
a couple interesting things came up
1) in selecting the next best character, simply going off of probabilities doesn't work. say i have "ea" in the name so far, and we need the next character.
So there's a distribution of what's next based on pairs: "aa", "ab", etc.
simply going off of random.choices() where the weights are the calculated weights still produces gibberish which was expected but also fun to see
really small step, but built a basic training loop! So the micro network I made infact actually learns.
Still largely unable to discern what I found the most interesting; so I think building and training a basic GPT seems like the logical next step