avatarTracy Stengel

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4024

Abstract

n><span class="hljs-attr">"username"</span><span class="hljs-punctuation">:</span><span class="hljs-string">"your_username"</span><span class="hljs-punctuation">,</span><span class="hljs-attr">"key"</span><span class="hljs-punctuation">:</span><span class="hljs-string">"012345678abcdef012345678abcdef1a"</span><span class="hljs-punctuation">}</span></pre></div><h1 id="5aa2">Load Dataset</h1><p id="b5cb">Download the Databricks Dolly 15k dataset and preprocess it for fine-tuning. For demonstration purposes, we’ll use a subset of the data.</p><div id="32f1"><pre>!wget -O databricks-dolly-15k.jsonl https://huggingface.co/datasets/databricks/databricks-dolly-15k/resolve/main/databricks-dolly-15k.jsonl</pre></div><h1 id="caa3">Load Model</h1><p id="4aef">Instantiate the GemmaCausalLM model for fine-tuning from the preset architecture.</p><div id="3070"><pre>gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(<span class="hljs-string">"gemma_2b_en"</span>) gemma_lm.summary()</pre></div><h1 id="572f">Inference Before Fine-Tuning</h1><p id="45ea">Query the model with prompts to observe its responses before fine-tuning, assessing its baseline performance.</p><div id="5fb3"><pre>prompt = template.<span class="hljs-built_in">format</span>( instruction=<span class="hljs-string">"What should I do on a trip to Europe?"</span>, response=<span class="hljs-string">""</span>, ) sampler = keras_nlp.samplers.TopKSampler(k=<span class="hljs-number">5</span>, seed=<span class="hljs-number">2</span>) gemma_lm.<span class="hljs-built_in">compile</span>(sampler=sampler) <span class="hljs-built_in">print</span>(gemma_lm.generate(prompt, max_length=<span class="hljs-number">256</span>))</pre></div><div id="a6dc"><pre>Instruction: What should I do on a trip to Europe?

Response: It's easy, you just need to follow these steps:

First you must book your trip with a travel agency. Then you must choose a country and a city. Next you must choose your hotel, your flight, and your travel insurance And last you must pack for your trip.

What are the benefits of a travel agency?

Response: Travel agents have the best prices, they know how to negotiate and they can find deals that you won't find on your own.

What are the disadvantages of a travel agency?

Response: Travel agents are not as flexible as you would like. If you need to change your travel plans last minute, they may charge you a fee for that.

How do I choose a travel agency?

Response: There are a few things you can do to choose the right travel agent. First, check to see if they are accredited by the Better Business Bureau. Second, check their website and see what kind of information they offer. Third, look at their reviews online to see what other people have said about their experiences with them.

How does a travel agency make money?</pre></div><h1 id="3b79">LoRA Fine-Tuning</h1><p id="9bc6">Enable LoRA and fine-tune the model with the dataset, adjusting hyperparameters as needed.</p><div id="644e"><pre><span class="hljs-comment"># Enable LoRA for the model and set the LoRA rank to 4.</span> gemma_lm.backbone.enable_lora(rank=<span class="hljs-number">4</span>) gemma_lm.summary()</pre></div><div id="3fb1"><pre><span class="hljs-comment"># Limit the input sequence length to 512 (to control memory usage).</span> <span class="hljs-comment"># REDUCE THIS IF YOU RUN INTO ERRORS (its because your computer is weak)</span> gemma_lm.preprocessor.sequence_length <span class="hljs-punctuation">=</span> <span class="hljs-number">512</span> <span class="hljs-comment"># Use AdamW (a common optimizer for transformer models).</span> optimizer <span class="hljs-punctuation">=</span> keras.optimizers.AdamW<span class="hljs-punctuation">(</span> learning_rate<span class="hljs-punctuation">=</span><span class="hljs-number">5</span>e-<span class="hljs-number">5</span>, weight_decay<span class="hljs-punctuation">=</span><span class="hljs-number">0.01</span>, <span class="hljs-punctuation">)</span> <span class="hljs-comment

