150 lines
4.4 KiB
Plaintext
150 lines
4.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Zajęcia 10: Neural Networks\n",
|
|
"\n",
|
|
"Wszystkie zadania ćwiczeniowe należy rozwiązywać w języku Python w kopii Jupyter Notebook'a dla danych zajęć w wyznaczonych miejscach (komórki z komentarzem `# Solution`).\n",
|
|
"\n",
|
|
"Nie należy usuwać komórek z treścią zadań.\n",
|
|
"\n",
|
|
"Należy wyświetlać outputy przy pomocy `print`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Zadanie 1\n",
|
|
"\n",
|
|
"Na podstawie zbioru treningowego https://git.wmi.amu.edu.pl/ryssta/tripadvisor-review-prediction/src/branch/master/train zmodyfikuj zmienną prompt w dwóch różnych wariantach (prosty i złożony) i dokonaj predykcji dla 20 wybranych przykładów (po 4 dla każdej oceny od 1 do 5) przy pomocy modelu języka Qwen/Qwen2.5-0.5B-Instruct.\n",
|
|
"\n",
|
|
"Na podstawie odpowiedzi z modelu dokonaj ekstrakcji wartości liczbowej z outputu modelu a następnie:\n",
|
|
"1. Oblicz wartość metryki RMSE (na podstawie predykcji i prawdziwych labeli - można skorzystać z sklearn) dla outputów z obu promptów.\n",
|
|
"2. Napisz wnioski odnośnie tego, czy model radzi sobie dobrze z tym zadaniem (mimo że nie był uczony na danych treningowych).\n",
|
|
"3. Napisz wnioski, czy dwie różne wersje prompta wpływają na wyniki.\n",
|
|
"\n",
|
|
"W celu przyspieszenia obliczeń można skorzystać z platformy Google Colab i uruchomić model na karcie graficznej.\n",
|
|
"\n",
|
|
"Opis wariantów (wersji prompta):\n",
|
|
"\n",
|
|
"1. Wariant prosty prompta - prosta, krótka instrukcja (podejście zero-shot)\n",
|
|
"2. Wariant złożony prompta - złożona instrukcja, można podać przykład kilku tekstów i ich ocen (technika few-shot)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
|
"\n",
|
|
"model_name = \"Qwen/Qwen2.5-0.5B-Instruct\"\n",
|
|
"\n",
|
|
"model = AutoModelForCausalLM.from_pretrained(\n",
|
|
" model_name,\n",
|
|
" torch_dtype=\"auto\"\n",
|
|
" # device_map=\"auto\" - uncomment it on google colab\n",
|
|
")\n",
|
|
"tokenizer = AutoTokenizer.from_pretrained(model_name)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Review score: 2\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"prompt = \"\"\"Rate the following review from 1 to 5. 1 means negative review, 5 means positive review. The review:\n",
|
|
"\n",
|
|
"It was not amazing experience.\n",
|
|
"\n",
|
|
"Answer using only single number.\"\"\"\n",
|
|
"\n",
|
|
"messages = [\n",
|
|
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
|
|
" {\"role\": \"user\", \"content\": prompt}\n",
|
|
"]\n",
|
|
"\n",
|
|
"text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
|
|
"model_inputs = tokenizer([text], return_tensors=\"pt\").to(model.device)\n",
|
|
"\n",
|
|
"generated_ids = model.generate(**model_inputs, max_new_tokens=2)\n",
|
|
"generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]\n",
|
|
"\n",
|
|
"response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n",
|
|
"print(f\"Review score: {response}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'2'"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"response"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Solution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"SIMPLE_PROMPT = \"\"\n",
|
|
"COMPLEX_PROMPT = \"\""
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "base",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|