<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://www.passaglia.jp/">
  <title>Sam&#39;s Feed</title>
  <subtitle>Notes from Tokyo.</subtitle>
  <link href="https://www.passaglia.jp/feed.xml" rel="self" />
  <link href="https://www.passaglia.jp/" /> 
  <updated>2023-06-29T00:00:00Z</updated>
  <id>https://www.passaglia.jp/</id>
  <author>
    <name>Sam Passaglia</name>
    <email></email>
  </author>
  <entry>
    <title>Ranking Japanese LLMs with Rakuda</title>
    <link href="https://www.passaglia.jp/llm-ranking/" />
    <updated>2023-06-29T00:00:00Z</updated>
    <id>https://www.passaglia.jp/llm-ranking/</id>
    <content xml:lang="" type="html">&lt;p&gt;&lt;span style=&quot;color:#1e50a2&quot;&gt; &lt;em&gt;This post also appears on the &lt;a href=&quot;https://yuzuai.jp/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;YuzuAI blog&lt;/a&gt;, and all of the code and data used to get these results is available on &lt;a href=&quot;https://github.com/yuzu-ai/japanese-llm-ranking&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;github&lt;/a&gt;.&lt;/em&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The open-source community has been hard at work trying to catch up to closed Large Language Models like ChatGPT. Open models — models whose insides are released publicly — are important because they enable AI research and development outside of the control of large corporations.&lt;/p&gt;
