avatarAnne Bonfert

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

2504

Abstract

Training</b>: Train the model on your dataset, adjusting the weights of the pre-trained model minimally to fit the new task.</li></ol><h1 id="2b3a">Implementation in Python Using Hugging Face</h1><p id="1209">Here’s a step-by-step guide to implementing transfer learning with Hugging Face’s Transformers library.</p><h2 id="cd98">1. Install Required Libraries</h2><div id="ef5f"><pre>pip install transformers datasets</pre></div><h2 id="0f8c">2. Load a Pre-trained Model</h2><p id="df2e">First, choose a pre-trained model. For example, we’ll use BERT for a text classification task.</p><div id="2de3"><pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BertTokenizer, BertForSequenceClassification

<span class="hljs-comment"># Load the tokenizer and model</span> tokenizer = BertTokenizer.from_pretrained(<span class="hljs-string">'bert-base-uncased'</span>) model = BertForSequenceClassification.from_pretrained(<span class="hljs-string">'bert-base-uncased'</span>, num_labels=<span class="hljs-number">2</span>)</pre></div><h2 id="5e2d">3. Prepare Your Dataset</h2><p id="e504">You can use the <code>datasets</code> library from Hugging Face to load and prepare your dataset.</p><div id="b27c"><pre><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

<span class="hljs-comment"># Load a dataset (e.g., the IMDb movie reviews dataset)</span> dataset = load_dataset(<span class="hljs-string">'imdb'</span>) <span class="hljs-comment"># Tokenize the dataset</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">examples</span>): <span class="hljs-keyword">return</span> tokenizer(examples[<span class="hljs-string">'text'</span>], padding=<span class="hljs-string">"max_length"</span>, truncation=<span class="hljs-literal">True</span>) tokenized_datasets = dataset.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)</pre></div><h2 id="7357">4. Fine-Tune the Model</h2><p id="1060">Fine-tuning involves training the model on your specific dataset. You can use the <code>Trainer</code> API from Hugging Face to simplify this process.</p><div id="0069"><pre><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Trainer, TrainingArguments

<span class="hljs-comment"># Define training arguments</span> training_args = TrainingArgumen

Options

ts( output_dir=<span class="hljs-string">'./results'</span>, evaluation_strategy=<span class="hljs-string">"epoch"</span>, per_device_train_batch_size=<span class="hljs-number">8</span>, per_device_eval_batch_size=<span class="hljs-number">8</span>, num_train_epochs=<span class="hljs-number">3</span>, weight_decay=<span class="hljs-number">0.01</span>, ) <span class="hljs-comment"># Define the Trainer</span> trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_datasets[<span class="hljs-string">'train'</span>], eval_dataset=tokenized_datasets[<span class="hljs-string">'test'</span>], ) <span class="hljs-comment"># Train the model</span> trainer.train()</pre></div><h2 id="c70a">5. Evaluate the Model</h2><p id="39aa">After training, evaluate the model to see how well it performs on the test set.</p><div id="f7a4"><pre><span class="hljs-comment"># Evaluate the model</span> eval_results = trainer.evaluate() <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Evaluation results: <span class="hljs-subst">{eval_results}</span>"</span>)</pre></div><h2 id="e3a9">6. Save the Fine-Tuned Model</h2><p id="1121">Finally, save the fine-tuned model for future use.</p><div id="297e"><pre><span class="hljs-comment"># Save the model and tokenizer</span> model.save_pretrained(<span class="hljs-string">"./fine-tuned-bert"</span>) tokenizer.save_pretrained(<span class="hljs-string">"./fine-tuned-bert"</span>)</pre></div><h1 id="254a">Additional Tips</h1><ul><li><b>Leverage Hugging Face’s community:</b> Explore pre-trained models and datasets shared by others.</li><li><b>Experiment with different architectures:</b> Try different model architectures to find the best fit.</li><li><b>Regularization:</b> Use techniques like dropout or L1/L2 regularization to prevent overfitting.</li><li><b>Data augmentation:</b> Increase data diversity by applying transformations to your dataset.</li></ul><h1 id="30b5">Conclusion</h1><p id="559f"><b>Transfer learning</b> with <b>Hugging Face</b> is a powerful technique to accelerate model development and improve performance, especially for <b>NLP</b> tasks. By leveraging pre-trained models and fine-tuning them on specific tasks, you can achieve high accuracy with minimal training time and resources. Hugging Face’s <b>Transformers </b>library makes this process straightforward, providing tools and pre-trained models that are easy to adapt to various tasks.</p></article></body>

DECEMBER MONTHLY CHALLENGE

A Medieval Christmas Market in a Historical Old Town

Esslingen — always worth a visit

The gate to the medieval Christmas market. | Credit: Anne Bonfert

