Bakery Inquiry Model Tuned With Gemma
Copyright 2024 Google LLC.
Fine-tune Gemma models using LORA for Cake Boss Example
Adding additional changes based on feedback
Setup
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 572.2/572.2 kB 5.4 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.2/5.2 MB 15.4 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 13.3 MB/s eta 0:00:00
Select a backend
Keras is a high-level, multi-framework deep learning API designed for simplicity and ease of use. Using Keras 3, you can run workflows on one of three backends: TensorFlow, JAX, or PyTorch.
For this tutorial, configure the backend for JAX.
Import packages
Import Keras and KerasNLP.
Load Dataset
Load Model
KerasNLP provides implementations of many popular model architectures. In this tutorial, you'll create a model using GemmaCausalLM, an end-to-end Gemma model for causal language modeling. A causal language model predicts the next token based on previous tokens.
Create the model using the from_preset method:
Cake prompt
This is from the untuned model. The results aren't exactly what we'd like
From the following get the type of inquiry, (order or request for information), filling, flavor, size, and pickup location and put it into a json
Hi,
I'd like to order a red velvet cake with custard filling. Please make it 8 inch round
and pick it up from the bakery on 22nd street.
Thanks!
```json
{
"inquiry_type": "order",
"filling": "custard",
"flavor": "red velvet",
"size": "8 inch round",
"pickup_location": "22nd street bakery"
}
```
```json
{
"inquiry_type": "request",
"filling": "custard",
"flavor": "red velvet",
"size": "8 inch round",
"pickup_location": "22nd street bakery"
}
```
```json
{
"inquiry_type": "order",
"filling": "custard",
"flavor": "red velvet",
"size": "8 inch round",
"pickup_location": "22nd street bakery"
{'prompt': "\nI'd like four jellabi and three gulab Jamun.\n",
, 'response': {'type': 'order',
, 'items': [{'name': 'Jellabi', 'quantity': 4},
, {'name': 'Gulab Jamun', 'quantity': 3}]}} {'prompt': '\nDo you sell strawberry cakes with vanilla frosting with custard inside?\n',
, 'response': {'type': 'inquiry',
, 'items': [{'name': 'cake',
, 'filling': 'custard',
, 'frosting': 'vanilla',
, 'flavor': 'strawberry',
, 'size': 'null'}]}} LoRA Fine-tuning
The LoRA rank determines the dimensionality of the trainable matrices that are added to the original weights of the LLM. It controls the expressiveness and precision of the fine-tuning adjustments.
A higher rank means more detailed changes are possible, but also means more trainable parameters. A lower rank means less computational overhead, but potentially less precise adaptation.
This tutorial uses a LoRA rank of 4. In practice, begin with a relatively small rank (such as 4, 8, 16). This is computationally efficient for experimentation. Train your model with this rank and evaluate the performance improvement on your task. Gradually increase the rank in subsequent trials and see if that further boosts performance.
Be careful for over or underfit
- Rank
- Learning Rate
Note that enabling LoRA reduces the number of trainable parameters significantly (from 2.6 billion to 2.9 million).
Epoch 1/3 8/8 ━━━━━━━━━━━━━━━━━━━━ 83s 6s/step - loss: 0.7486 - sparse_categorical_accuracy: 0.6278 Epoch 2/3 8/8 ━━━━━━━━━━━━━━━━━━━━ 42s 2s/step - loss: 0.5113 - sparse_categorical_accuracy: 0.6984 Epoch 3/3 8/8 ━━━━━━━━━━━━━━━━━━━━ 6s 770ms/step - loss: 0.3469 - sparse_categorical_accuracy: 0.7796
<keras.src.callbacks.history.History at 0x7c8dfc58b1c0>
Inference after fine-tuning
After fine-tuning, responses follow the instruction provided in the prompt.
Order Prompt
Hi, I'd like to order an 8 inch red velvet cake with custard filling
{'type': 'order', 'items': [{'name': 'cake', 'filling': 'custard', 'size': '8 inch', 'flavor': 'red velvet'}]}
Hi Indian Bakery Central,
I'd like to order one lemon cake that has vanilla filing, 2 gulab jamun and 1 penda
{'type': 'order', 'items': [{'name': 'lemon cake', 'filling': 'vanilla', 'quantity': '1'}, {'name': 'gulab jamun', 'quantity': '2'}, {'name': 'penda', 'quantity': '1'}]}
Hello, do you have 20 pendas and 10 ladoos? Also Can you make a chocolate cake with raspberry filling?
{'type': 'inquiry', 'items': [{'name': 'pendas', 'quantity': 20}, {'name': 'ladoos', 'quantity': 10}], 'order': {'cake': 'chocolate', 'filling': 'raspberry', 'size': 'null'}}
Summary and next steps
This tutorial covered LoRA fine-tuning on a Gemma model using KerasNLP. Check out the following docs next:
- Learn how to generate text with a Gemma model.
- Learn how to perform distributed fine-tuning and inference on a Gemma model.
- Learn how to use Gemma open models with Vertex AI.
- Learn how to fine-tune Gemma using KerasNLP and deploy to Vertex AI.
Run in Google Colab