profile

Machine Learning for Medical Imaging

LMFlow: Finetune LLMs Easily

Published 11 months agoΒ β€’Β 2 min read

Hello Reader,

Welcome to another edition of PYCAD newsletter where we cover interesting topics in Machine Learning and Computer Vision applied to Medical Imaging. The goal of this newsletter is to help you stay up-to-date and learn important concepts in this amazing field! I've got some cool insights for you below ↓

​

LMFlow

Have you heard of LMFlow?

It’s a framework that allows you to easily finetune open source large language models on your own datasets!

Here are the key features that are supported by the toolkit:

  • Continous pretraining, instruction tuning and RLHF on user-defined datasets.
  • Simple and extensible APIs for developers.
  • Efficient tuning with low-rank adaptation (LoRA).
  • A novel RLHF algorithm RAFT (Reward rAnked FineTuning) to simplify RLHF pipeline for generative models.
  • A simplified model inference framework.

Below you can see the overall system design of LMFlow.

Note: LMFlow is not to be mixed with MLFlow, which is an MLOps framework.

More on LMFlow in the original paper and their github repo.

​

How to go from Mathematical Modeling to a Neural Network Model

Most machine learning research is about going from mathematical modeling to ML model implementation. Here’s how to go from conditional probability to a neural architecture.
​
Let's start by defining a simple conditional probability problem. Consider a supervised learning task where we have input data X and target data Y, and we want to model the conditional probability P(Y | X), meaning the probability of Y given X.
​
A common way to model this in machine learning is to assume that this probability follows some parametric form and then use the data to estimate the parameters of this model.
​
For instance, we could assume that P(Y | X) is a Gaussian distribution with mean Β΅(X) and standard deviation Οƒ(X). This mean Β΅(X) and standard deviation Οƒ(X) could be any functions of X, but in order to learn them from data, we often assume they can be parameterized with some parameters ΞΈ, and are differentiable with respect to these parameters.
​
This is where neural networks come in. A neural network is just a function approximator that's highly flexible and differentiable, making it suitable to represent these functions Β΅(X) and Οƒ(X).
​
Let's assume that our neural network is a simple feed-forward network with parameters ΞΈ. Then we can write our model as:
​
Β΅(X; ΞΈ) = NN_Β΅(X; ΞΈ)
Οƒ(X; ΞΈ) = NN_Οƒ(X; ΞΈ)
​
P(Y | X; ΞΈ) = N(Y; NN_Β΅(X; ΞΈ), NN_Οƒ(X; ΞΈ)^2)
​
Here, NN_Β΅ and NN_Οƒ are two neural networks which take the same input X and share the same parameters ΞΈ, and N is the Gaussian distribution. Their outputs represent the mean and standard deviation of the Gaussian distribution of Y given X.
​
To train this model, we would use a method called maximum likelihood estimation (MLE), which aims to find the parameters ΞΈ that maximize the likelihood of the observed data.
​
For our Gaussian model, this corresponds to minimizing the mean squared error between Y and NN_Β΅(X; ΞΈ).
​
Below, you can see how we might implement this in code using PyTorch.
​
In this code, we have a neural network that outputs two values for each input: a mean and a standard deviation. The loss function is defined as the negative log-likelihood of the Gaussian distribution, which we try to minimize using gradient descent.

Why would you want to understand this?

Because it will allow you to build intuition about how some of the powerful models in generative AI work. For example, variational autoencoders (VAEs) and stable diffusion process both have some level of conditional probability that is then modeled in the form of a neural network.


​

​

What'd you think of today's edition?

​

​

That's it for this week's edition, I hope you enjoyed it!

​

Machine Learning for Medical Imaging

by Nour Islam Mokhtari from pycad.co

πŸ‘‰ Learn how to build AI systems for medical imaging domain by leveraging tools and techniques that I share with you! | πŸ’‘ The newsletter is read by people from: Nvidia, Baker Hughes, Harvard, NYU, Columbia University, University of Toronto and more!

Read more from Machine Learning for Medical Imaging

Hi Reader,, Welcome to the PYCAD newsletter, where every week you receive doses of machine learning and computer vision techniques and tools to help you learn how to build AI solutions to empower the most vulnerable members of our society, patients. TotalSegmentator : Whole Body Segmentation at your Fingertips This free tool available online can do full body segmentation, it's called TotalSegmentator. I have already mentioned this tool in a previous edition of the newsletter, but in this...

9 days agoΒ β€’Β 3 min read

Hello Reader, Welcome to another edition of PYCAD newsletter where we cover interesting topics in Machine Learning and Computer Vision applied to Medical Imaging. The goal of this newsletter is to help you stay up-to-date and learn important concepts in this amazing field! I've got some cool insights for you below ↓ A Medical Imaging Expert Told Me This Recently I saw a post on LinkedIn where a medical imaging expert showcased his work of segmenting the lungs and its bronchial trees. You can...

16 days agoΒ β€’Β 2 min read

Hello Reader, Welcome to another edition of PYCAD newsletter where we cover interesting topics in Machine Learning and Computer Vision applied to Medical Imaging. The goal of this newsletter is to help you stay up-to-date and learn important concepts in this amazing field! I've got some cool insights for you below ↓ How we helped accelerate inference time for a client's AI product Below is a screenshot of a benchmark we did for a client of ours. The goal was to accelerate inference time. This...

22 days agoΒ β€’Β 3 min read
Share this post