<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sanghvirajit on Medium]]></title>
        <description><![CDATA[Stories by Sanghvirajit on Medium]]></description>
        <link>https://medium.com/@sanghvirajit?source=rss-768086a2598c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MMl3OCm4fQu2_3YPZoOtcg.png</url>
            <title>Stories by Sanghvirajit on Medium</title>
            <link>https://medium.com/@sanghvirajit?source=rss-768086a2598c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 12:36:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sanghvirajit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Enhancing Healthcare Diagnostics with Retrieval-Augmented Generation (RAG): Leveraging LangChain…]]></title>
            <link>https://sanghvirajit.medium.com/enhancing-healthcare-diagnostics-with-retrieval-augmented-generation-rag-leveraging-langchain-2befddfe5859?source=rss-768086a2598c------2</link>
            <guid isPermaLink="false">https://medium.com/p/2befddfe5859</guid>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[diagnostic-healthcare]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Sanghvirajit]]></dc:creator>
            <pubDate>Thu, 13 Mar 2025 22:46:06 GMT</pubDate>
            <atom:updated>2025-03-13T22:46:06.804Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Enhancing Healthcare Diagnostics with Retrieval-Augmented Generation (RAG): Leveraging LangChain, ChromaDB, and Hugging Face</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ImVyx96rl4vlNOwm2pxqXA.png" /><figcaption>Health care diagnostics — <a href="https://myacare.com/blog/aidriven-healthcare-diagnostics-a-revolutionary-frontier-in-medicine">https://myacare.com/blog/aidriven-healthcare-diagnostics-a-revolutionary-frontier-in-medicine</a></figcaption></figure><h3>Introduction</h3><p>The integration of AI in healthcare is revolutionising diagnostics, particularly with advancements in <strong>Retrieval-Augmented Generation (RAG)</strong>. By combining retrieval-based knowledge with generative capabilities, RAG enhances traditional AI models, delivering more accurate and reliable results. In this blog, we explore how RAG can be applied to <strong>diagnosing Parkinson’s disease using gait analysis results of a patient</strong>. Leveraging <strong>LangChain, ChromaDB</strong> vector database for efficient data retrieval and <strong>LLaMA-3</strong> via the Groq API for intelligent generation, we demonstrate a cutting-edge approach to AI-driven medical diagnostics.</p><p><a href="https://python.langchain.com/docs/introduction/"><strong>LangChain</strong></a> is a popular open-source framework designed for developing applications powered by Large Language Models (LLMs). It provides tools and abstractions to integrate LLMs with external data sources, memory, and reasoning capabilities, making it easier to build AI-driven applications such as chatbots, RAG (Retrieval-Augmented Generation) systems, and autonomous agents.</p><h3>What is Retrieval-Augmented Generation (RAG)?</h3><p>RAG is an AI approach that enhances Large Language Models (LLMs) by retrieving external knowledge from structured databases before generating responses. This method prevents hallucinations and ensures that AI-generated outputs are grounded in factual, domain-specific knowledge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*Qm99aiEVpJA0jo0gp1pRAw.png" /><figcaption>RAG (Retrieval-Augmented Generation) <a href="https://www.datacamp.com/de/blog/what-is-retrieval-augmented-generation-rag">https://www.datacamp.com/de/blog/what-is-retrieval-augmented-generation-rag</a></figcaption></figure><h3>Key Components of RAG:</h3><ol><li><strong>Retrieval</strong> — Extracting relevant knowledge from a structured database (data source).</li><li><strong>Augmentation</strong> — Injecting retrieved knowledge into the model’s prompt.</li><li><strong>Generation</strong> — Producing an output strictly based on the query and retrieved knowledge.</li></ol><p><em>Let’s get started with the implementation of RAG using Langchain and Hugging Face.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*xBCHK9MVTNnQ6z_nrndrog.png" /></figure><p>In our implementation, we:</p><ul><li>Store our knowledge base documents as pdf files in our <em>knowledge_base</em> dir.</li><li>Embed knowledge using SentenceTransformer with <strong>“all-MiniLM-L6-v2”</strong> model from HuggingFaceEmbeddings.</li><li>Store embeddings in ChromaDB for efficient retrieval.</li><li>Prepare the query. Inject retrieved knowledge from ChromaDB based on the query into the model’s prompt.</li><li>Use <strong>LLaMA-3 model</strong>(via the Groq API) to generate a diagnostic response based on retrieved knowledge.</li></ul><p>The knowledge based pdf which we will use is <a href="https://www.frontiersin.org/journals/neurology/articles/10.3389/fneur.2024.1472956/full">“Gait analysis in the early stage of Parkinson’s disease with a machine learning approach.”</a></p><h3>Libraries</h3><h3>Step-by-Step breakdown of the Implementation</h3><ol><li><strong>Save the knowledge based pdf under <em>knowledge_base</em> dir.</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*JTjn-EIjYdQszL409tAKXA.png" /></figure><p><strong>2. Install the libraries</strong></p><pre>!pip install uvicorn<br>!pip install fastapi<br>!pip install groq<br>!pip install langchain<br>!pip install langchain_community<br>!pip install unstructured<br>!pip install unstructured[pdf]<br>!pip install sentence-transformers<br>!pip install chromadb</pre><p><strong>3. Store the groq_api_key in .env file</strong></p><pre>groq_api_key=&quot;YOUR_API_KEY&quot;</pre><p>which can be retrieved using dotenv</p><pre>from dotenv import load_dotenv<br><br># Load environment variables from .env file<br>load_dotenv()<br><br># groq api key<br>groq_api_key = os.getenv(&quot;groq_api_key&quot;)</pre><p><strong>4. Load the documents from the knowledge base</strong></p><p>We will use <a href="https://python.langchain.com/v0.1/docs/modules/data_connection/document_loaders/file_directory/">DirectoryLoader</a> from langChain.</p><pre>from langchain_community.document_loaders import DirectoryLoader<br><br>def load_documents(DATA_PATH):<br>  loader = DirectoryLoader(DATA_PATH, glob=&quot;*.pdf&quot;)<br>  documents = loader.load()<br>  return documents<br><br>DATA_PATH = &quot;knowledge_base&quot;<br>documents = load_documents(DATA_PATH)<br></pre><p>result would look as follow:</p><pre>[Document(metadata={&#39;source&#39;: &quot;knowledge_base/Gait analysis in the early stage of Parkinson&#39;s disease with a machine learning approach.pdf&quot;}, page_content=&quot;OPEN ACCESS\n\nEDITED BY Christopher D. Stephen, Massachusetts General Hospital and Harvard Medical School, United States\n\nREVIEWED BY Diego Kaski, University College London, United Kingdom Giulia Corniani, Harvard Medical School, United States\n\nCORRESPONDENCE Cui Wang sjnsktg@163.com\n\nRECEIVED 30 July 2024 ACCEPTED 18 September 2024 PUBLISHED 08 October 2024\n\nCITATION Yin W, Zhu W, Gao H, Niu X, Shen C, Fan X and Wang C (2024) Gait analysis in the early stage of Parkinson’s disease with a machine learning approach. Front. Neurol. 15:1472956. doi: 10.3389/fneur.2024.1472956\n\nCOPYRIGHT © 2024 Yin, Zhu, Gao, Niu, Shen, Fan and Wang. This is an open-access article distributed under the terms of the Creative Commons Attribution License (CC BY). The use, distribution or reproduction in other forums is permitted, provided the original author(s) and the copyright owner(s) are credited and that the original publication in this journal is cited, in accordance with accepted academic practice. No use, distribution or reproduction is permitted which does not comply with these terms.\n\nFrontiers in Neurology\n\nTYPE Original Research PUBLISHED 08 October 2024 DOI 10.3389/fneur.2024.1472956\n\nGait analysis in the early stage of Parkinson’s disease with a machine learning approach\n\nWenchao Yin 1, Wencheng Zhu 2, Hong Gao 3, Xiaohui Niu 1, Chenxin Shen 1, Xiangmin Fan 4 and Cui Wang 1*\n\n1 Department of Neurology, Central Hospital of Dalian University of Technology, Dalian, China, 2 Beijing CAS-Ruiyi Information Technology Co., Ltd., Beijing, China, 3 School of Science, Dalian Maritime University, Dalian, China, 4 Institute of Software, Chinese Academy of Sciences, Beijing, China\n\nBackground: Gait disorder is a prominent motor symptom in Parkinson’s disease (PD), objective and quantitative assessment of gait is essential for diagnosing and treating PD, particularly in its early stage.\n\nMethods: This study utilized a non-contact gait assessment system to investigate gait characteristics between individuals with PD and healthy controls, with a focus on early-stage PD. Additionally, we trained two machine learning models to differentiate early-stage PD patients from controls and to predict MDS- UPDRS III score.\n\nResults: Early-stage PD patients demonstrated reduced stride length, decreased gait speed, slower stride and swing speeds, extended turning time, and reduced cadence compared to controls. Our model, after an integrated analysis of gait parameters, accurately identified early-stage PD patients. Moreover, the model indicated that gait parameters could predict the MDS-UPDRS III score using a machine learning regression approach.\n\nConclusion: The non-contact gait assessment system facilitates the objective and quantitative evaluation of gait disorder in PD patients, effectively distinguishing those in the early stage from healthy individuals. The system holds significant potential for the early detection of PD. It also harnesses gait parameters for a reasoned prediction of the MDS-UPDRS III score, thereby quantifying disease severity. Overall, gait assessment is a valuable method for the early identification and ongoing monitoring of PD.\n\nKEYWORDS\n\nParkinson’s disease, gait analysis, early-stage diagnosis, MDS-UPDRS III score, non-contact assessment, machine learning\n\n1 Introduction\n\nParkinson’s disease (PD) is one of the most common neurodegenerative diseases, affecting approximately 1% of the population over 60 years of age (1). The typical motor symptoms of PD include resting tremor, bradykinesia, rigidity, postural and gait disorders. Among these, gait disorder is one of the principal symptoms in PD patients. Patients exhibit characteristic gait patterns such as reduced turning agility, short and slow steps, festinating, and freezing of gait (2, 3). Given the strong correlation between gait disorders and diminished quality of life, precise gait assessment is vital (4, 5). However, it is challenging for neurologists to assess gait\n\n01\n\nfrontiersin.org\n\nYin et al.\n\nin the early stage of PD (6). Early gait disorders are very subtle, and some patients even exhibit motor symptoms without conscious gait complaints (7); Additionally, the presence of short and slow steps is a common trait in the aging population (8, 9), complicating the differentiation of PD-induced gait changes from age-related alterations. Consequently, precise identification and surveillance of gait anomalies are essential for the effective treatment and prognosis of PD, particularly in its early stage.\n\nCurrently, the clinical assessments of gait in PD patients rely on traditional scales, such as the Section III of the modified movement disorder society version of the unified Parkinson’s disease rating scale (MDS-UPDRS III), the Timed-Up and Go test, and the Freezing of Gait Questionnaire, and so on (10). However, these scales depend on the subjective assessments of clinical physicians and have the limitations of being semi-quantitative, time-consuming, and potentially leading to inconsistent and imprecise results. In recent years, along with the rapid advancement of science and technology, a variety of objective and quantitative gait assessment techniques have gradually matured, propelling PD gait research into a new stage (11), such as multi-camera motion capture systems, wearable sensors, and pressure-sensitive insoles. However, the existing methods also have various shortcomings. For instance, multi-camera motion capture systems offer the highest capture accuracy and are considered as the golden standard in clinical gait analysis (11), yet they are costly and demand a large space, making them difficult to popularize currently. Wearable sensors, while portable, still face several challenges, such as discomfort during wear, data synchronization, and noise contamination. In summary, although new technologies show great potential in gait assessment in PD, they still need further improvement and optimization in terms of popularization and clinical application (12). In order to provide more refined and convenient gait monitoring methods for PD patients, future research should focus on enhancing the universality of the technology, reducing costs, improving user experience, and maintaining the accuracy and reliability of the data in the same time.\n\nBased on the current challenges faced by PD gait assessments, we have utilized a non-contact gait assessment system (ReadyGo, Beijing CAS-Ruiyi Information Technology Co., Ltd.) (13), in order to overcome the limitations of traditional evaluation methods. With its non-invasive characteristic, real-time data collection capability, cost-effectiveness, and unique ability to capture rich 3D skeletal information, ReadyGo has quickly became an ideal choice for gait assessment in both clinical and scientific research. Through implicit monitoring, this technology not only reduced the discomfort of patients but also captured the most authentic gait data in a natural state, providing a solid foundation for precision medicine and personalized treatment.\n\nDespite the increasing number of objective and quantitative assessments of gait disorder in PD patients in recent years, there is a relative lack of research focusing on the gait characteristics in the early stage of the disease. Our study aimed to deep explore the gait disorder characteristics of PD patients, especially those in the early stage, and to explore whether gait assessment can effectively identify differences between early-stage PD patients and healthy elderly individuals.\n\nBy conducting a detailed comparative analysis of gait parameters between early-stage PD patients and healthy controls (HC), we hoped to reveal the unique gait patterns of early-stage PD patients. This would not only help improve the accuracy and timeliness of early\n\nFrontiers in Neurology\n\n02\n\n10.3389/fneur.2024.1472956\n\ndiagnosis but also provide key information for predicting PD progression and optimizing intervention strategies. Our research was expected to bring advancements to the early diagnosis and management of PD, especially in the aspects of identification and monitoring of gait disorder, opening up new methods for improving the quality of life for patients.\n\n2 Materials and methods\n\n2.1 Participants\n\nIn this study, 63 patients with primary PD were encompassed, with 27 being male and 36 being female. The inclusion criteria were as follows: (1) Meeting the 2015 Movement Disorder Society (MDS) diagnostic criteria for primary PD (14); (2) Hoehn and Yahr (H&amp;Y) stages between stage 1 and 3; (3) The Mini-Mental State Examination (MMSE) score of 24 or above. Additionally, 65 gender- and age-matched healthy participants were selected as the healthy control group, including 35 males and 30 females. The ages of participants ranged from 46 to 85 years old, and all of them were able to complete the tests without any assistance from others. Exclusion criteria included: (1) Atypical Parkinsonism; (2) Severe systemic diseases (such as musculoskeletal, cardiovascular, cerebrovascular and respiratory) and other neurological diseases; (3) Uncorrected visual impairments, or diseases that could alter gait patterns. This study was approved by the Ethics Committee of Central Hospital of Dalian University of Technology (Reference No. YN2022-039-57). Each participant signed the informed consent before participating in this study. The study was performed according to the guidelines of the declaration of Helsinki.\n\n2.2 Clinical assessment\n\nDemographic information was collected, including age, gender, height (cm), weight (kg), and disease duration. All patients were assessed by two experienced neurologists in movement disorders. The severity of the disease were evaluated using the H&amp;Y staging scale (15) which score ranged from 0 (no symptoms) to 5 (wheelchair bound or bedridden unless aided) and the MDS-UPDRS III (16) which consisted of 33 items with a score ranged from 0 (no symptoms) to 132 (severe motor symptoms). Cognitive was evaluated using the MMSE which score ranged from 0 to 30, with higher score indicating better cognitive function (17).\n\n2.3 Gait assessment\n\nGait parameters were assessed using ReadyGo. Unlike the traditional multi-camera system, ReadyGo system innovatively utilizes a set of integrated cameras, including one RGB (red/green/ blue) camera and a single depth camera, to capture and analyze three- dimensional (3D) motion data. The main advantage of this system lies in its unique skeletal tracking technology, which uses deep learning algorithms for precise positioning of skeletal points without requiring participants to wear any additional sensors, greatly enhancing the experience of the participants and the convenience of data collection\n\nfrontiersin.org\n\nYin et al.\n\n(Figure 1). By meticulously analyzing the gait of PD patients, the system can automatically extract and quantify up to 19 key gait parameters, covering many aspects of the gait cycle, including gait speed, cadence, stride length, swing and stance phases, and so on, providing numerous details for a comprehensive understanding of gait disorder in PD patients. The accuracy and sensitivity of the ReadyGo system have been validated in previous studies (13), showing high reliability in capturing key gait parameters such as stride length and gait speed.\n\n2.4 Procedure\n\n1 Scale assessment: MDS-UPDRS III and H&amp;Y staging scale were performed by two experienced neurologists in movement disorders, and then gait assessment was carried out. PD patients underwent all of the above, and healthy controls only underwent gait assessment but without clinical scale assessments.\n\n2 Gait assessment: the gait assessment device was placed in the equipment placement area which was 1.5 meters away from the endline. Participants stood at the starting line which was 4.5 meters directly in front of the device. During the test, participants were asked to walk at their self-selected comfortable pace without using any assistant device, start from the starting line, walk to the end line, turn around, and return to the starting line, repeat this process three times before ending the recording (Figure 2). Each participant should undergo a practice trial before the test to ensure that they understood the instructions clearly.\n\nThe gait data was achieved through a non-contact method, 19 gait parameters were extracted based on the images of gait and depth\n\nFIGURE 1 Human skeletal point tracking and motion recognition (the yellow markers indicate the key skeletal points tracked by the ReadyGo system).\n\nFrontiers in Neurology\n\n03\n\n10.3389/fneur.2024.1472956\n\ninformation, including stride length (left, right), step height (left, right), step width, gait speed, stride speed (left, right), swing speed (left, right), turning time, cadence (left, right), swing phase (left, right), stance phase (left, right), double support phase (left, right). The specific definitions were shown in Table 1 and the gait cycle was shown in Figure 3.\n\n2.5 Modeling\n\nIn this study, we defined patients in the H&amp;Y 1 and 2 as early- stage PD and constructed a classification model using gait parameters of early-stage PD and healthy controls. The classification model was constructed to distinguish early-stage PD from healthy controls using gait parameters. Ten gait parameters with a statistical significance level of p &lt; 0.001 were selected as input features. The dataset was split into a training set (70%) and a test set (30%) to evaluate the model’s performance. We utilized the LightGBM algorithm to build the classifier. The model was trained and validated using the training set, and its predictive performance was evaluated on the test set.\n\nThe Receiver Operating Characteristic (ROC) curve was performed as an important tool to evaluate the performance of the model, exploring whether gait parameters can identify early-stage PD from healthy controls. The ROC curve showed the relationship between the true positive rate (sensitivity) and the false positive rate (1-specificity), visually presented the classification capability of the model at different thresholds. In this study, we paid particular attention to the Area Under the Curve (AUC) of the ROC, the closer the value is to 1, indicated the better classification performance of the model, i.e., the stronger ability of gait parameters to identify early- stage PD.\n\nBy comprehensively analyzing the ROC curve and AUC value of the model, we hoped to validate the feasibility of gait parameters as early diagnostic biomarkers for PD, to provide a new perspective and basis for the early detection, intervention, and management of PD. This research would also lay a theoretical foundation for the subsequent development of more precise and personalized early screening tools for PD, promoting continuous advancement in clinical practice.\n\nIn the process of deepening our research, we adopted a more refined analytical strategy, using the Random Forest algorithm to train a machine learning regression model, which used all the 19 gait parameters as input features and MDS-UPDRS III score as output labels. This methodological shift aimed to explore how gait parameters quantitatively correlate with the severity of PD. By training a Random Forest regression model, we could predict the MDS-UPDRS III score of PD patients. To evaluate the performance of the model, we utilized the Leave-One-Out Cross Validation (LOOCV) method. In LOOCV, each instance of the dataset is used once as a test set while the remaining instances form the training set. This iterative process ensures that every data point is used for both training and validation, providing a robust assessment of the model’s generalization capability and predictive accuracy.\n\nWe could comprehensively evaluate the performance of the regression model by using these metrics: R-squared (R2), Mean Absolute Error (MAE), Mean Absolute Percentage Error (MAPE). R2 measured how well the model fitted the data, ranging from 0 to 1, the closer the value was to 1, indicated a better fit of the model. MAE\n\nfrontiersin.org\n\nYin et al.\n\n10.3389/fneur.2024.1472956\n\nFIGURE 2 The procedure of gait assessment. (A) The schematic diagram of gait assessment (Line A is the starting line which is the departure point for the gait assessment; Line B is the end line which is the turning point for the gait assessment; C represents the area where the gait assessment device is placed). (B) The photograph of the actual procedure of gait assessment.\n\nquantified the average absolute difference between the predicted value and true value, the smaller value suggested higher predictive accuracy of the model. MAPE was the average absolute percentage error expressed as a percentage, which was better for assessing the relative error between predicted value and true value, which was often used to understand the prediction accuracy of a model over different ranges.\n\n2.6 Statistical analysis\n\namong multiple groups were made using the Kruskal-Wallis test followed by post-hoc test. Categorical variables were described by frequency, and group comparisons were made using the Pearson Chi-square test. The statistically significant difference was considered p &lt; 0.05 in two-tailed tests. In this study, scikit-learn data analysis library of Python 3.8 was used to train and verify the classification and regression models. Meanwhile, drawing libraries such as matplotlib and seaborn were used to visually display the distribution characteristics of data and prediction effects of the models.\n\nThe statistical analysis in this study was analyzed using SPSS 26.0 (IBM Corp., Armonk, NY). Continuous variables with normal distribution were presented as mean ± standard deviation (x ± s), the comparisons between two groups were made using the Independent Samples t-Test, the comparisons among multiple groups with homogeneous variances were made using one-way ANOVA followed by Bonferroni post-hoc test, the comparisons among multiple groups with non-homogeneous variances were made using the Welch test followed by Games-Howell (A) post-hoc test. Continuous variables with non-normal distribution were presented as median and interquartile distances [M (P25, P75)], and comparisons between two groups were made using the Mann–Whitney U test, while comparisons\n\n3 Results\n\n3.1 Demographic and clinical characteristics\n\nThe demographic data between PD patients and the healthy controls were comparable (the first four lines in Table 2), there were no significant statistical differences in gender, age, height, and weight between the two groups (p &gt; 0.05).\n\nThe disease duration of PD patients ranged from 1 to 10 years, the MMSE score ranged from 24 to 30 points, the median MDS-UPDRS\n\nFrontiers in Neurology\n\n04\n\nfrontiersin.org\n\nYin et al.\n\nIII score was 25.5 points, and H&amp;Y stages ranged from 1 to 3, including 13 individuals in stage of H&amp;Y 1, 38 individuals in stage of H&amp;Y 2, and 12 individuals in stage of H&amp;Y 3.\n\n3.2 Comparison of gait parameters between PD patients and healthy controls\n\nThere were statistically significant differences in gait parameters between PD patients and healthy controls except for step width and right step height. The PD patients exhibited shorter stride length, slower gait speed, slower stride speed, slower swing speed, longer turning time, slower cadence, longer percentage of stance phase, shorter percentage of swing phase, and longer percentage of double support phase (Table 3). These results confirmed the “short and slow” gait characteristics of PD patients.\n\nTABLE 1 Specific definitions of gait parameters in this study.\n\nGait parameter\n\nDefinition\n\nStride length-L/R (m)\n\nThe distance between two landings of the left/\n\nright foot.\n\nStep height-L/R (m)\n\nThe highest distance from the ground during the\n\nswing of the left/right foot.\n\nStep width (m)\n\nThe average of the width of the left and right feet\n\nin each image frame.\n\nGait speed (m/s)\n\nAverage speed during straight travel (not\n\nincluding the turning time).\n\nStride speed-L/R (m/s)\n\nAverage speed during a left/right stride.\n\nSwing speed-L/R (m/s)\n\nAverage speed during a left/right swing.\n\nTurning time (s)\n\nThe time from turning start to turning end.\n\nCadence-L/R (steps/min)\n\nFrequency of left/right footstep.\n\nSwing phase-L/R (%)\n\nPercentage of left/right swing phase time in the\n\nleft/right stride time.\n\nStance phase-L/R (%)\n\nPercentage of left/right stance phase time in the\n\nleft/right stride time.\n\nDouble support-L/R (%)\n\nPercentage of double support phase time in the\n\nleft/right stride time.\n\nL, left; R, right.\n\nFIGURE 3 Gait cycle.\n\nFrontiers in Neurology\n\n05\n\n10.3389/fneur.2024.1472956\n\n3.3 Comparison of gait parameters among H&amp;Y 1, H&amp;Y 2, and H&amp;Y 3\n\nPD patients were divided into three groups according to the H&amp;Y stages: H&amp;Y 1, H&amp;Y 2, and H&amp;Y 3. We found that there were statistically significant differences in stride length, step height, gait speed, stride speed, swing speed, and percentage of double support phase among the three groups. Post-hoc test showed that these differences occurred between H&amp;Y 1 and H&amp;Y 3, and between H&amp;Y 2 and H&amp;Y 3, but there was no statistically significant difference when comparing H&amp;Y 1 with H&amp;Y 2. That was to say, compared with H&amp;Y 1 and H&amp;Y 2, H&amp;Y 3 exhibited shorter stride length, lower step height, slower gait speed, slower stride speed, slower swing speed, and a longer percentage of time with both feet on the ground (Table 4; Figure 4).\n\n3.4 Comparison of gait parameters between early-stage PD and healthy controls\n\nFrom Table 4, we could find that there was no statistically significant difference in gait parameters between H&amp;Y 1 and H&amp;Y 2. Therefore, we defined H&amp;Y 1 and H&amp;Y 2 as early stage and compared them with the healthy controls. We found that early-stage PD patients had shorter stride length (left, right), slower gait speed, slower stride speed (left, right), slower swing speed (left, right), slower cadence (left, right), and longer turning time. This indicated that the gait parameters such as stride length, gait speed, stride speed, swing speed, turning time, and cadence were the first to be affected in the early stage of PD.\n\n3.5 ROC analysis\n\nFrom Table 5, we selected 10 gait parameters with a statistical significance level of p &lt; 0.001 when comparing early-stage PD with healthy controls, including stride length (left, right), gait speed, stride speed (left, right), swing speed (left, right), turning time, and cadence (left, right). We performed ROC analysis on the combined gait parameters mentioned above, and evaluated the ability of gait\n\nfrontiersin.org\n\nYin et al.\n\nTABLE 2 Demographic and clinical characteristics.\n\nVariable\n\nPD (n\u2009=\u200963)\n\nHC (n\u2009=\u200965) p value\n\nGender (male/female)\n\n27/36\n\n35/30\n\n0.214\n\nAge (years)\n\n66.90 ± 8.43\n\n67.40 ± 7.17\n\n0.721\n\nHeight (cm)\n\n165.10 ± 7.98\n\n165.46 ± 7.96\n\n0.795\n\nWeight (kg)\n\n67.83 ± 12.38\n\n66.92 ± 11.37\n\n0.665\n\nDisease duration (years)\n\n1–10\n\nNA\n\nNA\n\nMMSE (score)\n\n24–30\n\nNA\n\nNA\n\nH&amp;Y stage\n\nNA\n\nNA\n\n1\n\n13\n\n2\n\n38\n\n3\n\n12\n\nMDS-UPDRS III\n\n25.50 (17.00, 39.00)\n\nNA\n\nNA\n\n(score)–overall\n\nMDS-UPDRS III\n\n13.15 ± 4.30\n\nNA\n\nNA\n\n(score)–H&amp;Y 1\n\nMDS-UPDRS III\n\n25.50 (21.00, 35.25)\n\nNA\n\nNA\n\n(score)–H&amp;Y 2\n\nMDS-UPDRS III\n\n48.83 ± 11.89\n\nNA\n\nNA\n\n(score)–H&amp;Y 3\n\nPD, Parkinson’s disease; HC, healthy controls; MMSE, Mini-Mental State Examination; H&amp;Y, Hoehn and Yahr; MDS-UPDRS III, section III of the modified movement disorder society version of the unified Parkinson’s disease rating scale; NA, not applicable.\n\nparameters to distinguish early-stage PD from healthy controls. The accuracy was 91.43%, the sensitivity was 93.33%, the specificity was 90.0%, and the area under the curve (AUC) was 0.99, indicating that the gait parameters could correctly distinguish 91.43% early-stage PD from healthy controls. We further analyzed the feature contribution of the included 10 gait parameters, feature contribution was evaluated using the SHAP (SHapley Additive exPlanations) method, which quantifies each parameter’s contribution to the model’s predictions. The contribution degree was in the following order: cadence (right), gait speed, turning time, cadence (left), swing speed (right), stride length (left), stride speed (left), stride length (right), swing speed (left), and stride speed (right). Cadence, gait speed, and turning time had the greatest influence of the gait parameters for distinguishing between early-stage PD and the healthy controls, while stride length, stride speed, and swing speed had a secondary influence (Figure 5).\n\n3.6 Predicting MDS-UPDRS III score\n\nTo explore whether gait parameters could predict MDS-UPDRS III score, we trained a machine learning regression model using all the 19 gait parameters as input features and MDS-UPDRS III score as the output label. The scatter plot indicated that the model had a strong explanatory power for MDS-UPDRS III score (R2 = 0.897), with MAE of 4.015 and MAPE of 0.198 (Figure 6). The scatter plot showed the relationship between the predicted value and the true value of the regression model. It could be visually seen from the figure that the model performance for predicting MDS-UPDRS III score was good.\n\nFrontiers in Neurology\n\n06\n\n10.3389/fneur.2024.1472956\n\nTABLE 3 Comparison of gait parameters between PD patients and healthy controls.\n\nGait parameter\n\nPD (n\u2009=\u200963) HC (n\u2009=\u200965)\n\np value\n\nStride length-L (m)\n\n0.89 ± 0.23\n\n1.10 ± 0.15\n\n&lt; 0.001\n\nStride length-R (m)\n\n0.89 ± 0.24\n\n1.09 ± 0.15\n\n&lt; 0.001\n\nStep height-L (m)\n\n0.10 ± 0.03\n\n0.12 ± 0.02\n\n0.004\n\nStep height-R (m)\n\n0.10 ± 0.03\n\n0.10 (0.09, 0.12)\n\n0.127\n\nStep width (m)\n\n0.14 (0.12, 0.15)\n\n0.13 ± 0.02\n\n0.091\n\nGait speed (m/s)\n\n0.72 ± 0.23\n\n1.10 (1.01, 1.21)\n\n&lt; 0.001\n\nStride speed-L (m/s)\n\n0.80 ± 0.24\n\n1.20 ± 0.21\n\n&lt; 0.001\n\nStride speed-R (m/s)\n\n0.80 ± 0.24\n\n1.20 ± 0.22\n\n&lt; 0.001\n\nSwing speed-L (m/s)\n\n1.93 ± 0.45\n\n2.74 ± 0.36\n\n&lt; 0.001\n\nSwing speed-R (m/s)\n\n1.94 ± 0.47\n\n2.75 ± 0.41\n\n&lt; 0.001\n\nTurning time (s)\n\n1.60 (1.21, 2.06)\n\n1.03 (0.89, 1.31)\n\n&lt; 0.001\n\nCadence-L (steps/min)\n\n112.49 (100.00,\n\n119.99)\n\n128.57 (120.00,\n\n138.46)\n\n&lt; 0.001\n\nCadence-R (steps/min)\n\n105.88 (100.00,\n\n128.57 (120.00,\n\n&lt; 0.001\n\n112.50)\n\n141.76)\n\nSwing phase-L (%)\n\n31.17 ± 3.17\n\n32.39 ± 2.69\n\n0.021\n\nSwing phase-R (%)\n\n31.09 ± 3.50\n\n33.33 (31.43,\n\n34.62)\n\n&lt; 0.001\n\nStance phase-L (%)\n\n68.82 ± 3.17\n\n67.61 ± 2.69\n\n0.022\n\nStance phase-R (%)\n\n68.90 ± 3.50\n\n66.67 (65.39,\n\n&lt; 0.001\n\n68.57)\n\nDouble support-L (%)\n\n38.14 ± 6.34\n\n34.62 (33.33,\n\n&lt; 0.001\n\n36.85)\n\nDouble support-R (%)\n\n37.43 ± 6.21\n\n34.62 (33.33,\n\n36.85)\n\n0.004\n\nL, left; R, right. p values in bold indicate statistical significance (p &lt; 0.05).\n\n4 Discussion\n\nOur study used a non-contact gait assessment system to assess and quantify gait parameters in patients with PD. Our findings confirmed that there were significant differences in gait parameters between PD patients and healthy controls. PD patients had slower gait speed and shorter stride length, which was consistent with the existing research. Gait is a very important motor function in daily life, gait disorder is closely associated with the quality of life (18). Therefore, early recognition and monitoring of gait is crucial for the diagnosis, treatment, and prognosis of PD patients. It is challenging to identify gait abnormalities in the early stage of PD, even to detect gait abnormalities without the complaint of gait disorder. Previous studies defined the early stage as H&amp;Y stage below 2.5 (19, 20).\n\nWe divided PD patients into three groups according to H&amp;Y stages and found that compared with H&amp;Y 1–2, H&amp;Y 3 had shorter stride length, lower step height, slower gait speed, slower stride speed, slower swing speed, and a longer percentage of time with both feet on the ground. But there was no statistically significant difference between H&amp;Y 1 and H&amp;Y 2. To explore gait abnormalities in the early stage of PD, we defined H&amp;Y 1 and H&amp;Y 2 as early stage, and compared them with healthy controls. We found that early-stage PD had shorter stride length, slower gait speed, slower stride speed, slower\n\nfrontiersin.org\n\nYin et al.\n\n10.3389/fneur.2024.1472956\n\nTABLE 4 Comparison of gait parameters among H&amp;Y 1, H&amp;Y 2, and H&amp;Y 3.\n\nGait Parameter\n\nH&amp;Y stage\n\np value\n\nH&amp;Y 1 (n\u2009=\u200913) H&amp;Y 2 (n\u2009=\u200938) H&amp;Y 3 (n\u2009=\u200912)\n\nOverall\n\n1 vs 2\n\n1 vs 3\n\n2 vs 3\n\nStride length-L (m)\n\n1.03 ± 0.13\n\n0.91 ± 0.22\n\n0.66 ± 0.22\n\n&lt;0.001\n\n0.060\n\n&lt;0.001\n\n0.007\n\nStride length-R (m)\n\n1.02 ± 0.17\n\n0.91 ± 0.22\n\n0.66 ± 0.23\n\n&lt;0.001\n\n0.383\n\n&lt; 0.001\n\n0.001\n\nStep height-L (m)\n\n0.11 ± 0.02\n\n0.11 ± 0.03\n\n0.07 ± 0.02\n\n&lt;0.001\n\n1.000\n\n0.001\n\n0.001\n\nStep height-R (m)\n\n0.11 ± 0.03\n\n0.11 ± 0.03\n\n0.08 ± 0.02\n\n0.025\n\n1.000\n\n0.068\n\n0.029\n\nStep width (m)\n\n0.13 ± 0.02\n\n0.13 ± 0.02\n\n0.14 (0.13, 0.15)\n\n0.706\n\n\n\n\n\n\n\nGait speed (m/s)\n\n0.82 ± 0.16\n\n0.75 ± 0.21\n\n0.54 ± 0.24\n\n0.003\n\n0.855\n\n0.004\n\n0.011\n\nStride speed-L (m/s)\n\n0.92 ± 0.19\n\n0.82 ± 0.22\n\n0.60 ± 0.23\n\n0.002\n\n0.420\n\n0.002\n\n0.012\n\nStride speed-R (m/s)\n\n0.90 ± 0.22\n\n0.83 ± 0.22\n\n0.61 ± 0.24\n\n0.004\n\n0.984\n\n0.005\n\n0.011\n\nSwing speed-L (m/s)\n\n2.07 ± 0.32\n\n1.99 ± 0.44\n\n1.56 ± 0.44\n\n0.006\n\n1.000\n\n0.012\n\n0.009\n\nSwing speed-R (m/s)\n\n2.15 ± 0.42\n\n1.97 ± 0.47\n\n1.61 ± 0.40\n\n0.011\n\n0.686\n\n0.011\n\n0.049\n\nTurning time (s)\n\n1.36 ± 0.39\n\n1.68 (1.26, 2.17)\n\n1.95 ± 1.00\n\n0.105\n\n\n\n\n\n\n\nCadence-L (steps/min)\n\n107.28 ± 12.05\n\n110.50 ± 13.05\n\n114.74 ± 20.39\n\n0.441\n\n\n\n\n\n\n\nCadence-R (steps/min)\n\n106.83 ± 12.48\n\n105.88 (99.34,\n\n107.87 ± 16.93\n\n0.948\n\n\n\n\n\n\n\n112.50)\n\nSwing phase-L (%)\n\n32.31 ± 4.29\n\n31.29 ± 2.57\n\n29.55 ± 3.12\n\n0.156\n\n\n\n\n\n\n\nSwing phase-R (%)\n\n33.19 ± 3.93\n\n31.34 (29.91, 33.85)\n\n28.51 ± 3.49\n\n0.005\n\n0.866\n\n0.006\n\n0.021\n\nStance phase-L (%)\n\n67.68 ± 4.29\n\n68.70 ± 2.57\n\n70.45 ± 3.12\n\n0.156\n\n\n\n\n\n\n\nStance phase-R (%)\n\n66.80 ± 3.93\n\n68.66 (66.14, 70.09)\n\n71.49 ± 3.49\n\n0.005\n\n0.866\n\n0.006\n\n0.021\n\nDouble support-L (%)\n\n34.96 ± 8.35\n\n36.78 (34.24, 40.81)\n\n43.10 ± 6.09\n\n0.012\n\n1.000\n\n0.018\n\n0.027\n\nDouble support-R (%)\n\n34.91 ± 8.39\n\n36.14 (33.33, 39.97)\n\n41.79 ± 5.76\n\n0.014\n\n1.000\n\n0.018\n\n0.037\n\nL, left; R, right. p values in bold indicate statistical significance (p &lt; 0.05).\n\nFIGURE 4 Comparison of gait parameters among H&amp;Y 1, H&amp;Y 2 and H&amp;Y 3. The filled circles represent the median, lower and upper lines represent the 25th and 75th percentile, respectively. *represents p\u2009&lt;\u20090.05, **represents p\u2009&lt;\u20090.01, ***represents p\u2009&lt;\u20090.001.\n\nFrontiers in Neurology\n\n07\n\nfrontiersin.org\n\nYin et al.\n\n10.3389/fneur.2024.1472956\n\nTABLE 5 Comparison of gait parameters between early-stage PD and healthy controls.\n\nGait Parameter\n\nEarly-stage PD (n\u2009=\u200951)\n\nHC (n\u2009=\u200965)\n\np value\n\nStride length-L (m)\n\n0.94 ± 0.20\n\n1.10 ± 0.15\n\n&lt; 0.001\n\nStride length-R (m)\n\n0.94 ± 0.21\n\n1.09 ± 0.15\n\n&lt; 0.001\n\nStep height-L (m)\n\n0.11 ± 0.03\n\n0.12 ± 0.02\n\n0.175\n\nStep height-R (m)\n\n0.11 ± 0.03\n\n0.10 (0.09, 0.12)\n\n0.781\n\nStep width (m)\n\n0.13 ± 0.02\n\n0.13 ± 0.02\n\n0.156\n\nGait speed (m/s)\n\n0.76 ± 0.20\n\n1.10 (1.01, 1.21)\n\n&lt; 0.001\n\nStride speed-L (m/s)\n\n0.84 ± 0.22\n\n1.20 ± 0.21\n\n&lt; 0.001\n\nStride speed-R (m/s)\n\n0.85 ± 0.22\n\n1.20 ± 0.22\n\n&lt; 0.001\n\nSwing speed-L (m/s)\n\n2.01 ± 0.41\n\n2.74 ± 0.36\n\n&lt; 0.001\n\nSwing speed-R (m/s)\n\n2.02 ± 0.46\n\n2.75 ± 0.41\n\n&lt; 0.001\n\nTurning time (s)\n\n1.56 (1.23, 2.05)\n\n1.03 (0.89, 1.31)\n\n&lt; 0.001\n\nCadence-L (steps/min)\n\n109.68 ± 12.77\n\n128.57 (120.00, 138.46)\n\n&lt; 0.001\n\nCadence-R (steps/min)\n\n105.88 (100.00, 112.50)\n\n128.57 (120.00, 141.76)\n\n&lt; 0.001\n\nSwing phase-L (%)\n\n31.55 ± 3.08\n\n32.39 ± 2.69\n\n0.123\n\nSwing phase-R (%)\n\n31.42 (30.00, 33.97)\n\n33.33 (31.43, 34.62)\n\n0.001\n\nStance phase-L (%)\n\n68.44 ± 3.08\n\n67.61 ± 2.69\n\n0.127\n\nStance phase-R (%)\n\n68.57 (66.02, 70.00)\n\n66.67 (65.39, 68.57)\n\n0.003\n\nDouble support-L (%)\n\n36.97 ± 5.86\n\n34.62 (33.33, 36.85)\n\n0.008\n\nDouble support-R (%)\n\n36.41 ± 5.90\n\n34.62 (33.33, 36.85)\n\n0.086\n\nL, left; R, right. p values in bold indicate statistical significance (p &lt; 0.05).\n\nFIGURE 5 (A) Shows the ROC curve of the selected gait parameters in early-stage PD. (B) Shows the feature contribution of the selected gait parameters.\n\nswing speed, slower turning time, and slower cadence. This indicated that in the early stage of PD, stride length, gait speed, stride speed, swing speed, turning time, and cadence were the first to be affected, suggested that they could be used to detect gait abnormalities in the early stage. In the progressive stage of the disease, step height, gait speed, stride speed, and swing speed decrease further, and the percentage of time with both feet on the ground was prolonged, indicated that these parameters could be used to monitor the progression of the disease.\n\nWe innovatively proposed an auxiliary diagnostic method based on fine-grained gait feature analysis, aimed to identify signs of PD in the early stage, we paid a particular attention to the\n\npotential value of gait parameters in the early diagnosis of PD. By using the machine learning model, we conducted an in-depth exploration of the selected ten gait parameters, including stride length (left, right), gait speed, stride speed (left, right), swing speed (left, right), turning time, and cadence (left, right). The results showed that these gait parameters could effectively distinguish early-stage PD from healthy controls. The model showed an encouraging classification performance, with an accuracy of up to 91%, the sensitivity of 93%, and the specificity also maintained at a high level of 90%. This strongly proved the practicality and reliability of the constructed model in the auxiliary diagnosis of early-stage PD.\n\nFrontiers in Neurology\n\n08\n\nfrontiersin.org\n\nYin et al.\n\nFIGURE 6 The scatter plot of the relationship between the predicted value and true value of the regression model (the horizontal axis of the scatter plot represents the true value of the MDS-UPDRS III score and the vertical axis represents the predicted value of the MDS-UPDRS III score).\n\nTo more comprehensively evaluate the potential of gait parameters in quantifying disease severity, we further explored their association with MDS-UPDRS III score. We constructed a predictive model using all the 19 gait parameters as input features and MDS-UPDRS III score as output label. The model showed excellent explanatory power and predictive accuracy: a high R2 value indicated that the model could effectively explain most of the score variations; a low MAE value indicated good consistency between the predicted value and true value of the model; and a low MAPE value highlighted the high precision of the model in predicting MDS-UPDRS III score. In summary, our research not only confirmed the importance of gait parameters in the early diagnosis of PD but also demonstrated their great potential in quantifying disease progression. This provided a new perspective and tool for the future clinical management and personalized treatment of PD.\n\nWhile our study has revealed the potential of gait parameters in the auxiliary diagnosis of early-stage PD, there were still some limitations that should be acknowledged. The primary challenge lied in the limitation of sample size, that was, the relatively small number of participants. A small sample size might affect the power of statistical analysis and potentially constrain the generalizability and stability of the results. Additionally, the current study focused only on spatiotemporal parameters, such as stride length and gait speed, without involving more detailed kinematic parameters like joint angles. In light of these limitations, we plan to expand the sample size in future research and include more gait parameters, such as kinematic parameters, in order to build a more comprehensive and accurate diagnostic model.\n\n5 Conclusion\n\nIn summary, the non-contact gait assessment system we used was capable of objectively and quantitatively evaluating gait disorder in PD patients, providing clinicians with a valuable tool for predicting MDS-UPDRS III score. Our machine learning models could accurately distinguish early-stage PD from healthy controls by integrating analysis of gait parameters such as stride length, gait speed, stride and swing speed, turning time, and cadence, and the model could also make reasonable prediction of MDS-UPDRS III score. This achievement reinforced the role of gait analysis in the early diagnosis\n\nFrontiers in Neurology\n\n09\n\n10.3389/fneur.2024.1472956\n\nof PD and paved the way for the development of early intervention and personalized treatment strategies for PD.\n\nData availability statement\n\nThe original contributions presented in the study are included in the article/supplementary material, further inquiries can be directed to the corresponding author.\n\nEthics statement\n\nThe studies involving humans were approved by Central Hospital of Dalian University of Technology (Reference No. YN2022-039-57). The studies were conducted in accordance with the local legislation and institutional requirements. The participants provided their written informed consent to participate in this study.\n\nAuthor contributions\n\nWY: Conceptualization, Data curation, Formal analysis, Methodology, Writing – original draft, Writing – review &amp; editing. WZ: Formal analysis, Methodology, Writing – review &amp; editing. HG: Writing – review &amp; editing. XN: Data curation, Writing – review &amp; editing. CS: Data curation, Writing – review &amp; editing. XF: Writing – review &amp; editing. CW: Methodology, Resources, Supervision, Writing – review &amp; editing.\n\nFunding\n\nThe author(s) declare that financial support was received for the research, authorship, and/or publication of this article. This study was supported by Institutional research fund (2022ZZ220), Dalian life and health field guidance program project (2022-224-37).\n\nConflict of interest\n\nXiangmin Fan is an editorial board member of Frontiers. This has no impact on the peer review process and the final decision. Wencheng Zhu is an employee of Beijing CAS-Ruiyi Information Technology Co., Ltd.\n\nThe remaining authors declare that the research was conducted in the absence of any commercial or financial relationships that could be construed as a potential conflict of interest.\n\nPublisher’s note\n\nAll claims expressed in this article are solely those of the authors and do not necessarily represent those of their affiliated organizations, or those of the publisher, the editors and the reviewers. Any product that may be evaluated in this article, or claim that may be made by its manufacturer, is not guaranteed or endorsed by the publisher.\n\nfrontiersin.org\n\nYin et al.\n\nReferences\n\n1. Tysnes OB, Storstein A. Epidemiology of Parkinson’s disease. J Neural Transm.\n\n(2017) 124:901–5. doi: 10.1007/s00702-017-1686-y\n\n2. Mirelman A, Bonato P, Camicioli R, Ellis TD, Giladi N, Hamilton JL, et al. Gait impairments in Parkinson&#39;s disease. Lancet Neurol. (2019) 18:697–708. doi: 10.1016/ S1474-4422(19)30044-4\n\n3. Mermelstein S, Barbosa P, Kaski D. Neurological gait assessment. Pract Neurol.\n\n(2024) 24:11–21. doi: 10.1136/pn-2023-003917\n\n4. Creaby MW, Cole MH. Gait characteristics and falls in Parkinson&#39;s disease: a systematic review and meta-analysis. Parkinsonism Relat Disord. (2018) 57:1–8. doi: 10.1016/j.parkreldis.2018.07.008\n\n5. Schrag A, Jahanshahi M, Quinn N. What contributes to quality of life in patients with Parkinson&#39;s disease? J Neurol Neurosurg Psychiatry. (2000) 69:308–12. doi: 10.1136/ jnnp.69.3.308\n\n6. Rizzo G, Copetti M, Arcuti S, Martino D, Fontana A, Logroscino G. Accuracy of clinical diagnosis of Parkinson disease. Neurology. (2016) 86:566–76. doi: 10.1212/ WNL.0000000000002350\n\n7. Fereshtehnejad SM, Romenets SR, Anang JBM, Latreille V, Gagnon JF, Postuma RB. New clinical subtypes of Parkinson disease and their longitudinal progression. JAMA Neurol. (2015) 72:863–73. doi: 10.1001/jamaneurol.2015.0703\n\n8. Aboutorabi A, Arazpour M, Bahramizadeh M, Hutchins SW, Fadayevatan R. The effect of aging on gait parameters in able-bodied older subjects: a literature review. Aging Clin Exp Res. (2016) 28:393–405. doi: 10.1007/s40520-015-0420-6\n\n9. Melzer I, Oddsson LIE. The effect of a cognitive task on voluntary step execution in healthy elderly and young individuals. J Am Geriatr Soc. (2004) 52:1255–62. doi: 10.1111/j.1532-5415.2004.52353.x\n\n10. Opara JA, Malecki A, Malecka E, Socha T. Motor assessment in Parkinson&#39;s\n\ndisease. AAEM. (2017) 24:411–5. doi: 10.5604/12321966.1232774\n\n11. Guo Y, Yang JX, Liu YX, Chen X, Yang GZ. Detection and assessment of Parkinson&#39;s disease based on gait analysis: a survey. Front Aging Neurosci. (2022) 14:916971. doi: 10.3389/fnagi.2022.916971\n\nFrontiers in Neurology\n\n10\n\n10.3389/fneur.2024.1472956\n\n12. Bonato P, Feipel V, Corniani G, Arin-Bal G, Leardini A. Position paper on how technology for human motion analysis and relevant clinical applications have evolved over the past decades: striking a balance between accuracy and convenience. Gait Posture. (2024) 113:191–203. doi: 10.1016/j.gaitpost.2024.06.007\n\n13. Jing Y, Qin PN, Fan XM, Qiang W, Wencheng Z, Sun W, et al. Deep learning- assisted gait parameter assessment for neurodegenerative diseases: model development and validation. J Med Internet Res. (2023) 25:e46427. doi: 10.2196/46427\n\n14. Postuma RB, Berg D, Stern M, Poewe W, Olanow CW, Oertel W, et al. MDS clinical diagnostic criteria for Parkinson’s disease. Movement Disord. (2015) 30:1591–601. doi: 10.1002/mds.26424\n\n15. Hoehn MM, Yahr MD. Parkinsonism: Onset, progression, and mortality.\n\nNeurology. (1967) 17:427–42. doi: 10.1212/WNL.17.5.427\n\n16. Goetz CG, Tilley BC, Shaftman SR, Stebbins GT, Fahn S, Martinez-Martin P, et al. Movement disorder society-sponsored revision of the unified parkinson’s disease rating scale (MDS-UPDRS): scale presentation and clinimetric testing results. Mov Disord. (2008) 23:2129–70. doi: 10.1002/mds.22340\n\n17. Tombaugh TN, Mcintyre NJ. The mini-mental state examination: a (1992) 40:922–35. doi: J Am Geriatr Soc.\n\n17. Tombaugh TN, Mcintyre NJ. The mini-mental state examination: a (1992) 40:922–35. doi: review.\n\n18. Ellis T, Cavanaugh JT, Earhart GM, Ford MP, Foreman KB, Dibble LE. Which measures of physical function and motor impairment best predict quality of life in Parkinson’s disease? Parkinsonism Relat Disord. (2011) 17:693–7. doi: 10.1016/j. parkreldis.2011.07.004\n\n19. Nair P, Shojaei Baghini M, Pendharkar G, Chung HM. Detecting early-stage Parkinson’s disease from gait data. Proc Inst Mech Eng H. (2023) 237:1287–96. doi: 10.1177/09544119231197090\n\n20. Pistacchi M, Gioulis M, Sanson F, de Giovannini E, Filippi G, Rossetto F, et al. Gait analysis and clinical correlations in early Parkinson’s disease. Funct Neurol. (2017) 32:28–34. doi: 10.11138/FNeur/2017.32.1.028\n\nfrontiersin.org&quot;)]</pre><p>Once the documents are loaded, we split them into smaller chunks for efficient retrieval.</p><p><strong>5. Data Chunking: Split the document into smaller chunks</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*ViKbaXVpCaXQ_1ljirk2FA.png" /><figcaption>Data Chunking — <a href="https://vectify.ai/blog/LargeDocumentSummarization">https://vectify.ai/blog/LargeDocumentSummarization</a></figcaption></figure><p>Data chunking is the process of breaking down your data into smaller, more manageable pieces.</p><p>There are several “<a href="https://python.langchain.com/v0.1/docs/modules/data_connection/document_transformers/"><strong>TextSplitter</strong></a><strong>”</strong> in LangChain, to chose from. We will us use <strong>“</strong><a href="https://python.langchain.com/v0.1/docs/modules/data_connection/document_transformers/recursive_text_splitter/"><strong>RecursiveCharacterTextSplitter</strong></a><strong>”</strong>. RecursiveCharacterTextSplitter is a text-splitting utility from LangChain that recursively splits a document into smaller chunks while trying to preserve meaningful context. It’s commonly used when working with large text data in LLMs (large language models) to ensure the model receives manageable input sizes while maintaining coherence.</p><pre>from langchain_text_splitters import RecursiveCharacterTextSplitter<br><br>def chunking(documents):<br>    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024,<br>                                                   chunk_overlap=500,<br>                                                   length_function=len,<br>                                                   add_start_index=True)<br>    chunks = text_splitter.split_documents(documents=documents)<br>    return chunks<br><br>chunks = chunking(documents)<br>print(f&#39;Split {len(documents)} documents into {len(chunks)} chunks&#39;)</pre><pre># Result<br><br>Split 1 documents into 258 chunks</pre><p>The <em>chunk_overlap</em> parameter ensures that context is preserved when splitting text into smaller chunks. It prevents information loss at chunk boundaries by keeping a portion of the previous chunk in the next one. To make sure it’s working as intended, we will print few chunks.</p><pre>for i in range(158, 160):  # Check around the problematic chunk index<br>    print(f&quot;Chunk {i}:\n{chunks[i].page_content}\n&quot;)</pre><pre># Chunk 158:<br><br>Author contributions<br><br>YG, XC, and GZY designed the review and revised the manuscript critically. YG, JY, YL, and XC performed the literature search and analysis and wrote the paper. YG, JY, and YL analyzed the obtained articles. All authors have read and agreed to the published version of the manuscript.<br><br>Funding<br><br>This work was supported by the Science and Technology of under Grant Shanghai Municipality Commission the Foundation of National Facility for 20DZ2220400, Interdisciplinary Translational Medicine Program of Shanghai Jiao Tong University under Grant YG2021QN117, the National Natural Science Foundation of China under Grant 61922075, and the USTC Research Funds of the Double First-Class Initiative under Grant KY2100000123.<br><br>(Shanghai),<br><br>the<br><br>Acknowledgments<br><br>We would like to thank Mr. Yunbo Li and Miss Mengnan He<br><br>for the contribution of literature search.<br><br>Conﬂict of interest<br><br># Chunk 159: <br><br>(Shanghai),<br><br>the<br><br>Acknowledgments<br><br>We would like to thank Mr. Yunbo Li and Miss Mengnan He<br><br>for the contribution of literature search.<br><br>Conﬂict of interest<br><br>The authors declare that the research was conducted in the absence of any commercial or ﬁnancial relationships that could be construed as a potential conﬂict of interest.<br><br>frontiersin.org<br><br>Guo et al.<br><br>Publisher’s note<br><br>All claims expressed in this article are solely those of the authors and do not necessarily represent those of their aﬃliated<br><br>References<br><br>Abdulhay, E., Arunkumar, N., Narasimhan, K., Vellaiappan, E., and Venkatraman, V. (2018). Gait and tremor investigation using machine learning techniques for the diagnosis of Parkinson disease. Future Generat. Comput. Syst. 83, 366–373. doi: 10.1016/j.future.2018.02.009</pre><p>We can clearly see that the first few character of chunk 159 matches to the last few character of chunk 158.</p><p><strong>6. Vector Embedding — Let’s load the Embedding model to embed the chunks</strong></p><p><strong>Vector embedding</strong> is a technique used to represent data — such as words, sentences, images, or other types of information — as numerical vectors in a high-dimensional space. These vectors capture meaningful relationships and similarities between data points, making them useful for machine learning, natural language processing (NLP), and AI applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*Tc5YCEhF7i4MyqKf3odISw.png" /><figcaption>Vector Embedding — <a href="https://weaviate.io/blog/how-to-choose-an-embedding-model">https://weaviate.io/blog/how-to-choose-an-embedding-model</a></figcaption></figure><p>We will use <a href="https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2"><strong><em>Hugging Face</em></strong></a> for loading the embedding model “all-MiniLM-L6-v2” using sentence-transformers.</p><pre>from langchain_community.embeddings import HuggingFaceEmbeddings<br><br>def load_embedding_model():<br>    embedding_model = HuggingFaceEmbeddings(model_name=&quot;sentence-transformers/all-MiniLM-L6-v2&quot;)<br>    return embedding_model<br><br>embedding_model = load_embedding_model()</pre><p><strong>7. Storing the knowledge into ChromaDB</strong></p><p><a href="https://github.com/chroma-core/chroma"><strong>ChromaDB</strong></a><strong> </strong>is an open-source vector database designed for storing, searching, and retrieving embeddings efficiently. It is widely used in LLM-powered applications, Retrieval-Augmented Generation (RAG), and semantic search.</p><p>We will embed the chunk using the embedding model and save it to ChromaDB vector database:</p><pre>from langchain_community.vectorstores import Chroma<br><br>def save_to_chroma(chunks, CHROMA_PATH, embedding_model):<br>    if os.path.exists(CHROMA_PATH):<br>        shutil.rmtree(CHROMA_PATH)  # Clear existing database<br>    db = Chroma.from_documents(chunks, embedding_model, persist_directory=CHROMA_PATH)<br>    db.persist()<br><br>CHROMA_PATH = &quot;chroma&quot;<br>save_to_chroma(chunks, CHROMA_PATH, embedding_model)</pre><p>after running the code, embedded chunks would be store in the ChromaDB vector database under the folder <strong>“chroma”, </strong>which we will use later to retrieve the relevant knowledge based on our query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*qXE8OiNTph4AQRaIVw1VkQ.png" /></figure><p><strong>8. Let’s prepare the Query</strong></p><p>We will be using gait analysis data from a patient with Parkinson’s disease. <a href="https://invivowellness.com/what-is-gait-analysis-how-can-it-help-you-walk-and-run-better/"><strong><em>Gait analysis</em></strong></a> is the systematic evaluation of human walking patterns, commonly applied in medical diagnostics, sports science, and biomechanics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*fteT8oqXEO77pFOTXBVpjA.png" /><figcaption>Gait Analysis — <a href="https://invivowellness.com/what-is-gait-analysis-how-can-it-help-you-walk-and-run-better/">https://invivowellness.com/what-is-gait-analysis-how-can-it-help-you-walk-and-run-better/</a></figcaption></figure><p>Gait analysis involves capturing and analysing movement data to evaluate walking mechanics and identify abnormalities in gait patterns. One of the most advanced solutions for rapid gait assessment is <a href="https://en.o-dhs.com/produkt/orthelligent-vision/"><strong><em>Orthelligent VISION</em></strong></a><strong><em> </em></strong>developed by<strong><em> </em></strong><a href="https://www.linkedin.com/company/oped-gmbh/posts/?feedView=all"><strong><em>OPED GmbH</em></strong></a>, which provides a comprehensive analysis within just five minutes. <a href="https://www.linkedin.com/company/orthelligent/posts/?feedView=all"><strong><em>Orthelligent</em></strong></a> leverages computer vision techniques and deep learning-based pose estimation models to interpret and assess human motion.</p><p>Building on this foundation, we will take gait analysis a step further by integrating <strong>LLMs (Large Language Models) and RAG (Retrieval-Augmented Generation)</strong>. This approach will enable us to generate diagnoses based on a patient’s gait analysis results, supplemented by insights from research papers and a structured medical knowledge base.</p><p>Gait analysis results are retrieved from the Orthelligent Vision result report and reorganised in JSON format as follow:</p><pre>{<br>    &quot;result_details&quot;: [<br>        {<br>            &quot;name&quot;: &quot;Gait Symmetry&quot;,<br>            &quot;description&quot;: &quot;The gait symmetry indicates how symmetrical the left and right step length is. The higher the value, the better.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 80,<br>                &quot;nominal_lower&quot;: 90,<br>                &quot;nominal_upper&quot;: 100,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Gait Speed&quot;,<br>            &quot;description&quot;: &quot;The gait speed indicates how fast the patient was walking during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 1.5,<br>                &quot;nominal_lower&quot;: 2,<br>                &quot;nominal_upper&quot;: 5,<br>                &quot;unit&quot;: &quot;km/h&quot;<br>            }<br><br>        },<br>        {<br>            &quot;name&quot;: &quot;Cadence&quot;,<br>            &quot;description&quot;: &quot;The gait cadence indicates the total number of steps taken within a minute during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 80,<br>                &quot;nominal_lower&quot;: 90,<br>                &quot;nominal_upper&quot;: 115,<br>                &quot;unit&quot;: &quot;Steps/min&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Double Support Left&quot;,<br>            &quot;description&quot;: &quot;The gait double support left indicates proportion of time that both feet of the patient are on the ground during the gait cycle of left leg.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 30,<br>                &quot;nominal_lower&quot;: 10,<br>                &quot;nominal_upper&quot;: 40,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Double Support Right&quot;,<br>            &quot;description&quot;: &quot;The gait double support right indicates proportion of time that both feet of the patient are on the ground during the gait cycle of right leg.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 35,<br>                &quot;nominal_lower&quot;: 10,<br>                &quot;nominal_upper&quot;: 40,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Gait Variability Left&quot;,<br>            &quot;description&quot;: &quot;The gait variability indicates step-to-step length fluctuation of left / right leg during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 7,<br>                &quot;nominal_lower&quot;: 0,<br>                &quot;nominal_upper&quot;: 5,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Gait Variability Right&quot;,<br>            &quot;description&quot;: &quot;The gait variability indicates step-to-step length fluctuation of left / right leg during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 8,<br>                &quot;nominal_lower&quot;: 0,<br>                &quot;nominal_upper&quot;: 5,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Step Length Left&quot;,<br>            &quot;description&quot;: &quot;The left gait step length indicates the average distance between the point of initial contact of the left foot to the point of initial contact of the right foot during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 50,<br>                &quot;nominal_lower&quot;: 55,<br>                &quot;nominal_upper&quot;: 80,<br>                &quot;unit&quot;: &quot;cm&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Step Length Right&quot;,<br>            &quot;description&quot;: &quot;The right gait step length indicates the average distance between the point of initial contact of the right foot to the point of initial contact of the left foot during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 48,<br>                &quot;nominal_lower&quot;: 55,<br>                &quot;nominal_upper&quot;: 80,<br>                &quot;unit&quot;: &quot;cm&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Step Time Left&quot;,<br>            &quot;description&quot;: &quot;The gait step time indicates the average time elapsed from initial contact of the left foot to initial contact of the right foot during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 0.59,<br>                &quot;nominal_lower&quot;: 0.51,<br>                &quot;nominal_upper&quot;: 0.65,<br>                &quot;unit&quot;: &quot;s&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Step Time Right&quot;,<br>            &quot;description&quot;: &quot;The gait step time indicates the average time elapsed from initial contact of the right foot to initial contact of the left foot during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 0.61,<br>                &quot;nominal_lower&quot;: 0.51,<br>                &quot;nominal_upper&quot;: 0.65,<br>                &quot;unit&quot;: &quot;s&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Stance Time Left&quot;,<br>            &quot;description&quot;: &quot;The gait stance time indicates the average percentage of time during which the left foot is in contact with the ground during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 80,<br>                &quot;nominal_lower&quot;: 55,<br>                &quot;nominal_upper&quot;: 65,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        },<br>        {<br>            &quot;name&quot;: &quot;Stance Time Right&quot;,<br>            &quot;description&quot;: &quot;The gait stance time indicates the average percentage of time during which the right foot is in contact with the ground during the analysis.&quot;,<br>            &quot;score_dict&quot;: {<br>                &quot;nominal_value&quot;: 75,<br>                &quot;nominal_lower&quot;: 55,<br>                &quot;nominal_upper&quot;: 65,<br>                &quot;unit&quot;: &quot;%&quot;<br>            }<br>        }<br>    ]<br>}</pre><p>This data we will store in JSON format, named as <em>parkinson_patient_data.json </em>in our working dir.</p><p><strong>Let’s load the json data, extract the gait features and prepare the query</strong>.</p><pre>def read_json(file_path)<br>  with open(file_path, &quot;r&quot;) as f:<br>      gait_data_json = json.load(f)<br>  return gait_data_json<br><br>def extract_gait_features(data: Dict):<br>    gait_data = {}<br>    for param in data[&quot;result_details&quot;]:<br>        name = param[&quot;name&quot;]<br>        nominal_value = param[&quot;score_dict&quot;][&quot;nominal_value&quot;]<br>        nominal_lower = param[&quot;score_dict&quot;][&quot;nominal_lower&quot;]<br>        nominal_upper = param[&quot;score_dict&quot;][&quot;nominal_upper&quot;]<br>        unit = param[&quot;score_dict&quot;][&quot;unit&quot;]<br>        gait_data[name] = (nominal_value, nominal_lower, nominal_upper, unit)<br>    return gait_data<br><br>def generate_query(gait_data):<br>    query = &quot;Patient gait analysis results: &quot;<br>    for key, value in gait_data.items():<br>        if isinstance(value, tuple):<br>            query += f&quot;{key} for the patient is {value[0]} {value[3]} and the reference range for healthy patient is between {value[1]} {value[3]} and {value[2]} {value[3]}, &quot;<br>    query += &quot;What does this indicate about Parkinson&#39;s disease?&quot;<br>    return query<br><br>file_path = &quot;parkinson_patient_data.json&quot;<br>gait_data_json = read_json(file_path)<br>gait_data = extract_gait_features(gait_data_json)<br>query = generate_query(gait_data)</pre><pre># query<br><br>Patient gait analysis results: Gait Symmetry for the patient is 80 % and <br>the reference range for healthy patient is between 90 % and 100 %, <br>Gait Speed for the patient is 1.5 km/h and the reference range for healthy <br>patient is between 2 km/h and 5 km/h, Cadence for the patient is 80 Steps/min <br>and the reference range for healthy patient is between 90 Steps/min and <br>115 Steps/min, Double Support Left for the patient is 30 % and the <br>reference range for healthy patient is between 10 % and 40 %, <br>Double Support Right for the patient is 35 % and the reference range for <br>healthy patient is between 10 % and 40 %, <br>Gait Variability Left for the patient is 7 % <br>and the reference range for healthy patient is between 0 % and 5 %, <br>Gait Variability Right for the patient is 8 % and the reference range for <br>healthy patient is between 0 % and 5 %, <br>Step Length Left for the patient is 50 cm and the reference range for <br>healthy patient is between 55 cm and 80 cm, <br>Step Length Right for the patient is 48 cm and <br>the reference range for healthy patient is between 55 cm and 80 cm, <br>Step Time Left for the patient is 0.59 s and the reference range for <br>healthy patient is between 0.51 s and 0.65 s, <br>Step Time Right for the patient is 0.61 s and the <br>reference range for healthy patient is between 0.51 s and 0.65 s, <br>Stance Time Left for the patient is 80 % and the reference range for <br>healthy patient is between 55 % and 65 %, <br>Stance Time Right for the patient is 75 % and the reference range for <br>healthy patient is between 55 % and 65 %, <br>What does this indicate about Parkinson&#39;s disease?</pre><p>Above query would be automatically generated from the input json file.</p><p><strong>9. Load the saved ChromaDB vector database, run the query to retrieve the relevant information from our knowledge base pdf using retrievers</strong></p><p>A <strong>retriever</strong> in LangChain is a component responsible for fetching relevant documents or data chunks from a knowledge source based on a given query. Unlike traditional keyword-based search, retrievers typically use <strong>vector embeddings</strong> and similarity search to retrieve the most relevant information efficiently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*6Co-xdOoywK621bpym8JjA.png" /><figcaption><a href="https://python.langchain.com/docs/concepts/retrievers/">https://python.langchain.com/docs/concepts/retrievers/</a></figcaption></figure><p>Retrieval is performed based on query similarity using <em>similarity_search_with_relevance_scores</em></p><pre># Load the existing database<br>def load_chroma(CHROMA_PATH, embedding_model):<br>    <br>    db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_model)<br>    <br>    return db<br><br># Retrieve relevant knowledge from ChromDB<br>def retrieve_knowledge(query, db):<br>    <br>    results = db.similarity_search_with_relevance_scores(query, k=3)<br><br>    context_text = &quot;\n\n---\n\n&quot;.join([doc.page_content for doc, _score in results])<br>    context_sources = [doc.metadata.get(&quot;source&quot;, None) for doc, _score in results]<br><br>    return context_text, context_sources<br><br># load the vector_store db using our previous CHROMA_PATH and imported embedding_model<br>vector_store = load_chroma(CHROMA_PATH=CHROMA_PATH, embedding_model=embedding_model)<br><br># Retrieve relevant knowledge<br>retrieved_knowledge, retrieved_sources = retrieve_knowledge(query, vector_store)</pre><p><strong>10. Creating a Structured Prompt</strong></p><p>Now, let’s define a strict prompt template that ensures the LLM only provides fact-based diagnoses using our query and retrieved_knowledge:</p><pre>def get_prompt_template(query, retrieved_knowledge):<br>    <br>    PROMPT_TEMPLATE = f&quot;&quot;&quot;<br><br>    You are a specialized medical AI diagnostic assistant providing gait analysis diagnoses. <br><br>    Answer the question based on following relevant medical insights from the knowledge base:<br><br>    {retrieved_knowledge}<br><br>    **Determine Relevance**<br>    - If the question does not pertain to gait analysis or is unrelated to the relevant medical insights, respond with: &quot;Diagnose is not possible because it is out of scope.&quot; <br>    - If uncertain, respond with: &quot;Insufficient data!&quot; <br><br>    **Diagnostic Assessment**<br>    - Strictly base your responses on relevant medical insights from the knowledge base.<br>    - Provide a diagnostic assessment of the patient&#39;s gait and possible relation to Parkinson&#39;s disease based on the medical insights.<br><br>    Now, based on the above conditions, generate the response but don&#39;t repeat the determine relevance and diagnostic assessments. <br>    At the end diagnose should have the result saying if the user has parkinson disease or not with the word &quot;likelihood&quot;.<br><br>    -----<br><br>    Answer the question based on the above context: {query}<br>    &quot;&quot;&quot;<br><br>    return PROMPT_TEMPLATE<br><br>PROMPT_TEMPLATE = get_prompt_template(query, retrieved_knowledge)</pre><p><strong>11. Generate response using llama-3 Model with Groq API</strong></p><p>We call the Groq API with the prompt and generate the diagnosis only based on our saved knowledge base:</p><pre>from groq import Groq<br><br># Generate diagnosis using Grok and llama<br>def generate_diagnosis(PROMPT_TEMPLATE, api_key):<br><br>    client = Groq(api_key=api_key)<br>    completion = client.chat.completions.create(<br>                                                model=&quot;llama-3.1-8b-instant&quot;,<br>                                                messages=[{&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You answer only based on the provided JSON data.&quot;},<br>                                                        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: PROMPT_TEMPLATE}],<br>                                                temperature=0,<br>                                                max_completion_tokens=1024,<br>                                                top_p=1,<br>                                                stream=True,<br>                                                stop=None,<br>                                            )<br>    <br>    return completion<br><br># Generate diagnosis using llama-3<br>diagnosis = generate_diagnosis(PROMPT_TEMPLATE, groq_api_key)<br>diagnosis_text = &quot;&quot;.join([chunk.choices[0].delta.content or &quot;&quot; for chunk in diagnosis])<br><br># As medical diagnose we would like to make sure that the diagnose is confirmed later by medical professiosnals.<br>diagnosis_text += &quot; However, possible further assessment with the doctors is recommended and diagnosis should be confirmed by a qualified medical professional.&quot;</pre><p><strong>12. Let’s wrap the complete code under FastAPI endpoint</strong></p><pre>from fastapi import FastAPI, HTTPException<br>from typing import Dict<br><br># FastAPI Endpoint<br>@app.post(&quot;/diagnose_parkinson/&quot;)<br>async def analyze_gait(data: Dict):<br>    try:<br><br>        # Extract gait features<br>        gait_data = extract_gait_features(data)<br><br>        # Generate query based on gait data<br>        query = generate_query(gait_data)<br>        query += &quot;What does this indicate about Parkinson&#39;s disease?&quot;<br><br>        # load the vector_store db<br>        vector_store = load_chroma(CHROMA_PATH=CHROMA_PATH, embedding_model=embedding_model)<br><br>        # Retrieve relevant knowledge<br>        retrieved_knowledge, retrieved_sources = retrieve_knowledge(query, vector_store)<br><br>        # get prompt template<br>        PROMPT_TEMPLATE = get_prompt_template(query=query, retrieved_knowledge=retrieved_knowledge)<br><br>        # Generate diagnosis using GPT<br>        diagnosis = generate_diagnosis(PROMPT_TEMPLATE, groq_api_key)<br>        diagnosis_text = &quot;&quot;.join([chunk.choices[0].delta.content or &quot;&quot; for chunk in diagnosis])<br>        diagnosis_text += &quot; However, possible further assessment with the doctors is recommended and diagnosis should be confirmed by a qualified medical professional.&quot;<br><br>        payload =  {<br>                &quot;query&quot;: query,<br>                &quot;retrieved_sources&quot;: retrieved_sources,<br>                &quot;retrived_content&quot;: retrieved_knowledge,<br>                &quot;diagnosis&quot;: diagnosis_text.replace(&quot;\n&quot;, &quot; &quot;)<br><br>            }<br>        <br>        return payload[&quot;diagnosis&quot;]</pre><p>Let’s send the request to the local API,</p><pre>import requests<br>import json<br><br># local API<br>url = &quot;http://127.0.0.1:8000/diagnose_parkinson/&quot;<br><br># Read the saved gait analysis data<br>file_path = &quot;parkinson_patient_data.json&quot;<br>with open(file_path, &quot;r&quot;) as f:<br>    gait_data = json.load(f)<br><br># Send POST request<br>response = requests.post(url, json=gait_data)<br><br># Print response<br>if response.status_code == 200:<br>    print(response.json())<br>else:<br>    print(response.status_code)</pre><p><strong>Diagnosis Result:</strong></p><pre># Diagnosis_result<br><br>The patient&#39;s gait analysis results indicate several abnormalities that are <br>relevant to Parkinson&#39;s disease. The patient&#39;s Gait Speed of 1.5 km/h is <br>significantly lower than the reference range for healthy patients, which <br>is between 2 km/h and 5 km/h. This suggests a slower gait speed, which is a <br>common feature in Parkinson&#39;s disease patients.  The patient&#39;s Cadence of <br>80 Steps/min is also lower than the reference range for healthy patients, <br>which is between 90 Steps/min and 115 Steps/min. This indicates a reduced <br>walking speed, which is consistent with Parkinson&#39;s disease.  The patient&#39;s <br>Double Support Left and Double Support Right are both higher than the <br>reference range for healthy patients, indicating a longer time spent with <br>both feet on the ground. This is a characteristic feature of Parkinson&#39;s <br>disease, where patients often experience difficulty with gait initiation <br>and termination.  The patient&#39;s Step Length Left and Step Length Right are <br>both shorter than the reference range for healthy patients, which is between <br>55 cm and 80 cm. This suggests a reduced stride length, which is a common <br>feature in Parkinson&#39;s disease patients.  The patient&#39;s Gait Variability Left <br>and Gait Variability Right are both higher than the reference range for <br>healthy patients, indicating increased variability in gait patterns. <br>This is consistent with Parkinson&#39;s disease, where patients often experience <br>difficulty with gait coordination and stability.  <br>The patient&#39;s Stance Time Left and Stance Time Right are both higher than the <br>reference range for healthy patients, indicating a longer time spent in the <br>stance phase of gait. This is a characteristic feature of Parkinson&#39;s disease, <br>where patients often experience difficulty with gait initiation and <br>termination.  Considering these abnormalities, the likelihood of Parkinson&#39;s <br>disease is moderate. However, possible further assessment with the doctors is <br>recommended and diagnosis should be confirmed by a qualified medical <br>professional.</pre><p>It clearly identifies the abnormalities in the gait parameters and based on our saved knowledge based document, gives the correct diagnosis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*t4TeoT64fhez15TZAIfz8Q.png" /></figure><h3><strong>End-to-End Implementation</strong></h3><p><a href="https://github.com/sanghvirajit/Healthcare-Diagnostics-RAG">https://github.com/sanghvirajit/Healthcare-Diagnostics-RAG</a></p><p>If you’re interested in exploring the detailed implementation, here’s the <a href="https://github.com/sanghvirajit/Healthcare-Diagnostics-RAG">GitHub link</a>, you can clone and run it locally. Follow the instructions mentioned in the <em>README.md</em> file.</p><h3>Conclusion</h3><p>By leveraging Retrieval-Augmented Generation (RAG), this system ensures that AI-driven diagnostics are precise, transparent, and firmly rooted in real medical knowledge. Integrating ChromaDB for retrieval and LLaMA-3 for response generation enhances diagnostic accuracy while minimizing AI hallucinations. This approach marks a significant advancement in AI-powered healthcare, delivering reliable assessments that improve patient outcomes.</p><p>To further refine results, GraphRAG — combining Knowledge Graphs with RAG — should be implemented for even more structured and context-aware diagnostics.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2befddfe5859" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Complete Guide to Adam and RMSprop Optimizer]]></title>
            <link>https://medium.com/analytics-vidhya/a-complete-guide-to-adam-and-rmsprop-optimizer-75f4502d83be?source=rss-768086a2598c------2</link>
            <guid isPermaLink="false">https://medium.com/p/75f4502d83be</guid>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Sanghvirajit]]></dc:creator>
            <pubDate>Sat, 20 Feb 2021 15:47:31 GMT</pubDate>
            <atom:updated>2025-03-14T07:54:18.832Z</atom:updated>
            <content:encoded><![CDATA[<h3>Everything you need to know about Adam and RMSprop Optimizer</h3><p>Starting from the algorithm to its implementation.</p><p>Optimization is a mathematical discipline that determines the “best” solution in a quantitatively well-defined sense. Mathematical <a href="https://www.analyticsvidhya.com/blog/2021/10/a-comprehensive-guide-on-deep-learning-optimizers/?utm_source=Backlink&amp;utm_medium=SEO"><strong>optimization</strong></a> of the processes governed by partial differential equations has seen considerable progress in the past decade, and since then it has been applied to a wide variety of disciplines e.g., science, engineering, mathematics, economics, and even commerce. Optimization theory provides algorithms to solve well-structured optimization problems along with the analysis of those algorithms. A typical optimization problem includes an objective function that is to be minimized or maximized with the given constraints. Optimization theory provides algorithms to solve well-structured optimization problems along with the analysis of those algorithms. Optimization algorithms in <a href="https://www.analyticsvidhya.com/blog/2022/01/machine-learning-algorithms/?utm_source=Backlink&amp;utm_medium=SEO"><strong>machine learning</strong></a> (especially in neural networks) aim at minimizing an objective function (generally called loss or cost function), which is intuitively the difference between the predicted data and the expected values</p><p>Stochastic gradient-based optimization is of core practical importance in many fields of science and engineering. Many problems in these fields can be cast as the optimization of some scalar parameterized objective function requiring maximization or minimization with respect to its parameters. Gradient descent is an optimization algorithm that uses the gradient of the objective function to navigate the search space. Several optimization algorithms based on gradient descent exist in the literature, but just to name a few the classification of Gradient descent optimization algorithms goes as follows,</p><h3><strong>First-order optimization algorithm</strong></h3><p>first-order methods use the first derivatives of the function to minimize.</p><ol><li>Momentum</li><li>Nesterov accelerated gradient</li><li>Adagrad</li><li>Adadelta</li><li><strong>RMSprop</strong></li><li><strong>Adam</strong></li><li>Adamax</li><li>Nadam</li><li>AMSGrad</li></ol><h3><strong>Second-order optimization algorithm</strong></h3><p>second-order methods make use of the estimation of the Hessian matrix (second derivative matrix of the loss function with respect to its parameters).</p><ol><li>Newton method</li><li>Conjugate gradient</li><li>Quasi-Newton method</li><li>Levenberg-Marquardt algorithm.</li></ol><p>In this article, we will go through the Adam and RMSprop starting from its algorithm to its implementation in python, and later we will compare its performance.</p><blockquote>Let J(θ) be a function parameterized by a model’s parameters θ ∈ Rn, sufficiently differentiable of which one seeks a minimum. The gradient method builds a sequence that should in principle approach the minimum. For this, we start from any value x_0 (a random value for example) and we construct the recurrent sequence by:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/292/1*YmInlbwUxWNyBtf7cHBmDg.png" /></figure><p>where η is the learning rate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*SzzUmvSQZY-U4eq5RuFbxw.png" /><figcaption>Python code for Gradient Descent</figcaption></figure><p>In a normal stochastic gradient descent algorithm, we fixed the value of the learning rate for all the recurrent sequences hence, it results in slow convergence. For <strong>adaptive methods like Adam and RMSprop</strong>, the learning rate is variable for each parameter. This method is ensured to converge, even if the input sample is not linearly separable, to a minimum of the error function for a well-chosen learning rate.</p><p>Although <strong>ADAGRAD </strong>works well for sparse settings, its performance has been observed to deteriorate in settings where the loss functions are nonconvex and gradients are dense due to the rapid decay of the learning rate in these settings since it <strong>uses all the past gradients in the update</strong>. To tackle this issue, several variants of the ADAGRAD, such as <strong>RMSprop, ADAM, ADADELTA</strong>, etc have been proposed which mitigate the rapid decay of the learning rate using the <strong>exponential moving averages of squared past gradients</strong>, essentially limiting the reliance of the update to only the past few gradients.</p><h3>RMSprop Optimizer</h3><p>RMSprop is a gradient-based optimization technique used in training neural networks. It was proposed by the father of back-propagation, Geoffrey Hinton. Gradients of very complex functions like neural networks have a tendency to either vanish or explode as the data propagates through the function (refer to vanishing gradients problem). Rmsprop was developed as a stochastic technique for mini-batch learning.</p><p>RMSprop deals with the above issue by using a moving average of squared gradients to normalize the gradient. This normalization balances the step size (momentum), decreasing the step for large gradients to avoid exploding and increasing the step for small gradients to avoid vanishing.</p><p>Simply put, RMSprop uses an adaptive learning rate instead of treating the learning rate as a hyperparameter. This means that the learning rate changes over time.</p><p>RMSprop’s update rule:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*bdgqAQdEgpyBZscgNgx6hQ.png" /><figcaption>RMSprop optimizer’s update rule</figcaption></figure><h3><strong>Let’s code in Python</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*AP6HZfwrf_QcEGNdjEoS_A.png" /><figcaption>Python code for RMSprop</figcaption></figure><h3><strong>ADAM optimizer</strong></h3><p>Adam (Kingma &amp; Ba, 2014) is a first-order-gradient-based algorithm of stochastic objective functions, based on adaptive estimates of lower-order moments. Adam is one of the latest state-of-the-art optimization algorithms being used by many practitioners of machine learning. The first moment normalized by the second moment gives the direction of the update.</p><p>Adam’s update rule:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*dAIl_nEgxW0H2GafvqSgEQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*FnZxx-ssZXdTzIuuf1NEzw.png" /><figcaption>Figure 1: ADAM algorithm [1]</figcaption></figure><h3><strong>Let’s code in Python</strong></h3><p>The python code of the ADAM optimizer will look as follow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*2nVzuQ8oxaUSXQTDQG9xKw.png" /><figcaption>Python code for ADAM optimizer</figcaption></figure><p>Yeah, it’s that simple.</p><h3><strong>Results</strong></h3><p>I have coded the neural network from scratch and have implemented the above optimizers. This neural network was then trained for 100 epochs on the MNIST handwritten dataset which contains 60,000 training examples and 10,000 testing examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*sokR20ZAyFDTNm9WGrnoPA.png" /><figcaption>Figure 2: Results</figcaption></figure><h3>Extension</h3><p>Just for reference, I have also implemented the Adamax optimizer which is an extension to the Adam optimizer, as you can see from the results. If you are more interested in the implementation of Adamax, I recommend the readers to read the paper <a href="https://arxiv.org/pdf/1412.6980.pdf">Diederik P. Kingma, Jimmy Lei Ba. ADAM: A METHOD FOR STOCHASTIC OPTIMIZATION.</a></p><h3><strong>Conclusion</strong></h3><p>In this article, we have seen simple and computationally efficient algorithms for gradient-based optimization. We have seen how the RMSprop and ADAM optimizers are straightforward and easy to implement. The experiments confirm the analysis of the rate of convergence in convex problems.</p><p>If you are more interested to look at the detailed code of the neural network from scratch, you can find that on my Github.</p><p><a href="https://github.com/sanghvirajit/Feedforward_Neural_Network">https://github.com/sanghvirajit/Feedforward_Neural_Network</a></p><h3><strong>References</strong></h3><p>[1] <a href="https://arxiv.org/pdf/1412.6980.pdf">Diederik P. Kingma, Jimmy Lei Ba. ADAM: A METHOD FOR STOCHASTIC OPTIMIZATION.</a></p><p>[2] <a href="https://docs.google.com/viewer?url=http%3A%2F%2Fwww.cs.toronto.edu%2F~hinton%2Fcoursera%2Flecture6%2Flec6.pdf">Tieleman, T. and Hinton, G. Lecture 6.5 — RMSProp, COURSERA: Neural Networks for Machine Learning. Technical report, 2012.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75f4502d83be" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/a-complete-guide-to-adam-and-rmsprop-optimizer-75f4502d83be">A Complete Guide to Adam and RMSprop Optimizer</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applied Computational Thinking Using Python — Multigrid methods]]></title>
            <link>https://medium.com/analytics-vidhya/applied-computational-thinking-using-python-multigrid-methods-64c86113e60b?source=rss-768086a2598c------2</link>
            <guid isPermaLink="false">https://medium.com/p/64c86113e60b</guid>
            <category><![CDATA[computation]]></category>
            <category><![CDATA[computational-thinking]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sanghvirajit]]></dc:creator>
            <pubDate>Sat, 13 Feb 2021 11:06:19 GMT</pubDate>
            <atom:updated>2021-02-14T17:21:51.361Z</atom:updated>
            <content:encoded><![CDATA[<h3>Applied Computational Thinking Using Python — Multigrid Methods</h3><p>Imagine you were asked to solve a complex problem, and to make things even more challenging you cannot use the tools you normally be using, which helps to solve the problem. Sounds challenging right? Without the strategy, it certainly would be. Here in this article, we will talk about the common process for solving complex problems, know as computational thinking.</p><p>Computational thinking is the thought process involved in formulating the problem and expressing its solution in such a way that computer, human, or machine can effectively carry it out.</p><p>Computational thinking involves 4 basic steps,</p><ol><li><strong>Decomposition </strong>— Breaking down a complex problem into a smaller problem.</li><li><strong>pattern recognition</strong> — looking for similarities among and within problems.</li><li><strong>Abstraction — </strong>Only focusing on what we actually need, and ignoring the irrelevant pieces of information.</li><li><strong>Algorithms</strong> — Developing the computer code to solve the problem.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*rr2dugDcOJ-X-l6lzpGV5A.png" /><figcaption>Figure 1: Computational Thinking</figcaption></figure><p>This approach of computational thinking will be useful when you’re not just looking for any answer to a problem, but the best answer to the problem.</p><h3><strong>Multigrid Methods</strong></h3><p>In this article, to understand the approaches in computational thinking we will walk through the concept of multigrid methods. We will solve the Laplacian problem, using multigrid methods in a very simple way.</p><p>Let’s start with the single grid and then later we will move to multigrid</p><h3><strong>Physical Problem</strong></h3><p>Let’s consider the 2D physical problem shown in Figure 1, and we want to solve the Laplacian equation on this problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/1*a7iyyMAMWLBG3klhJx8G7w.png" /><figcaption>Figure 2: Physical problem</figcaption></figure><p>Laplacian equation is given by,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*AUTp_Z6k2M8O5hqbIMXEyg.png" /></figure><p>setting,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*2mt9el9Yt3kiuZCBK2MYew.png" /></figure><p>Discretization of Laplace equation will look like,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*snhh2YLS7ycbv1-_vox7xg.png" /></figure><p>Jacobi iterative method to solve the above problem takes the following form,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*ORhYfcPy7t38ODuevKCkfQ.png" /></figure><h3><strong>Let’s code in Python</strong></h3><p><strong>Boundary conditions</strong></p><p>We will discretize the domain in the (n x n) grid and apply the boundary conditions. For our example, we will carry out the simulation on four different grids (32 x 32), (64 x 64), (128 x 128), and (256 x 256).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*IvgkxMpr_jis2aTiRSg2KQ.png" /></figure><p>We will use the heatmap for the visualization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*8PhVYRbuk60MxFutsxjv3Q.png" /><figcaption>Figure 3: Grid with Boundary conditions</figcaption></figure><p>Let’s code Jacobi steps,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*9gEeGoXqQFZxofhGOV8SNw.png" /></figure><p>Simulation (Jacobi step) was allowed to run for four different grids until the simulation criteria were satisfied.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*aJIc-MqtRViAU1zmnjQo-g.png" /><figcaption>Figure 4: Simulation result on 32x32 grid (Left) and on 64x64 grid (Right)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*kO_1QMI7xY625DW7QHHKig.png" /><figcaption>Figure 5: Simulation result on 128x128 grid (Left) and on 256x256 grid (Right)</figcaption></figure><p>Results were as follow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*B7sFreaQq_QKyIPgbTZDyQ.png" /><figcaption>Table 1: Simulation results</figcaption></figure><p>As we can clearly see from Figures 4 and 5, the solution gets smoother and smoother with the grid getting finer and finer but at the expense of the computational time. It’s also clear that the simulation converges faster on the coarser grid when compares to the finer grid. <strong>Hence, now the question is, is there any way we can reduce the computational time and still get accurate results as we got on the finer grid</strong>? <strong>This is nothing but computational thinking</strong>. To answer this question, What if we run the simulation on the coarser grid and then map the results on the finer grid? Will that work? Let’s see!</p><h3><strong>Multigrid Methods</strong></h3><p>In numerical analysis, a <strong>multigrid method</strong> (<strong>MG method</strong>) is an algorithm for solving differential equations using a hierarchy of discretizations. The main idea of multigrid is to accelerate the convergence of a basic iterative method. There are many choices of multigrid methods with varying trade-offs between the speed of solving a single iteration and the rate of convergence with said iteration. The 3 main types are V-Cycle, F-Cycle, and W-Cycle. In this article, we will use V-Cycle.</p><p>Following are the basic steps in multigrid methods,</p><ol><li>Discretise the domain with the finer grid.</li><li>Run Jacobi for few iterations. (Pre- Smoothing)</li><li>Interpolate the results from the fine grid to a coarser grid. (Restriction)</li><li>Run the simulation until convergence.</li><li>Interpolate the results back to the finer grid. (Prolongation)</li><li>Run Jacobi again for few iterations (Post-smoothing)</li></ol><p>Let’s see how the python code for the Restriction and Prolongation looks like,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*yn7RVCkLTFaKp4Z-eoVE8A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*Lcd8Lb6S2YMb3H0uoLnWLQ.png" /></figure><p>For our 2D discrete problem, we will start with discretizing the domain with (256 x 256) grid, run the simulation for few iterations, interpolate the results to the coarser grid (128 x 128), run the simulation until convergence, interpolate the results back to the finer grid (256 x 256), and finally run the simulation again for few iterations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n4CVqRrfeVdFx9Eaa7g--w.png" /><figcaption>Figure 6: Multigrid V-cycle</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*smxhedHLSHFeZ_lNSo35QA.png" /><figcaption>Figure 7: Simulation result from the single grid (Left) and multigrid(Right)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*fFu_YfBp6llk2M-ysngwAw.png" /><figcaption>Table 2: Simulation results</figcaption></figure><p>We can see from Table 2, how the simulation runtime was decreased. In this article, we saw, how we can use computational thinking to speedup computational runtime, and together with that we also saw how the multigrid methods for solving differential equations works.</p><p>This article was inspired by the Computational Thinking course from MIT.</p><p>If you are more interested to look at the detailed code, please check it out on my GitHub <a href="https://github.com/sanghvirajit19/Laplacian_Problem/tree/working">https://github.com/sanghvirajit19/Laplacian_Problem/tree/working</a></p><h3><strong>Conclusion</strong></h3><p>Hence, we as computational thinkers, it’s in our hands how to approach any complex problem where we get the best results by using less computational resources.</p><h3><strong>Reference</strong></h3><p>[1] <a href="https://computationalthinking.mit.edu/Fall20/lecture18/">Lecture 18 — Hierarchical Thinking, Greedy Algorithms, Jacobi’s Method, and Multigrid</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64c86113e60b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/applied-computational-thinking-using-python-multigrid-methods-64c86113e60b">Applied Computational Thinking Using Python — Multigrid methods</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Vectorization of Convolution Layer in Convolution Neural Networks (CNNs)]]></title>
            <link>https://medium.com/analytics-vidhya/on-vectorization-of-convolution-layer-in-convolution-neural-networks-cnns-1775cb4d30e6?source=rss-768086a2598c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1775cb4d30e6</guid>
            <category><![CDATA[vectorization]]></category>
            <category><![CDATA[deeplearing]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[convolution-neural-net]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sanghvirajit]]></dc:creator>
            <pubDate>Sun, 07 Feb 2021 16:34:51 GMT</pubDate>
            <atom:updated>2021-02-10T16:48:40.165Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Unboxing the black box!</strong></p><p>In deep learning, a <strong>convolutional neural network</strong> (<strong>CNN</strong>, or <strong>ConvNet</strong>) is a class of deep neural networks. CNN&#39;s have shown a remarkable state of the art performance in many applications such as in image and video recognition, recommender systems, image classification, image segmentation, medical image analysis, and natural language processing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4OUonEDfZwCfR4Y-G-h1fw.jpeg" /><figcaption><strong>Figure 1: Convolution Neural Network.</strong></figcaption></figure><p><strong>In this article, I will only focus on vectorizing the single convolution layer and not the whole convolution neural network.</strong></p><h3><strong>Convolution Operation</strong></h3><p>In image processing, a kernel, convolution matrix, or mask is a small matrix. It is used for blurring, sharpening, embossing, edge detection, and more. This is accomplished by doing a convolution between a kernel and an image.</p><p>A convolution is a type of matrix operation, consisting of a kernel, a small matrix of weights, that slides over input data performing element-wise multiplication with the part of the input it is on, then summing the results into an output.</p><p>In mathematics (in particular, functional analysis), <strong>convolution</strong> is a mathematical operation on two functions (<em>f</em> and <em>g</em>) that produces a third function ( f * g ) that expresses how the shape of one is modified by the other The general form for matrix convolution is given by,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*bIEB-CraOh2Rl-mr0bNurA.png" /><figcaption><strong>Figure 2: Matrix convolution [1].</strong></figcaption></figure><p>Intuitively, a convolution allows for weight sharing — reducing the number of effective parameters — and image translation (allowing for the same feature to be detected in different parts of the input space).</p><p>The 3D visualization of the convolution operation can be seen as follow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*_4QUitzCqzEqMhz5eK_Lzg.png" /><figcaption><strong>Figure 3: 3D visualization of the convolution operation.</strong></figcaption></figure><p>Depending upon the type of the kernel, the different features from the input image can be extracted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*ol3n2FaVPoxVgXbEgBtEvA.png" /><figcaption><strong>Figure 4: Common Filters [1].</strong></figcaption></figure><p>The output of the convolution operation is generally termed as <strong>“Feature maps”</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*wvwT9LlHcDfexIiv7fWWxA.gif" /><figcaption><strong>Figure 5: Convolved Feature.</strong></figcaption></figure><p>This convolution operation is the backbone of the convolution neural networks.</p><h3><strong>Let’s code Convolution Operation</strong></h3><p>Python code for the simple convolution operation for a single input image will look something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*SuPvzC2Ol0UrjItlXGlx5A.png" /><figcaption><strong>Figure 6: Code for Convolution operation for a single Input image.</strong></figcaption></figure><p>The above operation will give the following results,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*CJ7BATm-1UO1lchdXOOFJQ.png" /><figcaption><strong>Figure 7: Original Image (Left) and Gaussian Blur (Right).</strong></figcaption></figure><p>But the above implementation will perform the convolution on only one input image, <strong>what if we have multiple images inputted into the network (e.g mini-batch)</strong>? Obviously, the simplest thing that will come to mind is to simply loop the above operation all over the mini-batch. Assuming a 2D back and white image, it has 1 channel (e.g (28, 28, 1)) in comparison to the colored image that has 3 channels (RGB) (e.g (28, 28, 3)). Hence, the code for the above settings will look something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*5SsRxBxDPU8ewWOQFiNQqQ.png" /><figcaption><strong>Figure 8: Code for Convolution operation for multiple Input images.</strong></figcaption></figure><p>Let’s apply this to the handwritten digits — MNIST dataset. MNIST dataset consists of 60,000 training images of 28 x 28. The input size will be (60000, 28, 28, 1). After applying the convolution layer with a kernel size of 3 x 3 x 1 and with 32 filters (Output Channel), the output of the layer will be (60000, 26, 26, 32).</p><p>The output of the convolution layer with 32 different filters will look something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mzhBgABeMq2rHdRqPDGm9Q.png" /><figcaption><strong>Figure 9: Output of the Convolution Layer with 32 filters.</strong></figcaption></figure><p>But the above method of implementation will work <strong>very slow</strong> considering the looping over all the input images (60000 images for MNIST) in the dataset. Hence, the above method is not the most prominent one and the <strong>vectorization method is the key</strong>, in order to better understand and facilitate the parallel implementation. Key steps in training and testing deep CNNs are abstracted as matrix and vector operators, upon which parallelism can be easily achieved.</p><p>Considering the <strong>mini-batch of 5000 images</strong>, the above implementation takes <strong>658.169 Sec. </strong>As the number of input images increases, the runtime also increases exponentially.</p><h3>Strategy to vectorize convolution</h3><p>Vectorization refers to the process that transforms the original data structure into a vector representation so that the scalar operators can be converted into a vector implementation.</p><p>Considering the input image of 3 x 3 with 3 different filters of 2 x 2, during the convolution operation each filter will run over the same input image and will result in an output feature map of 2 x 2. Hence, 3 different filters will result in 3 “Feature maps”. There is a way to vectorize this operation.</p><p>Steps to vectorize the above operation includes,</p><ol><li>Convert all kernels/ filters to rows and get a kernel matrix.</li><li>Split your input (image) into slices for convolution then convert to columns and get an input matrix. You can append other inputs (images) to form a mini-batch</li><li>multiply input matrix with the kernels matrix. In the result matrix, each row is one feature map.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*99_1GwFpT5TVvK8RlVPmKA.png" /><figcaption><strong>Figure 10: Strategy to vectorize convolution. Illustration of the way to convolve a 3x3 input image with three 2x2 kernels and generates three 2x2 feature maps [2].</strong></figcaption></figure><p>A similar strategy was used for implementation and the code for the input size of (5000, 28, 28, 1) for 32 different filters which result in the output feature map of (5000, 26, 26, 32) will look something like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bAQpbkbadDUYhqoI2daihQ.png" /><figcaption><strong>Figure 11: Flattening the input image</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*3E9f7-7MASyjmGlOhFlVKw.png" /><figcaption><strong>Figure 12: Code for Vectorizing the Convolution Operation for multiple input images.</strong></figcaption></figure><p>The above implementation resulted in similar output but now the implementation runtime was reduced to only <strong>1.511 Sec</strong>,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CCbLBX43HnloPgfD9RH5lA.png" /><figcaption><strong>Figure 13: Output of vectorized Convolution Layer with 32 filters.</strong></figcaption></figure><p>For further details, I recommend the readers to have a look at the paper <strong>“On Vectorization of Deep Convolutional Neural Networks for Vision Tasks” by Jimmy SJ. and Ren Li Xu.</strong></p><h3><strong>Summary</strong></h3><p>In this article, I elaborated on how the vectorization of the Convolution Layer in Deep Convolution Neural Networks (CNNs) works. Vectorization is the key to reduce the total run time.</p><h3><strong>References</strong></h3><p>[1] https://en.wikipedia.org/wiki/Kernel_(image_processing)</p><p>[2] <a href="https://en.wikipedia.org/wiki/Convolutional_neural_network">https://en.wikipedia.org/wiki/Convolutional_neural_network</a></p><p>[3] <a href="http://lxu.me/mypapers/vcnn_aaai15.pdf">Jimmy SJ. Ren Li Xu. On Vectorization of Deep Convolutional Neural Networks for Vision Tasks.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1775cb4d30e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/on-vectorization-of-convolution-layer-in-convolution-neural-networks-cnns-1775cb4d30e6">On Vectorization of Convolution Layer in Convolution Neural Networks (CNNs)</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>