Christmas markets and drinking Glühwein (mulled wine) are part of the German culture. It doesn’t matter how small a village might be, they will have a Christmas market.

It’s been a few years since I last visited one of Germany’s Christmas markets and it was certainly time for another visit. David had never been on one outside of his home country and I thought the best one to visit would be the one in Esslingen rather than Stuttgart.

Esslingen is a district and city in southern Germany bordering the region’s capital, Stuttgart. During the second world war, Esslingen was spared from most of the bombings.

While I still remember what they taught us in school, I couldn’t find any information on it online as to why. It just said, “it was a miracle” since most other towns in the South were being flattened during the war.

But it wasn’t just “a miracle”. There was more behind it as our teachers taught us in primary school. It was either the princess or some English royal person, I can’t remember exactly, who had roots in the city and said “you can bomb the entire country, but please spare Esslingen”.

And this is why the town has kept most of its old buildings and the heart of the city. It’s a beautiful city with lots of half-timbered houses and historic buildings.

This is why it’s probably not a surprise that this exact city is known to have one of the most beautiful medieval Christmas markets in the world.

Historical buildings in Esslingen. | Credit: Anne Bonfert

My husband, David, is into the history of places and loves medieval stories which is why I thought he’d love this one even more.

Esslingen has a “normal” Christmas market as well but even that one is less commercialized than others (in my eyes) and still has lots of handmade crafts for sale.

Decorations. | Credit: Anne Bonfert

Decorations are unique and so are the people working there.

It's Christmas time. | Credit: Anne Bonfert

You can buy loads of cheeses and meats from the region and drink of course cups filled with Glühwein which you need as it gets colder.

Yummy. | Credit: Anne Bonfert

But then you walk through an old wooden gate welcoming you to the medieval market and everything changes. The decorations, the writings and even the language the people speak.

The people working at the stalls on the medieval market live their role as a person presenting past times.

A different vibe. | Credit: Anne Bonfert

You can get similar foods as on the other Christmas markets as well such as roast chestnuts and sausages in a bun but there are many other more creative and interesting options as well. My favorite one was a piece of camembert cheese in some kind of self-made dough.

Roast chestnuts. | Credit: Anne Bonfert

With the cheese in one hand and the Glühwein in the other hand, I struggled to take pictures of the other interesting-looking stalls.

As I said, it’s been probably over seven years since I last visited the Christmas market in Esslingen and I could only hope it didn’t change or at least not in a negative way.

And I was relieved. Over all those years, the market kept its original charm.

Christmas stalls at this medieval market. | Credit: Anne Bonfert

From handmade buttons, bracelets and other jewelry to scarves, bottles of gin and interestingly-sounding drinks you could find everything.

Shots from interesting-looking bottles. | Credit: Anne Bonfert

In the middle of it all was a stage with local artists performing a song dressed in medieval clothes. It was entertaining and certainly fitted the theme of the market.

The stage. | Credit: Anne Bonfert

We found another section of the market called Zwergenland which means “the land of the dwarves”. We didn’t know what to expect until we saw the obvious. An entertainment area for our dwarves of the society. The children.

The ferris wheel. | Credit: Anne Bonfert

From a Ferris Wheel that was being turned through someone’s manpower to axe throwing and catapulting challenges, you could find everything.

Kids playground. | Credit: Anne Bonfert

The only thing sounding a bit odd was the typical medieval and traditional German Stockbrot (stick bread, literally dough wrapped around a stick made over the fire) now being sold as a “vegan delicacy”.

But let them be. They always need new sales tactics.

Stick bread. | Credit: Anne Bonfert

Oh, and this is one of the wood ovens every second stall had where all the bread for the sausages and other foods was made.

A wood oven. | Credit: Anne Bonfert

It was hard to leave but eventually, we left the crowds behind and walked through the shopping street of the city center. Christmas lights were up around town adding to the charming flair of Esslingen.

Walking through the city at night. | Credit: Anne Bonfert

This visit was certainly not one to disappoint any expectations and I can only recommend every traveler to come by and explore this unique medieval Christmas Market.

This is a writing prompt for the Globetrotters monthly challenge. Read about the guidelines here:

Read about other submissions:

Jillian Amatt - Artistic Voyages with “Christmas Looks A Little Different Everywhere We Go

Aarti Tailor with “A Festive Weekend In Brussels

Akemi Sagawa with “The Magic of Christmas Market in Vienna

Join my email list here if you would like to read more travel essays or sign up for the Medium membership to receive unlimited access to my and other writers’ stories out here (I will receive a commission fee in return).

WordPress | Shutterstock | Instagram | YouTube | Mailchimp | Amazon

Travel
Christmas
Germany
Medieval
Monthly Challenge
Recommended from ReadMedium