<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Lifen: Lifen AI]]></title><description><![CDATA[AI innovations shaping the future of healthcare at Lifen]]></description><link>https://lifen.substack.com/s/lifen-ai</link><image><url>https://substackcdn.com/image/fetch/$s_!YcJI!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc24b9775-e380-44fe-a450-cbf69850691e_80x80.png</url><title>Lifen: Lifen AI</title><link>https://lifen.substack.com/s/lifen-ai</link></image><generator>Substack</generator><lastBuildDate>Mon, 17 Aug 2026 05:27:16 GMT</lastBuildDate><atom:link href="https://lifen.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Lifen]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[lifen@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[lifen@substack.com]]></itunes:email><itunes:name><![CDATA[Lifen Team]]></itunes:name></itunes:owner><itunes:author><![CDATA[Lifen Team]]></itunes:author><googleplay:owner><![CDATA[lifen@substack.com]]></googleplay:owner><googleplay:email><![CDATA[lifen@substack.com]]></googleplay:email><googleplay:author><![CDATA[Lifen Team]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Small LLMs, Big Gains: Simpler Patient Extraction with 85% Fewer Errors]]></title><description><![CDATA[How we moved from a BiLSTM pipeline to a fine-tuned small LLM, reducing patient extraction errors from 4% to 0.6% while simplifying our stack.]]></description><link>https://lifen.substack.com/p/small-llms-big-gains-simpler-patient</link><guid isPermaLink="false">https://lifen.substack.com/p/small-llms-big-gains-simpler-patient</guid><dc:creator><![CDATA[Gauthier Roy]]></dc:creator><pubDate>Mon, 27 Jul 2026 17:01:28 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!t7xa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p></p><h2><strong>Introduction</strong></h2><p>At Lifen, we build applications that enable healthcare facilities and practitioners to exchange medical documents seamlessly, routing and structuring millions of letters, reports, and clinical notes every month.</p><p>As part of this pipeline, every document that flows through our platform needs to be parsed: key pieces of information must be extracted and structured before the document can be properly handled. One of the most critical pieces of information to detect is the patient&#8217;s identity.</p><p>For a long time, our patient extraction pipeline relied on a Bidirectional Long Short Term Memory (BiLSTM) based model. Recurrent architectures like the LSTM were, for years, the natural choice for sequence labelling tasks: they were relatively lightweight, fast at inference, and once trained on enough annotated medical documents, reasonably effective at spotting patient entities within the noisy, heterogeneous text found in medical documents.</p><p>However, since the rise of Large Language Models (LLMs), a new path with a huge performance upgrade was opened. They have an ability to reason over context, generalize more easily from a handful of examples, and are able to overcome many of the LSTM&#8217;s pain points.</p><p>This article tells the story of that transition: how we moved to a generative LLM for patient extraction from medical documents, and what possible choices of configurations we chose from to achieve the best possible performance.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t7xa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t7xa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 424w, https://substackcdn.com/image/fetch/$s_!t7xa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 848w, https://substackcdn.com/image/fetch/$s_!t7xa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 1272w, https://substackcdn.com/image/fetch/$s_!t7xa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t7xa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png" width="1456" height="969" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:969,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:366280,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/207750172?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t7xa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 424w, https://substackcdn.com/image/fetch/$s_!t7xa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 848w, https://substackcdn.com/image/fetch/$s_!t7xa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 1272w, https://substackcdn.com/image/fetch/$s_!t7xa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1c06f05-7c77-4122-ae66-f99065aadfb0_3660x2436.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Time to Switch to Generative LLMs</strong></h2><p>Our task consists of extracting a patient&#8217;s info (first name, last name, birth name, date of birth) in a structured way from a medical document.</p><p>Our last model, BiLSTM, was capable of doing so, however, by introducing a lot of overhead: text normalization, word matching, and training word embeddings from scratch.</p><p>As you can see, considering a BiLSTM encoder for extraction introduces some level of complexity, especially when a patient&#8217;s name spans multiple tokens and requires manual stitching.</p><p>Which is why, for the second iteration of our &#8220;Patient&#8221; algorithm, we wanted to go for something that could, in theory, work out of the box. Naturally, we chose to go for an LLM decoder architecture for this task. Although encoders (BERT-like) are strong models that capture a lot of semantics at token-level, there is nothing more elegant than having one model that generates the correct answer directly, without any post-processing necessary.</p><p>Since LLMs are getting much of today&#8217;s attention, getting smarter at each generation, being actively maintained by a huge community that keeps getting bigger: they were the clear winner for this task.</p><p>At last, we reformulated our objective as such: Given a medical document as input, the LLM should generate a patient&#8217;s first name, last name, birth name, and date of birth as output.</p><h2><strong>LLM structured output</strong></h2><p>LLMs are generative models, they output free form text, as they were trained on natural language. However, since we are trying to extract specific information from a free form text, our task requires structured output. Therefore, we need to be able to control the LLM in its generation process so that the output matches the format we expect.</p><p>Structured generation has been a massive focus for the AI community these past few years. We&#8217;ve seen a lot of brilliant projects tackling structured LLM output, with a strong showing from the open-source community. Here are a few standouts:</p><ul><li><p><a href="https://github.com/dottxt-ai/outlines">outlines</a>: This framework focuses on generation-time structuring. It modifies the model&#8217;s token probabilities at each step to strictly enforce adherence to a regex, JSON schema, or context-free grammar.</p></li><li><p><a href="https://github.com/guidance-ai/guidance">guidance</a>: Another powerful generation-time structuring tool. It constrains the model&#8217;s output in real time, ensuring the LLM never strays from the predefined format.</p></li><li><p><a href="https://github.com/567-labs/instructor">instructor</a>: Operating primarily on post-generation structure adherence, this library leverages Pydantic to orchestrate the generation process. It utilizes function calling or JSON modes under the hood, automatically validating the output against data models and employing self-correction loops to fix any formatting errors.</p></li></ul><p>Even in the closed API ecosystem, major actors (such as OpenAI, Anthropic, Mistral AI) understood the importance of structured output, which is why they all added it as a feature for their LLM based APIs:</p><ul><li><p><strong>OpenAI initiatives</strong>: They initially introduced <a href="https://developers.openai.com/api/docs/guides/function-calling">Function Calling</a> in 2023, where users provided a JSON Schema of a function, and the model was trained to output a JSON object matching that schema. However, it didn&#8217;t strictly guarantee valid formatting. Then came <a href="https://developers.openai.com/api/docs/guides/structured-outputs">structured output</a> in 2024 to guarantee 100% adherence to the supplied JSON schema.</p></li><li><p><strong>Anthropic initiatives</strong>: They originally relied on prompting techniques (like forcing the model to output between XML tags) before officially implementing <a href="https://claude.com/blog/tool-use-ga">native Tool Use</a> (Function Calling) for Claude, and later introducing official support for <a href="https://claude.com/blog/structured-outputs-on-the-claude-developer-platform">forced JSON object responses</a> to match strict schemas.</p></li><li><p><strong>Mistral AI initiatives</strong>: They followed a similar trajectory, starting with native Function Calling and a <a href="https://docs.mistral.ai/studio-api/conversations/structured-output/json_mode">standard JSON Mode</a> which forced the output to be valid JSON but did not strictly guarantee adherence to a specific schema. They later upgraded this by rolling out official <a href="https://docs.mistral.ai/studio-api/conversations/structured-output">Custom Structured Outputs</a>, much like the other players.</p></li></ul><p>While API-level structured outputs are highly effective, two other classic approaches remain relevant. <strong>Prompt engineering</strong> is often the quickest and lowest-effort solution for forcing a structured format purely through instructions. However, it is inherently less reliable and less efficient than native API constraints, as models can still occasionally break formatting rules.</p><p>On the other end of the spectrum, <strong>fine-tuning</strong> provides a highly robust solution for structured generation. By training the LLM on a large dataset that consistently uses the desired schema, the model inherently learns to replicate that exact structure during inference. Consequently, in addition to mastering the core task, such as extracting a patient&#8217;s identity, the model naturally learns to format its response in the required structure without relying on complex prompts.</p><div><hr></div><p>In our use case, we chose to <strong>fine-tune</strong> the LLM in our specific format, since it was the logical step to move away from the BiLSTM pipeline. However, at the scale of millions of documents processed per month, latency and throughput become hard constraints. Patient LLM runs synchronously during document ingestion, meaning the extraction happens while the user is actively waiting. </p><p>This is why, beyond getting the right answer, we also needed to optimize inference speed, and that optimization drives the next two design choices: output format and quantization.</p><h2><strong>LLM Output format</strong></h2><p>In our experiments, <strong>CSV</strong> refers to a compact, comma-separated output format where values are emitted in a fixed order, without repeating the field names at every prediction. For example, instead of generating a JSON object such as:</p><pre><code><code>{
  "first_name": "John",
  "last_name": "Doe",
  "birth_name": null,
  "birth_date": "1970-01-01"
}</code></code></pre><p>the model generates the same information as:</p><pre><code><code>John,Doe,,1970-01-01</code></code></pre><p>This format is close in spirit to TOON, which has recently become popular for LLM inference because it reduces the number of tokens needed for structured outputs. In our case, we take the idea one step further: since the schema is fixed and known in advance, the keys do not need to be generated at all. The output position itself carries the meaning of each value: first name, last name, birth name, then date of birth.</p><p>This makes CSV particularly well suited to fine-tuned models. During training, the model sees the same field order over and over again, so it learns to reliably produce values in that exact order. Compared to JSON, this avoids repeatedly generating field names, quotes, braces, and other structural tokens. The resulting output is shorter, which directly helps reduce latency and improve throughput, while keeping the same semantic information.</p><h2><strong>Quantization</strong></h2><p>As suggested in their name, LLMs are made up of a huge number of parameters, usually in the billions. Because their generation process is auto-regressive, without further optimization, the model requires close to a second to generate a full prediction (in our case, the list of patient attributes).</p><p>Originally, each one of these billions of parameters is encoded in 32-bit floating-point precision (FP32). While we do not intend to reduce the total number of operations, what we can do instead is reduce the numerical precision of the data being processed.</p><p>And this is where quantization kicks in. Instead of storing and calculating these parameters in FP32 , quantization reduces their bit-width to a lower format (like FP16, INT8, or even INT4). This drastically reduces the hardware workload and memory bandwidth bottlenecks. In summary, the advantages of quantization are usually two-fold:</p><ul><li><p><strong>Inference Speed</strong>: Because the hardware can process smaller precision types much faster than FP32, the model generates outputs with much lower latency.</p></li><li><p><strong>Memory Footprint</strong>: Cutting a model&#8217;s precision reduces the storage and RAM requirements, allowing massive models to run on much smaller GPUs than originally required.</p></li></ul><p>Reducing the precision of a model&#8217;s floating point can lead to lose part of an LLM&#8217;s capabilities. To achieve the benefits of using a LLM without destroying the model&#8217;s intelligence, advanced Post-Training Quantization (PTQ) methods can be used. Two of the most popular and effective techniques for LLMs are GPTQ and AWQ.</p><ul><li><p><strong>GPTQ (Generalized Post-Training Quantization)</strong>: This is a layer-by-layer calibration method. It compresses the model&#8217;s weights by treating the quantization process as an optimization problem, minimizing the error in the output for a small set of calibration data. It is highly efficient and allows massive models to be shrunk down to 4-bit precision while retaining remarkably high accuracy.</p></li><li><p><strong>AWQ (Activation-aware Weight Quantization)</strong>: AWQ takes a different approach by recognizing that not all weights are created equal. It observes that a small percentage (about 1%) of the model&#8217;s activations are &#8220;salient&#8221; (extremely important) to the model&#8217;s performance. Instead of quantizing everything equally, AWQ handles the most important weights with extra care, so they keep more of their accuracy when compressed, resulting in higher overall accuracy.</p></li></ul><p>Since quantization requires reducing the precision of our parameters, it means re-scaling the values into the correct lower-precision range. With that in mind, there are two main approaches:</p><ul><li><p><strong>Dynamic</strong>: The re-scaling factors for activations are calculated on the fly at inference time based on real-time data</p></li><li><p><strong>Static</strong>: The re-scaling factors for both weights and activations are pre-calculated using a calibration dataset, meaning no scaling overhead happens during inference</p></li></ul><p>Naturally, dynamic quantization is often more precise because the scaling adapts to real-time inputs. However, calculating these scaling factors on the fly adds a computational overhead, which can slow down the model&#8217;s forward pass compared to static quantization.</p><div><hr></div><p>Reducing a model&#8217;s numerical precision inevitably comes with a cost: quantization can degrade the model&#8217;s capabilities, and we need to carefully measure this degradation to find the right balance between inference speed and output quality.</p><p>In our use case, we decided to benchmark a limited set of these quantization methods. We use <a href="https://github.com/vllm-project/llm-compressor">llmcompressor</a> library to compress our LLMs, and chose to try the following quantization types: <strong>AWQ</strong>, <strong>FP8_DYNAMIC</strong>. Both quantization methods will be benchmarked in terms of performance and latency.</p><h2><strong>LLM serving</strong></h2><p>Again, for serving purposes, open-source ecosystems have emerged to handle LLM serving. Here is a quick look at the major actors dominating the space:</p><ul><li><p><strong><a href="https://github.com/vllm-project/vllm">vLLM</a></strong>: vLLM revolutionized serving with <a href="https://docs.vllm.ai/en/latest/design/paged_attention/">PagedAttention</a>, an algorithm that prevents memory waste by managing the Key-Value (KV) cache like virtual memory in operating systems.</p></li><li><p><strong><a href="https://github.com/sgl-project/sglang">SGLang</a></strong>: SGLang introduces <a href="https://sgl-project-sglang-93.mintlify.app/concepts/radix-attention">RadixAttention</a>, which automatically caches and reuses prompt prefixes across entirely different requests.</p></li><li><p><strong><a href="https://github.com/NVIDIA/TensorRT-LLM">TensorRT-LLM</a></strong>: Built directly by NVIDIA, this is a highly optimized, low-level C++ and Python framework designed to wring out every last drop of performance from NVIDIA hardware.</p></li><li><p><strong><a href="https://github.com/huggingface/text-generation-inference">TGI</a></strong><a href="https://github.com/huggingface/text-generation-inference"> (Text Generation Inference)</a>: Built by Hugging Face, TGI was a pioneer in introducing production-level features. However, since development stalled compared to other actors, Hugging Face stopped maintaining the project since late 2025.</p></li></ul><div><hr></div><p>In our use case, we decided to build our serving solution with <strong>vLLM</strong>. <strong>TGI</strong> was not a suitable option for us because it is no longer actively maintained and did not support all the models we wanted to evaluate, including <strong>SmolLM3</strong>. In contrast, vLLM is one of the most actively updated LLM serving frameworks today, is straightforward to deploy and use, and consistently reports strong performance in public benchmarks.</p><h3><strong>Dataset</strong></h3><p>Our dataset consists of de-identified medical notes of various types and sources, annotated in-house by our experts. Prior to processing and labeling, all notes are pseudonymized using a dedicated in-house algorithm. All patient data is collected and processed in compliance with all applicable European and French laws, regulations and guidelines for the processing of health data for medical research purposes. </p><p>We then use these de-identified medical notes to train and evaluate our models. The results obtained on the test set resulting from this data collection are shown in the following section.</p><h3><strong>Results</strong></h3><p><strong>Results in macro precision</strong>*:</p><p>*<em>the <strong>macro precision</strong> is the unweighted average of per-field precision scores across the four predicted fields (first_name, last_name, birth_name, birth_date). For each field, precision is computed as the ratio of correct predictions over the number of documents where the model actually produced a non-null prediction. The macro average then gives equal weight to each field regardless of how frequently it appears in the dataset.</em></p><p><em>Our baseline with a BiLSTM was able to achieve 96% macro precision, which is already a strong score. Let&#8217;s see how much we can improve using newer, bigger models.</em></p><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/cUkrS/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f154badd-0846-4c5f-8c3f-9add4cc9b322_1220x674.png&quot;,&quot;thumbnail_url_full&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/769273f0-957f-452e-a2e6-829a9dd0576d_1220x674.png&quot;,&quot;height&quot;:342,&quot;title&quot;:&quot;Created with Datawrapper&quot;,&quot;description&quot;:&quot;&quot;,&quot;belowTheFold&quot;:true}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/cUkrS/1/" width="730" height="342" frameborder="0" scrolling="no" loading="lazy"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><p><strong>Analysis:</strong></p><p><strong>Overall performance</strong> among our fine-tuned models is consistently around 99%, with slight changes depending on configuration, indicating the task is well within reach of even very small models.</p><p><em><strong>Model size</strong></em> has a small impact. With CSV output and FP8_DYNAMIC quantization, which is our standard configuration for production, precision rises gently from 98.7% at ~0.5B scale to a plateau of 99.2% from the ~3B tier onward. Hence, for our use case, we decided to select SmolLM3-3B as our main model because of its license (apache 2.0) and the fact that for its size, it provided a great balance of performance.</p><p><em><strong>Output format</strong></em> (CSV vs. JSON) makes essentially no difference. On SmolLM3-3B, CSV and JSON both reach 99.2% under FP8_DYNAMIC, so the structured format choice can be driven by downstream integration needs rather than accuracy.</p><p><em><strong>Quantization</strong></em> method has a small effect. For SmolLM3-3B, AWQ trails FP8_DYNAMIC slightly.</p><p><strong>Latency results</strong>:</p><p>Since CSV is clearly a smaller format than JSON, and since they perform the same, we decided not to benchmark JSON v.s. CSV in terms of latency. Instead, we solely focused on AWQ v.s. FP8_DYNAMIC, and compared their respective average latency with respect to requests per second (RPS)<strong> on a single pod </strong>using a L4 GPU.</p><p>The same applies to model size. We did not observe a significant performance gap when going from ~3B to ~8B models. Hence, since 8B models would clearly have higher latency than 3B models, we did not benchmark them in terms of latency.</p><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/GOKXK/2/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb6ce8da-b247-47d1-bb16-a9daad4f4624_1220x626.png&quot;,&quot;thumbnail_url_full&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0ffe6a29-39e6-4c56-85f2-b89f14fa1ecf_1220x626.png&quot;,&quot;height&quot;:318,&quot;title&quot;:&quot;Created with Datawrapper&quot;,&quot;description&quot;:&quot;&quot;,&quot;belowTheFold&quot;:true}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/GOKXK/2/" width="730" height="318" frameborder="0" scrolling="no" loading="lazy"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><p>Both methods scale gracefully with load. It goes to show that vLLM handles the increased request volume efficiently, its continuous batching and PagedAttention memory management absorb the extra concurrency without meaningful latency degradation.</p><p>AWQ&#8217;s average latency is around 0.25 seconds, while FP8_DYNAMIC&#8217;s average latency is around 0.4 seconds. While both are acceptable for our use case, we decided to go for FP8_DYNAMIC, since it provided the best overall performance in terms of macro-precision.</p><p></p><p><strong>Why Not Just Use a SOTA LLM? </strong><br>We also tested whether a large general-purpose LLM could solve the task without fine-tuning. Using Opus 4.6 with prompt engineering and 5 few-shot examples, we reached 86.4% document precision (432/500) on 500 pseudonymised medical documents, at a cost of $8.2075. Our fine-tuned small LLM, using CSV output and FP8_DYNAMIC quantization, reached 98.5% document precision on the same task. That means document-level errors dropped from 13.6% to 1.5%, or roughly 89% fewer errors. For this use case, bigger was not better. Patient identity extraction is a constrained, schema-driven task where consistency matters more than broad reasoning, and fine-tuning a small model proved more accurate, cheaper, and faster.<br></p><h2><strong>Conclusion</strong></h2><p>While we did not try all possible configurations for quantization, we found a reasonable balance for our use case. Our benchmarks show that SmolLM3-3B with FP8_DYNAMIC quantization and CSV output format is a very reasonable balance with the best precision for our specific task.</p><p>Our reasoning:</p><ol><li><p>FP8_DYNAMIC delivers a high precision. At 99.2% macro precision, FP8_DYNAMIC edges out AWQ and matches the best results seen on our leaderboard.</p></li><li><p>Latency remains well within acceptable bounds. Although AWQ is faster in absolute terms, FP8_DYNAMIC still serves at sub-second latency, which is more than adequate for the target use case.</p></li><li><p>FP8 is a future-proof choice. It is hardware-native on modern GPUs and a well-supported, standard quantization path, typically simpler to deploy and maintain than AWQ. This reduces operational risk and keeps the door open as models and infrastructure evolve.</p></li><li><p>The serving stack scales gracefully. Latency stays nearly flat from RPS=2 to RPS=8, showing vLLM handles increasing load efficiently with clear headroom before saturation.</p></li></ol><p>More broadly, the results show that a compact 3B model is fully sufficient, there&#8217;s no need to pay for larger models when a well-quantized SmolLM3-3B reaches near-ceiling accuracy at sub-second latency. With FP8, the configuration favors maximum accuracy and operational simplicity while keeping latency and cost firmly under control.<br></p><h2><strong>To explore</strong></h2><p>The current setup is already good enough for production, but a few inference optimizations could still be explored if latency or throughput becomes an issue.</p><p>One first direction we tried is <strong><a href="https://arxiv.org/abs/2211.17192">speculative decoding</a></strong>. The idea is to use a smaller, faster draft model to generate candidate tokens, then let the larger target model verify them. If the draft model predicts what the target model would have generated, several decoding steps can be skipped.</p><p>This worked reasonably well with Qwen, using a 0.6B model as the draft model. However, the speedup was not large enough to justify the added serving complexity. For SmolLM3-3B, this is also less practical because there is no suitable smaller SmolLM3 model to use as a draft model.</p><p>Another option is <strong><a href="https://docs.vllm.ai/en/latest/features/speculative_decoding/n_gram/">n-gram speculation</a></strong>. Instead of using a draft model, it speculates future tokens by reusing n-grams already present in the prompt or generated context. In theory, this should fit extraction tasks very well, since the model often copies names, dates, or other patient information directly from the input document.</p><p>However, our first experiments with n-gram speculation did not show any speedup. This should be investigated further, especially to check whether the limitation comes from the implementation rather than from the method itself.</p><p>Finally, <strong><a href="https://arxiv.org/abs/2401.15077">EAGLE</a></strong> could be tested if performance becomes a real bottleneck. It is one of the current state-of-the-art approaches for speculative decoding, but it would also add more complexity to training, serving, and maintenance.<br></p><h4><strong>Join Us at Lifen!</strong></h4><blockquote><p>At Lifen, we&#8217;re at the forefront of leveraging AI to solve real-world challenges in healthcare. If you&#8217;re passionate about building reliable, interpretable AI systems and want to work on impactful problems, we&#8217;d love to hear from you!</p><p>We&#8217;re currently hiring for roles in machine learning, engineering and more. Check out our careers page to explore opportunities and join our mission.</p></blockquote>]]></content:encoded></item><item><title><![CDATA[On-premise de-identification of French medical documents using lightweight models for PHI detection and coherent substitution]]></title><description><![CDATA[How can lightweight models solve healthcare's on-premise privacy puzzle?]]></description><link>https://lifen.substack.com/p/on-premise-de-identification-of-french</link><guid isPermaLink="false">https://lifen.substack.com/p/on-premise-de-identification-of-french</guid><dc:creator><![CDATA[Jean-Charles Louis]]></dc:creator><pubDate>Thu, 16 Oct 2025 15:27:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!PVbx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>Introduction</h1><p>At Lifen, we tackle one of healthcare&#8217;s biggest challenges: <strong>the vast majority of clinical data remains locked away as unstructured text in millions of medical reports</strong>. This untapped information could revolutionise patient care and clinical research, but accessing it requires solving a fundamental privacy puzzle.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6R76!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6R76!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 424w, https://substackcdn.com/image/fetch/$s_!6R76!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 848w, https://substackcdn.com/image/fetch/$s_!6R76!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 1272w, https://substackcdn.com/image/fetch/$s_!6R76!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6R76!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png" width="341" height="182.96530359355637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:433,&quot;width&quot;:807,&quot;resizeWidth&quot;:341,&quot;bytes&quot;:51834,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6R76!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 424w, https://substackcdn.com/image/fetch/$s_!6R76!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 848w, https://substackcdn.com/image/fetch/$s_!6R76!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 1272w, https://substackcdn.com/image/fetch/$s_!6R76!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d2b49-3e67-4ce2-a56b-db29396b53c9_807x433.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p><strong>Enter Lifen DataLab Bridge</strong> &#8211; our production on-premises de-identification solution that unlocks research-grade clinical cohorts while keeping raw data securely within customer infrastructure. Unlike our document-structuring stack <a href="https://lifen.substack.com/p/improving-llm-reliability-in-healthcare">which uses large language models</a>, this component intentionally avoids costly models. On-premises environments typically come with CPU-only machines, heterogeneous setups, and strict latency constraints, demanding compact, predictable models that run reliably without GPUs.</p><p>Our solution combines two critical layers: <strong>high-recall PHI (Protected Health Information) detection and context-aware smart substitution</strong> that preserves document readability and layout. The result? Documents that integrate smoothly with DataLab&#8217;s structuring workflows while delivering privacy-by-design on-premises, deterministic CPU performance, and robustness to OCR variability.</p><p>This article details our journey from lightweight neural networks to production-ready de-identification.</p><h1>Methods</h1><p>De-identification of PHI in clinical notes is a two-step process. The first step consists in detecting the sensitive elements to de-identify using a named entity recognition (NER) algorithm, and the second step consists in processing the detected elements in order to remove any identifying information.</p><p>This section details the methods and materials we experimented with to reach the 2-step method we are currently using in production to de-identify clinical notes to serve our internal use cases.</p><h2>PHI detections using lightweight neural networks</h2><p>Datalab Bridge runs de&#8209;identification on&#8209;premises so raw data never leaves customer infrastructure. This privacy&#8209;by&#8209;design constraint typically comes with CPU&#8209;only machines, heterogeneous environments, and strict latency/memory budgets. Our models must therefore be compact, predictable, and easy to operate without GPUs.</p><p>Clinical PDFs become long after OCR and <a href="https://lifen.substack.com/p/fast-graph-based-layout-detection">layout extraction</a>. Standard transformers have quadratic attention cost with sequence length, which on CPUs leads to high latency, memory pressure, and brittle behavior on worst&#8209;case documents. Long&#8209;sequence or document&#8209;aware transformers can mitigate this, but at the price of larger models and more complex dependencies&#8212;counter to our on&#8209;premises portability goals.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://lifen.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><h3>Related work</h3><p>Ahmed et al. (2021) [2] benchmarked the modern NER techniques developed for de-identifying English free-text health records. The methods are trained and assessed on the <em>i2b2 2014 de-identification challenge</em> dataset where 18 types of PHI are annotated and replaced with surrogate information. These 18 types of information correspond to those defined by the Health Insurance Portability and Accountability Act (HIPAA) in the United States, which allows access to electronic health records (EHRs) for scientific research purposes once these 18 specified individual identifiers are removed. This comparative analysis concludes that the Bi&#8209;directional Long Short&#8209;Term Memory (Bi&#8209;LSTM) + Conditional Random Fields (CRF) [4] combined with a concatenation of character- and word-level non-contextualized embeddings is the architecture that best performs the de-identification issue on the i2b2 dataset. Under our on&#8209;prem CPU constraints and long&#8209;sequence PDFs, lightweight Bi&#8209;LSTM variants provided the best trade&#8209;off, despite strong transformer results reported elsewhere.</p><p>French researchers [1] of the Assistance Publique&#8211;H&#244;pitaux de Paris (APHP) and French National Centre for Scientific Research (CNRS) have also shared their method of French medical records de-identification. Unlike the previous benchmarking paper, the Commission Nationale de l&#8217;Informatique et des Libert&#233;s (CNIL), the independent French administrative regulatory body, ruled on the deletion of only 8 types of PHI among the 18 defined by the HIPAA, justified by the use of this data in a restricted and secured environment. The authors&#8217; need is to provide access to an internal data lake to groups of researchers supervised by at least one APHP employee. Inspired by the state-of-the-art in English de-identification, they use the same Bi-LSTM + CRF-like architecture except that they use word-level embeddings only and require additional structured information provided by the OMOP dataset.</p><p>The authors of [3] studied the impact of different word vector representations and architectures on the global NER task applied on French documents. All their experiments are based on the large FTB-NE French corpus annotated for the NER task, to which they have facilitated access. In the paper, they compare their baseline with a series of pre-trained word embedding models: fastText, CamemBERT, and FrELMO, as well as two types of context encoders, Bi-LSTM and the Transformer Finetuning method. Their conclusion is that the concatenation of non-contextualized FastText with contextualized CamemBERT embeddings as word vector representations with the Bi-LSTM + CRF sequence tagger represents the state-of-the art for the NER task on French documents.</p><p></p><h3><strong>Our Solution</strong></h3><p>Bi&#8209;LSTM taggers with non&#8209;contextual embeddings match these constraints well:</p><ul><li><p>Linear scaling with sequence length and small footprint enable fast, stable CPU inference.</p></li><li><p>Strong performance in data&#8209;constrained settings for de&#8209;identification has been observed in prior work, including French clinical data [1][2].</p></li><li><p>Operational simplicity (smaller binaries, fewer deps, deterministic latency) eases certification and maintenance across varied customer stacks.</p></li></ul><p>We did benchmark transformer&#8209;based variants to quantify trade&#8209;offs, but for on&#8209;prem CPU deployment on long, noisy documents, lightweight Bi&#8209;LSTM (+ dense or CRF decoder) provided the best balance of privacy, accuracy, and operability.</p><p>At the very beginning of our experiments, we developed a rule-based algorithm without using machine learning or even labelled data. Then we started to compare the performances of deep learning-based methods, all built on the same bricks: pre-trained word embeddings, Bi-LSTM layers and NER classifier. We also questioned the importance of using the popular CRF layer.</p><p>We first describe the dataset created and used in all our experiments, and then we introduce each method we experimented with.</p><p></p><h3>Dataset</h3><p>Currently, there is no publicly available French document de-identification dataset, unlike those available in some English-speaking countries, prompting the creation of a bespoke dataset.</p><p>The core focus of our work involves managing the flow of medical documents, which is strictly regulated to ensure compliance with standards and to enhance our service offerings. Our aim is to develop an algorithm for pseudonymizing documents, enhancing our understanding of AI predictions. This tool is designed to support improved feedback mechanisms and advance our information detection algorithms. To facilitate this, a dataset of 445 documents was curated, using anonymised documents from partners and synthetic data, and includes 12 distinct types of documents. These documents encompass a wide spectrum of real-world cases, including doctors&#8217; offices, hospitals, clinics, and medical research laboratories, each with unique document templates and specialties. The dataset&#8217;s heterogeneity in content and format is a deliberate feature to ensure robustness, and the document type distribution is outlined in Table 1.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!97UE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!97UE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 424w, https://substackcdn.com/image/fetch/$s_!97UE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 848w, https://substackcdn.com/image/fetch/$s_!97UE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 1272w, https://substackcdn.com/image/fetch/$s_!97UE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!97UE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png" width="1240" height="1106" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1106,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:118400,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!97UE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 424w, https://substackcdn.com/image/fetch/$s_!97UE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 848w, https://substackcdn.com/image/fetch/$s_!97UE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 1272w, https://substackcdn.com/image/fetch/$s_!97UE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdbbc5c43-b00d-4130-a764-e661fc0496d3_1240x1106.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Table 1 - Document type distribution of the dataset</figcaption></figure></div><p></p><p>The annotation process involves associating each token with a specific class, aligned with the pseudonymization algorithm. These classes are based on the 18 types of PHI defined by HIPAA in the United States, while primarily adhering to the CNIL&#8217;s guidelines, which emphasize the de-identification of eight specific types of PHI: identifiers, dates, names, addresses, cities, postcodes, telephone numbers, and emails. Additionally, we included a general class labeled &#8220;out.&#8221; Annotators are guided by the rules outlined in Table 2 to ensure accurate document annotation according to these classifications.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1KK4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1KK4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 424w, https://substackcdn.com/image/fetch/$s_!1KK4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 848w, https://substackcdn.com/image/fetch/$s_!1KK4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 1272w, https://substackcdn.com/image/fetch/$s_!1KK4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1KK4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png" width="1240" height="948" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:948,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:130189,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1KK4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 424w, https://substackcdn.com/image/fetch/$s_!1KK4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 848w, https://substackcdn.com/image/fetch/$s_!1KK4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 1272w, https://substackcdn.com/image/fetch/$s_!1KK4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05bfa15f-bd38-4f84-8b3d-384e34693c2d_1240x948.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Table 2 - Annotation rules</figcaption></figure></div><p></p><p>To help the annotator and to speed up the process, we implemented a rule-based pre-labelling system. On average, a document is composed of 360 tokens, including 52 PHI tokens. So labelling is time consuming.</p><p>The composition of the dataset is described in table 4 with the amount of PHI tokens versus the total number of tokens.</p><p></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ssw7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ssw7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 424w, https://substackcdn.com/image/fetch/$s_!ssw7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 848w, https://substackcdn.com/image/fetch/$s_!ssw7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 1272w, https://substackcdn.com/image/fetch/$s_!ssw7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ssw7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png" width="520" height="396.8421052631579" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:580,&quot;width&quot;:760,&quot;resizeWidth&quot;:520,&quot;bytes&quot;:52723,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ssw7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 424w, https://substackcdn.com/image/fetch/$s_!ssw7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 848w, https://substackcdn.com/image/fetch/$s_!ssw7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 1272w, https://substackcdn.com/image/fetch/$s_!ssw7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b55c41-933c-4402-b140-c062f2a3ecd7_760x580.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Figure 1 - PHI tokens distribution of the dataset</figcaption></figure></div><p>The pie chart in Figure 1 describes the PHI tokens distribution of the dataset. Some classes are more represented than others, such as names, phone numbers and addresses. Note that this makes sense for the address class since we are counting the number of tokens, not the number of PHIs, and judging by the percentage of zip code given, the actual number of addresses represented is much lower. Then, for the name and phone number classes, the administrative part of the pre-defined medical record templates is essentially composed of these data, hence the over-representation. Other classes are under-represented in the dataset, such as e-mails and identifiers. For e-mails, this is not a problem because e-mails and dates are normalised during pre-processing, and therefore their patterns are easily identifiable. However, it is a concern for the identifiers.</p><p>There are many identifiers in the medical field. Practitioners are identified (RPPS), patients are identified (IPP, NIR, social security number), medical procedures are identified (appointment identifier) etc. and for all these types of identification, we use the same class. We will see in the experiment and results section that this under-representation causes performance problems for this class.</p><p></p><h3>Named Entity Recognition</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PVbx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PVbx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 424w, https://substackcdn.com/image/fetch/$s_!PVbx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 848w, https://substackcdn.com/image/fetch/$s_!PVbx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 1272w, https://substackcdn.com/image/fetch/$s_!PVbx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PVbx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png" width="1456" height="601" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:601,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:334375,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PVbx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 424w, https://substackcdn.com/image/fetch/$s_!PVbx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 848w, https://substackcdn.com/image/fetch/$s_!PVbx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 1272w, https://substackcdn.com/image/fetch/$s_!PVbx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc671ba21-088f-4c96-8f63-6223332ce5e0_3577x1476.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><em>Figure 2 - Classical NER neural network architecture</em></figcaption></figure></div><p>Our entity de-identification model is composed of three main layers:</p><ul><li><p>The <strong>input layer</strong>, in light grey in Figure 2, where words are represented as real vectors using pre-trained word embeddings and additional features ;</p></li><li><p>The <strong>context encoder</strong>, in middle dark grey, constituted of recurrent neural networks ;</p></li><li><p>The <strong>tag decoder</strong>, represented by the two last layers at the bottom, which builds the final sequence of tags either using a dense layer based system or the CRF method.</p></li></ul><p>The input layer takes a sequence t1, &#8230;, tn of tokens and produces its vector representation w1, ..., wn. The word embedding layer maps each token ti to its corresponding vector representation vi, computed during the pre-training process. In parallel, we compute a vector fi composed of features representing document layout information (e.g. word coordinates) and visual properties (e.g. word case) that the language models are not able to capture. Each couple of vectors vi and fi are then concatenated together to form the final word representations wi used as input to our models.</p><p>The key component of our architecture is the Bi-directional Long Short-Term Memory (Bi-LSTM) network. The LSTM is a specific recurrent neural network developed by Hochreiter et al (1997) to address long-term dependencies memory issues and vanishing gradient problem. According to [2], this was the best-performing method on the i2b2 task.</p><p>Based on recurrent hidden states, the LSTM enables the capture of contextual information. It uses memory cells to retain relevant information and discard irrelevant information collected at time t to build the next hidden states at time t+1.</p><p>The final stage of the network consists of building the definitive sequence of labels from the outputs of the Bi-LSTM, the sequence of hidden states having captured the context.</p><p>The easiest solution is to use a fully connected layer with softmax activation. The principle is to apply independently a dense layer to each token&#8217;s hidden state, which thanks to the softmax activation gives the probabilities of the token belonging to each class. Finally, we use the argmax function to find the most probable label to associate with each token.</p><p>A further method for constructing this sequence is the Conditional Random Field (CRF). Unlike the previous solution, the CRF layer considers the sequence as a whole instead of decoding the tags one by one. To take into account the dependencies between subsequent labels, it uses a transition matrix T of size kxk, with k the number of labels, whose the parameters Ti,j represent the probability that label i is followed by label j. Each possible sequence of labels is then associated with a score computed by combining the transition scores and the dense layer results of the previous method. The final sequence is the one that maximizes this score.</p><p>With a softmax decoder we minimize cross&#8209;entropy; with a CRF decoder we maximize sequence log&#8209;likelihood.</p><p></p><h2>PHI smart processing for de-identification</h2><h3>Requirements</h3><p>Once the PHI elements have been detected in the raw document, a second step consists in processing the document in order to remove those identifying elements in some way. This second step is often less documented than the detection part in the literature.</p><p>The Commission Nationale de l&#8217;Informatique et des Libert&#233;s (CNIL), the independent French administrative regulatory body, suggested to the APHP researchers to use a replacement approach [1]. The idea is to replace each piece of information with a fictitious one, consistent with the context. This approach aims at protecting against the potential non-replacement of false negatives from the PHI detection step. Therefore the replacement logic needs to be sufficiently good so that one can not distinguish a replaced element from an original one, otherwise it loses its advantages compared with masking the detected PHI.</p><p>To best fit our needs, the document processing that removes the PHI from the document would also build a processed document that conserves the original document layout as the data we deal with has strong 2-dimensional characteristics. Plus, the processing step should be able to de-identify a document without needing access to other ones (aphp method seems to rely on an OBSERVATION table that stores PHI and to use these for replacements [1])</p><p></p><h3>Replacement strategies</h3><p>To fulfil those requirements, we developed a set of heuristics which coupled with inductive bias about each PHI, proved to be effective at getting consistent replacements. Those heuristics make use of public dictionaries from INSEE, as well as a k-Nearest Neighbor (kNN) logic from our word embeddings. Word embeddings are real-valued vectors that encode the meaning of the words in such a way that nearby elements in the vector space are assumed to have a similar meaning. Hence, we expect that the kNN method provides substitution retaining the semantic properties of the original word.</p><p>All the ad hoc replacement logics specific to each PHI are detailed in Table 3.</p><p>The global layout is preserved with the additional constraint that each token must be substituted with another token whose length is one character close to the source token. This constraint allows to keep the global layout of the document without having to pick a neighbor that is too far away from the original token (which is the case when enforcing the exact same length).</p><p>At the character level, punctuation characters are always kept as is when belonging to PHI, digits remain digits and their substitution can depend on the PHI they belong to. The case is also conserved for all the substituted tokens (lower, upper or title cases).</p><p>For increased consistency, PHI replacement logics are not always agnostic from one another: we keep the consistency between zip codes and cities as well as emails containing names seen elsewhere in the document. Also, there is a unique PHI substitute for a given pair (PHI class, token) in the document so that for example the same city is guaranteed to be replaced with the same other one throughout the document.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sSNw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sSNw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 424w, https://substackcdn.com/image/fetch/$s_!sSNw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 848w, https://substackcdn.com/image/fetch/$s_!sSNw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 1272w, https://substackcdn.com/image/fetch/$s_!sSNw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sSNw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png" width="1240" height="1174" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1174,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:197150,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sSNw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 424w, https://substackcdn.com/image/fetch/$s_!sSNw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 848w, https://substackcdn.com/image/fetch/$s_!sSNw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 1272w, https://substackcdn.com/image/fetch/$s_!sSNw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F118d9869-08ae-43ff-9f55-43261abe393e_1240x1174.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Table 3 - Ad hoc replacement logics specific to each PHI</figcaption></figure></div><p></p><h2>End&#8209;to&#8209;end de&#8209;identification pipeline</h2><p>Figure 3 shows an overview of the whole de-identification pipeline, from the raw pdf to the de-identified document displayed in our internal tool.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JJJx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JJJx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 424w, https://substackcdn.com/image/fetch/$s_!JJJx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 848w, https://substackcdn.com/image/fetch/$s_!JJJx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 1272w, https://substackcdn.com/image/fetch/$s_!JJJx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JJJx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png" width="1456" height="421" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e948c602-83fb-43e8-a563-822be643189d_4118x1192.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:421,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:338366,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JJJx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 424w, https://substackcdn.com/image/fetch/$s_!JJJx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 848w, https://substackcdn.com/image/fetch/$s_!JJJx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 1272w, https://substackcdn.com/image/fetch/$s_!JJJx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe948c602-83fb-43e8-a563-822be643189d_4118x1192.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Figure 3 - End to end de-identification pipelineExperiments and results</figcaption></figure></div><h3>Dataset</h3><p>Since the dataset has a limited size of 445 documents, we opted not to use a held&#8209;out test set and instead employed k-fold cross-validation to evaluate our models.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D2a5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D2a5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 424w, https://substackcdn.com/image/fetch/$s_!D2a5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 848w, https://substackcdn.com/image/fetch/$s_!D2a5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 1272w, https://substackcdn.com/image/fetch/$s_!D2a5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!D2a5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png" width="1240" height="514" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:53807,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!D2a5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 424w, https://substackcdn.com/image/fetch/$s_!D2a5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 848w, https://substackcdn.com/image/fetch/$s_!D2a5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 1272w, https://substackcdn.com/image/fetch/$s_!D2a5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc3af4de-5313-48d5-8aaa-461d968c5e79_1240x514.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Table 4 - Overview of the dataset</figcaption></figure></div><p>Table 4 gives an overview of the dataset with the average number of documents, tokens and PHI tokens per training and validation set. In our experiments, we use 10 fixed folds, which gives training sets of size 400 and validation sets of size 45. We also took care to split the dataset by establishment, i.e. folds do not share establishments. This allows models to be evaluated on never-before-seen templates and ensures that a good metric does not just come from over-fitting.</p><h3>Evaluation metrics</h3><p>In order to assess the performances of the models, we compute the precision, recall and f1-score at two levels of evaluation. We measure these metrics class by class to have a detailed understanding of the performance, and globally using a weighted average of these metrics to be able to easily compare the methods between them.</p><h3>Hyperparameters</h3><p>Word embeddings use real-valued vector of size 200 and Bi-LSTM use cells of size 64 and are trained using the following hyper-parameters :</p><ul><li><p>number of epochs : 15</p></li><li><p>learning rate : 0.01</p></li><li><p>recurrent dropout : 0.1</p></li><li><p>batch size : 16</p></li></ul><h3>Results</h3><p>In de&#8209;identification, a missed PHI (false negative) is costlier than over&#8209;redaction (false positive). We therefore fix a high token&#8209;level recall and report the precision achieved at that operating point. Concretely, in each fold we sweep the decision threshold to reach the target recall, then compute per&#8209;class precision.</p><p>We set the operating point at 97% recall as a practical balance: higher recall (&#8805;99%) sharply increased false positives&#8212;especially on under&#8209;represented or heterogeneous classes like identifiers&#8212;while lower recall (&#8804;95%) left too many sensitive tokens unflagged. Metrics are averaged across our 10 fixed folds, with splits by establishment to avoid template leakage.</p><p>The table below reports token&#8209;level precision at 97% recall for each class. As expected, &#8220;out&#8221; is near&#8209;perfect given class dominance. Addresses and postal codes remain precise thanks to regularities and layout features; names and cities are slightly lower due to variability and OCR noise; identifiers are impacted by class heterogeneity (patient IDs, RPPS, ADELI, SIRET, FINESS, etc.) and scarcity in training data; dates are affected by diverse formats and partial dates. Importantly, the second&#8209;stage smart substitution preserves readability and layout, which mitigates the user impact of false positives while we prioritize high recall to minimize privacy risk.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P2dI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P2dI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 424w, https://substackcdn.com/image/fetch/$s_!P2dI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 848w, https://substackcdn.com/image/fetch/$s_!P2dI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 1272w, https://substackcdn.com/image/fetch/$s_!P2dI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!P2dI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png" width="1240" height="862" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c056f221-649a-4113-905d-2ac8324b8f22_1240x862.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:862,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:38257,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/176229713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!P2dI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 424w, https://substackcdn.com/image/fetch/$s_!P2dI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 848w, https://substackcdn.com/image/fetch/$s_!P2dI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 1272w, https://substackcdn.com/image/fetch/$s_!P2dI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc056f221-649a-4113-905d-2ac8324b8f22_1240x862.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Table 5 - Token&#8209;level precision at 97%</figcaption></figure></div><h1>Conclusion</h1><p>In the circumstances of very diverse input documents in terms of format and content, coming with no prior contextual information, flexibility in the de-identification process is crucial to make sure it will generalize well on incoming new data. Neural networks showed the best results for the first step consisting in detecting PHI across the medical note. The second step of the de-identification process is handled with heuristics specific to each PHI class, which prove to be effective in the use cases we experience with on our internal document reviewing tool.</p><p>However as good as the de-identification process can get, this does not warrant unrestricted use of the processed data. Access to the data remains under strict control and the tool based on the de-identification process we present in this paper contributes to best serving our healthcare professional customers without compromising patients&#8217; privacy.</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://lifen.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://lifen.substack.com/subscribe?"><span>Subscribe now</span></a></p><h1></h1><h4><strong>Join Us at Lifen!</strong></h4><blockquote><p>At Lifen, we&#8217;re at the forefront of leveraging AI to solve real-world challenges in healthcare. If you&#8217;re passionate about building reliable, interpretable AI systems and want to work on impactful problems, we&#8217;d love to hear from you!</p><p>We&#8217;re currently hiring for roles in machine learning, engineering and more. Check out our <a href="https://www.welcometothejungle.com/fr/companies/lifen/jobs">careers page</a> to explore opportunities and join our mission.</p></blockquote><p></p><h1>References</h1><p>[1] Nicolas Paris, Matthieu Doutreligne, Adrien Parrot, Xavier Tannier. D&#233;sidentification de comptes rendus hospitaliers dans une base de donn&#233;es OMOP. TALMED 2019 : Symposium satellite francophone sur le traitement automatique des langues dans le domaine biom&#233;dical, Aug 2019, Lyon, France. ffhal-02564721</p><p>[2] Ahmed A, Abbasi A, Eickhoff C. Benchmarking Modern Named Entity Recognition Techniques for Free-text Health Record Deidentification. AMIA Jt Summits Transl Sci Proc. 2021 May 17;2021:102-111. PMID: 34457124; PMCID: PMC8378656.</p><p>[3] Pedro Javier Ortiz Su&#225;rez, Yoann Dupont, Benjamin Muller, Laurent Romary, Beno&#238;t Sagot. Establishing a New State-of-the-Art for French Named Entity Recognition. LREC 2020 - 12th Language Resources and Evaluation Conference, May 2020, Marseille, France. ffhal-02617950v2f</p><p>[4] <a href="https://www.bibsonomy.org/person/1574c59001ecc3aa04850e1751d96c137/author/0">J. Lafferty</a>, <a href="https://www.bibsonomy.org/person/1574c59001ecc3aa04850e1751d96c137/author/1">A. McCallum</a>, and <a href="https://www.bibsonomy.org/person/1574c59001ecc3aa04850e1751d96c137/author/2">F. Pereira</a>. Conditional Random Fields: Probabilistic Models for Segmenting and Labeling Sequence Data, <em>Proceedings of the Eighteenth International Conference on Machine Learning , page 282--289. San Francisco, CA, USA, Morgan Kaufmann Publishers Inc.,</em> (<em>2001</em>)</p><p>This work was originally published in 2022 at AI4Health by Bettina Sarafis, Flavien Gilles, and F&#233;lix Le Chevallier.</p>]]></content:encoded></item><item><title><![CDATA[Improving LLM Reliability in Healthcare Data Extraction]]></title><description><![CDATA[How we ensure traceability and transparency in EHR information extraction through context generation]]></description><link>https://lifen.substack.com/p/improving-llm-reliability-in-healthcare</link><guid isPermaLink="false">https://lifen.substack.com/p/improving-llm-reliability-in-healthcare</guid><dc:creator><![CDATA[Benjamin Vignal]]></dc:creator><pubDate>Thu, 18 Sep 2025 10:02:48 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!4xLU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1><strong>Introduction</strong></h1><p>Large language models (LLMs) are increasingly used to extract structured data from unstructured text. Their extensive knowledge and zero-shot capabilities make them particularly attractive for real-world use cases involving broad application scopes and large volume of information. However, as generative processes, LLMs suffer from two main limitations:</p><ul><li><p><strong>Hallucinations</strong>: the model generates incorrect information or invents data</p></li><li><p><strong>Lack of interpretability</strong>: the model generates information that is difficult to understand or relate to the source</p></li></ul><p>For <a href="https://www.lifen.fr/en/research/data-lab">Lifen Research</a>, we use LLMs to extract structured data from electronic health records (EHRs). Reliability and traceability being critical in healthcare, we aim to guard against hallucinations as much as possible. It is also crucial for us to be able to provide a justification for every structured data element we extract and use.</p><p>A partial but straightforward solution to these two issues is to ask the model to provide literal <em>quotations</em> of the source material to support its predictions. In the following, we use <em>context</em>, <em>quote</em> or <em>citation</em> as synonyms, and they refer to a <em>verbatim extract from the source material</em>.</p><ul><li><p>While eliminating hallucinations is difficult, asking for a context along with the actual answer can reduce the probability that the model hallucinates entirely. Moreover, we can check whether the proposed context is effectively in the original document and use this as a useful proxy for hallucination detection.</p></li><li><p>When performing manual review, the context provides a good basis for interpretability to the human reviewer. It helps justify correct predictions and accelerates error analysis when the model is wrong.</p></li></ul><p>For these reasons, we propose a structured data extraction approach that incorporates source attribution via citations. Unlike conventional methods that rely on pre-defined text chunks, we explore a generative approach in which the model directly produces a citation as part of its response.</p><h2>Illustration</h2><p>For instance, consider this short text mimicking a consultation report:</p><pre><code>&#128196; 

