|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Model Training Callbacks\n", |
| 8 | + "\n", |
| 9 | + "You can use the library to log your model training progress to Strikes.\n" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import dreadnode as dn\n", |
| 19 | + "\n", |
| 20 | + "dn.configure(\n", |
| 21 | + " token=\"<YOUR API KEY>\", # Replace with your token\n", |
| 22 | + ")" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": null, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "from datasets import load_dataset\n", |
| 32 | + "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n", |
| 33 | + "\n", |
| 34 | + "# Load dataset\n", |
| 35 | + "dataset = load_dataset(\"glue\", \"sst2\")\n", |
| 36 | + "tokenizer = AutoTokenizer.from_pretrained(\"distilbert-base-uncased\")\n", |
| 37 | + "\n", |
| 38 | + "# Take a small portion of the dataset\n", |
| 39 | + "dataset[\"train\"] = dataset[\"train\"].select(range(1000))\n", |
| 40 | + "dataset[\"validation\"] = dataset[\"validation\"].select(range(1000))\n", |
| 41 | + "\n", |
| 42 | + "# Preprocessing function\n", |
| 43 | + "def preprocess_function(examples):\n", |
| 44 | + " return tokenizer(examples[\"sentence\"], truncation=True, padding=\"max_length\")\n", |
| 45 | + "\n", |
| 46 | + "# Tokenize the dataset\n", |
| 47 | + "tokenized_datasets = dataset.map(preprocess_function, batched=True)\n", |
| 48 | + "\n", |
| 49 | + "# Load model\n", |
| 50 | + "model = AutoModelForSequenceClassification.from_pretrained(\"distilbert-base-uncased\", num_labels=2)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "from transformers import Trainer, TrainingArguments\n", |
| 60 | + "\n", |
| 61 | + "from dreadnode.integrations.transformers import DreadnodeCallback\n", |
| 62 | + "import dreadnode as dn\n", |
| 63 | + "\n", |
| 64 | + "# Define training arguments\n", |
| 65 | + "training_args = TrainingArguments(\n", |
| 66 | + " output_dir=\"./results\",\n", |
| 67 | + " learning_rate=2e-5,\n", |
| 68 | + " per_device_train_batch_size=6,\n", |
| 69 | + " per_device_eval_batch_size=6,\n", |
| 70 | + " num_train_epochs=5,\n", |
| 71 | + " weight_decay=0.01,\n", |
| 72 | + " eval_strategy=\"steps\",\n", |
| 73 | + " eval_steps=5,\n", |
| 74 | + " load_best_model_at_end=False,\n", |
| 75 | + " push_to_hub=False,\n", |
| 76 | + " run_name=\"distilbert-sst2-demo\",\n", |
| 77 | + ")\n", |
| 78 | + "\n", |
| 79 | + "# Initialize Trainer with RiggingCallback\n", |
| 80 | + "trainer = Trainer(\n", |
| 81 | + " model=model,\n", |
| 82 | + " args=training_args,\n", |
| 83 | + " train_dataset=tokenized_datasets[\"train\"],\n", |
| 84 | + " eval_dataset=tokenized_datasets[\"validation\"],\n", |
| 85 | + " tokenizer=tokenizer,\n", |
| 86 | + " callbacks=[DreadnodeCallback(project=\"training\")],\n", |
| 87 | + ")\n", |
| 88 | + "\n", |
| 89 | + "# Train the model\n", |
| 90 | + "trainer.train()\n", |
| 91 | + "\n", |
| 92 | + "# Evaluate the model\n", |
| 93 | + "trainer.evaluate()" |
| 94 | + ] |
| 95 | + } |
| 96 | + ], |
| 97 | + "metadata": { |
| 98 | + "kernelspec": { |
| 99 | + "display_name": ".venv", |
| 100 | + "language": "python", |
| 101 | + "name": "python3" |
| 102 | + }, |
| 103 | + "language_info": { |
| 104 | + "codemirror_mode": { |
| 105 | + "name": "ipython", |
| 106 | + "version": 3 |
| 107 | + }, |
| 108 | + "file_extension": ".py", |
| 109 | + "mimetype": "text/x-python", |
| 110 | + "name": "python", |
| 111 | + "nbconvert_exporter": "python", |
| 112 | + "pygments_lexer": "ipython3", |
| 113 | + "version": "3.10.14" |
| 114 | + } |
| 115 | + }, |
| 116 | + "nbformat": 4, |
| 117 | + "nbformat_minor": 2 |
| 118 | +} |
0 commit comments