ORIGINAL ARTICLE: Christoph F. Kurz and Adriana N. König. Machine learning for potion development at Hogwarts. arXiv:2307.00036 (2023). DOI: https://doi.org/10.48550/arXiv.2307.00036
Article Abstract: Objective: To determine whether machine learning methods can generate useful potion recipes for research and teaching at Hogwarts School of Witchcraft and Wizardry. Design: Using deep neural networks to classify generated recipes into a standard drug classification system. Setting: Hogwarts School of Witchcraft and Wizardry. Data sources: 72 potion recipes from the Hogwarts curriculum, extracted from the Harry Potter Wiki. Results: Most generated recipes fall into the categories of psychoanaleptics and dermatologicals. The number of recipes predicted for each category reflected the number of training recipes. Predicted probabilities were often above 90% but some recipes were classified into 2 or more categories with similar probabilities which complicates anticipating the predicted effects. Conclusions: Machine learning powered methods are able to generate potentially useful potion recipes for teaching and research at Hogwarts. This corresponds to similar efforts in the non-magical world where such methods have been applied to identify potentially effective drug combinations.
In the world of Harry Potter, potions are magical drinks that can do amazing things, like changing your appearance or making you lucky. But making potions is not easy. You need special equipment, ingredients, and skills. Even a tiny mistake can ruin your potion or cause an accident. Just ask Neville Longbottom, who got boils all over his face in class.
​
At Hogwarts, students learn how to brew potions from the first to the fifth year. If they are good enough, they can continue studying potions in their sixth and seventh years. Some of the best potion makers in history are Professor Snape, who taught potions at Hogwarts for many years, and Harry Potter, who followed some secret tips in his textbook .
​
But what if there was a way to create new potions without risking explosions or poisoning? What if we could use artificial intelligence (AI) to help us? AI is a branch of computer science that can learn from data and solve problems. It has been used for many things, like finding new drugs or designing molecules.
​
In this work, we use AI to generate recipes for magic potions. We use a neural network, which is a type of AI that can learn from examples, to predict what effect a potion will have based on its ingredients. We also use a random generator to create new combinations of ingredients that have never been used before. We hope that this will help us discover new and exciting potions that can benefit the wizarding world.

Data Import

First, we import the data. We collected all recipes from various Harry Potter fan sites and wikis and classified them
according to the Anatomical Therapeutic Chemical (ATC) classification system:
dataClean=Import["https://raw.githubusercontent.com/krz/hp-potion-discovery/main/hp_potions.wl"]

Model Development

Load the Pre-trained Model

Load the BioBERT model. This language model has been pre-trained on large-scale biomedical corpora comprising over 18
billion words from PubMed abstracts and full-text articles.
In[]:=
sentenceembedding=NetAppend[NetModel["BioBERT Trained on PubMed and PMC Data"],"last"->SequenceLastLayer[]]
Out[]=
NetChain
Inputport:
string
Outputport:
vector(size: 768)

How many recipes belong to each class:
In[]:=
Counts@Values[dataClean]//Dataset
Out[]=
dermatologicals
8
psychoanaleptics
21
poison
11
anesthetics
2
various
8
musculo-skeletal system
12
antiparasitic products, insecticides and repellents
2
antiinfectives for systemic use
4
psycholeptics
2
sensory organs
1
respiratory system
1

Finetuning of the Model

Build embeddings on training data
In[]:=
trainembeddings=sentenceembedding[dataClean[[All,1]],TargetDevice->"CPU"]->dataClean[[All,2]];
Add a new Softmax layer for classification
In[]:=
classifierhead=NetChain[{DropoutLayer[],11,SoftmaxLayer[]},"Output"->NetDecoder[{"Class",{"dermatologicals","psychoanaleptics","poison","anesthetics","various","musculo-skeletal system","antiparasitic products, insecticides and repellents","antiinfectives for systemic use","psycholeptics","sensory organs","respiratory system"}}]]
Out[]=
NetChain
uniniti
aliz
ed
Inputport:
array
Outputport:
class


Model Training

Train the model on the recipes
In[]:=
bertresults=NetTrain[classifierhead,trainembeddings,TargetDevice->"CPU",MaxTrainingRounds->2000]
Out[]=
NetChain
Inputport:
vector(size: 768)
Outputport:
class