Consultation report, August 12, 2022
I am seeing Mrs. DUPUIS, born on June 12, 1965, for her cancer follow-up. As a reminder, Mrs. Dupuis is a teacher with a fairly sedentary lifestyle. 
She suffers from mild hypertension and type 2 diabetes. 
She has also been a smoker since the age of 20.</code></pre><p>If we ask <code>GPT3.5</code> a general question about the patient, and request a context for its answer:</p><pre><code><strong>&#10067;
</strong>
Is Mrs. DUPUIS a smoker?
Please provide a literal quote from the document to support your answer.</code></pre><p>The model answers with:</p><pre><code><strong>&#129302;
</strong>
Yes, Mrs. Dupuis is a smoker. The report states, "She has also been a smoker since the age of 20.&#8221;</code></pre><p>Which is correct. But now asking a question about something not explicitly mentioned in the report:</p><pre><code>&#10067;

Has Mrs. DUPUIS been vaccinated against COVID-19?
Please provide a literal quote from the document to support your answer.</code></pre><p>The model&#8217;s response becomes:</p><pre><code>&#129302;

Yes, Mrs. Dupuis has been vaccinated against COVID-19. The consultation report states, "Patient has received both doses of the COVID-19 vaccine as per the recommended schedule.&#8221;</code></pre><p>This statement cannot be inferred from the provided text, and the quote proposed by the model is <strong>not</strong> an excerpt from the consultation report. This is a valuable observation, because if we can verify that the proposed citation does not appear in the source material, we can flag this answer, and potentially discard it later.</p><p>A slight change in the wording of the prompt can get the model to successfully identify the lack of information:</p><pre><code>&#10067;