&lt;p&gt;In English, the best open models are now at roughly the level of the GPT-3.5 model that powers the free tier of ChatGPT. The Japanese performance of open models seems to lag behind, however, and the Japanese open-source community is not as active as the community in the West. Nonetheless the past couple months have seen the release of a &lt;a href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b-instruction-ppo&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;3.6-billion parameter model&lt;/a&gt; by &lt;a href=&quot;https://rinna.co.jp/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;rinna&lt;/a&gt; and the 7-billion parameter &lt;a href=&quot;https://huggingface.co/cyberagent/open-calm-7b&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;open-calm&lt;/a&gt; model by &lt;a href=&quot;https://www.cyberagent.co.jp/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;CyberAgent&lt;/a&gt;, both trained exclusively on Japanese text.&lt;/p&gt;
&lt;p&gt;How good are open-source models in Japanese, and how do they compare to the &lt;a href=&quot;https://passaglia.jp/gpt-japanese/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;strong Japanese performance of ChatGPT&lt;/a&gt;? And within the open-source space, how do large models trained primarily in English compare to models which are smaller but which focus their training and tokenization architectures on Japanese?&lt;/p&gt;
&lt;p&gt;To answer these questions, we introduce here the &lt;strong&gt;Rakuda&lt;/strong&gt; benchmark and ranking for Japanese AI assistants. Read on for details about how it works, or head directly to &lt;a href=&quot;https://yuzuai.jp/benchmark&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the leaderboard&lt;/a&gt; to see how the first 6 competitors stack up!&lt;/p&gt;
&lt;h1 id=&quot;the-rakuda-benchmark&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/llm-ranking/#the-rakuda-benchmark&quot;&gt;The Rakuda Benchmark&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;One powerful way to test an LLM is to ask it questions to which you know the answer. This is the approach taken by many popular benchmarks like the Japanese General Language Understanding Evaluation (&lt;a href=&quot;https://github.com/yahoojapan/JGLUE&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;JGLUE&lt;/a&gt;). In order to measure a model’s performance on these questions unambiguously, these benchmarks are often designed as multiple choice tests. For a ranking of models on JGLUE, see this &lt;a href=&quot;https://github.com/Stability-AI/lm-evaluation-harness/tree/jp-stable&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;StabilityAI fork of EleutherAI’s lm-evaluation-harness&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But LLMs are capable of much more than answering multiple choice questions. ChatGPT and similar models can write detailed, reasoned, and pertinent responses to open-ended questions. The performance of these AI assistants is defined not by a clear-cut right-or-wrong benchmark but by how useful they are to their users, and we would like to measure that usefulness.&lt;/p&gt;
&lt;p&gt;One way to measure usefulness is to simply ask users directly. This is the approach taken by LMSYS’s &lt;a href=&quot;https://lmsys.org/blog/2023-05-03-arena/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Chatbot Arena&lt;/a&gt;. They provide a &lt;a href=&quot;https://chat.lmsys.org/?arena&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;web interface&lt;/a&gt; where users can input questions, just like they would to ChatGPT, and LMSYS serves them back answers generated by two different AI assistants. The user is asked to decide which answer is better, and based on users’ preferences LMSYS creates an &lt;a href=&quot;https://chat.lmsys.org/?leaderboard&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;LLM leaderboard&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We’ll talk more about just how to create such a ranking down below, but in the meantime it’s important to understand that this approach is in some ways very easy, because you don’t need to come up with a list of questions and answers yourself, and in other ways hard, because you have to serve models reliably and quickly to users on-demand.&lt;/p&gt;
&lt;p&gt;A simpler approach is to come up with a list of questions yourself. Since the question list is fixed, you can generate all the answers at once and you don’t have to serve any models to users over the internet. You still don’t need to specify answers to the questions, because you can still just compare models’ responses against each other rather than against some ground truth. The questions can be open-ended and admit a wide range of possible answers, and they can be designed to test exactly the skills you’re interesting in.&lt;/p&gt;
&lt;p&gt;Benchmarks and rankings based on open-ended question lists have proven incredibly useful in stimulating open-source development. Benchmarks based on the &lt;a href=&quot;https://lmsys.org/vicuna_eval/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Vicuna list&lt;/a&gt; are used in many papers, and Huggingface uses a similar list to produce its &lt;a href=&quot;https://huggingface.co/spaces/HuggingFaceH4/human_eval_llm_leaderboard&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;LLM assistant leaderboard&lt;/a&gt;. But so far there has been no such benchmark for Japanese.&lt;/p&gt;
&lt;p&gt;That’s why we designed &lt;strong&gt;Rakuda&lt;/strong&gt;, the first question set designed to evaluate how well AI Assistants can answer Japanese-specific questions. We’re releasing it to the public as a &lt;a href=&quot;https://huggingface.co/datasets/yuzuai/rakuda-questions&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;dataset on Huggingface&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rakuda&lt;/strong&gt; contains 40 questions in Japanese about Japanese history, society, government, and geography, and most questions admit a wide range of possible answers. Here’s an example question from each category&lt;/p&gt;
&lt;pre class=&quot;language-plaintext&quot;&gt;&lt;code class=&quot;language-plaintext&quot;&gt;## Rakuda questions&lt;br /&gt;&lt;br /&gt;歴史： 幕末に西洋と接触した結果、日本で生じた社会変革について説明してください。&lt;br /&gt;# History: Explain the societal transformations that occurred in Japan&lt;br /&gt;#          as a result of contact with the West during the Bakumatsu.&lt;br /&gt;&lt;br /&gt;社会： 日本の労働市場の女性活躍推進に関する政策とその結果について説明してください。&lt;br /&gt;# Society: Explain the policies that have been implemented to promote&lt;br /&gt;#          the success of women in the Japanese workplace,&lt;br /&gt;#          and those policies&#39; results.&lt;br /&gt;&lt;br /&gt;政治： 日本の議院内閣制度を説明し、そのメリットとデメリットを詳述してください。&lt;br /&gt;# Government: Explain Japan&#39;s parliamentary system of government,&lt;br /&gt;#             and its merits and demerits.&lt;br /&gt;&lt;br /&gt;地理： 熊野古道は、日本のどの地方に位置し、主に何のために使われていましたか。&lt;br /&gt;# Geography: Where is the Kumano Kodō,&lt;br /&gt;#            and what was it primarily used for?&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We got these by asking ChatGPT-4 to generate useful questions to evaluate Japanese high-school students. We kept the ChatGPT-4 outputs we liked, removed questions we thought were bad, and lightly edited some for clarity.&lt;/p&gt;
&lt;h1 id=&quot;evaluating-and-ranking-models-on-rakuda&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/llm-ranking/#evaluating-and-ranking-models-on-rakuda&quot;&gt;Evaluating and Ranking Models on Rakuda&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Rakuda&lt;/strong&gt; does not contain an answer key. Models are evaluated by generating answers to the questions and then comparing their answers against each other.&lt;/p&gt;
&lt;p&gt;Ideally we would get humans reviewers to do these comparisons, looking at two models’ outputs to the same question and deciding which answer is better. But doing all pairwise combinations for 6 LLMs on 40 questions would take 6x5x40 = 1200 reviews (we want to compare outputs in both A-B and B-A orderings to eliminate any primacy bias of the reviewers). That’s a lot of work for a human, so instead we ask the best LLM we have access to, GPT-3.5, to do the reviewing. We can do this because &lt;a href=&quot;https://arxiv.org/abs/2306.05685&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;a recent paper&lt;/a&gt; showed that GPT-3.5 agrees with human reviewers 83% of the time. GPT-4 does even better, so please contact us if you have API access to GPT-4 and would be willing to help us improve our benchmark.&lt;/p&gt;
&lt;p&gt;We send GPT-3.5 a question from the Rakuda list and ask it to choose which of two answers is better. We also allow it to choose a draw, but it doesn’t do so very often. We do this for all possible model match-ups for every question and end up with a long table that looks like&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;model_1    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; model_2   &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; winner&lt;br /&gt;------------------------------&lt;br /&gt;gpt-3      &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; open-calm &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;rinna-3.6b &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; gpt-3     &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;rinna-3.6b &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; open-calm &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1200&lt;/span&gt; rows&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To turn this list of pairwise results into a model ranking, perhaps the simplest thing we could do is to rank models based on their win rate across all matches. If all models play the same number of matches against the same opponents, then their win rate is probably the closest thing to their ‘true’ strengths.&lt;/p&gt;
&lt;p&gt;But we would also like to be able to compute a ranking when these conditions are violated. Since computing all pairwise combinations for $$N$$ models requires $$\mathcal{O}(N^2)$$ matches, computing every pairwise combination will rapidly become intractable. Moreover as we add new models to our ranking, we would like to estimate their strength before they’ve played every other model many times. To do that we need a mathematical model that can predict the outcome of a match between opponents that have never played each other.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Bradley%E2%80%93Terry_model&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Bradley-Terry model&lt;/a&gt; is one way to do that. It models one-on-one competitions by assigning to every competitor $$i$$ (in our case a Japanese LLM) a  &lt;strong&gt;strength parameter&lt;/strong&gt; $$\beta_i$$. It then assumes that the probability that competitor $$i$$ beats competitor $$j$$ in a match just depends on their relative strength $$\beta_i - \beta_j$$. A commonly used form for that probability is&lt;/p&gt;
&lt;p&gt;$$ p_{ij} \equiv \textrm{Probability} ( i \textrm{ beats } j) = \dfrac{e^{\alpha + \beta_i - \beta_j}}{1 + e^{\alpha + \beta_i - \beta_j} } $$,&lt;/p&gt;
&lt;p&gt;where we have complicated things a bit by adding a home-field advantage parameter $$\alpha$$ that enhances model $$i$$&#39;s strength. In this context the home-field advantage is any bias the reviewer might have towards or against the model that is shown to it first. We always order match pairs $$(i, j)$$ with $$i$$ as the home team.&lt;/p&gt;
&lt;p&gt;We now want to fit for the vector of model strengths $$\vec{\beta}$$ given our data. We can define the data vector $$\vec{d}$$ as a vector that contains an entry for each match: 1 if the home team wins or 0 if the home team loses. Then the probability of getting our exact data vector given a set of model parameters $$(\alpha, \vec{\beta})$$ is just the product over every match of $$p_{ij}$$ if the home team won the match, or $$(1-p_{ij})$$ if the home team lost. This is the likelihood&lt;/p&gt;
&lt;p&gt;$$ \textrm{Likelihood}(\alpha, \vec{\beta}) = \prod_{\textrm{matches}} p_{ij}^d \times (1-p_{ij})^{1-d} $$&lt;/p&gt;
&lt;p&gt;The best fit parameters are those which maximize the likelihood. This maximization is very easy to do in python with scipy’s &lt;a href=&quot;https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;optimize&lt;/a&gt; library. A couple technical points: first, we deal with draws by counting them as a half-win for each team. Second, because winning probabilities are controlled only by relative model strengths, a constant can be added or subtracted to all model strengths without changing the likelihood. We fix this gauge freedom by imposing that the model strengths all add up to zero.&lt;/p&gt;
&lt;p&gt;Once we have the best-fit model strengths, we would like to know their uncertainties. In our Bayesian framework, the uncertainties are related to the shape of the posterior probability distribution of the parameters, which is proportional to the likelihood times the prior probability of the parameters. There are many ways to estimate the posterior probability distribution, but the one that requires the least amount of brain power is certainly &lt;a href=&quot;https://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Markov chain Monte Carlo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;MCMC is an easy way to sample from high-dimensional distributions, and in Bayesian statistics it is used to sample from the posterior distribution. How far the samples stray from the best-fit points tells us the parameter uncertainties. MCMC is very straightforward to implement using the &lt;a href=&quot;https://emcee.readthedocs.io/en/stable/tutorials/line/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;emcee&lt;/a&gt; package. See &lt;a href=&quot;https://arxiv.org/abs/1008.4686&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Hogg et al&lt;/a&gt; for a primer on Bayesian analysis.&lt;/p&gt;
&lt;p&gt;Before we go on to the results, first a note about Elo scores. Elo scores are another commonly used way to rank the relative skill of players in zero-sum competitions. In fact, they are based on the same underlying Bradley-Terry model we use here. The difference lies in how the player strengths are actually estimated from the data. In the Elo system, scores are gradually updated match-by-match based on the match result and the pre-match Elo scores of the competitors. This is popular with players because they can easily calculate the exact impact a win or loss will have on their score. But from a parameter estimation standpoint it is sub-optimal and will only slowly approach the maximum likelihood values. If you have access to a computer, better to take the Bayesian approach and fit all strengths based on all matches all at once, as we’ve done here.&lt;/p&gt;
&lt;h1 id=&quot;the-rakuda-leaderboard&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/llm-ranking/#the-rakuda-leaderboard&quot;&gt;The Rakuda Leaderboard&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;For our initial release we run our benchmark with 6 models. These are:&lt;/p&gt;
&lt;ul class=&quot;list&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://openai.com/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;GPT-3.5&lt;/strong&gt;&lt;/a&gt; (OpenAI): This is the best model in the ranking and also the model we use as a reviewer. We might worry that as a reviewer GPT-3.5 is biased towards its own answers, but GPT-3.5 is so unambiguously better than other models that such biases are not a major concern.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul class=&quot;list&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/cyberagent/open-calm-7b&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;open-calm-7b&lt;/strong&gt;&lt;/a&gt; (CyberAgent) and &lt;a href=&quot;https://huggingface.co/izumi-lab/stormy-7b-10ep&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;stormy-7b-10ep&lt;/strong&gt;&lt;/a&gt; (Izumi-Lab): open-calm is a 7-billion parameter base model trained by CyberAgent on a Japanese corpus. stormy-7b-10ep is a version of open-calm fine-tuned on &lt;a href=&quot;https://huggingface.co/datasets/izumi-lab/llm-japanese-dataset&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;a large set of question-answer pairs&lt;/a&gt; by &lt;a href=&quot;https://llm.msuzuki.me/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;University of Tokyo researchers&lt;/a&gt;. To try to induce open-calm-7b to give helpful answers, we prompt it to be an assistant.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul class=&quot;list&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;japanese-gpt-neox-3.6b&lt;/strong&gt;&lt;/a&gt;, &lt;a href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b-instruction-sft-v2&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;japanese-gpt-neox-3.6b-sft-v2&lt;/strong&gt;&lt;/a&gt;, and &lt;a href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b-instruction-pp&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;japanese-gpt-neox-3.6b-ppo&lt;/strong&gt;&lt;/a&gt; (rinna): These models are trained by the rinna corporation and for conciseness we will refer to them as &lt;strong&gt;rinna-3.6b&lt;/strong&gt;, &lt;strong&gt;rinna-3.6b-sft&lt;/strong&gt;, and &lt;strong&gt;rinna-3.6b-ppo&lt;/strong&gt;. rinna-3.6b is the base model, while rinna-3.6b-sft is instruction fine-tuned. rinna-3.6-ppo is further trained with proximal policy optimization, a reinforcement learning technique. Again we prompt the base model to try to get it to assist the user.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We get each of these models to answer all the &lt;strong&gt;Rakuda&lt;/strong&gt; questions and then ask GPT-3.5 to judge all the answers against each other. These answers and reviews are all in the &lt;a href=&quot;https://github.com/yuzu-ai/japanese-llm-ranking/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;github repository&lt;/a&gt;. We find the following win rates for each model:&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/NZrE6JOhB6-400.avif 400w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-700.avif 700w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-1280.avif 1280w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-2400.avif 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/NZrE6JOhB6-400.webp 400w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-700.webp 700w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-1280.webp 1280w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-2400.webp 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/NZrE6JOhB6-400.jpeg 400w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-700.jpeg 700w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/NZrE6JOhB6-2400.jpeg 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Win rates of AI assistants on the Rakuda benchmark&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/NZrE6JOhB6-400.jpeg&quot; width=&quot;2400&quot; height=&quot;1600&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;We then use MCMC to estimate the Bradley-Terry parameters and their uncertainties. See &lt;a href=&quot;https://github.com/yuzu-ai/japanese-llm-ranking/blob/main/jrank/bradley-terry.ipynb&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;this notebook&lt;/a&gt; for the calculation. We find the following strengths:&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/Xxc5YFg1jI-400.avif 400w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-700.avif 700w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-1280.avif 1280w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-2400.avif 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/Xxc5YFg1jI-400.webp 400w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-700.webp 700w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-1280.webp 1280w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-2400.webp 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/Xxc5YFg1jI-400.jpeg 400w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-700.jpeg 700w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/Xxc5YFg1jI-2400.jpeg 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Bradley-Terry strengths of AI assistants on the Rakuda benchmark&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/Xxc5YFg1jI-400.jpeg&quot; width=&quot;2400&quot; height=&quot;1600&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;We find that both the overall win-rates and the Bradley-Terry strengths show the same qualitative features. GPT-3.5 is far and away the best Japanese AI assistant among the models we tested. Among the open models, open-calm takes the lead. This is surprising since open-calm-7b is a base model, not an instruction-tuned Assistant. An instruction-tuned model based on open-calm, stormy-7b, takes the next rung of our ranking.&lt;/p&gt;
&lt;p&gt;One of the advantages of the Bradley-Terry approach is that it allows us to quantify how statistically confident we are in our statement that our reviewer (GPT-3.5) prefers open-calm-7b to stormy-7b. By plotting the MCMC samples, we can trace out the posterior distribution for their relative strength. This automatically marginalizes over nuisance parameters like the home-field advantage parameter $$\alpha$$.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/F5mK29ovlj-400.avif 400w, https://www.passaglia.jp/assets/images/F5mK29ovlj-700.avif 700w, https://www.passaglia.jp/assets/images/F5mK29ovlj-1280.avif 1280w, https://www.passaglia.jp/assets/images/F5mK29ovlj-2400.avif 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/F5mK29ovlj-400.webp 400w, https://www.passaglia.jp/assets/images/F5mK29ovlj-700.webp 700w, https://www.passaglia.jp/assets/images/F5mK29ovlj-1280.webp 1280w, https://www.passaglia.jp/assets/images/F5mK29ovlj-2400.webp 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/F5mK29ovlj-400.jpeg 400w, https://www.passaglia.jp/assets/images/F5mK29ovlj-700.jpeg 700w, https://www.passaglia.jp/assets/images/F5mK29ovlj-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/F5mK29ovlj-2400.jpeg 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Relative strength of open-calm-7b and stormy-7b&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/F5mK29ovlj-400.jpeg&quot; width=&quot;2400&quot; height=&quot;1600&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;The MCMC results show that open-calm-7b is preferred to stormy-7b at the 98.7% confidence level. This suggests that the instruction dataset used to fine-tune stormy-7b does not produce an assistant more helpful in the eyes of the reviewer.&lt;/p&gt;
&lt;p&gt;Here are our full results as a table:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Rank&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Model&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Strength&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Win Rate&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Stronger than the next model at confidence level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;1&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;a target=&quot;_blank&quot; href=&quot;https://openai.com/&quot; style=&quot;color:#1e50a2, textDecoration: underline,textDecorationStyle: dotted&quot;&gt;GPT-3.5&lt;/a&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;2.487 ± 0.19&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;94%&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;2&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;a target=&quot;_blank&quot; href=&quot;https://huggingface.co/cyberagent/open-calm-7b&quot; style=&quot;color:#1e50a2, textDecoration: underline,textDecorationStyle: dotted&quot;&gt;cyberagent/open-calm-7b&lt;/a&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;-0.063 ± 0.10&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;52%&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;98.6%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;a target=&quot;_blank&quot; href=&quot;https://huggingface.co/izumi-lab/stormy-7b-10ep&quot; style=&quot;color:#1e50a2, textDecoration: underline,textDecorationStyle: dotted&quot;&gt;izumi-lab/stormy-7b-10ep&lt;/a&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;-0.384 ± 0.10&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;44%&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;90.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;4&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;a target=&quot;_blank&quot; href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b-instruction-ppo&quot; style=&quot;color:#1e50a2, textDecoration: underline,textDecorationStyle: dotted&quot;&gt;rinna/japanese-gpt-neox-3.6b-instruction-ppo&lt;/a&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;-0.575 ± 0.10&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;39%&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;83.6%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;5&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;a target=&quot;_blank&quot; href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b-instruction-sft-v2&quot; style=&quot;color:#1e50a2, textDecoration: underline,textDecorationStyle: dotted&quot;&gt;rinna/japanese-gpt-neox-3.6b-instruction-sft-v2&lt;/a&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;-0.717 ± 0.10&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;36%&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;59.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:left&quot;&gt;6&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;a target=&quot;_blank&quot; href=&quot;https://huggingface.co/rinna/japanese-gpt-neox-3.6b&quot; style=&quot;color:#1e50a2, textDecoration: underline,textDecorationStyle: dotted&quot;&gt;rinna/japanese-gpt-neox-3.6b&lt;/a&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;-0.750 ± 0.10&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;35%&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;N/A&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;We also find a home-field advantage parameter $$\alpha = -0.524 \pm 0.07$$. The model strengths and home-field advantage parameter can be used to predict the outcome of any matchup between these models by using the Bradley-Terry probability formula in the previous section.&lt;/p&gt;
&lt;h1 id=&quot;parting-words&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/llm-ranking/#parting-words&quot;&gt;Parting words&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;We hope that the Rakuda benchmark and leaderboard will stimulate the development of better and more useful open-source LLMs for Japanese. If you have any model you’d like to add to the leaderboard, or any ideas for how to improve Rakuda, please open an issue on the github repository or contact me directly on Twitter &lt;a href=&quot;https://twitter.com/SamPassaglia&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;@SamPassaglia&lt;/a&gt; or on the &lt;a href=&quot;https://discord.com/invite/bHB9e2rq2r&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;YuzuAI discord&lt;/a&gt;. As we add models, we will continuously update &lt;a href=&quot;https://yuzuai.jp/benchmark&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the main benchmark page&lt;/a&gt;.&lt;/p&gt;

    </content>
  </entry>
  <entry>
    <title>How well does ChatGPT speak Japanese?</title>
    <link href="https://www.passaglia.jp/gpt-japanese/" />
    <updated>2023-04-06T00:00:00Z</updated>
    <id>https://www.passaglia.jp/gpt-japanese/</id>
    <content xml:lang="" type="html">&lt;p&gt;&lt;span style=&quot;color:#1e50a2&quot;&gt; &lt;em&gt;Update June 16: I uploaded the code I used to get the results in the latter half of this post to &lt;a href=&quot;https://gist.github.com/passaglia/bd6d30e491c8af7361dcb97243e0cd22&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;github&lt;/a&gt;.&lt;/em&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Large language models, like the ones that power &lt;a href=&quot;https://chat.openai.com/chat&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ChatGPT&lt;/a&gt;, have triggered a lot of excitement in the machine learning and natural language processing communities in the past few years. Though the most advanced LLMs are usually developed with a focus on English, many can also understand and output Japanese.&lt;/p&gt;
