Live data from Hacker News

Using a Markov chain to generate readable nonsense with 20 lines of Python

benhoyt.com

41–50 of 77 posts

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#41
post #33
post #29

Earlier quoted context omitted.

Also: it works much better in English than in other languages I know. English grammar is simple, and many of the function words carry a strong prediction for the next tokens.

Which languages are harder for LLMs? Is there any writeup or analysis of this?

Any with inflections and particularly with grammatical gender.

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#42
post #9

As many know and point out, this idea is now very old (40+ years?). The big problem is that it creates "word salad" [0]. In the generative art and procgen community, the term "10,000 bowls of oatmeal" problem has been used [1]. Taking a step back, this is a perennial problem, even with AI old and new. I've heard that the early (good) chess bots, after Deep Blue, had a problem of only being locally context sensitive a…

The output is "word salad" because the probabilities of the model are uniform, albeit implicitly- eyballing the python, it selects the next n-gram uniformly at random. A better n-gram model would calculate the probability of an n-gram following another n-gram according to their frequency in the training corpus. With a larger corpus and better training techniques (some smoothing for one thing) you'd get much more cohe…

> A better n-gram model would calculate the probability of an n-gram following another n-gram according to their frequency in the training corpus. With a larger corpus and better training techniques (some smoothing for one thing) you'd get much more coherent output.

I don't understand what you mean here, isn't that exactly what an n-gram model is already designed to achieve where n > 1? Since this is a 2-gram model isn't it already doing this?

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#43
When I read about Markov Chains and hallucination of LLMs, the first thing that comes to my mind is Reggie watts [0] performances, also remind me i used to do the same thing when i was bored in the classroom, but on my paper notebooks,just nonsense that seems plausible, it was kind of entering in a meditative state, it felt good. when i discovered watts i thought to my self, 'what?! I cloud have done that for a living?'.

[0] https://www.youtube.com/watch?v=UXyHf_SpUUI

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#44
post #33
post #29

Earlier quoted context omitted.

Also: it works much better in English than in other languages I know. English grammar is simple, and many of the function words carry a strong prediction for the next tokens.

Which languages are harder for LLMs? Is there any writeup or analysis of this?

Idk. First, the obvious candidates: those with small, low quality text corpora. Second, I expect LLMs to perform less on long distance dependencies. English has a pretty strict word order, and the constituents are close to each other. There's also a lot of input available, so there are more large gaps to learn from. But (some) Germanic languages have some weird rules which allows constituents to move far away, so it's likely LLMs will make more errors there. Also, free word order languages might be a problem, although I've always suspected that those languages aren't that free in reality: speakers probably have strong preferences.

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#45
post #9

As many know and point out, this idea is now very old (40+ years?). The big problem is that it creates "word salad" [0]. In the generative art and procgen community, the term "10,000 bowls of oatmeal" problem has been used [1]. Taking a step back, this is a perennial problem, even with AI old and new. I've heard that the early (good) chess bots, after Deep Blue, had a problem of only being locally context sensitive a…

The output is "word salad" because the probabilities of the model are uniform, albeit implicitly- eyballing the python, it selects the next n-gram uniformly at random. A better n-gram model would calculate the probability of an n-gram following another n-gram according to their frequency in the training corpus. With a larger corpus and better training techniques (some smoothing for one thing) you'd get much more cohe…

The output is "word salad" because the probabilities of the model are uniform, albeit implicitly- eyballing the python, it selects the next n-gram uniformly at random.

From a quick look, it doesn't seem to sample uniformly. w3 is added to a list for the context w1, w2, not a set. So, say word A occurs twice as often in a particular context as B, it will be in the list twice as often. So, even though a uniform choice function is used, the probability of A getting samples is twice as high.

You get a word salad because a trigram model has to little context to do anything else. This is a well-known issue with Markov and hidden Markov models.

(Fun fact: some hidden Markov model taggers switch to a different trigram distribution for V2 languages after seeing the finite verb, otherwise they often fail catastrophically in the verb cluster due to the limited context.)

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#46
post #28
post #16

I was doing a similar experiment recently to generate random names that sound like names from a specific language. I was breaking the list of names apart into 3 and 2 letter parts, marking which fragments are from the start, middle and end. To generate the words I started from a random one from the start fragments, then continued with a random one from the middle fragments that starts with the latter that the previou…

I remember finding a set of english names generated with a markov chain, I wish I know how to make it, they sounded good.

> I wish I know how to make it

You can learn! I know it's easy to say something is easy to a beginner, but figuring out Markov chains is truly something you can get the basics of over a weekend if you've ever written any software at all.

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#47
post #9

As many know and point out, this idea is now very old (40+ years?). The big problem is that it creates "word salad" [0]. In the generative art and procgen community, the term "10,000 bowls of oatmeal" problem has been used [1]. Taking a step back, this is a perennial problem, even with AI old and new. I've heard that the early (good) chess bots, after Deep Blue, had a problem of only being locally context sensitive a…

And the logic salad problem will not be solved by LLMs because there is nothing in their construction that makes them capable of understanding logic.

Can you elaborate? I would argue that logic has even stronger token prediction characteristics than language.

As a total handwave, I would expect an LLM trained on formal logic and a huge corpus of proofs to produce pretty strong logic output.

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#48
I had an idea.

What if you trained a markov chain and got weights of every pair or sequence of pairs for a certain length.

Could you also do rotations with this information in vector space? With multiple points of freedom?

What if you could do inverse kinematics with markov chains?

Like robot planning for a goal?

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#49
post #10

I wonder how this would behave if trained on 1 trillion words like the LLMs. Also, Is training a Markov cheaper that training neural nets? It would be a great way to cut AI costs if they could be made as effective as neural nets. There is also an interesting post by Orange duck. [0] [0] https://theorangeduck.com/page/17-line-markov-chain

The only way to make it sound less like a word salad is to increase the n-gram length (e.g. use the last 10 words to predict the next), but at that point it starts repeating the corpus. I guess technically you can train on a huge corpus like those of NNs to mitigate that, but you’ll end up with more refined word salads then (edit: refined as in “locally coherent but globally still a word salad”)

Yes, the problem with such long context lengths is sparsity - you still don't have enough data points to make representative many-gram distributions. This was exactly the motivation for Bengio and others to propose neural language models in 2003:

https://dl.acm.org/doi/10.5555/944919.944966

I recommend everyone interested in neural language models and/or wondering why we can't scale up Markov models by just using longer ngrams to read this paper. It's pretty accessible and explains how we got to where we are now in 2023.

Re: Using a Markov chain to generate readable nonsense with 20 lines of Python

#50
post #5

This is exactly the approach I used back in the late 80s; I published a Markov chain text generator on comp.sources.games that digested Usenet posts, built tables and used the previous two words to generate the next word. It was mildly amusing at the time. Someone resurrected it and put on GitHub, which I'm fine with.

Mark V. Shaney?

No, it was called markov3. Mark V. Shaney was similar but from Bell labs. See https://github.com/cheako/markov3 (not coded very well but I got better).
Post reply on HN