Options

"># Exclude layernorm and bias terms from decay.</span> optimizer.exclude_from_weight_decay<span class="hljs-punctuation">(</span>var_names<span class="hljs-punctuation">=</span><span class="hljs-punctuation">[</span><span class="hljs-string">"bias"</span>, <span class="hljs-string">"scale"</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">)</span>

gemma_lm.compile<span class="hljs-punctuation">(</span> loss<span class="hljs-punctuation">=</span>keras.losses.SparseCategoricalCrossentropy<span class="hljs-punctuation">(</span>from_logits<span class="hljs-punctuation">=</span><span class="hljs-literal">True</span><span class="hljs-punctuation">)</span>, optimizer<span class="hljs-punctuation">=</span>optimizer, weighted_metrics<span class="hljs-punctuation">=</span><span class="hljs-punctuation">[</span>keras.metrics.SparseCategoricalAccuracy<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">]</span>, <span class="hljs-punctuation">)</span> gemma_lm.fit<span class="hljs-punctuation">(</span>data, epochs<span class="hljs-punctuation">=</span><span class="hljs-number">1</span>, batch_size<span class="hljs-punctuation">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">)</span></pre></div><h1 id="f9b8">Inference After Fine-Tuning</h1><p id="2c82">Evaluate the model’s responses post-fine-tuning, observing any improvements in quality and relevance.</p><div id="8778"><pre>rompt = template.<span class="hljs-built_in">format</span>( instruction=<span class="hljs-string">"What should I do on a trip to Europe?"</span>, response=<span class="hljs-string">""</span>, ) sampler = keras_nlp.samplers.TopKSampler(k=<span class="hljs-number">5</span>, seed=<span class="hljs-number">2</span>) gemma_lm.<span class="hljs-built_in">compile</span>(sampler=sampler) <span class="hljs-built_in">print</span>(gemma_lm.generate(prompt, max_length=<span class="hljs-number">256</span>))</pre></div><div id="6e5e"><pre>Instruction: What should I do on a trip to Europe?

Response: If you have the time, I would visit London, Paris, Rome, and Berlin. If you're in London, you have to visit Buckingham Palace. If you're in Paris, you have to visit Notre Dame and the Eiffel Tower. If you're in Rome, you have to visit the Coliseum. If you're in Berlin, you have to visit the Brandenburg Gate.</pre></div><p id="3274">Fine-tuning Gemma models with LoRA offers a powerful approach to enhance NLP responses for specific tasks or domains. By leveraging this technique and appropriate datasets, you can customize Gemma models to suit your needs effectively. Experimentation with hyperparameters and training data size can further refine model performance. This tutorial serves as a starting point for utilizing Gemma models and LoRA fine-tuning in your NLP projects, opening up avenues for more accurate and contextually relevant text generation.</p><p id="7fce"><b>BONUS Full Code: For a Python Code Tutor Training Example <a href="https://colab.research.google.com/drive/1V0yAyt4Q88dOv_1GPgJp0vCXERE1moQU?usp=sharing"></a></b><a href="https://colab.research.google.com/drive/1V0yAyt4Q88dOv_1GPgJp0vCXERE1moQU?usp=sharing">https://colab.research.google.com/drive/1V0yAyt4Q88dOv_1GPgJp0vCXERE1moQU?usp=sharing</a></p><ul><li><i>Parts of this article were written using Generative AI</i></li><li>Subscribe/leave a comment if you want to stay up-to-date with the latest AI trends.</li><li>Earn <b>$25</b> and <b>4.60% APY</b> for <b>FREE</b> through my referral at <a href="https://www.sofi.com/invite/money?gcp=52bc84ca-74f5-404f-a669-74efac971248&amp;isAliasGcp=false">SoFi Bank Here</a></li></ul><p id="ebf0"><b><i>Plug: <a href="https://piusanalytics.gumroad.com/"></a></i></b><i><a href="https://piusanalytics.gumroad.com/">Checkout all my digital products on Gumroad here</a>.<b> </b>Please purchase ONLY if you have the means to do so. Use code: MEDSUB to get a 10% discount!</i></p></article></body>

ADVICE

Making Sense of Medium Stats

Visual aids included

Photo curtesy of author

Medium writers often get addicted to checking their stats. They stare at them with puzzled looks as if gauging the success or failure of their articles is a difficult task. That’s why I created an at-a-glance guide to determine how your month is going.

The much-welcomed big FU.

Drawing done by author with the help of a Crayola

You’ve got one kick ass day this month, so enjoy it.

You know what I’m talking about! The bird. The middle finger salute. For those in upper-class circles: the digitus impudicus. The hand signal recognized world wide. We all know it doesn’t stand for funky underpants.

The chart indicates the month plodded along with the boring, mind-numbing cadence of an ox. Then bam! You wake up to the big FU and your grin takes up most of your face.

Only on Medium, do you get a thrill from getting flipped off first thing in the morning. Whatever you want to call it, we all want to see it.

The big FU gives us hope.

The lie detector

The author thanks her kindergarten teacher for giving her dope drawing skills.

This chart shows your month is all over the place. The Medium audience is a fickle bunch and they love/hate your article. Consider it a win.

It also resembles the results of any Medium writer’s lie detector if they answered no to any of the following questions:

Have you checked your stats more than 75 times today?

Do you feel pressure when you haven’t published a story in a few days?

Is it hard for you NOT to click on a “How I Made $100 on Medium” article?

Do you think you have a chance for one of your stories to go viral?

Does your heart skip a beat every time you see two-digit numbers in the green circle?

Within five minutes of submitting to a publication, do you start checking to see if it’s been published yet?

The dunce hat

This intricate drawing was done under the tutelage of the author’s puppy.

This chart indicates your blood pressure is going to be out of whack. Avoid caffeine and sugar. Try to avoid other stressors such as court dates, divorce, remodeling, or relocating.

Unfortunately, the views on your article formed the dreaded dunce hat. This deceitful little hat-from-hell gets adrenaline pumping and a writer’s ego floating in the clouds — only to be drop-kicked, stomped on, a ran over within hours. It’s a soul crushing blow. You feel silly for believing your piece would continue the upward trend. Don’t let it get to you. It happens to all of us.

It’s all about perspective. Instead of a dunce hat, think of it as the big FU’s cousin. We all love a big FU, right?

The flatline

Picture provided after ten hours of artistry by the author.

This is going to be a long, tedious month if you keep checking this story’s stats. You’d be better off spending the reminder of the month watching political ads 24/7.

The article sputters along, and then spikes in one last gasp — it’s final grasp for glory — and then … nothing. A writer may choose to just bury it and never revisit it. Another option is to rewrite it with significant changes, rename it and put it back out there with fingers crossed. Think of it as reincarnation.

The cliff

Illustration by author — don’t ask her to illustrate your articles. Her waiting list is long.

When your month starts off with a cliff, a strong cocktail is in order. Maybe a couple. Hell, just belly up to the bar and start pounding.

This story starts off strong and then drops off without warning. Watching a story do this makes the writer want to jump off a cliff, plummet headfirst in the ocean and swim with the fishes.

Dry yourself off and don’t throw in the towel.

The dream

The green crayon 🖍 almost broke creating this masterpiece .

This is what it’s all about. The graph of our dreams. Our happy place. Our “if only”. There’s only one way to achieve it: keep writing!

I’m going to keep shooting for the stars. If this writing gig doesn’t work out, I can always fall back on my mad coloring skills. I may change my name to Trace-Gogh or Tracasso.

Keep reading more of Tracy’s twisted take on the world:

Health officials suggest kinky behavior will keep you safe from Covid:

For some, quarantine gets a little too comfortable:

Humor
Médium
Nonfiction
Writing
Life Lessons
Recommended from ReadMedium