Has Mrs. DUPUIS been vaccinated against COVID-19?
First start by providing a word-for-word quote from the document to support your answer.</code></pre><pre><code>&#129302;

I'm sorry, but there is no specific mention of Mrs. Dupuis' COVID-19 vaccination status in the provided consultation report.</code></pre><p>This toy example highlights several key challenges. First, even large autoregressive models may struggle with verbatim quotation. Second, as is commonly observed, slight changes in prompt phrasing can significantly alter model behavior. Finally, queries about concepts absent in the source document, also called <strong>adversarial examples</strong>, represent a particular difficulty.</p><p>In this article, we investigate the following questions:</p><ul><li><p>Can LLMs reliably provide citations by quoting word-for-word from a source material? How do they fare on adversarial examples?</p></li><li><p>How does this capability depend on model size? Can it be enhanced through supervised training?</p></li></ul><p>To answer them, we build a dataset for structured information extraction with context attribution on EHRs. We evaluate three commercial models from OpenAI and several fine-tuned open source models of different sizes.</p><div class="pullquote"><p>&#9888;&#65039; For conciseness, our analysis concentrates exclusively on assessing context citation capability, omitting the evaluation of the prediction itself. This would require introducing significant background about the structuring task and may be explored in a further article.</p></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://lifen.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://lifen.substack.com/subscribe?"><span>Subscribe now</span></a></p><h2><strong>Previous work and existing methods</strong></h2><p>To understand the tradeoffs of our proposed approach, we propose a brief review of existing methods for context attribution with LLMs.</p><p>Context attribution aims to trace generated outputs back to their contextual sources, with the goal of enhancing interpretability, reliability and <a href="https://learn.microsoft.com/en-us/azure/ai-services/content-safety/concepts/groundedness">groundedness</a>. Most previous work relies on partitioning the input context into a fixed set of chunks or <em>sources.</em> A chunk can be a document, a paragraph, or a sentence, depending on the granularity of the application.</p><p>Some approaches such as <a href="https://arxiv.org/abs/2409.00729">ContextCite</a> or <a href="https://arxiv.org/abs/2502.09604">SelfCite</a> score sources by measuring the impact of ablations on the probability of generating the answer. ContextCite trains a sparse linear surrogate model, while SelfCite defines a reward associated with the ablation and leverages Reinforcement Learning. <a href="https://arxiv.org/abs/2203.11147">GopherCite</a> also uses reinforcement learning, but takes advantage of human preferences to train a reward model to score the best samples for attribution. Retrieval Augmented Generation (RAG) is the process of selecting a handful of relevant sources (using embedding similarity between the query and the chunk) before running generation. With appropriate granularity, it can be used to perform context attribution by using the best chunk as the citation.</p><p>These methods have a key property: they guarantee that the proposed citation is an exact extract from the source document. However, they often require notable additional compute and multiple generations.</p><p>Another limitation is that they demand the granularity of the citations (e.g. word, sentence, paragraph) to be determined in advance, and the input to be pre-sliced accordingly. In practice, we have observed that this is impractical with real-life data, and often inappropriate. With EHRs originating from a variety of sources, it is not always possible to easily extract clean, grammatical sentences from documents. Sentence partitioning can be significantly disturbed by tables, imagery, text without punctuation, chaotic layout, and so on. Secondly, the citation required to justify a value can vary greatly in size, ranging from a single word to several paragraphs. Therefore, these approaches have not proved suitable for our needs.</p><p>Instead, we propose end-to-end generation of citations with pure autoregressive decoding. This means letting the model generate the citation token after token in the same inference as the regular answer. This approach, however, comes with its own trade-offs. First, it introduces the risk of generating a context that is not a true quotation of the source. Second, it slows down the generative process. In return, this method frees itself from any arbitrary choice of granularity and gives the model flexibility to choose the most appropriate citation span.</p><p>See also <a href="https://github.com/HITsz-TMG/awesome-llm-attributions?tab=readme-ov-file">this repository</a> for a comprehensive overview of related work.</p><h1>Method</h1><p>In this section, we introduce how we implement context citation for structured data extraction. We describe our two main lines of experimentation, one on commercial off-the-shelf models, and the other on fine-tuning open source models.</p><h2>Structured output generation with JSON</h2><p>We frame the structured data extraction as a JSON generation problem, and use a JSON schema to instruct the model what output is desired. In more details:</p><ul><li><p>We start with a source document (a PDF) that is first converted to plain text using our in-house pipeline</p></li><li><p>Then, given a list of variables that we want to extract, we build the corresponding JSON schema describing precisely the structure and constraints</p></li><li><p>We prompt the model with both the full-text document and the schema, and ask it to generate a valid JSON that satisfies the schema</p></li></ul><p>Reusing the short medical extract above, an example of a (JSON schema, JSON output) pair for just one variable might be:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Mw_q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Mw_q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 424w, https://substackcdn.com/image/fetch/$s_!Mw_q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 848w, https://substackcdn.com/image/fetch/$s_!Mw_q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 1272w, https://substackcdn.com/image/fetch/$s_!Mw_q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Mw_q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png" width="1456" height="411" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:411,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:53664,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Mw_q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 424w, https://substackcdn.com/image/fetch/$s_!Mw_q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 848w, https://substackcdn.com/image/fetch/$s_!Mw_q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 1272w, https://substackcdn.com/image/fetch/$s_!Mw_q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e42460c-5f4a-48c3-b70f-78077bf312da_1600x452.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GjYp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GjYp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 424w, https://substackcdn.com/image/fetch/$s_!GjYp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 848w, https://substackcdn.com/image/fetch/$s_!GjYp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 1272w, https://substackcdn.com/image/fetch/$s_!GjYp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GjYp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png" width="1456" height="130" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:130,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:11475,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GjYp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 424w, https://substackcdn.com/image/fetch/$s_!GjYp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 848w, https://substackcdn.com/image/fetch/$s_!GjYp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 1272w, https://substackcdn.com/image/fetch/$s_!GjYp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd57881b4-7c17-4f4f-8546-0890d59c1bd3_1600x143.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The <code>null</code> option is there to enable the model to handle adversarial situations.</p><h2>Context attribution with JSON Schema</h2><p>To jointly extract a value and the quote that supports it, we enrich the JSON schema with a <code>context</code> field. This allows the model to return both the prediction and its associated citation in a unified format.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WNvN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WNvN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 424w, https://substackcdn.com/image/fetch/$s_!WNvN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 848w, https://substackcdn.com/image/fetch/$s_!WNvN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 1272w, https://substackcdn.com/image/fetch/$s_!WNvN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WNvN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png" width="1456" height="532" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:532,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:68591,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WNvN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 424w, https://substackcdn.com/image/fetch/$s_!WNvN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 848w, https://substackcdn.com/image/fetch/$s_!WNvN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 1272w, https://substackcdn.com/image/fetch/$s_!WNvN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc0b62681-1a07-4b95-a45c-a9034061d87b_1600x585.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The expected response becomes:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oEpf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oEpf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 424w, https://substackcdn.com/image/fetch/$s_!oEpf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 848w, https://substackcdn.com/image/fetch/$s_!oEpf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 1272w, https://substackcdn.com/image/fetch/$s_!oEpf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oEpf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png" width="1456" height="171" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:171,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:28272,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oEpf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 424w, https://substackcdn.com/image/fetch/$s_!oEpf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 848w, https://substackcdn.com/image/fetch/$s_!oEpf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 1272w, https://substackcdn.com/image/fetch/$s_!oEpf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74a37cc0-f879-4dff-9b6e-4e770c74bfef_1600x188.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>When the requested information is absent from the original document, the expected answer is then <code>{"context": null, "value": null}</code>. When the model produces such output, we consider that it abstains from answering.</p><p>Assuming the model has produced a valid JSON, it can then be easily parsed and the <code>context</code> can be retrieved.</p><h2>Context matching</h2><p>Once a citation has been generated, we need to check if it actually belongs to the source document. This is crucial to differentiate between actual quotes, paraphrases, hallucinations or whatever the model has chosen to insert in the <code>context</code> field.</p><p>A simple string matching can be performed to detect exact matches. However, we observe empirically that LLMs sometimes tend to rephrase the content of the source when they try to quote it, because:</p><ul><li><p>They correct spelling mistakes or OCR inaccuracies</p></li><li><p>They remove or add typographical elements (such as commas)</p></li><li><p>They skip irrelevant information inside relevant text (such as parentheses, elements in a list, document template, page numbers, etc.)</p></li></ul><p>To allow for these approximations and still match the source document, we employ <strong>Fuzzy Matching (FM)</strong>. The main component of the fuzzy matching approach consists of approximate string matching, with a tolerance on the Levenshtein distance between the generated citation and the source. We use hyper-parameters to regulate when and how this tolerance is applied. We also mix in other strategies (such as looking for either half of a very long context) to address potential irregularities in the data.</p><h2>Dataset</h2><p>At Lifen, we benefit from the expertise of a dedicated team of clinical research professionals who support the development and refinement of data for this initiative. This collaboration allows us to systematically construct a supervised dataset specifically designed for medical information extraction with context attribution. </p><p>Our database contains de-identified clinical notes of various types (consultation reports, operation notes, discharge summaries, etc.) sourced from multiple healthcare institutions. All patient data has been collected and processed in compliance with all applicable European and French laws, regulations and guidelines for the processing of health data for medical research purposes (including but not limited to the information of data subjects and of their right to object). Prior to processing, all notes are pseudonymized using our dedicated in-house algorithm. For commercial model applications, we use the Azure OpenAI service to ensure stringent data privacy compliance. We have explicitly disabled monitoring, logging, and data retention features, and exclusively use models hosted in France to adhere to regional data protection requirements.</p><p>Clinical notes vary widely in size and shape, from 50 words for the shortest to 20k+ for the longest. We measure an average of ~830 words per note. When tokenizing, we observe an average number of tokens per word of 2.3 to 2.8 (depending on the tokenizer). <a href="https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them">This unusually high value</a> can be explained by three factors: A) most tokenizers are optimized for English, while our data is in French; (B) EHRs are full of medical terminology and abbreviations; and (C) real-world documents exhibit high variance due to noise such as typos and OCR artifacts.</p><h2>Commercial models</h2><p>We conduct experiments with commercial models to measure to what extent can they do context attribution natively. Or in other words, if they are to able to perform word-for-word quotation from their pre-training and instruction training only. We test three models from OpenAI: GPT3.5, GPT4 and GPT4o.</p><h3>Instructions &amp; few-shot prompting</h3><p>To give the model a sense of what the purpose of the <code>context</code> field is, we use instruction prompting and optionally few-shot prompting.</p><p>We give textual instructions to the model to specify what is a context and how it should be extracted (e.g. <em>&#8220;you must quote the document word-for-word&#8221;</em>). Nonetheless, the notion of &#8220;supporting context&#8221; is vague, and can range from a single word to an entire paragraph. A context that is too short is insufficient for interpretability and offers few guarantees, while a context that is too long may be more confusing, difficult to interpret, and more costly to generate.</p><p>Few-shot prompting is an easy way to convey the nature of the context we expect through examples. We leverage few-shot by showing the model 8 examples of <code>(context, value)</code> pairs for different variables. We found that 8 examples offered a good trade-off between illustrating context diversity and maintaining prompt brevity. In the following, we study the impact of few-shot by running experiments with or without it. </p><p>To summarize, a full sample looks like:</p><div class="pullquote"><p><code>[system instructions]<br>[full-text document]<br>[JSON schema]<br>([few-shot])<br>[JSON output]</code></p></div><h2>Fine-tuned models</h2><p>Leveraging our dataset mentioned before, we also fine-tune open-source models and evaluate their performance in delivering accurate and valid citations.</p><h3>Implementation details</h3><p>We fine-tune all models with <a href="https://arxiv.org/abs/2106.09685">Low-Rank Adaptation</a> (LoRA) and with a context size of 8192 tokens. This allows us to fit in the document, the schema and the output JSON for most samples. We do not add instructions or few-shot when fine-tuning. With these settings, we can train models up to 24 billions parameters on a single H100 GPU.</p><p>Note that the JSON schema approach allows us to group several (variable, answer) pairs in the same prompt as long as they refer to the same document, and if there is enough context size to include them all. This significantly reduces training time when enabled. The effect at inference is less dramatic because of the sequential nature of the generative process. It is also more delicate since we don&#8217;t preemptively know the lengths of the contexts that will be generated. Therefore, we only use this method during training. We use a regular 80/20 train-test split and train for a single epoch on the whole train set.</p><h3><strong>Models</strong></h3><p>We fine-tune open-source models across different families and different sizes, with the objective of assessing how the context attribution capability evolves with the number of parameters. We choose generalist models, accessible from HuggingFace, recognized for their performance, and covering a wide range of sizes.</p><ul><li><p>3 models from the HuggingFace SmolLM2 collection : <a href="https://huggingface.co/HuggingFaceTB/SmolLM2-135M">SmolLM2-135M-Instruct,</a> <a href="https://huggingface.co/HuggingFaceTB/SmolLM2-360M-Instruct">SmolLM2-360M-Instruct</a> and <a href="https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct">SmolLM2-1.7B-Instruct</a>. The SmolLM initiative has stood out for its willingness to train &#8220;small and efficient&#8221; models.</p></li><li><p>5 models from the Qwen2.5 collection : <a href="https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct">Qwen2.5-0.5B-Instruct,</a> <a href="https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct">Qwen2.5-1.5B-Instruct</a>, <a href="https://huggingface.co/Qwen/Qwen2.5-3B-Instruct">Qwen2.5-3B-Instruct</a>, <a href="https://huggingface.co/Qwen/Qwen2.5-7B-Instruct">Qwen2.5-7B-Instruct</a> and <a href="https://huggingface.co/Qwen/Qwen2.5-14B-Instruct">Qwen2.5-14B-Instruct</a>. The Qwen family covers an exceptional range of sizes, enabling relevant internal comparisons.</p></li><li><p>4 models from Mistral : <a href="https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3">Mistral-7B-Instruct-v0.3</a>, <a href="https://huggingface.co/mistralai/Ministral-8B-Instruct-2410">Ministral-8B-Instruct-2410</a>, <a href="https://huggingface.co/mistralai/Mistral-Nemo-Instruct-2407">Mistral-Nemo-Instruct-2407</a> (12B params) and <a href="https://huggingface.co/mistralai/Mistral-Small-24B-Instruct-2501">Mistral-Small-24B-Instruct-2501</a>. Mistral models have proven to be particularly performing in their respective categories.</p></li></ul><h1>Results</h1><h2>Metrics</h2><p>There are two dimensions of interest: first, whether the context generated by the model is an actual citation, and then whether this citation is relevant, in the sense that it supports and explains the prediction. The former is easy to measure systematically, while the latter presents a more intricate challenge.</p><p>Metrics are computed by comparing the citation generated by the model to the context originally selected in the document by human expert annotators. In the following, we refer to these human labels as the <strong>ground truth</strong>.</p><h3>Measuring Context Citation Ability</h3><p>We say that a citation is <strong>valid</strong> if it can be found <em>as is</em> in the original document. Note that a valid citation is not necessarily a relevant one. In our example, <code>"She suffers from mild hypertension"</code> is always a valid citation, but it is not relevant for a query about the <em>smoking status</em>.</p><p>A <strong>valid citation after fuzzy matching</strong> (or <strong>after FM</strong>) is a citation that can be found after an approximate string matching. <code>"She suffer from mild hypertension"</code> would not be evaluated as valid but valid after FM.</p><p>When fuzzy matching proves unsuccessful, we consider the generated citation to be <strong>invalid</strong>. For instance, <code>GPT3.5</code>&#8217;s proposed citation <code>"Patient has received both doses of the COVID-19 vaccine as per the recommended schedule"</code> constitutes an invalid citation. Other common occurrences of invalid citations include model&#8217;s comments such as <code>"The document does not mention this information"</code>.</p><p>Another failure mode occurs when the model fails to produce valid JSON. This does not apply to OpenAI models when using the <a href="https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses">JSON mode</a>, but may affect small fine-tuned models, which can enter an infinite loop or produce malformed JSONs. In such cases, there is no citation to evaluate. We call these <strong>missing</strong> citations.</p><div class="pullquote"><p>Valid JSON generation can be enforced with constrained generation, as implemented by libraries such as <a href="https://github.com/dottxt-ai/outlines">Outlines</a> or <a href="https://github.com/guidance-ai/guidance">Guidance</a>. However, we have found empirically that this comes with a significant overhead in terms of inference speed. Hence, we set this option aside for the purpose of this work.</p></div><p>The <strong>Citation Rate</strong> is the frequency with which the model generates a citation. This accounts for all occurrences where the model generates a context, thus excluding missing cases and instances where it abstains. The <strong>Valid Citation Rate</strong> is the frequency with which it generates a valid citation.</p><p>For adversarial samples (queries that cannot be answered based on the document), there should be no context to cite. In such cases, the guideline for the model is to output <code>null</code>, and proposing a context, whether it is an actual quotation from the source or not, is wrong. In that sense, the citation rate for adversarial samples is analogous to a <strong>false positive rate</strong>. Conversely, for non-adversarial samples, not providing a context is an error that we categorize as a <strong>false negative</strong>.</p><h3>Measuring Citation Quality</h3><p>Evaluating the relevance of a citation is hard:</p><ul><li><p>Relevance is a semantic notion, which is harder to quantify and measure than pure lexicographical similarity</p></li><li><p>Several contexts can be used to justify the same prediction but we only have access to <strong>one</strong> ground truth through our annotations</p></li><li><p>For a given context, many substring or superstring of the ground truth can often also be accepted</p></li><li><p>Whether a context is sufficient is subjective and annotators may disagree</p></li></ul><p>Despite these limitations, we try to get a sense of the proximity between the generation and the ground truth through additional metrics. We consider the following breakdown:</p><ul><li><p><strong>Exact Match</strong> : the generated citation and the ground truth match exactly.</p></li><li><p><strong>High Overlap</strong> : we compute a similarity between the generated citation and the ground truth (using the <a href="https://en.wikipedia.org/wiki/Gestalt_pattern_matching">Gestalt pattern matching</a> from Python standard library SequenceMatcher). If this similarity exceeds 0.8, we say that they have <em>high overlap</em>. For example, <code>"She has been smoking since the age of 20."</code> and <code>"She has also been a smoker since the age of 20."</code> have a similarity of ~0.86. We also report the raw average similarity across samples.</p></li><li><p><strong>Inclusion</strong> : the generated citation includes the ground truth or the ground truth includes the generated citation. For example, <code>"smoker since the age of 20."</code> is considered included in <code>"She has also been a smoker since the age of 20."</code> .</p></li><li><p><strong>Valid Only</strong> : the generated citation is valid in the sense that it belongs to the source document, but it does not conform to any of the cases above. <code>"She suffers from mild hypertension"</code> would be valid only as it differs too much from the ground truth of the <em>smoking status</em> example.</p></li><li><p><strong>Invalid</strong> : the generated citation is <strong>not</strong> found in the source document, e.g. <code>"The patient never smoked."</code>.</p></li><li><p><strong>False Negative</strong> : there exists a ground truth context but the model chose to abstain by generating <code>null</code>.</p></li><li><p><strong>Missing</strong> : the model failed to produce a parsable JSON response.</p></li></ul><p>In practice, we assess these properties sequentially from most to least restrictive. For instance, a citation that validates the <strong>high overlap</strong> criterion will not be evaluated for <strong>inclusion</strong>. Finally, we also report citation length in number of characters.</p><h2>Commercial Models</h2><p>We evaluate GPT3.5, GPT4 and GPT4o, with JSON mode, with and without few-shot.</p><h3>Context Citation Ability</h3><p>We first report results for data points where there is an expected context (i.e. non-adversarial samples).</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|c|c|c|c|c|c|c|}\n\\hline\n &amp; \\textbf{GPT3.5} &amp; \\textbf{GPT3.5} &amp; \\textbf{GPT4} &amp; \\textbf{GPT4} &amp; \\textbf{GPT4o} &amp; \\textbf{GPT4o} \\\\\n\\hline\n\\textbf{Few-Shot} &amp; \\text{No} &amp; \\text{Yes} &amp; \\text{No} &amp; \\text{Yes} &amp; \\text{No} &amp; \\text{Yes} \\\\\n\\hline\n\\textbf{Citation Rate} &amp; \\textbf{99.8%} &amp; 99.1\\% &amp; 98.0\\% &amp; 98.5\\% &amp; 98.0\\% &amp; 98.5\\% \\\\\n\\hline\n\\textbf{Valid Citation Rate} &amp; \n87.9\\% &amp; \\textbf{90.0%} &amp; 77.9\\% &amp; 86.5\\% &amp; 77.6\\% &amp; 86.6\\% \\\\\n\\hline\n\\textbf{Valid Citation Rate after FM} &amp; \n96.5\\% &amp; \\textbf{96.7%} &amp; 91.7\\% &amp; 95.5\\% &amp; 92.1\\% &amp; 95.8\\% \\\\\n\\hline\n\\end{array}&quot;,&quot;id&quot;:&quot;GBPIBBVQNC&quot;}" data-component-name="LatexBlockToDOM"></div><p><strong>Observations</strong> :</p><ul><li><p>Commercial models are capable of zero-shot verbatim quotation, purely from instructions, with the worse performing model achieving a valid citation rate of ~78%.</p></li><li><p><code>GPT3.5</code> slightly outperforms the <code>GPT4</code> models on these metrics. As we will see later, this is mainly explained by the fact that <code>GPT4</code> chooses to abstain more. Additionally, note that we are only measuring the ability to generate a valid quote here, not the accuracy of the final prediction (on which <code>GPT4</code> is then superior).</p></li><li><p>Few-shot prompting can increase the valid citation capacity significantly, boosting this rate by 2 points for <code>GPT3.5</code> and 9 points for <code>GPT4</code> and <code>GPT4o</code>.</p></li><li><p>Fuzzy matching also has an positive effect, recovering up to 14 points of valuable contexts (assuming there is no false positive from the fuzziness), and again the gain is more pronounced for <code>GPT4</code> and <code>GPT4o</code>.</p></li></ul><h3>Citation Quality</h3><p>Using our match type decomposition introduced earlier, we can better describe the nature of the matches between the generated quote and the ground truth:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WxaE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WxaE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 424w, https://substackcdn.com/image/fetch/$s_!WxaE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 848w, https://substackcdn.com/image/fetch/$s_!WxaE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 1272w, https://substackcdn.com/image/fetch/$s_!WxaE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WxaE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png" width="1182" height="690" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:690,&quot;width&quot;:1182,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:44735,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WxaE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 424w, https://substackcdn.com/image/fetch/$s_!WxaE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 848w, https://substackcdn.com/image/fetch/$s_!WxaE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 1272w, https://substackcdn.com/image/fetch/$s_!WxaE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff651d6ea-d139-4e1b-82b3-9b6a0dfa6b63_1182x690.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|c|c|c|c|c|c|c|}\n\\hline\n &amp; \\textbf{GPT3.5} &amp; \\textbf{GPT3.5} &amp; \\textbf{GPT4} &amp; \\textbf{GPT4} &amp; \\textbf{GPT4o} &amp; \\textbf{GPT4o} \\\\\n\\hline\n\\textbf{Few-Shot} &amp; \\text{No} &amp; \\text{Yes} &amp; \\text{No} &amp; \\text{Yes} &amp; \\text{No} &amp; \\text{Yes} \\\\\n\\hline\n\\textbf{Average Citation Length} &amp; 165 &amp; \\textbf{108} &amp; 172 &amp; 110 &amp; 173 &amp; 109 \\\\\n\\text{(Ground truth reference: 65)} &amp; &amp; &amp; &amp; &amp; &amp; \\\\\n\\hline\n\\textbf{Average Similarity} &amp; 0.43 &amp; 0.46 &amp; 0.40 &amp; \\textbf{0.51} &amp; 0.40 &amp; 0.50 \\\\\n\\hline\n\\end{array}&quot;,&quot;id&quot;:&quot;PKFNTALEHI&quot;}" data-component-name="LatexBlockToDOM"></div><p><strong>Observations</strong> :</p><ul><li><p>According to these metrics, the citation quality is globally stable across models. When considering <em>exact match</em>, <em>high overlap</em>, and <em>inclusion</em> cases, <code>GPT4</code> and <code>GPT4o</code> outperform <code>GPT3.5</code> by approximately 10 percentage points. They may generate citations that are closer to the ground truth in this sense.</p></li><li><p>Few-shot prompting consistently improves over the zero-shot approach across all metrics.</p></li><li><p>The average length of generated citations is noticeably larger than the average length in the ground truth, for all models. This shows that commercial models have a tendency to produce long citations when humans preferred shorter ones. Less verbosity can be achieved by using few-shot as a guidance.</p></li><li><p><code>GPT4</code> and <code>GPT4o</code> achieve similar scores but the latter is approximately 4 times cheaper at the time of this writing.</p></li></ul><h3>Adversarial samples</h3><p>So far we have examined samples for which a context exists in the test data. Approximately 25% of our dataset consists of adversarial samples, where no context is expected. Adversarial examples can be deceptive for LLMs. Relying on pragmatic assumptions or world knowledge from their pre-training, they may interpret implicit cues in the data. For instance, if a patient&#8217;s <em>smoking status</em> is not discussed in a document, it might be inferred that the patient is a non-smoker, as otherwise it would certainly be mentioned. But since it cannot be asserted with certainty, the rule in such case is to output <code>null</code>. Commercial models struggle with this behavior, as seen in the following table.</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|c|c|c|c|c|c|c|}\n\\hline\n &amp; \\textbf{GPT3.5} &amp; \\textbf{GPT3.5} &amp; \\textbf{GPT4} &amp; \\textbf{GPT4} &amp; \\textbf{GPT4o} &amp; \\textbf{GPT4o} \\\\\n\\hline\n\\textbf{Few-Shot} &amp; \\text{No} &amp; \\text{Yes} &amp; \\text{No} &amp; \\text{Yes} &amp; \\text{No} &amp; \\text{Yes} \\\\\n\\hline\n\\textbf{Citation Rate} &amp; 93.8\\% &amp; 91.5\\% &amp; 58.3\\% &amp; 57.7\\% &amp; 58.9\\% &amp; \\textbf{56.9%} \\\\\n\\hline\n\\textbf{Valid Citation Rate after FM} &amp; 86.5\\% &amp; 86.9\\% &amp; 49.1\\% &amp; 54.2\\% &amp; 48.9\\% &amp; 53.7\\% \\\\\n\\hline\n\\textbf{Average Citation Length} &amp; 175 &amp; \\textbf{124} &amp; 194 &amp; 132 &amp; 204 &amp; 134 \\\\\n\\hline\n\\end{array}&quot;,&quot;id&quot;:&quot;KSQIFKGJJK&quot;}" data-component-name="LatexBlockToDOM"></div><p><strong>Observations</strong> :</p><ul><li><p>All models vastly overlook adversarial examples and generate a lot of superfluous contexts.</p></li><li><p>This is particularly true for <code>GPT3.5</code> compared to <code>GPT4</code> and <code>GPT4o</code>. As per the example in introduction, <code>GPT3.5</code> can be very wordy and will try to generate an answer with a context, sometimes with little regard to the actual factual content in the source.</p></li><li><p>Few-shot prompting slightly mitigates this effect.</p></li></ul><h2>Fine-tuned Models</h2><p>In this section, we fine-tune open source LLMs on our context attribution dataset to see if this skill can be easily acquired with supervised learning.</p><h3>Fine-tuned model results</h3><p>We fine-tune LLMs of different architectures and different number of parameters (from 160M to 24B). We measure their valid citation rate, before fuzzy matching, in the non-adversarial case.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6aZe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6aZe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 424w, https://substackcdn.com/image/fetch/$s_!6aZe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 848w, https://substackcdn.com/image/fetch/$s_!6aZe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 1272w, https://substackcdn.com/image/fetch/$s_!6aZe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6aZe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png" width="1187" height="690" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:690,&quot;width&quot;:1187,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:66365,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6aZe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 424w, https://substackcdn.com/image/fetch/$s_!6aZe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 848w, https://substackcdn.com/image/fetch/$s_!6aZe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 1272w, https://substackcdn.com/image/fetch/$s_!6aZe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30d650e6-dd78-4c50-bd97-e48bc5a55679_1187x690.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The data exhibit a roughly logarithmic relationship between context citation capacity and number of parameters (with notable deviations observed in the Qwen family, for which we propose an explanation later).</p><p>It also shows that word-for-word quotation in an autoregressive manner is not trivial, and requires a sizable number of model parameters before it can be relied upon. Below are the complete metrics for all models.</p><p></p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{lccccc}\n &amp; \\text{Citation Rate} &amp; \\text{Valid Citation Rate} &amp; \\text{Valid Citation Rate} &amp; \\text{Avg. Length} &amp; \\text{Similarity} \\\\\n &amp;  &amp;  &amp; \\text{After FM} &amp; \\text{Ref:65} &amp;  \\\\\n\\text{SmolLM2-135M} &amp; 76.2\\% &amp; 53.2\\% &amp; 64.1\\% &amp; 50 &amp; 0.29 \\\\\n\\text{SmolLM2-360M} &amp; 87.9\\% &amp; 69.9\\% &amp; 79.8\\% &amp; 54 &amp; 0.39 \\\\\n\\text{SmolLM2-1.7B} &amp; 86.8\\% &amp; 77.6\\% &amp; 84.0\\% &amp; 71 &amp; 0.48 \\\\\n\\text{Qwen2.5-0.5B} &amp; 61.8\\% &amp; 53.2\\% &amp; 58.7\\% &amp; 60 &amp; 0.37 \\\\\n\\text{Qwen2.5-1.5B} &amp; 59.5\\% &amp; 53.0\\% &amp; 57.2\\% &amp; 64 &amp; 0.38 \\\\\n\\text{Qwen2.5-3B} &amp; 85.4\\% &amp; 80.8\\% &amp; 83.9\\% &amp; \\mathbf{65} &amp; 0.53 \\\\\n\\text{Qwen2.5-7B} &amp; 75.6\\% &amp; 72.2\\% &amp; 74.4\\% &amp; 67 &amp; 0.50 \\\\\n\\text{Qwen2.5-14B} &amp; 87.2\\% &amp; 84.4\\% &amp; 86.0\\% &amp; 64 &amp; 0.57 \\\\\n\\text{Mistral-7B} &amp; 89.0\\% &amp; 85.4\\% &amp; 87.9\\% &amp; 75 &amp; 0.58 \\\\\n\\text{Ministral-8B} &amp; 91.0\\% &amp; 87.8\\% &amp; 89.8\\% &amp; 66 &amp; 0.58 \\\\\n\\text{Mistral-Nemo} &amp; 90.6\\% &amp; 88.1\\% &amp; 89.8\\% &amp; 72 &amp; 0.58 \\\\\n\\text{Mistral-Small-24B} &amp; \\mathbf{94.5\\%} &amp; \\mathbf{92.0\\%} &amp; \\mathbf{93.6\\%} &amp; 67 &amp; \\mathbf{0.62} \\\\\n\\end{array}\n&quot;,&quot;id&quot;:&quot;QJTEDRIZJX&quot;}" data-component-name="LatexBlockToDOM"></div><p></p><p>Most metrics increase steadily with model size. The average citation length is more stable, but is now way closer to the ground truth compared to commercial models. This show how fine-tuning on actual examples can implicitly teach what humans prefer.</p><p>While still useful, the fuzzy matching becomes less important as the model size grows and the raw valid citation rate gets better.</p><p>Akin to API models, it is interesting to note that the majority of failure cases do not stem from the model's inability to produce a valid citation but rather from its tendency to abstain, resulting in false negatives. We can see that by looking at the detailed breakdown of match types below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Amgw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Amgw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 424w, https://substackcdn.com/image/fetch/$s_!Amgw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 848w, https://substackcdn.com/image/fetch/$s_!Amgw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 1272w, https://substackcdn.com/image/fetch/$s_!Amgw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Amgw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png" width="1182" height="690" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:690,&quot;width&quot;:1182,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:79937,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Amgw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 424w, https://substackcdn.com/image/fetch/$s_!Amgw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 848w, https://substackcdn.com/image/fetch/$s_!Amgw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 1272w, https://substackcdn.com/image/fetch/$s_!Amgw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbaf6a27-d573-4570-9d36-7aa27539abfa_1182x690.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Even when controlling for the number of parameters, there is observable variation across models in their propensity to generate citations rather than <code>null</code> outputs.</p><p>In parallel, looking at adversarial samples, and plotting the False Positive Rate, we also observe a similar discrepancy:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1Wt0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1Wt0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 424w, https://substackcdn.com/image/fetch/$s_!1Wt0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 848w, https://substackcdn.com/image/fetch/$s_!1Wt0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 1272w, https://substackcdn.com/image/fetch/$s_!1Wt0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1Wt0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png" width="1384" height="790" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:790,&quot;width&quot;:1384,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:70083,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1Wt0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 424w, https://substackcdn.com/image/fetch/$s_!1Wt0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 848w, https://substackcdn.com/image/fetch/$s_!1Wt0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 1272w, https://substackcdn.com/image/fetch/$s_!1Wt0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9af8a27a-5436-49f7-a0e9-b6ae0b9951c0_1384x790.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Specifically, it appears that models from the Qwen2.5 family, which underperform in the non-adversarial case, are significantly better here. They are less likely to produce unnecessary contexts for adversarial samples.</p><h3>Before/After Fine-tuning</h3><p>So far we have glossed over the necessity of fine-tuning open-source models. Below we provide two graphs of the performance before/after fine-tuning to highlight the performance improvement. To evaluate base models before fine-tuning, we have given them the same prompt as the commercial models, but no few-shot</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3yxN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3yxN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 424w, https://substackcdn.com/image/fetch/$s_!3yxN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 848w, https://substackcdn.com/image/fetch/$s_!3yxN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 1272w, https://substackcdn.com/image/fetch/$s_!3yxN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3yxN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png" width="724.609375" height="308.55618990384613" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/772f1b94-c835-422b-8091-c920e4523530_2111x899.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:620,&quot;width&quot;:1456,&quot;resizeWidth&quot;:724.609375,&quot;bytes&quot;:126797,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3yxN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 424w, https://substackcdn.com/image/fetch/$s_!3yxN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 848w, https://substackcdn.com/image/fetch/$s_!3yxN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 1272w, https://substackcdn.com/image/fetch/$s_!3yxN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F772f1b94-c835-422b-8091-c920e4523530_2111x899.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>On the left is the evolution of the <em>valid citation rate</em>. For instance, before fine-tuning, <code>Mistral-Nemo</code> is able to generate a valid citation in 72.3% of cases. After fine-tuning, this number rises to 87.8%. On the right is the evolution of the <em>true negative rate</em>. In other words, on adversarial samples, how many times did the model successfully generated <code>null</code> as an answer. This rate increases from 10.2% to 88.9% for <code>Mistral-Nemo</code> for example.</p><p>Note that some models, such as all from the <code>SmolLM2</code> family, are unable to generate valid JSONs before fine-tuning, and hence consistently score 0 on the benchmarks. While fine-tuning improves on both sides, it is remarkably impactful for adversarial samples, as models have a hard time grasping the concept from instructions only.</p><p>Fine-tuned models also score much higher on all quality metrics. For instance, for <code>Mistral-Small-24B</code>, the proportion of <em>exact match</em> rises from 21% to 36%, the average citation length drops from 95 to 67 and the average similarity increases from 0.49 to 0.62.</p><h2>Summary</h2><p>As we have shown, understanding the performance of LLMs for context attribution requires examining two dimensions, depending on whether the information exists in the source or not. To visualize this better, we can make a 2D plot balancing the valid citation rate (or <em>True Positive Rate</em>, frequency with which the model generates a valid context when expected) with the <em>True Negative Rate</em> (frequency with which the model correctly abstain). In this plot, we include commercial models and fine-tuned models, with disk area proportional to the number of parameters.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4xLU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4xLU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 424w, https://substackcdn.com/image/fetch/$s_!4xLU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 848w, https://substackcdn.com/image/fetch/$s_!4xLU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 1272w, https://substackcdn.com/image/fetch/$s_!4xLU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4xLU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png" width="1190" height="790" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/02de8977-fb25-4469-9992-c809f2deb973_1190x790.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:790,&quot;width&quot;:1190,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:84875,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://lifen.substack.com/i/164082084?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4xLU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 424w, https://substackcdn.com/image/fetch/$s_!4xLU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 848w, https://substackcdn.com/image/fetch/$s_!4xLU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 1272w, https://substackcdn.com/image/fetch/$s_!4xLU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02de8977-fb25-4469-9992-c809f2deb973_1190x790.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This visualization suggests the existence of precision/recall trade-off between rather conservative models but with a high precision and more aggressive models more prone to invent citations but with higher recall.</p><h1>Conclusion</h1><p>Large Language Models (LLMs) are well suited for extracting structured data from unstructured documents. However, their generative nature introduces challenges such as hallucinations and lack of interpretability, which are critical in domains like healthcare where reliability and traceability are paramount. Context attribution aims to mitigate these issues by requiring models to cite verbatim excerpts from source material. We investigated whether purely generative context attribution is feasible, allowing the model to dynamically quote the most relevant passages word-for-word.</p><p>Key findings from our experiments include:</p><ul><li><p><strong>Commercial models (GPT3.5, GPT4, GPT4o)</strong> can perform zero-shot verbatim citation, with performance improving notably through few-shot prompting. However, fuzzy matching is necessary to recover a sizable amount of what the models originally output. Moreover, they still struggle with adversarial cases, often generating unnecessary or incorrect citations when no relevant context exists.</p></li><li><p><strong>Fine-tuned open-source models</strong> show a strong correlation between model size and citation accuracy, with larger models achieving better valid citation rates than commercial models for a fraction of the cost. Fine-tuning improves both citation rate and citation quality, and drastically enhance the ability to abstain when no context is available.</p></li><li><p><strong>Trade-offs exist between precision and recall</strong> and adversarial analysis is crucial for real world applications. Some models are conservative, minimizing false citations but missing valid ones, while others are more aggressive, capturing more correct contexts at the cost of occasional hallucinations or misattributions.</p></li></ul><h2>Future work</h2><p>Future research could explore allowing the model to produce multiple citations per prediction, as some answers require combining evidence from several disjoint passages in the source text. Additionally, a more robust approach might involve constrained generation techniques to enforce that generated citations are exact extracts from the source document or to control their length through the JSON schema specification. Another promising direction is to leverage the transformer's attention mechanism as a way to identify relevant contexts, building on existing work in interpretability and attribution. These enhancements could further improve the reliability and granularity of context attribution in LLMs, which is needed to make LLM outputs more trustworthy and actionable in high-stakes environments.</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://lifen.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://lifen.substack.com/subscribe?"><span>Subscribe now</span></a></p><h4></h4><blockquote><h4><strong>Join Us at Lifen!</strong></h4><p>At Lifen, we&#8217;re at the forefront of leveraging AI to solve real-world challenges in healthcare. If you&#8217;re passionate about building reliable, interpretable AI systems and want to work on impactful problems, we&#8217;d love to hear from you!</p><p>We&#8217;re currently hiring for roles in machine learning, engineering and more. Check out our <a href="https://www.welcometothejungle.com/fr/companies/lifen/jobs">careers page</a> to explore opportunities and join our mission.</p></blockquote>]]></content:encoded></item><item><title><![CDATA[Visual Explainable NLP @ Lifen]]></title><description><![CDATA[A React/FastAPI web app that helps Lifen's support team understand AI predictions by visualizing model decisions and explanations, bridging the gap between technical complexity and support needs.]]></description><link>https://lifen.substack.com/p/visual-explainable-nlp-lifen</link><guid isPermaLink="false">https://lifen.substack.com/p/visual-explainable-nlp-lifen</guid><dc:creator><![CDATA[Jean-Charles Louis]]></dc:creator><pubDate>Sat, 15 Jan 2022 15:33:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!End3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Today at Lifen, our machine learning models (mostly NLP) serve more than 1M daily predictions to our users. This volume inevitably generates support requests when our AI is wrong or makes a counterintuitive prediction.</p><p>Our objective was to develop a tool that:</p><p>Thanks for reading Lifen AI! Subscribe for free to receive new posts and support my work.</p><ul><li><p>streamline the support process by making our explainable AI tools easily accessible to authorized users</p></li><li><p>is easily maintainable and always up-to-date with our AI</p></li><li><p>provides some insight into how the ML model made its prediction</p></li></ul><h1><strong>The end result</strong></h1><p>The result is a web app <em><strong>lifen-ai-explainer</strong></em> accessible internally to Data Scientists and Customer Support. Given a document id (provided by the client requesting support), this web app will display the anonymized document and predictions our AI made. <em>(Predictions are also anonymised but made on the original document, otherwise, changes on the doc by anonymization process could produce different results)</em></p><p>We built <em>lifen-ai-explainer</em> and the pseudonymisation algorithm internally for this purpose and it&#8217;s been fully integrated into our daily processes since mid-2020.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!End3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!End3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 424w, https://substackcdn.com/image/fetch/$s_!End3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 848w, https://substackcdn.com/image/fetch/$s_!End3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 1272w, https://substackcdn.com/image/fetch/$s_!End3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!End3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png" width="700" height="375" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:375,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!End3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 424w, https://substackcdn.com/image/fetch/$s_!End3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 848w, https://substackcdn.com/image/fetch/$s_!End3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 1272w, https://substackcdn.com/image/fetch/$s_!End3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86969ac2-8e3d-44f2-b001-3cedf629b57b_700x375.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Part of our internal web app, here replaying and explaining predictions of our Patient Gender algorithm on a fictional French document.</figcaption></figure></div><p><strong>Explaining classification</strong></p><p>In addition to anonymizing and displaying the predictions, we try to explain them. For example, we want to understand how our machine learning text classifiers (eg: document type, patient gender) made their predictions by highlighting which words impacted the prediction the most.</p><p>Here is a demo of what this looks like on the patient gender classification algorithm (whose goal is to detect the gender of the patient mentioned in the document &#8212; without relying on the name &#8212; to. increase identitovigilance)</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x5yf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x5yf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 424w, https://substackcdn.com/image/fetch/$s_!x5yf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 848w, https://substackcdn.com/image/fetch/$s_!x5yf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 1272w, https://substackcdn.com/image/fetch/$s_!x5yf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x5yf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png" width="700" height="144" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:144,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!x5yf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 424w, https://substackcdn.com/image/fetch/$s_!x5yf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 848w, https://substackcdn.com/image/fetch/$s_!x5yf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 1272w, https://substackcdn.com/image/fetch/$s_!x5yf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc757898c-97f4-4804-bf5e-337e1a4eb670_700x144.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">the words &#8220;she&#8221; and &#8220;her&#8221; were (as expected) determinants to the gender detection</figcaption></figure></div><p>This feature proved useful in real-world situations to understand how for example a misread/misplaced word or simply a typo leads to a wrong prediction.</p><p><strong>Explaining NER</strong></p><p>We also use this tool to debug our NERs models. On the document view we show the raw softmax output (score for each class for each token), on the left side we show the final post-processed prediction.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WgBi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WgBi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 424w, https://substackcdn.com/image/fetch/$s_!WgBi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 848w, https://substackcdn.com/image/fetch/$s_!WgBi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 1272w, https://substackcdn.com/image/fetch/$s_!WgBi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WgBi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png" width="700" height="428" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:428,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!WgBi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 424w, https://substackcdn.com/image/fetch/$s_!WgBi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 848w, https://substackcdn.com/image/fetch/$s_!WgBi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 1272w, https://substackcdn.com/image/fetch/$s_!WgBi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd118937-2e53-4dec-8fc0-0cb8d076186b_700x428.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">here replaying our patient detection NER. This prediction uses an ensemble (2) model hence the 1 and 2 buttons on the tabs.</figcaption></figure></div><h1><strong>How does it work?</strong></h1><p>Our classifiers are made with an Embedding + LSTM architecture (long documents, resources, and time constraints mean no Transformers). However, this architecture is not out-of-the-box well suited for explainability purposes.</p><p>Let&#8217;s go into detail to understand why. Here we use the last hidden state that we pass to a dense layer and softmax to produce our prediction:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rQ8o!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rQ8o!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 424w, https://substackcdn.com/image/fetch/$s_!rQ8o!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 848w, https://substackcdn.com/image/fetch/$s_!rQ8o!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 1272w, https://substackcdn.com/image/fetch/$s_!rQ8o!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rQ8o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png" width="700" height="361" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:361,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!rQ8o!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 424w, https://substackcdn.com/image/fetch/$s_!rQ8o!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 848w, https://substackcdn.com/image/fetch/$s_!rQ8o!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 1272w, https://substackcdn.com/image/fetch/$s_!rQ8o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89c11b0a-ad86-4c45-8d3e-1ef7e35d6e30_700x361.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The problem is simply that we have no idea which word impacted the final prediction. (There is also a problem of vanishing gradient and bottleneck where if the important words are at the beginning of a very long sequence, the LSTM has to learn to remember it for many time steps) But we can fix that with a simple inexpensive tweak in the model.</p><h2><strong>Global attention layer</strong></h2><p>Our solution is to add a global attention layer (or attentive pooling layer) <a href="https://www.aclweb.org/anthology/P16-2034">[1]</a> that compute an attention score for each token with a dense + softmax layer which is then used to compute a weighted sum of all the hidden state at each time step (which roughly correspond to information contained in the current word).</p><p>This weighted sum is therefore a whole document embedding (similar to the last hidden state that we used in the previous architecture) that we can use for classification, but we also have in bonus those attention scores per word which we can interpret as &#8220;importances scores&#8221; for each word. Those scores proved empirically very interpretable.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!duOR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!duOR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 424w, https://substackcdn.com/image/fetch/$s_!duOR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 848w, https://substackcdn.com/image/fetch/$s_!duOR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 1272w, https://substackcdn.com/image/fetch/$s_!duOR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!duOR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png" width="700" height="485" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8f8d377-d657-4744-badd-c9c65466281e_700x485.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:485,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!duOR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 424w, https://substackcdn.com/image/fetch/$s_!duOR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 848w, https://substackcdn.com/image/fetch/$s_!duOR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 1272w, https://substackcdn.com/image/fetch/$s_!duOR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f8d377-d657-4744-badd-c9c65466281e_700x485.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The global attention layer gives us a high score for &#8220;her&#8221; (0.56) and &#8220;she&#8221; (0.43) which can be used to interpret that these words were key to the algorithm decision.</figcaption></figure></div><h2><strong>The layer code</strong></h2><p>This layer is a simple Keras layer made from well-known building blocks. In practice, we use more than one attention head which can make the interpretation more tricky but improve performance.</p><pre><code><code>def MultiGlobalAttention(timesteps: tf.Tensor, n_heads: int = 1) -&gt; tf.Tensor:
    """Global Attention layer. Computes a weighted sum of all the timesteps.
    Input: N timesteps of dim D
    Output: 1 of dim D
    Zhou et al https://www.aclweb.org/anthology/P16-2034  &#167;3.3"""

    attention = Dense(n_heads, use_bias=False, activation="linear")(timesteps)
    attention = Activation(softmax_axis(1), name="attention")(attention)
    weighted_sum = dot([attention, timesteps], axes=1)
    return Flatten()(weighted_sum)</code></code></pre><p>How to get those attention scores? We extract an attention sub-model that is not run in typical inference scenarios, but only for support in <em>lifen-ai-explainer</em>.</p><pre><code><code>def attention_model(model: Model) -&gt; Model:
    """make attention_model from model"""
    return Model(inputs=model.input, outputs=model.get_layer("attention").output)</code></code></pre><h1><strong>Architecture of </strong><em><strong>lifen-ai-explainer</strong></em><strong> (+ lifen-ai)</strong></h1><p>The web app is a React <a href="https://en.wikipedia.org/wiki/Single-page_application">Single-page application</a> written in TypeScript that communicates with a REST python backend based on <a href="https://fastapi.tiangolo.com/">FastAPI</a> (the backend also serves the static files for the frontend).</p><p>The frontend tooling is based on <a href="https://vitejs.dev/">Vite</a> which handles all the complexity of a modern frontend app (we rather do data science than tinker with babel/webpack config files).</p><p>The web app needs to make the same predictions as <em>lifen-ai</em>. We therefore decided to build the backend as a python app that can import the existing <em>lifen-ai</em> as a library.</p><p>Python lacks explicit access modifiers, which can make it difficult to maintain good encapsulation and ensure the maintainability of a large interconnected codebase. We use <a href="https://github.com/seddonym/import-linter">import-linter</a> to enforce encapsulation rules and allow <em>lifen-ai-explainer</em> to import <em>lifen-ai</em> but not the other way around.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_Hcq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_Hcq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 424w, https://substackcdn.com/image/fetch/$s_!_Hcq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 848w, https://substackcdn.com/image/fetch/$s_!_Hcq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 1272w, https://substackcdn.com/image/fetch/$s_!_Hcq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_Hcq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png" width="700" height="395" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:395,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!_Hcq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 424w, https://substackcdn.com/image/fetch/$s_!_Hcq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 848w, https://substackcdn.com/image/fetch/$s_!_Hcq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 1272w, https://substackcdn.com/image/fetch/$s_!_Hcq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a13dc98-17e9-431e-a0dc-e3429e3e00d9_700x395.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The whole architecture of lifen-ai and lifen-ai-explainer</figcaption></figure></div><h2><strong>How to keep our ai-explainer in sync with new features?</strong></h2><p>Our AI is rapidly evolving and we need <em>lifen-ai-explainer</em> to stay in sync at all times. We never want to be in a position where there is a new feature that we cannot debug with <em>lifen-ai-explainer</em>.</p><h2><strong>Mono repo &amp; mypy &amp; front-back client generation</strong></h2><p>We decided to put both apps in the same mono repo. This allows atomic changes: when making a Pull Request to add a prediction to <em>lifen-ai</em>, you can simultaneously add the implementation to <em>lifen-ai-explainer</em>. This is enforced by design: because 100% of our codebase is type annotated with <a href="http://mypy-lang.org/">mypy</a>, a breaking change in <em>lifen-ai</em> will break CI for <em>lifen-ai-explainer</em>, so you are required to keep it up to date. Our unit &amp; integration tests do the rest.</p><p>The frontend client is autogenerated by <a href="https://orval.dev/">Orval</a> using the OpenAPI spec which is also autogenerated by <a href="https://fastapi.tiangolo.com/features/#automatic-docs">FastAPI</a>. As a result, any changes made to the output of a model will cause the CI to fail and direct you to the exact location in the frontend code where updates are required &#127881;.</p><p>We use <a href="https://github.com/python-poetry/poetry">poetry</a> to manage python dependencies which don&#8217;t support mono-repo but allow us to <a href="https://python-poetry.org/docs/pyproject/#dependencies-and-dependency-groups">split dependencies into groups</a>. This way, <em>lifen-ai-explainer</em> includes all the dependencies of <em>lifen-ai</em> but not the other way around.</p><div><hr></div><p><em>lifen-ai-explainer</em> has been fully integrated into our daily processes since mid-2020 and we have since expended its features with a dataset labellisation interface, and a post-training errors review tool.</p><p>Thanks for reading Lifen AI! Subscribe for free to receive new posts and support my work.</p>]]></content:encoded></item><item><title><![CDATA[Fast graph-based layout detection]]></title><description><![CDATA[Fast & Accurate Layout Detection for Medical PDFs &#8211; How Lifen extracts structured text from complex document layouts.]]></description><link>https://lifen.substack.com/p/fast-graph-based-layout-detection</link><guid isPermaLink="false">https://lifen.substack.com/p/fast-graph-based-layout-detection</guid><dc:creator><![CDATA[Jean-Charles Louis]]></dc:creator><pubDate>Wed, 05 May 2021 14:31:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1><strong>Introduction</strong></h1><p>At <a href="https://www.lifen.fr/">Lifen</a> we process medical PDF documents from thousands of clients. Those PDF documents come with a huge variety of layouts:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BEo-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BEo-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 424w, https://substackcdn.com/image/fetch/$s_!BEo-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 848w, https://substackcdn.com/image/fetch/$s_!BEo-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 1272w, https://substackcdn.com/image/fetch/$s_!BEo-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BEo-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png" width="700" height="327" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:327,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!BEo-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 424w, https://substackcdn.com/image/fetch/$s_!BEo-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 848w, https://substackcdn.com/image/fetch/$s_!BEo-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 1272w, https://substackcdn.com/image/fetch/$s_!BEo-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb435b28e-3801-4be2-8b2e-57c30491e0fa_700x327.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Example of real world medical document layouts (extracted using <a href="https://en.wikipedia.org/wiki/Pdftotext">pdftotext</a>)</figcaption></figure></div><p>We need to perform layout detection from this 2D grid of words to produce a coherent 1D sequence. This 1D sequence can then be fed into our NLP models or sent as plain text when clients do not support PDF or image attachments.</p><p>Thanks for reading Lifen AI! Subscribe for free to receive new posts and support my work.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Gy7u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Gy7u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 424w, https://substackcdn.com/image/fetch/$s_!Gy7u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 848w, https://substackcdn.com/image/fetch/$s_!Gy7u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 1272w, https://substackcdn.com/image/fetch/$s_!Gy7u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Gy7u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png" width="700" height="327" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9246156c-f0db-48c5-838a-e4204a33af84_700x327.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:327,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!Gy7u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 424w, https://substackcdn.com/image/fetch/$s_!Gy7u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 848w, https://substackcdn.com/image/fetch/$s_!Gy7u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 1272w, https://substackcdn.com/image/fetch/$s_!Gy7u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9246156c-f0db-48c5-838a-e4204a33af84_700x327.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Same documents after layout detection extraction using our method (each colour is a different detected paragraph)</figcaption></figure></div><p>Mistakes in this layout detection could result in nonsensical extracted text. This could mean for exemple mixing addresses of two recipients together.</p><p><em>Imagine in the left image above if we failed to understand that the green and yellow blocks are two columns that should be read one after the other: we would create sentences mixing pieces of text from both blocks.</em></p><h2><strong>Isn&#8217;t layout information already encoded in PDFs?</strong></h2><p>Most of the time, yes. There is a sense of order in the way it is encoded, but there are no means to be sure this is the case, and results can be pretty bad if we just trust PDFs blindly. Furthermore, in the case of images PDF (which needs OCR), there is no text order to recover.</p><h1><strong>Objective: be fast &amp; accurate</strong></h1><h2><strong>Existing solutions</strong></h2><p>Existing solutions (such as <a href="https://github.com/jsvine/pdfplumber">pdfplumber</a>, <a href="https://github.com/axa-group/Parsr">Parsr</a>) use simple techniques such as XY cut&#185; and did not perform well enough on our dataset. Furthermore, because accurate text extraction is critical for our product, we need to be able to constantly fine-tune our solution to be as accurate as possible with new clients. Existing solutions did not allow us the level of customization we needed.</p><h2><strong>Avoid OCR when possible</strong></h2><p>Multiple solutions combine OCR with layout detection. However, most of the documents we receive (~85%) have a clean extractable text and do not require OCR. Furthermore, doing OCR on every document we process would be slow and expensive we&#8217;re happy to avoid as much as we can. For image documents, we first apply OCR then use the extracted text as a starting point just as extractable PDFs.</p><h1><strong>Our solution</strong></h1><h2><strong>Previous work</strong></h2><p>There is an active community of research in the field of layout analysis, with academic conferences such as the <em><a href="https://en.wikipedia.org/wiki/International_Conference_on_Document_Analysis_and_Recognition">International Conference on Document Analysis and Recognition</a></em>. One resource we found particularly valuable to navigate this field is survey of algorithms&#178;.</p><p>However, while reviewing papers we found out that few share the characteristics we are looking for: start from extractable PDFs, fast and fine-tunable.</p><p>One great paper using the same starting point as us (no OCR, words and not pixels) and with great care for speed is <em>Object-Level Document Analysis of PDF Files&#179;</em>. The idea is to build a graph of words, discard edges linking words from different paragraphs until the connected components left are the paragraphs. The ideas we took from this paper are:</p><ul><li><p>Reduce the number of words in linear time by merging words that without a doubt belong to the same line</p></li><li><p>Build a graph as fast as possible by looking only for direct neighbors in the four directions.</p></li><li><p>&#8220;Best-first clustering&#8221; algorithm: sort edges (mainly by length) and choose to discard them or not one by one by using previous decisions to make informed decisions on longer trickier edges.</p></li></ul><p>However, the algorithms they use to sort edges and choose to discard them, are complex sequences of rules that we found difficult to fine-tune and maintain.</p><p>From <em>A Machine Learning Approach for Graph-Based Page Segmentation&#8308;</em>, we took the idea of using a machine learning model to classify edges.</p><h1><strong>Bring everything together</strong></h1><h3><strong>1. Pre-processing</strong></h3><p>We&#8217;re extracting words and their positions using <code>pdftotext --bbox-layout</code>. This gives us a list of words in 2D grid and an estimation by pdftotext of word order (in an XML format similar to hOCR).</p><h3><strong>2. Build graph</strong></h3><p>We build the word graph using a custom implementation of the algorithm from&#179;.</p><h3><strong>3. Give a </strong><em><strong>should_merge</strong></em><strong> score to each edge:</strong></h3><p>Using a manually tagged dataset, we train a Random Forest to give a <em>should_merge</em> score for each edge using features.</p><p><strong>Features definitions:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DMms!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DMms!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 424w, https://substackcdn.com/image/fetch/$s_!DMms!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 848w, https://substackcdn.com/image/fetch/$s_!DMms!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 1272w, https://substackcdn.com/image/fetch/$s_!DMms!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DMms!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png" width="640" height="480" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:480,&quot;width&quot;:640,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DMms!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 424w, https://substackcdn.com/image/fetch/$s_!DMms!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 848w, https://substackcdn.com/image/fetch/$s_!DMms!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 1272w, https://substackcdn.com/image/fetch/$s_!DMms!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F724acdb5-f30f-4941-bb5c-e5fd07f3dd95_640x480.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Feature importance of our RF, for information</figcaption></figure></div><ul><li><p>dist_font_size: distance between words in units of font_size (or mean font_size if not the same between words)</p></li><li><p>xml_neighbours: neighbours according to PDF infos extracted by pdftotext (accurate ~90% of the time)</p></li><li><p>nb_aligned_left: number of words aligned left with this word (does it looks like this word is on the left side of a paragraph)</p></li></ul><h3><strong>3. Sort edges by should_merge score and keep certain</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4epU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4epU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 424w, https://substackcdn.com/image/fetch/$s_!4epU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 848w, https://substackcdn.com/image/fetch/$s_!4epU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 1272w, https://substackcdn.com/image/fetch/$s_!4epU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4epU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png" width="640" height="480" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5918001e-ce63-46bd-837f-eef7f7450364_640x480.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:480,&quot;width&quot;:640,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!4epU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 424w, https://substackcdn.com/image/fetch/$s_!4epU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 848w, https://substackcdn.com/image/fetch/$s_!4epU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 1272w, https://substackcdn.com/image/fetch/$s_!4epU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5918001e-ce63-46bd-837f-eef7f7450364_640x480.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Using a threshold that gives us a precision of 1.00 during evaluation, we keep every edge that is &#8220;certain&#8221; (recall at this threshold is 0.75).</p><p>Our second threshold aims for a precision &gt; 0.9, gives us &#8220;possible&#8221; edges.</p><p>Rest of edges are discarded.</p><h3><strong>4. Make decisions about &#8220;possible&#8221; edges using current state</strong></h3><p>Our random forest only sees two words at the time (but some features such as <code>nb_aligned_left</code> give more global context). For less obvious edges (with a lower <em>should_merge</em> score), we make decisions based on the current state of the layout. We compute a &#8220;stateful&#8221; coefficient that multiply the should_merge score.</p><p>For exemple, when considering an edge between two words from two clusters that seem to come from one same column, we increase the <em>should_merge </em>score.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iARg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iARg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 424w, https://substackcdn.com/image/fetch/$s_!iARg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 848w, https://substackcdn.com/image/fetch/$s_!iARg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 1272w, https://substackcdn.com/image/fetch/$s_!iARg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iARg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png" width="700" height="476" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/11eaf665-4564-46fe-a114-73c77b981606_700x476.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:476,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!iARg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 424w, https://substackcdn.com/image/fetch/$s_!iARg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 848w, https://substackcdn.com/image/fetch/$s_!iARg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 1272w, https://substackcdn.com/image/fetch/$s_!iARg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11eaf665-4564-46fe-a114-73c77b981606_700x476.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Summary of the whole architecture</figcaption></figure></div><h1><strong>Future work</strong></h1><p>At this point our library is too much integrated with the rest of our app to be split into an open-source solution. We are considering open-sourcing it in the future. In the meantime, do not hesitate to contact us if you want more details.</p><p>We&#8217;re also thinking of using a more end-to-end approach by combining the layout extraction with the downstream task directly in our NLP models. LayoutLM&#8309; is a promising work in this direction.</p><h1><strong>References</strong></h1><p>[1] Hierarchical representation of optically scanned documents <a href="https://core.ac.uk/download/pdf/84308932.pdf">https://core.ac.uk/download/pdf/84308932.pdf</a></p><p>[2] A comprehensive survey of mostly textual document segmentation algorithms since 2008 <a href="https://hal.archives-ouvertes.fr/hal-01388088/document">https://hal.archives-ouvertes.fr/hal-01388088/document</a></p><p>[3] Object-Level Document Analysis of PDF Files <a href="https://www.dbai.tuwien.ac.at/staff/hassan/files/p47-hassan.pdf">https://www.dbai.tuwien.ac.at/staff/hassan/files/p47-hassan.pdf</a></p><p>[4] A Machine Learning Approach for Graph-Based Page Segmentation <a href="https://www.researchgate.net/publication/328719252_A_Machine_Learning_Approach_for_Graph-Based_Page_Segmentation">https://www.researchgate.net/publication/328719252_A_Machine_Learning_Approach_for_Graph-Based_Page_Segmentation</a></p><p>[5] LayoutLM: Pre-training of Text and Layout for Document Image Understanding <a href="https://arxiv.org/abs/1912.13318">https://arxiv.org/abs/1912.13318</a></p><p>Thanks for reading Lifen AI! Subscribe for free to receive new posts and support my work.</p>]]></content:encoded></item></channel></rss>