Show an example recipe
In[]:=
data[[1,1]]
Out[]=
Add crushed snake fangs to your cauldron and stir.Slice your Pungous Onions finely and place in cauldron, then heat the mixture.Add dried nettles. Add a dash of Flobberworm Mucus and stir vigorously.Add a sprinkle of powdered ginger root and stir vigorously again.Add pickled Shrake spines. Stir gently, so as not to overexcite the Shrake spines.Add a glug of stewed horned slugs. Add porcupine quills. Finally, wave your wand over the cauldron to finish the potion.

Generate New Recipes

Generate new recipes with between 3 and 8 individual steps
In[]:=
sentences=StringJoin@Keys[dataClean];
In[]:=
genSentences[]:=Module[{sen=StringJoin[StringJoin[#,"."]&/@RandomSample[StringSplit[sentences,"."],RandomInteger[{3,8}]]]},{sen,bertresults[sentenceembedding[sen],{"TopProbabilities",1}]}]
Generate example recipe and classify it
In[]:=
genSentences[]
Out[]=
{ Add five sliced caterpillars. Allow to simmer until the potion turns purple. Add 2 measures of Standard Ingredient to the mortar. Add Baneberry. Add three flying seahorses. Stirring Simmering lowers heat. Add Wormwood. Add the ground unicorn horn until it turns turquoise.,{psychoanaleptics0.996987}}
Generate 10000 random recipes and classify them (this takes ~40min)
In[]:=
SeedRandom[34231];res=Table[{genSentences[]},10000];
Flatten results
In[]:=
resFlat=Flatten[res[[All,All,2]],1];

Results

Most of the 10, 000 generated recipes fall into the psychoanaleptics category (n = 5549), followed by the dermatologicals
category (n = 1539) and the various category (n = 1487). 225 recipes fall into the poison category.
Figure 1: Predicted potion categories of the 10000 randomly generated recipes
In[]:=
BarChart[<|ReverseSort@Counts@Keys@Flatten@Normal@resFlat|>,ChartLabels->Placed[Keys@ReverseSort@Counts@Keys@Flatten@resFlat,Below,Rotate[#,90Degree]&],PlotTheme"Detailed",FrameLabel{None,"Number of generated recipes"}]
Out[]=
In[]:=
ReverseSort@Counts@Keys@Flatten@resFlat
Out[]=
psychoanaleptics5549,dermatologicals1539,various1487,musculo-skeletal system896,antiinfectives for systemic use253,poison225,antiparasitic products, insecticides and repellents23,anesthetics21,psycholeptics3,respiratory system3,sensory organs1
Predicted probabilities of belonging to a certain ATC category were often above 90%.
Figure 2: Histogram of the predicted probabilities
In[]:=
Histogram[Values@Flatten@resFlat,50,"Count",PlotTheme"Detailed",FrameLabel{"Probability","Count"}]
Out[]=
Some example recipes and their classification results:
In[]:=
bertresults[sentenceembedding["Add Baneberry. Add 2 bundles of knotgrass to the cauldron. Add Dogbane. Add syrup of hellebore until thepotion turns turquoise. Add a sprig of Peppermint to counteract side-effects. Add Honey water until it turnsback to a turquoise colour. Stir four times anti-clockwise. Add the Infusion of Wormwood."],{"TopProbabilities",3}]
Out[]=
{musculo-skeletal system0.00018395,various0.000304334,psychoanaleptics0.99944}
In[]:=
bertresults[sentenceembedding["Add Stewed Mandrake. Add Wormwood. Add a dash of Flobberworm Mucus and stir vigorously. Leave to brewand return in 8 hours (Copper), 14 hours (Brass), or 23 hours (Pewter). Shake and add wormwood until the potionturns green. Slice bursting mushrooms with knife, add to cauldron and stir clockwise until potion turns blue."],{"TopProbabilities",3}]

Conclusion

We used AI to create new potion recipes for Hogwarts. We found many new ways to mix ingredients and stir them to get different effects. AI can also help us find good or bad combinations of drugs or potions. But AI is not perfect. It needs a lot of data, and potions can be very different even in the same category. Also, AI can be dangerous if used for evil purposes. Imagine if someone made a potion stronger than anything Lord Voldemort ever used! And we are just muggles, so we don’t know how well our potions really work.

CITE THIS NOTEBOOK

Machine learning for potion development at Hogwarts​
by Christoph Kurz​
Wolfram Community, STAFF PICKS, August 3, 2023
​https://community.wolfram.com/groups/-/m/t/2982938