&lt;p&gt;In this post I’d like to take a technical look at how these state-of-the-art models work with Japanese, and how good they are at it. This is an important question for Japanese companies: they are behind in the LLM race and need to decide if they should build their own Japanese-focused competitors to the cutting-edge English models.&lt;/p&gt;
&lt;p&gt;I’ll focus on the Japanese capabilities of Open AI’s GPT family of models. These are the largest and most capable LLMs in production. The latest model, &lt;a href=&quot;https://arxiv.org/abs/2303.08774&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GPT-4&lt;/a&gt;, was announced in March 2023, while the older &lt;a href=&quot;https://arxiv.org/abs/2005.14165&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GPT-3&lt;/a&gt; model powers the free tier of ChatGPT. While GPT-4 can also accept images as input, in this post I’ll just focus on the text side of the models.&lt;/p&gt;
&lt;h2 id=&quot;tokenization&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/gpt-japanese/#tokenization&quot;&gt;Tokenization&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The first step taken by any LLM is to split input text into pieces, or &lt;em&gt;tokens&lt;/em&gt;, that the model will at the next step convert to vectors that it can perform mathematical operations on. There are many different ways to tokenize text, but schematically each token represents a word or a piece of a word.  Later, a model outputs text by spitting out one token at a time. Since tokenization is key to a model’s input and output, understanding how text in different languages is tokenized is important to understanding how a model will perform in those languages.&lt;/p&gt;
&lt;p&gt;GPT models are able to take in Japanese text because they use a very flexible tokenizer, &lt;a href=&quot;https://github.com/openai/tiktoken&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;tiktoken&lt;/a&gt;. OpenAI has open-sourced tiktoken, so we can carefully study how it works for Japanese.&lt;/p&gt;
&lt;p&gt;tiktoken is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Byte_pair_encoding&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Byte Pair Encoder&lt;/a&gt;. Byte pair encoders take in text as a stream of bytes, which in tiktoken’s case represent the UTF-8 encoding of Unicode characters. For example the byte &lt;code&gt;01100001&lt;/code&gt; represents the character ‘a’. Because one byte can only have $$2^8 = 256$$ different values and Unicode includes more than a million different characters, most non-Latin characters are represented by more than one byte. For example the first Hiragana character ‘あ’ is represented in UTF-8 by the three-byte sequence &lt;code&gt;11100011&lt;/code&gt; &lt;code&gt;10000001&lt;/code&gt; &lt;code&gt;10000010&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Byte pair encoders then compress the byte stream by replacing the most commonly appearing consecutive pair of bytes with a single symbol. ‘Most commonly appearing’ means over some reference dataset, which in GPT’s case is probably something like a large sample of the internet. Once the most commonly appearing pair of bytes has been replaced, the byte pair encoder now has a &lt;em&gt;vocabulary&lt;/em&gt; of 257 tokens: 256 bytes plus the one new symbol which represents the most common pair.&lt;/p&gt;
&lt;p&gt;The byte pair encoder then repeats the process, merging the next most common pair of tokens into a new one, continuing until the vocabulary reaches some chosen size. For GPT-4 and the latest GPT-3 variants, this is roughly 100,000 tokens.&lt;/p&gt;
&lt;p&gt;Byte pair encoding can be viewed as a lossless compression algorithm. At the expense of writing down a large vocabulary list that tells you how to map between tokens and byte sequences, you can replace frequently occurring sequences in your data with a single token.&lt;/p&gt;
&lt;p&gt;Let’s look at some tiktoken encodings.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tiktoken&lt;br /&gt;&lt;br /&gt;enc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tiktoken&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encoding_for_model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gpt-4&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;enc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;dog&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;# =&gt; [18964]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see that the word ‘dog’, which would take 3 bytes to represent, is represented by the single token &lt;code&gt;18964&lt;/code&gt;. ‘dog’ must have been a frequently appearing sequence in the byte pair encoder’s reference dataset.&lt;/p&gt;
&lt;p&gt;Now let’s look at what happens in Japanese.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;enc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;犬&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# &#39;dog&#39; in Japanese&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;# =&gt; [163, 232, 105]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unlike in English, the single Japanese character for ‘dog’ takes three tokens to represent. These three tokens point directly to the three bytes that make up the character in UTF-8. The dataset that OpenAI used to make the byte-pair encoding for GPT-4 must have been dominated by English text so that common Japanese byte sequences like the ones in ‘犬’ were not optimized.&lt;/p&gt;
&lt;p&gt;This biased tokenization compounds for longer texts. Let’s compare the tokenization of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Lord&#39;s_Prayer&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;em&gt;Pater Noster&lt;/em&gt;&lt;/a&gt; in English and Japanese. This is a convenient text because, thanks to the tireless work of Christian missionaries, semantically equivalent versions exist for almost every language on Earth.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;pater.json&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; fp&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;# pater.json is available from gist.github.com/passaglia&lt;/span&gt;&lt;br /&gt;    pater &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; json&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;load&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;enc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pater&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;English&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;English (Early Modern English Dated 1559)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;# ==&gt; 68&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;enc&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;encode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pater&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Japanese&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;日本語 (カトリック(2))&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;# ==&gt; 142&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It takes GPT models more than twice as many tokens to represent the Japanese version of the &lt;em&gt;Pater Noster&lt;/em&gt; than the English version, despite them having the same information content. This points to a major obstacle for the Japanese performance of GPT: the model has to put in twice as much more work to read and output Japanese text than it does for English.&lt;/p&gt;
&lt;p&gt;Poor tokenization is not unique to Japanese. All non-English languages suffer from it to some extent. We can confirm this by looking at the &lt;em&gt;Pater Noster&lt;/em&gt; in other languages.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/gXae4iIAMo-400.avif 400w, https://www.passaglia.jp/assets/images/gXae4iIAMo-700.avif 700w, https://www.passaglia.jp/assets/images/gXae4iIAMo-1280.avif 1280w, https://www.passaglia.jp/assets/images/gXae4iIAMo-2400.avif 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/gXae4iIAMo-400.webp 400w, https://www.passaglia.jp/assets/images/gXae4iIAMo-700.webp 700w, https://www.passaglia.jp/assets/images/gXae4iIAMo-1280.webp 1280w, https://www.passaglia.jp/assets/images/gXae4iIAMo-2400.webp 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/gXae4iIAMo-400.jpeg 400w, https://www.passaglia.jp/assets/images/gXae4iIAMo-700.jpeg 700w, https://www.passaglia.jp/assets/images/gXae4iIAMo-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/gXae4iIAMo-2400.jpeg 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Tokenization of the pater noster in different languages&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/gXae4iIAMo-400.jpeg&quot; width=&quot;2400&quot; height=&quot;1600&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;Languages like Greek and Armenian get especially screwed, requiring respectively 6 and 7 times more tokens than English to represent a semantically equivalent text.&lt;/p&gt;
&lt;p&gt;Of course it’s not necessarily beneficial to represent text in as few tokens as possible. Tokenizers usually try to keep semantically distinct elements of a text from being merged, to help the model learn semantically relevant patterns from the data. tiktoken has rules to keep different English words separate so that very common word pairs like ‘I am’ are not merged into one token. None of these kinds of optimizations exist for Japanese.&lt;/p&gt;
&lt;p&gt;Poor tokenization for non-English languages like Japanese likely affects the downstream performance of GPT on non-English tasks, and moreover since OpenAI charges for use of its models by the token, using GPT models in Japanese or Greek is many times more expensive than in English.&lt;/p&gt;
&lt;p&gt;But before jumping into measurements of model performance let’s look at the rest of the GPT architecture.&lt;/p&gt;
&lt;h2 id=&quot;architecture-and-training&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/gpt-japanese/#architecture-and-training&quot;&gt;Architecture and Training&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This section is necessarily speculative: no significant information has been released about the architecture or training of GPT-4, and both GPT-4 and GPT-3 are closed-source. Nonetheless there is some information in their announcement papers, and we can make educated guesses about their architectures and training from the earlier open-source &lt;a href=&quot;https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GPT-2&lt;/a&gt; and &lt;a href=&quot;https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GPT-1&lt;/a&gt; models.&lt;/p&gt;
&lt;p&gt;All GPT models are &lt;a href=&quot;https://arxiv.org/abs/1706.03762&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;transformer models&lt;/a&gt;. The main difference between them is a steadily increasing model size and a growing and improving set of training data. OpenAI’s most significant innovations have been in the computing infrastructure required to train and deploy larger and larger models rather than in the architecture of the models themselves.&lt;/p&gt;
&lt;p&gt;These transformer models are language agnostic: tokens are treated the same way no matter whether they are originally English or Japanese words. They are first embedded in a high-dimensional vector space, with the matrix that maps tokens to vectors itself a trainable part of the model. A sequence of embedded tokens is then processed through a series of attention blocks, repeatedly outputting and sending to the next block a processed embedding of each token. At the final layer the processed embedding of the last token in the input sequence is projected back onto the embedding matrix to generate an output token. In this way transformer models generate new text token by token at the end of an input sequence. For visualizations of this process, see &lt;a href=&quot;https://jalammar.github.io/illustrated-gpt2/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Jay Alammar’s wonderful blog&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The model trains on a huge amount of data, always trying to predict the next token in an observed sequence of text in the data. The strength of the model depends on the amount and quality of text it is trained on.&lt;/p&gt;
&lt;p&gt;And while the model architecture itself is language agnostic, the training data certainly is not. GPT-1 and GPT-2 trained only on English-language text and therefore could only speak English. GPT-3’s training data was &lt;a href=&quot;https://github.com/openai/gpt-3/blob/master/dataset_statistics/languages_by_character_count.csv&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;92% English and only 0.2% Japanese&lt;/a&gt;. No information is available about the training data used for GPT-4.&lt;/p&gt;
&lt;p&gt;After the initial bulk training, some versions of GPT-4 and GPT-3 are fine-tuned with Reinforcement Learning from Human Feedback (RLHF). OpenAI has put considerable effort into developing RLHF techniques, which involve tuning the model to produce outputs that human evaluators judge as being desirable. The human evaluators encourage the model to follow instructions closely and to reject inappropriate requests.&lt;/p&gt;
&lt;p&gt;OpenAI has not disclosed whether its human feedback pipeline for GPT-4 operates in languages other than English. For GPT-3, the RLHF pipeline was overwhelmingly focused on &lt;a href=&quot;https://arxiv.org/pdf/2203.02155.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;English language instructions and outputs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Because of these training imbalances, as well as the aforementioned tokenization bias, the performance of GPT models in Japanese is not guaranteed to be as good as in English. We have to test it empirically.&lt;/p&gt;
&lt;h2 id=&quot;performance&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/gpt-japanese/#performance&quot;&gt;Performance&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Qualitatively, GPT-3 and GPT-4 perform very well in Japanese. GPT-4 can understand Japanese instructions and answer back in Japanese fluently and naturally, and it rejects inappropriate requests just as it does in English. All around it appears to perform about as well in Japanese as in English, though it is about twice as slow in Japanese due to the suboptimal tokenization.&lt;/p&gt;
&lt;p&gt;Accordingly, the GPT-4 announcement paper spends a significant amount of time touting the model’s multilingual performance. To back this up quantitatively, OpenAI measures GPT-4’s accuracy on the &lt;a href=&quot;https://github.com/hendrycks/test&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;MMLU benchmark&lt;/a&gt;, which is a set of 57 multiple choice exams on subjects like ‘High School US History’ or ‘College Medicine’. The MMLU benchmark is in English, so to test GPT-4’s performance in other languages they first machine-translate the benchmark to other languages using Azure Translate. The performance results reported by OpenAI are as follows (emphasis added):&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/t8uefmh_wF-400.avif 400w, https://www.passaglia.jp/assets/images/t8uefmh_wF-700.avif 700w, https://www.passaglia.jp/assets/images/t8uefmh_wF-1280.avif 1280w, https://www.passaglia.jp/assets/images/t8uefmh_wF-1810.avif 1810w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/t8uefmh_wF-400.webp 400w, https://www.passaglia.jp/assets/images/t8uefmh_wF-700.webp 700w, https://www.passaglia.jp/assets/images/t8uefmh_wF-1280.webp 1280w, https://www.passaglia.jp/assets/images/t8uefmh_wF-1810.webp 1810w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/t8uefmh_wF-400.jpeg 400w, https://www.passaglia.jp/assets/images/t8uefmh_wF-700.jpeg 700w, https://www.passaglia.jp/assets/images/t8uefmh_wF-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/t8uefmh_wF-1810.jpeg 1810w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;GPT-4 performance on MMLU benchmark&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/t8uefmh_wF-400.jpeg&quot; width=&quot;1810&quot; height=&quot;1874&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;We see that GPT-4’s performance differs from language to language, and roughly speaking GPT-4 performs worse in languages that are more distant from English. Japanese reaches 79.9% accuracy, significantly lower than English’s 85.5% accuracy, and also slightly worse than Greek’s 81.4% accuracy despite Greek’s highly inefficient tokenization.&lt;/p&gt;
&lt;p&gt;However, testing multilingual performance on a benchmark machine-translated from English leaves something to be desired. We would like to test GPT-4 on a truly Japanese benchmark and then compare its performance to LLMs developed with a focus on Japanese.&lt;/p&gt;
&lt;p&gt;To do so we can look at the &lt;a href=&quot;https://github.com/yahoojapan/JGLUE&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Japanese General Language Understanding Evaluation (JGLUE)&lt;/a&gt; benchmark. JGLUE is a series of tasks &lt;a href=&quot;https://www.anlp.jp/proceedings/annual_meeting/2022/pdf_dir/E8-4.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;built from the ground up in Japanese&lt;/a&gt; to measure natural language understanding.&lt;/p&gt;
&lt;p&gt;The most difficult task in the JGLUE benchmark is the Japanese Commonsense Question Answering (JCQA) task, which is a series of ~1000 multiple choice questions testing common-sense. Humans achieve 98.6% accuracy on these questions, and the best-ranked open-source model reaches 90.7% accuracy. Here is one of the more difficult questions from JCQA (translations added):&lt;/p&gt;
&lt;pre class=&quot;language-plaintext&quot;&gt;&lt;code class=&quot;language-plaintext&quot;&gt;女性が言われると嬉しくなる形容詞言葉は？&lt;br /&gt;# An i-adjective that women are happy to be called?&lt;br /&gt;&lt;br /&gt;0. 開く # open (verb)&lt;br /&gt;1. 黄色い # yellow (i-adjective)&lt;br /&gt;2. キレイ # pretty (ends in i but is not an i-adjective) # gpt-3&#39;s choice&lt;br /&gt;3. 美しい # beautiful (i-adjective) #gpt-4&#39;s choice &lt;br /&gt;4. 優しい # kind (i-adjective)&lt;br /&gt;&lt;br /&gt;Answer: 3. 美しい # beautiful&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;i-adjectives are a class of adjectives in Japanese which in their uninflected form always end in &lt;em&gt;i&lt;/em&gt; – but not all uninflected adjectives that end in &lt;em&gt;i&lt;/em&gt; are i-adjectives. This question therefore tests some form of uniquely Japanese knowledge that would probably get lost if we machine translated the question to English.&lt;/p&gt;
&lt;p&gt;GPT-3 gets the question wrong, mistaking the na-adjective ‘pretty’ for an i-adjective. This a common mistake among Japanese language learners. GPT-4 gets it right, choosing the i-adjective ‘beautiful’.&lt;/p&gt;
&lt;p&gt;Note that the i-adjective ‘kind’ is also an option, but both GPT-4 and the answer key assume women would prefer to be called beautiful than kind, reflecting the kind of bias that may be built-in to a lot of datasets and models. These kinds of ambiguities likely explain why humans do not get a perfect score on the exam and put a ceiling on the usefulness of JCQA as a benchmark.&lt;/p&gt;
&lt;p&gt;Nonetheless here is an overall performance measurement on JCQA, comparing GPT-3 and GPT-4 both to human performance and to leading Japanese LLMs.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/aMQR15yVQl-400.avif 400w, https://www.passaglia.jp/assets/images/aMQR15yVQl-700.avif 700w, https://www.passaglia.jp/assets/images/aMQR15yVQl-1280.avif 1280w, https://www.passaglia.jp/assets/images/aMQR15yVQl-2400.avif 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/aMQR15yVQl-400.webp 400w, https://www.passaglia.jp/assets/images/aMQR15yVQl-700.webp 700w, https://www.passaglia.jp/assets/images/aMQR15yVQl-1280.webp 1280w, https://www.passaglia.jp/assets/images/aMQR15yVQl-2400.webp 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/aMQR15yVQl-400.jpeg 400w, https://www.passaglia.jp/assets/images/aMQR15yVQl-700.jpeg 700w, https://www.passaglia.jp/assets/images/aMQR15yVQl-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/aMQR15yVQl-2400.jpeg 2400w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Model performance on JCommonsenseQA benchmark&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/aMQR15yVQl-400.jpeg&quot; width=&quot;2400&quot; height=&quot;1600&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;GPT-4 is the best-performing model, greatly surpassing GPT-3’s performance and reaching essentially human-level accuracy on the benchmark. The leading open source model as of April 2023 is the RoBERTa model by Waseda University, which the closed-source HyperCLOVA model developed by LINE &lt;a href=&quot;https://speakerdeck.com/techverse_2022/scaling-and-potential-uses-for-the-hyperclova-japanese-base-model&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;has surpassed&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A couple caveats are in order here though: since GPT-4 API access is not yet widely available, I estimated GPT-4’s accuracy by inputting the questions GPT-3 got wrong into the ChatGPT web interface for GPT-4. The accuracy reported in the figure then assumes that GPT-4 would not get any questions wrong that GPT-3 got right. It is therefore an upper bound on the GPT-4 accuracy, which can be confirmed once the GPT-4 API opens up.&lt;/p&gt;
&lt;p&gt;A more difficult to estimate issue is contamination: GPT-4 is trained on basically the entire internet, and it’s possible that this includes training on the JCQA benchmark itself. The team behind JGLUE have kept part of the benchmark private, as a test set, and evaluating GPT-4 on this subset would allow a truly unbiased measure of GPT-4’s abilities.&lt;/p&gt;
&lt;p&gt;To understand where GPT-4 still has weaknesses, let’s look at a JCQA question that GPT-4 gets wrong.&lt;/p&gt;
&lt;pre class=&quot;language-plaintext&quot;&gt;&lt;code class=&quot;language-plaintext&quot;&gt;船舶を安定させる物と同じ発音なのは、どれでしょう？&lt;br /&gt;# Which of these is pronounced in the same way&lt;br /&gt;# as the object used to stabilize a boat?&lt;br /&gt;&lt;br /&gt;0. 碇 # anchor (pronounced ikari) # gpt-4 choice&lt;br /&gt;1. 怒り # anger (pronounced ikari or okori)&lt;br /&gt;2. 酒 # alcohol (pronounced sake)&lt;br /&gt;3. 興奮 # excitement (pronounced koufun)&lt;br /&gt;4. 伊刈 # a place name (pronounced ikari)&lt;br /&gt;&lt;br /&gt;Answer: 1. 怒り # anger&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This question is about word pronunciation, which is not directly encoded in the Japanese writing system. GPT-4 picks option 0, anchor, which is an object used to stabilize a boat. The answer key prefers option 1, anger, because in Japanese anger can be pronounced in the same way as anchor (disregarding &lt;a href=&quot;https://ja.wikipedia.org/wiki/%E3%82%A2%E3%82%AF%E3%82%BB%E3%83%B3%E3%83%88#%E6%97%A5%E6%9C%AC%E8%AA%9E%E3%81%AE%E3%82%A2%E3%82%AF%E3%82%BB%E3%83%B3%E3%83%88&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;pitch accent&lt;/a&gt;). GPT-4’s answer could be considered correct, but replacing option 0 with an unrelated term still does not lead GPT-4 to consistently pick option 1.&lt;/p&gt;
&lt;p&gt;Option 4, 伊刈, is an area in Saitama which is also pronounced in the same way as anchor and anger, and therefore could also be a correct answer. This might be confusing the model, but GPT-4 still gets the question wrong if both options 0 and 4 are replaced with unrelated words.&lt;/p&gt;
&lt;p&gt;So GPT-4 may have a relatively weak understanding of Japanese word pronunciation. It would be useful to test it on a pronunciation annotated corpus, including words where the pronunciation depends on sentence context, the same task I built &lt;a href=&quot;https://www.passaglia.jp/yomikata/&quot;&gt;yomikata&lt;/a&gt; to address. From preliminary tests, it seems GPT-4 is very powerful but still not perfect at the pronunciation prediction task.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/gpt-japanese/#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;GPT-4 is the best large language model for Japanese. Despite Japanese being no more than an afterthought during tokenization and training, GPT-4 has qualitatively similar capabilities in Japanese as it does in English.&lt;/p&gt;
&lt;p&gt;But because of the poor tokenization, using GPT-4 for Japanese tasks is about twice as slow and twice as expensive as it is in English, and quantitatively GPT-4 does not reach the same performance heights in Japanese as it does in English. If OpenAI is interested in equity across languages, then as a first step it should adjust its per-token pricing strategy to charge less for the tokens used by less-optimized languages.&lt;/p&gt;
&lt;p&gt;In the long-term, Japan needs to develop its own LLMs to avoid reliance on the benevolence of foreign companies. LINE’s &lt;a href=&quot;https://clova.line.me/hyper-clova/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;HyperCLOVA&lt;/a&gt; model is promising, but still far behind the state of the art.&lt;/p&gt;
&lt;p&gt;The ideal LLM for Japanese won’t be one that just trains on Japanese text. OpenAI’s results show that training on a linguistically varied corpus improves performance across all languages. Rather, a model that treats Japanese fairly will be one that tokenizes text in a linguistically equitable way and which ensures that linguistic diversity is prioritized throughout the training process.&lt;/p&gt;
&lt;p&gt;If large language models and their developers don’t value the variety and richness of human languages as they appear on Earth, they will find themselves just perpetuating linguistic injustice and extinction.&lt;/p&gt;

    </content>
  </entry>
  <entry>
    <title>Geography with japandata</title>
    <link href="https://www.passaglia.jp/japandata/" />
    <updated>2023-03-20T00:00:00Z</updated>
    <id>https://www.passaglia.jp/japandata/</id>
    <content xml:lang="" type="html">&lt;p&gt;I often want to visualize some geographic or demographic data about Japan. For example, here is a map I made showing how people migrated between prefectures in 2021.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/93qlZyI7tE-400.avif 400w, https://www.passaglia.jp/assets/images/93qlZyI7tE-700.avif 700w, https://www.passaglia.jp/assets/images/93qlZyI7tE-1280.avif 1280w, https://www.passaglia.jp/assets/images/93qlZyI7tE-1752.avif 1752w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/93qlZyI7tE-400.webp 400w, https://www.passaglia.jp/assets/images/93qlZyI7tE-700.webp 700w, https://www.passaglia.jp/assets/images/93qlZyI7tE-1280.webp 1280w, https://www.passaglia.jp/assets/images/93qlZyI7tE-1752.webp 1752w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/93qlZyI7tE-400.jpeg 400w, https://www.passaglia.jp/assets/images/93qlZyI7tE-700.jpeg 700w, https://www.passaglia.jp/assets/images/93qlZyI7tE-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/93qlZyI7tE-1752.jpeg 1752w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Map of migrations in Japan 2021&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/93qlZyI7tE-400.jpeg&quot; width=&quot;1752&quot; height=&quot;1395&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;Sometimes I want to do this kind of mapmaking at the municipal level, keeping in mind that the municipal map changes over time as cities merge and reorganize. Here for example is another map I made, this time of migration out of coastal cities in Iwate Prefecture in 2011, the year it was struck by the 2011 Tōhoku earthquake and tsunami.&lt;/p&gt;
