Chapter 1
Chapter 1
language models
INTRODUCTION TO LLMS IN PYTHON
Pre-training
Learns patterns
Computationally intensive
Pre-trained foundation model
Pre-training Fine-tuning
Computationally intensive
text_classifier = pipeline(task="text-classification",
model="nlptown/bert-base-multilingual-uncased-sentiment")
text = "Dear seller, I got very impressed with the fast delivery and careful packaging
sentiment = text_classifier(text)
print(sentiment)
llm = pipeline("text-classification")
text = "Walking amid Gion's Machiya wooden houses was a mesmerizing experience"
outputs = llm(text)
print(outputs[0]['label'])
POSITIVE
llm = pipeline("text-generation")
prompt = "The Gion neighborhood in Kyoto is famous for"
outputs = llm(prompt, max_length=100)
print(outputs[0]['generated_text'])
The Gion neighborhood in Kyoto is famous for making fish and seafood by the sea,
which made sense in the 1920s because it was the largest city of its age.
Walking amid Gion's Machiya wooden houses is a mesmerizing experience. The beautifully preserved
structures exuded an old-world charm. The glow of lanterns lining the narrow streets add to the ambiance.
Each stroll is an memorable journey through Japan's rich cultural history.
llm = pipeline("question-answering")
context = "Walking amid Gion's Machiya wooden houses was a mesmerizing experience."
question = "What are Machiya houses made of?"
outputs = llm(question=question, context=context)
print(outputs['answer'])
wooden
Caminar entre las casas de madera Machiya de Gion fue una experiencia fascinante.
Characteristics:
1 Image source: A. Vaswani, et al. "Attention is all you need". Arxiv, 2017: https://arxiv.org/pdf/1706.03762.pdf
Summarization
Question-answering
1 Image source: A. Vaswani, et al. "Attention is all you need". Arxiv, 2017: https://arxiv.org/pdf/1706.03762.pdf
num_encoder_layers,num_decoder_layers : num_decoder_layers = 6