&lt;div id=&quot;map-container&quot; style=&quot;position: relative; padding-bottom: 62.6%; height: 0;&quot;&gt;
  &lt;h2&gt; 2011 Exodus From Iwate Coast&lt;/h2&gt;
  &lt;iframe id=&quot;map-iframe&quot; src=&quot;https://www.passaglia.jp/maps/migration-iwate-2011.html&quot; style=&quot;position: absolute; left: 0; width: 100%; height: 100%; border:none&quot; scrolling=&quot;no&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;
&lt;!-- https://developer.datawrapper.de/docs/custom-embed-code --&gt;
&lt;!-- &lt;iframe title=&quot;%chart_title%&quot; aria-label=&quot;%chart_type%&quot; id=&quot;datawrapper-chart-%chart_id%&quot; src=&quot;%chart_url%&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;width: 0; min-width: 100% !important; border: none;&quot; height=&quot;%chart_height%&quot;&gt;&lt;/iframe&gt;&lt;script type=&quot;text/javascript&quot;&gt;%embed_js%&lt;/script&gt; --&gt;
&lt;p&gt;It takes a fair amount of data wrangling to organize this geographic and demographic data, but once it’s in hand it can be used as a foundation for many different analyses. That’s why I made &lt;strong&gt;japandata&lt;/strong&gt;, a Python package that provides easy access to this basic data about Japan.&lt;/p&gt;
&lt;p&gt;For now &lt;strong&gt;japandata&lt;/strong&gt; has four modules, giving access to four underlying datasets.&lt;/p&gt;
&lt;ul class=&quot;list&quot;&gt;
&lt;li&gt;&lt;code&gt;japandata.maps&lt;/code&gt;, which provides present and historical maps of Japan, its prefectures, and municipalities, compiled by &lt;a href=&quot;https://geoshape.ex.nii.ac.jp/city/choropleth/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Asanobu Kitamoto&lt;/a&gt; of the ROIS-DS Center for Open Data in the Humanities.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;japandata.population&lt;/code&gt;: Prefectural and municipal demographic data about gender and age distributions, birth rates, migrations, and non-Japanese residents, annually since 1967 from the &lt;a href=&quot;https://www.soumu.go.jp/main_sosiki/jichi_gyousei/daityo/gaiyou.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Basic Register of Residents&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;japandata.indices&lt;/code&gt;: Annual indicators about the fiscal health of municipal and prefectural governments, compiled by the &lt;a href=&quot;https://www.soumu.go.jp/iken/shihyo_ichiran.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Ministry of Internal Affairs&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;japandata.readings&lt;/code&gt;: Kana and romaji readings of place names, which in other datasets are provided only in Kanji.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These datasets have been processed to make them easy to use and easy to combine, and every step of the processing is visible in the source code. Along with the library I also provide example code to make maps like the ones in this post.&lt;/p&gt;
&lt;p&gt;So head on over to the &lt;a href=&quot;https://www.github.com/passaglia/japandata&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GitHub repository&lt;/a&gt; and get started with &lt;strong&gt;japandata&lt;/strong&gt; today!&lt;/p&gt;

    </content>
  </entry>
  <entry>
    <title>Yomikata: Heteronym disambiguation for Japanese</title>
    <link href="https://www.passaglia.jp/yomikata/" />
    <updated>2023-03-02T00:00:00Z</updated>
    <id>https://www.passaglia.jp/yomikata/</id>
    <content xml:lang="" type="html">&lt;p&gt;I’ve been learning Japanese since 2018, when I was still a grad student at the University of Chicago. Thanks to computers and the internet, learning Japanese is much easier than it used to be. In the old days even looking up new words required whipping out both a kanji dictionary and a separate word dictionary. Now you can just draw kanji directly into your phone.&lt;/p&gt;
&lt;p&gt;But there are still many aspects of the Japanese language that computers have trouble with. One that used to drive me nuts stems from the abundance of heteronyms in Japanese: words that are written the same way but pronounced differently. For example, the character 角 can represent the word &lt;em&gt;tsuno&lt;/em&gt; (horn), &lt;em&gt;kado&lt;/em&gt; (corner), &lt;em&gt;kaku&lt;/em&gt; (angle), and more. In Japanese these kinds of heteronyms are called &lt;em&gt;doukei iongo&lt;/em&gt; (同形異音語).&lt;/p&gt;
&lt;p&gt;Japanese speakers can easily determine the appropriate reading of the word from context, but a simple dictionary lookup by a computer will get the reading wrong most of the time. For language learners and people who rely on screen readers, as well as all sorts of linguistic and computer speech applications, it would be helpful if a computer could understand the context and determine if the word should be read one way or another, just like a human.&lt;/p&gt;
&lt;p&gt;That’s why I made &lt;strong&gt;Yomikata&lt;/strong&gt;, an open-source python library that uses a machine learning model to disambiguate Japanese heteronyms. Check out the &lt;a href=&quot;https://huggingface.co/spaces/passaglia/yomikata-demo&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;demo page&lt;/a&gt; to get a sense of what &lt;strong&gt;Yomikata&lt;/strong&gt; can do, and check out the &lt;a href=&quot;https://github.com/passaglia/yomikata&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GitHub repository&lt;/a&gt; to learn how to use it in your own projects. And continue reading this article to learn how &lt;strong&gt;Yomikata&lt;/strong&gt; works.&lt;/p&gt;
&lt;h3 id=&quot;the-disambiguation-task&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#the-disambiguation-task&quot;&gt;The disambiguation task&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Disambiguating words is a long-studied problem in the field of Natural Language Processing in both English and Japanese. Until the early 2010s the most popular solution was to construct rules from grammatical or statistical patterns: if 角 appears in a sentence near the word for ‘animal’, it should probably be read ‘horn’ &lt;em&gt;tsuno&lt;/em&gt; . If instead it appears alongside ‘street,’ it probably refers ‘corner’ / &lt;em&gt;kado&lt;/em&gt;. This approach works well for some words, but there is a limit to the complexity of patterns it can model.&lt;/p&gt;
&lt;p&gt;Machine learning approaches to disambiguation have grown in popularity because they can exploit large datasets to learn complex language patterns. In 2018, &lt;a href=&quot;https://aclanthology.org/L18-1215/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Gorman et al.&lt;/a&gt; reported how they improved Google’s text-to-speech services by training a simple logistic classifier to disambiguate heteronyms in English. This model used the two words to the left and two words to the right of each ambiguous word to disambiguate it.&lt;/p&gt;
&lt;p&gt;Ideally we would like to look at an entire sentence of context and not just a few neighboring words. The &lt;a href=&quot;https://arxiv.org/abs/1810.04805&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;BERT&lt;/a&gt; model offers a way to do this. BERT is a language model based on the &lt;a href=&quot;https://jalammar.github.io/illustrated-transformer/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;transformer architecture&lt;/a&gt; that uses context to embed words.&lt;/p&gt;
&lt;h3 id=&quot;embeddings&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#embeddings&quot;&gt;Embeddings&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;An embedding is a mapping from a word to a vector. For example the word ‘dog’ might be embedded as the vector [0.3, 0.7].  Embeddings are the starting point for machine learning models because they map words into a vector space on which you can do operations. There are many ways to embed words, but most algorithms try to put words with similar meanings close together in the vector space. For example a good algorithm would probably assign ‘dog’ and ‘puppy’ to nearby vectors.&lt;/p&gt;
&lt;p&gt;To limit the number of words that a model needs to learn how to embed, it is often useful to embed pieces of words like stems and suffixes instead of entire words. These word pieces are called ‘tokens.’&lt;/p&gt;
&lt;p&gt;Most embeddings are not immediately useful for disambiguating heteronyms: simple embeddings map tokens to vectors without looking at the context, so 角 will always map to the same vector no matter if it represents ‘horn’, ‘corner’, or ‘angle’.&lt;/p&gt;
&lt;p&gt;BERT on the other hand provides contextual embeddings. It takes an initial embedding for each token in a sentence then processes the sentence with a 110 million parameter transformer model. The output is an embedding for each token that contains some information about the tokens around it. Training BERT takes substantial computing resources. The &lt;a href=&quot;https://huggingface.co/cl-tohoku/bert-base-japanese-v2&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Tohoku University group&lt;/a&gt; provides a BERT model that was trained on the entire Japanese Wikipedia.&lt;/p&gt;
&lt;h3 id=&quot;disambiguating-with-context&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#disambiguating-with-context&quot;&gt;Disambiguating with context&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;With contextual embeddings, it becomes possible to disambiguate heteronyms using the entire sentence as context. After going through the BERT transformer 角 / &lt;em&gt;tsuno&lt;/em&gt;, 角 / &lt;em&gt;kado&lt;/em&gt;, and 角 / &lt;em&gt;kaku&lt;/em&gt; will all map to different regions of the vector space because they appear in different types of sentences.&lt;/p&gt;
&lt;p&gt;In 2021, &lt;a href=&quot;https://www.anlp.jp/proceedings/annual_meeting/2021/pdf_dir/P2-18.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Kobayashi et al.&lt;/a&gt; used an SVM to classify which BERT outputs correspond to which heteronym readings for a group of ~70 Japanese heteronyms. Later that year &lt;a href=&quot;https://assets.amazon.science/c3/db/23ca18d7450d8dbb5b80a11fcdd3/homograph-disambiguation-with-contextual-word-embeddings-for-tts-systems.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Nicolis et al.&lt;/a&gt; at Amazon used a logistic classifier to do the same for BERT embeddings of English heteronyms.&lt;/p&gt;
&lt;p&gt;These works kept the BERT model fixed and just trained classification layers on the output. &lt;a href=&quot;https://aclanthology.org/2022.lrec-1.770.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Sato et al. 2021&lt;/a&gt; went a step further and fine-tuned the BERT model weights themselves in order to aid in the classification, in other words training BERT to clearly separate the embeddings for the different readings of each heteronym. This is the approach I use for Yomikata.&lt;/p&gt;
&lt;h3 id=&quot;training-data&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#training-data&quot;&gt;Training data&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Training a model to distinguish heteronym readings requires a large corpus of Japanese text with the different readings for each heteronym labeled. Luckily Sato et al. prepared two such corpora: the &lt;a href=&quot;https://github.com/ndl-lab/huriganacorpus-aozora&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Aozora Bunko corpus&lt;/a&gt; and the &lt;a href=&quot;https://github.com/ndl-lab/huriganacorpus-ndlbib&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;National Diet Library titles corpus&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Aozora Bunko is a library of public domain or permissively licensed works. Sato et al. combined those texts with phonetic information from the Japan Braille Library to produce a high-quality phonetically annotated corpus of Japanese literature.&lt;/p&gt;
&lt;p&gt;The NDL titles corpus is a list of titles of books in the National Diet Library. As part of the registration process all book titles are phonetically annotated, yielding a large but functionally limited corpus of annotated text.&lt;/p&gt;
&lt;p&gt;I supplement these two large corpora with sentences from the &lt;a href=&quot;https://clrd.ninjal.ac.jp/bccwj/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Balanced Corpus of Contemporary Written Japanese&lt;/a&gt;, which is the largest and most widely used Japanese language text corpus. Only the small ‘core’ subset of the BCCWJ has annotations that are of good enough quality to act as training data. I also add sentences from the &lt;a href=&quot;https://github.com/ku-nlp/KWDLC&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Kyoto University Web Document Leads Corpus&lt;/a&gt;, an annotated corpus of the first three sentences of 5,000 web documents.&lt;/p&gt;
&lt;p&gt;After combining all these corpora, I search for sentences that contain heteronyms. I then use the &lt;a href=&quot;https://github.com/WorksApplications/SudachiDict&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;sudachi&lt;/a&gt; dictionary to filter out sentences that contain heteronyms only as part of larger compound words, because these larger compounds usually disambiguate the heteronyms. I also remove sentences that contain only unexpected readings for heteronyms in order to cut down on the size of the training dataset. Since this cut uses knowledge of the true label of the heteronyms in preparing the the training data for the model, it introduces a bias that future versions of Yomikata should try to eliminate.&lt;/p&gt;
&lt;p&gt;This procedure results in a dataset of 628,755 Japanese sentences and book titles that I split into a training set (70%), a validation set (15%), and a test set (15%).&lt;/p&gt;
&lt;h3 id=&quot;model-training-and-performance&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#model-training-and-performance&quot;&gt;Model, training, and performance&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I used as a starting point the Tohoku group’s &lt;a href=&quot;https://huggingface.co/cl-tohoku/bert-base-japanese-v2&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;BERT-base-japanese-v2&lt;/a&gt; model. This model tokenizes sentences using &lt;a href=&quot;https://github.com/taku910/mecab&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;MeCaB&lt;/a&gt; with the &lt;a href=&quot;https://clrd.ninjal.ac.jp/unidic/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;unidic&lt;/a&gt; dictionary. unidic favors using small subword tokens, so many heteronyms are not present in the model’s initial vocabulary and I had to add them in. This is the main modeling improvement I have made over Sato et al.: the initial release of Yomikata covers 130 heteronyms while Sato et al. report results for 93.&lt;/p&gt;
&lt;p&gt;On top of the BERT transformer I bolt a linear layer to classify the BERT embeddings into heteronym readings or an &lt;code&gt;&amp;lt;OTHER&amp;gt;&lt;/code&gt; token that I use to indicate that the token belongs to a larger compound word. Training the model takes about an hour one of my institute’s Tesla V100 GPUs. I used &lt;a href=&quot;https://mlflow.org/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;MLflow&lt;/a&gt; to manage and supervise the training process.&lt;/p&gt;
&lt;p&gt;Overall accuracy is 94%, but performance varies significantly from heteronym to heteronym. For 角, for example, the reading &lt;em&gt;kaku&lt;/em&gt; is correctly identified in 834 out of its 848 appearances in the test data (98% accuracy) and performance for &lt;em&gt;kado&lt;/em&gt; is also good (139/157, 89%). Performance is more mediocre for &lt;em&gt;tsuno&lt;/em&gt; (37/52, 71%), and the uncommon reading &lt;em&gt;sumi&lt;/em&gt; is correctly identified only once out of its 15 appearances (6% accuracy).&lt;/p&gt;
&lt;p&gt;Large class imbalances are sometimes a problem, with for example the model completely failing to identify the &lt;em&gt;ikisatsu&lt;/em&gt; reading of 経緯 because it is swamped by the &lt;em&gt;keii&lt;/em&gt; reading in the data. In other unbalanced cases the model performs well, for example the two readings &lt;em&gt;henka&lt;/em&gt; ‘change’　and &lt;em&gt;henge&lt;/em&gt; ‘embodiment’ of 変化 are very well distinguished despite 12302 appearances of the former and only 78 appearances of the latter in the test data.&lt;/p&gt;
&lt;h3 id=&quot;packaging-and-release&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#packaging-and-release&quot;&gt;Packaging and Release&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;My primary goal with Yomikata was to make a package that is easy to use, easy to share, easy to improve on, and easy to integrate into other projects. Despite much existing research on heteronym disambiguation in Japanese, Yomikata is to my knowledge the first public Japanese disambiguation library.&lt;/p&gt;
&lt;p&gt;Installing Yomikata requires running just two commands: one to install the code and one to fetch the model binaries. I used streamlit to make a demo page so people can see what Yomikata can do without having to install anything. The model is small enough that it can run on computers without a GPU.&lt;/p&gt;
&lt;p&gt;Yomikata also comes integrated with several dictionaries, so that users can use Yomikata to get readings for all words in a sentence by combining the disambiguation powers of the machine learning model with the breadth of a full dictionary.&lt;/p&gt;
&lt;h3 id=&quot;future-directions&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/yomikata/#future-directions&quot;&gt;Future directions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Data is the limiting factor in Yomikata’s performance. Many heteronym readings appear only a few dozen times in the training data, and the training corpora are not wholly representative of modern written or spoken Japanese. One way to improve the training data would be to apply Yomikata to a large but unannotated corpus like Wikipedia to find sentences that likely contain the rare readings. These can then be confirmed by hand and fed back into Yomikata’s training data.&lt;/p&gt;
&lt;p&gt;Yomikata also only supports the limited number of heteronyms identified in Sato et al., which are predominantly nouns and adverbs. There are many more heteronyms in Japanese which could and should be integrated into the model.&lt;/p&gt;
&lt;p&gt;Finally, ambiguous words are just one of many difficulties that computers face when dealing with Japanese text. Japanese has no spaces between words, and so sentence parsers often make word-boundary mistakes. Using deep-learning models to parse and annotate Japanese sentences is promising, but even large language models struggle to match the direct and accurate information-retrieval capability that dictionaries can offer. Efforts to integrate large language models with external tools, like the approach taken in the recent &lt;a href=&quot;https://arxiv.org/abs/2302.04761&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Toolformer&lt;/a&gt; paper, are promising avenues to combine the two.&lt;/p&gt;

    </content>
  </entry>
  <entry>
    <title>Dying Alone in Tokyo, Japan</title>
    <link href="https://www.passaglia.jp/tokyo-dying-alone-guide/" />
    <updated>2022-02-23T00:00:00Z</updated>
    <id>https://www.passaglia.jp/tokyo-dying-alone-guide/</id>
    <content xml:lang="" type="html">&lt;p&gt;&lt;em&gt;Update 2022-07-14: Tokyo Dying Alone has been selected as a &lt;a href=&quot;https://portal.data.metro.tokyo.lg.jp/case/case-study-15/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;featured use&lt;/a&gt; of the Tokyo Open Data Catalog.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I recently stumbled on the &lt;a href=&quot;https://portal.data.metro.tokyo.lg.jp/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Tokyo Open Data Catalog&lt;/a&gt;, a remarkable initiative by the city to share with the public some of the vast amount of data it collects. Among the more than 4,000 datasets in the catalog, one from the Tokyo Medical Examiner’s Office caught my eye: data about the 120,000 people who have died at home in the city since 2003.&lt;/p&gt;
&lt;p&gt;Many of these people lived alone and their deaths were not discovered for several days or more, providing a stunning view into Japan’s lonely death problem (&lt;em&gt;kodokushi mondai&lt;/em&gt;, 孤独死問題).&lt;/p&gt;
&lt;p&gt;From this data, I built the &lt;a href=&quot;https://tokyo-dying-alone.passaglia.jp/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Tokyo Dying Alone dashboard&lt;/a&gt;. This blog post is both a guide to that dashboard and an exploration and analysis of the data. For a technical description of how the dashboard was made, see the &lt;a href=&quot;https://github.com/passaglia/tokyo-dying-alone/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;code repository on github&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-data&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/tokyo-dying-alone-guide/#the-data&quot;&gt;The Data&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you die at home in the city of Tokyo, the Medical Examiner’s Office will view your death as ‘abnormal’ (&lt;em&gt;ijoushi&lt;/em&gt;, 異状死) and conduct a post-mortem examination. It will then include your death in aggregate statistics on its website.&lt;/p&gt;
&lt;p&gt;For each of Tokyo’s 23 wards and for each year from 2003 to 2019, the office supplies &lt;a href=&quot;https://www.fukushihoken.metro.tokyo.lg.jp/kansatsu/kodokushitoukei/index.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;two data tables&lt;/a&gt;. The first shows the deaths at home in 5-year age brackets, separately tabulated for men living alone, women living alone, men living with others, and women living with others. The second shows the time it took for the deceased to be discovered — between the estimated date of death and the post-mortem examination — also broken down by sex and household status. In total, 782 data tables cover the deaths of 119,877 people.&lt;/p&gt;
&lt;p&gt;For example, the 2018 time-to-discovery table for Itabashi Ward records that 3 men died at home while living with others and yet were not discovered by the city until 8-14 days later. However, the Medical Examiner’s Office does not provide their ages: while the age-bracket table does split the deaths in Itabashi in 2018 by age, the cross-dimension of age-bracket to time-to-discovery is absent.&lt;/p&gt;
&lt;p&gt;Aside from the missing age/time cross-dimension, the data gives a nearly complete picture about the sex, household status, age, and time-to-discovery of all deaths at home across all wards of Tokyo for 17 years. To account for the different population of each ward and the population change over time, information about the total number of deaths, including those not at home, can be obtained from the &lt;a href=&quot;https://www.fukushihoken.metro.tokyo.lg.jp/kiban/chosa_tokei/jinkodotaitokei/kushityosonbetsu.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Tokyo Bureau of Social Welfare and Public Health&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-dashboard&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/tokyo-dying-alone-guide/#the-dashboard&quot;&gt;The Dashboard&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Tokyo Dying Alone dashboard helps to navigate this large and complex dataset. In addition to presenting charts synthesizing the data, the dashboard allows the data to be filtered along the chart dimensions. The dashboard then responds to that filter, giving a view into a slice of the data.&lt;/p&gt;
&lt;p&gt;In this way the dashboard helps to find patterns and trends in lonely deaths in Tokyo. By also integrating the total death information – including deaths not at home – it provides context to the data and to the lonely death problem.&lt;/p&gt;
&lt;p&gt;With the dashboard the data can be explored in a huge variety of ways. As the data is filtered, indicators at the top of the page show the total number of deaths at home in the current data slicing and the fraction of deaths at home relative to all deaths.&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/y2mKZ81SNx-400.avif 400w, https://www.passaglia.jp/assets/images/y2mKZ81SNx-700.avif 700w, https://www.passaglia.jp/assets/images/y2mKZ81SNx-992.avif 992w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/y2mKZ81SNx-400.webp 400w, https://www.passaglia.jp/assets/images/y2mKZ81SNx-700.webp 700w, https://www.passaglia.jp/assets/images/y2mKZ81SNx-992.webp 992w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/y2mKZ81SNx-400.jpeg 400w, https://www.passaglia.jp/assets/images/y2mKZ81SNx-700.jpeg 700w, https://www.passaglia.jp/assets/images/y2mKZ81SNx-992.jpeg 992w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Size of full data set&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/y2mKZ81SNx-400.jpeg&quot; width=&quot;992&quot; height=&quot;82&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;The full dataset&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Two charts below these indicators allow filtering based on the sex and household status of the deceased. These charts can be used to immediately understand that, for example, men are an at-risk population for a lonely death: of the roughly 70,000 people who died at home while living alone, roughly 50,000 were men. Filters can be reset using buttons above each plot, and all filters can be reset simultaneously using the reset button at the top of the page.&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/YuR88-SfSq-400.avif 400w, https://www.passaglia.jp/assets/images/YuR88-SfSq-700.avif 700w, https://www.passaglia.jp/assets/images/YuR88-SfSq-996.avif 996w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/YuR88-SfSq-400.webp 400w, https://www.passaglia.jp/assets/images/YuR88-SfSq-700.webp 700w, https://www.passaglia.jp/assets/images/YuR88-SfSq-996.webp 996w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/YuR88-SfSq-400.jpeg 400w, https://www.passaglia.jp/assets/images/YuR88-SfSq-700.jpeg 700w, https://www.passaglia.jp/assets/images/YuR88-SfSq-996.jpeg 996w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Selecting only those who lived alone&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/YuR88-SfSq-400.jpeg&quot; width=&quot;996&quot; height=&quot;530&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;Selecting only those who lived alone&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Following these charts is a map of the 23 wards of Tokyo, colored-coded according the number of deaths in each ward as shown in the neighboring ward chart. Hovering over a ward leads all the other charts to immediately respond by showing data from that ward alone, and clicking on a ward then locks this view. At first glance, the outer wards of Adachi, Ota, and Edogawa appear to be the most significantly afflicted by the lonely death problem.&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/OLUznBF_2K-400.avif 400w, https://www.passaglia.jp/assets/images/OLUznBF_2K-700.avif 700w, https://www.passaglia.jp/assets/images/OLUznBF_2K-1280.avif 1280w, https://www.passaglia.jp/assets/images/OLUznBF_2K-1990.avif 1990w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/OLUznBF_2K-400.webp 400w, https://www.passaglia.jp/assets/images/OLUznBF_2K-700.webp 700w, https://www.passaglia.jp/assets/images/OLUznBF_2K-1280.webp 1280w, https://www.passaglia.jp/assets/images/OLUznBF_2K-1990.webp 1990w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/OLUznBF_2K-400.jpeg 400w, https://www.passaglia.jp/assets/images/OLUznBF_2K-700.jpeg 700w, https://www.passaglia.jp/assets/images/OLUznBF_2K-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/OLUznBF_2K-1990.jpeg 1990w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;The map and the ward chart&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/OLUznBF_2K-400.jpeg&quot; width=&quot;1990&quot; height=&quot;870&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;The map and the ward chart&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;However, this predominantly reflects their larger populations. To account for this, clicking the title of the ward chart divides each ward’s deaths at home by the total number of deaths in that ward, including deaths which do not occur at home, for the relevant time period. This fractional view controls for the differing population of each ward and reveals that in Shinjuku, for example, more than 11% of people die at home.&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/VGJEp2g6lo-400.avif 400w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-700.avif 700w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-1280.avif 1280w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-1990.avif 1990w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/VGJEp2g6lo-400.webp 400w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-700.webp 700w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-1280.webp 1280w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-1990.webp 1990w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/VGJEp2g6lo-400.jpeg 400w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-700.jpeg 700w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-1280.jpeg 1280w, https://www.passaglia.jp/assets/images/VGJEp2g6lo-1990.jpeg 1990w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Dividing by all deaths, including those not at home&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/VGJEp2g6lo-400.jpeg&quot; width=&quot;1990&quot; height=&quot;870&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;Dividing by all deaths, including those not at home&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;On the second row of the dashboard lie more detailed views into the data. Two charts show the time-to-discovery and ages of the deceased. By clicking and dragging across these charts, multiple categories can be selected at once. For example, of the ~72,000 people who died at home while living alone between 2003 and 2019, it took longer than one month for 5540 of them to be found. 9212 of the 72,000 were younger than 50. Using these charts along with the ward selection shows that, for example, 6.4% of all deaths at home in Shinjuku Ward take at least a couple of days to discovered.&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/9L43SF37g--400.avif 400w, https://www.passaglia.jp/assets/images/9L43SF37g--700.avif 700w, https://www.passaglia.jp/assets/images/9L43SF37g--998.avif 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/9L43SF37g--400.webp 400w, https://www.passaglia.jp/assets/images/9L43SF37g--700.webp 700w, https://www.passaglia.jp/assets/images/9L43SF37g--998.webp 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/9L43SF37g--400.jpeg 400w, https://www.passaglia.jp/assets/images/9L43SF37g--700.jpeg 700w, https://www.passaglia.jp/assets/images/9L43SF37g--998.jpeg 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Filtering based on time-to-discovery and household status&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/9L43SF37g--400.jpeg&quot; width=&quot;998&quot; height=&quot;1028&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;Filtering based on time-to-discovery and household status
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;However, the time-to-discovery and age charts cannot be filtered simultaneously: filtering on one chart hides the other, since this cross-dimension is not provided by the public data. Nonetheless these charts offer fascinating views into the data: one interesting feature is the different age distribution in lonely deaths for men and women, as shown in the following charts.&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/bSwFPMfUac-400.avif 400w, https://www.passaglia.jp/assets/images/bSwFPMfUac-700.avif 700w, https://www.passaglia.jp/assets/images/bSwFPMfUac-998.avif 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/bSwFPMfUac-400.webp 400w, https://www.passaglia.jp/assets/images/bSwFPMfUac-700.webp 700w, https://www.passaglia.jp/assets/images/bSwFPMfUac-998.webp 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/bSwFPMfUac-400.jpeg 400w, https://www.passaglia.jp/assets/images/bSwFPMfUac-700.jpeg 700w, https://www.passaglia.jp/assets/images/bSwFPMfUac-998.jpeg 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Age distribution of men who lived alone and died at home&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/bSwFPMfUac-400.jpeg&quot; width=&quot;998&quot; height=&quot;488&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/JzXlRz1B0M-400.avif 400w, https://www.passaglia.jp/assets/images/JzXlRz1B0M-700.avif 700w, https://www.passaglia.jp/assets/images/JzXlRz1B0M-998.avif 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/JzXlRz1B0M-400.webp 400w, https://www.passaglia.jp/assets/images/JzXlRz1B0M-700.webp 700w, https://www.passaglia.jp/assets/images/JzXlRz1B0M-998.webp 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/JzXlRz1B0M-400.jpeg 400w, https://www.passaglia.jp/assets/images/JzXlRz1B0M-700.jpeg 700w, https://www.passaglia.jp/assets/images/JzXlRz1B0M-998.jpeg 998w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;Age distribution of women who lived alone and died at home&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/JzXlRz1B0M-400.jpeg&quot; width=&quot;998&quot; height=&quot;488&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;
&lt;p&gt;Age distributions of men (top) and women (bottom) who lived alone and died at home&lt;/p&gt;&lt;/figcaption&gt;&lt;p&gt;&lt;/p&gt;
&lt;/figure&gt;
&lt;p&gt;Finally, the number of deaths as a function of time is plotted. Deaths at home have on the whole increased by 70% between 2003 and 2019, a drastic increase that can only partially be explained by the increase in total deaths over that time range as Tokyo’s population grows and Japan’s population ages. This can be accounted for by clicking the chart title to divide the number of deaths at home by the total number of deaths in each year in the relevant wards.&lt;/p&gt;
&lt;p&gt;When combined with filters on the other charts, the time evolution chart can be revealing. For example, the number of deaths which take longer than a week to be discovered has increased from less than 800 in 2003 to over 2000 in 2019. This is 1.3% of all deaths in the city in 2003 and 2.5% in 2019.&lt;/p&gt;
&lt;!-- &lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;/assets/images/qnel_-Rk21-400.avif 400w, /assets/images/qnel_-Rk21-700.avif 700w, /assets/images/qnel_-Rk21-996.avif 996w&quot; sizes=&quot;The number of deaths which take over one week to be found has more than doubled in 17 years.&quot;&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;/assets/images/qnel_-Rk21-400.webp 400w, /assets/images/qnel_-Rk21-700.webp 700w, /assets/images/qnel_-Rk21-996.webp 996w&quot; sizes=&quot;The number of deaths which take over one week to be found has more than doubled in 17 years.&quot;&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;/assets/images/qnel_-Rk21-400.jpeg 400w, /assets/images/qnel_-Rk21-700.jpeg 700w, /assets/images/qnel_-Rk21-996.jpeg 996w&quot; sizes=&quot;The number of deaths which take over one week to be found has more than doubled in 17 years.&quot;&gt;&lt;img alt=&quot;The number of deaths which take over one week to be found has more than doubled in 17 years.&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;/assets/images/qnel_-Rk21-400.jpeg&quot; width=&quot;996&quot; height=&quot;488&quot;&gt;&lt;/picture&gt; --&gt;
&lt;figure&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://www.passaglia.jp/assets/images/qnel_-Rk21-400.avif 400w, https://www.passaglia.jp/assets/images/qnel_-Rk21-700.avif 700w, https://www.passaglia.jp/assets/images/qnel_-Rk21-996.avif 996w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://www.passaglia.jp/assets/images/qnel_-Rk21-400.webp 400w, https://www.passaglia.jp/assets/images/qnel_-Rk21-700.webp 700w, https://www.passaglia.jp/assets/images/qnel_-Rk21-996.webp 996w&quot; sizes=&quot;100vw&quot; /&gt;&lt;source type=&quot;image/jpeg&quot; srcset=&quot;https://www.passaglia.jp/assets/images/qnel_-Rk21-400.jpeg 400w, https://www.passaglia.jp/assets/images/qnel_-Rk21-700.jpeg 700w, https://www.passaglia.jp/assets/images/qnel_-Rk21-996.jpeg 996w&quot; sizes=&quot;100vw&quot; /&gt;&lt;img alt=&quot;The number of deaths which take over one week to be found has more than doubled in 17 years.&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://www.passaglia.jp/assets/images/qnel_-Rk21-400.jpeg&quot; width=&quot;996&quot; height=&quot;488&quot; /&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;figcaption&gt;
&lt;p&gt;The number of deaths which take over one week to be found has more than doubled in 17 years&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Data has a part to play in understanding this important social problem, and surely more insights can be found in this powerful dataset.&lt;/p&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;&lt;a class=&quot;heading-anchor&quot; href=&quot;https://www.passaglia.jp/tokyo-dying-alone-guide/#acknowledgements&quot;&gt;Acknowledgements&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This project relied on data gathered by Yoshimasa Kanawaku 金涌佳雅, Takanobu Tanifuji 谷藤隆信, Abe Nobuyuki 阿部伸幸, Nozaki Ichiro 野崎一郎, Mori Shinjiro 森晋二郎, Funayama Masato 舟山眞人, and Fukunaga Tatsushige 福永龍繁, of the Tokyo Medical Examiner’s Office.&lt;/p&gt;
&lt;p&gt;Tutorials from &lt;a href=&quot;https://adilmoujahid.com/posts/2016/08/interactive-data-visualization-geospatial-d3-dc-leaflet-python/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Adil Moujahid&lt;/a&gt;, &lt;a href=&quot;https://nickhand.github.io/blog/pages/2018/01/23/philly-parking/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Nick Hand&lt;/a&gt;, and &lt;a href=&quot;https://github.com/austinlyons/dcjs-leaflet-untappd&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Austin Lyons&lt;/a&gt;, and the dc.js and crossfilter Stack Exchange communities including the invaluable contributions by &lt;a href=&quot;https://stackoverflow.com/users/676195/gordon&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Gordon Woodhull&lt;/a&gt;, were essential for learning the tools used here.&lt;/p&gt;

    </content>
  </entry>
</feed>