Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files- app.py +3 -1
- checkpoints/tiny-text-classifier.pt +3 -0
- scripts/predict_class.py +5 -4
- scripts/train_classifier.py +16 -12
- scripts/train_generator.py +5 -9
- src/task/training/util.py +8 -1
app.py
CHANGED
|
@@ -15,7 +15,7 @@ from src.task.inference.classify import TextClassifier
|
|
| 15 |
MODEL_PATH = Path("checkpoints/tiny_text_generator.pt")
|
| 16 |
|
| 17 |
CLASSIFIER_MODEL_PATH = Path(
|
| 18 |
-
"checkpoints/
|
| 19 |
)
|
| 20 |
|
| 21 |
CLASSIFIER_TOKENIZER_PATH = Path(
|
|
@@ -70,12 +70,14 @@ classifier_tokenizer = CharacterTokenizer.load(
|
|
| 70 |
CLASSIFIER_TOKENIZER_PATH
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
classifier_model = load_classifier_model(
|
| 74 |
checkpoint_path=CLASSIFIER_MODEL_PATH,
|
| 75 |
config=config,
|
| 76 |
vocab_size=classifier_tokenizer.vocab_size,
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
text_classifier = TextClassifier(
|
| 80 |
model=classifier_model,
|
| 81 |
tokenizer=classifier_tokenizer,
|
|
|
|
| 15 |
MODEL_PATH = Path("checkpoints/tiny_text_generator.pt")
|
| 16 |
|
| 17 |
CLASSIFIER_MODEL_PATH = Path(
|
| 18 |
+
"checkpoints/tiny-text-classifier.pt"
|
| 19 |
)
|
| 20 |
|
| 21 |
CLASSIFIER_TOKENIZER_PATH = Path(
|
|
|
|
| 70 |
CLASSIFIER_TOKENIZER_PATH
|
| 71 |
)
|
| 72 |
|
| 73 |
+
config.context_length = 384
|
| 74 |
classifier_model = load_classifier_model(
|
| 75 |
checkpoint_path=CLASSIFIER_MODEL_PATH,
|
| 76 |
config=config,
|
| 77 |
vocab_size=classifier_tokenizer.vocab_size,
|
| 78 |
)
|
| 79 |
|
| 80 |
+
config.context_length = 64
|
| 81 |
text_classifier = TextClassifier(
|
| 82 |
model=classifier_model,
|
| 83 |
tokenizer=classifier_tokenizer,
|
checkpoints/tiny-text-classifier.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cc414481ecb68c0bd4d56204b5496abaf70690eab6a316d6bab27ef3e5978c26
|
| 3 |
+
size 792521
|
scripts/predict_class.py
CHANGED
|
@@ -11,7 +11,7 @@ from src.infra.dataset.util import build_text
|
|
| 11 |
|
| 12 |
# Runtime configs
|
| 13 |
# corpus = "data/tiny_shakespear.txt"
|
| 14 |
-
checkpoint = "checkpoints/
|
| 15 |
|
| 16 |
# CLASS_TOKENIZER_PATH = "ag_news_tokenizer.json"
|
| 17 |
CLASS_TOKENIZER_PATH = Path("checkpoints/ag_news_tokenizer.json")
|
|
@@ -27,13 +27,14 @@ CLASS_NAMES = [
|
|
| 27 |
]
|
| 28 |
|
| 29 |
# Conf
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# Load model/checkpoint from file
|
| 34 |
model = load_classifier_model(
|
| 35 |
checkpoint_path=checkpoint,
|
| 36 |
-
config=
|
| 37 |
vocab_size=tokenizer.vocab_size,
|
| 38 |
)
|
| 39 |
|
|
@@ -42,7 +43,7 @@ model = load_classifier_model(
|
|
| 42 |
text_classifier = TextClassifier(
|
| 43 |
model=model,
|
| 44 |
tokenizer=tokenizer,
|
| 45 |
-
context_length=
|
| 46 |
)
|
| 47 |
|
| 48 |
|
|
|
|
| 11 |
|
| 12 |
# Runtime configs
|
| 13 |
# corpus = "data/tiny_shakespear.txt"
|
| 14 |
+
checkpoint = "checkpoints/tiny-text-classifier.pt"
|
| 15 |
|
| 16 |
# CLASS_TOKENIZER_PATH = "ag_news_tokenizer.json"
|
| 17 |
CLASS_TOKENIZER_PATH = Path("checkpoints/ag_news_tokenizer.json")
|
|
|
|
| 27 |
]
|
| 28 |
|
| 29 |
# Conf
|
| 30 |
+
model_config = ModelConfig()
|
| 31 |
+
model_config.context_length = 384
|
| 32 |
|
| 33 |
|
| 34 |
# Load model/checkpoint from file
|
| 35 |
model = load_classifier_model(
|
| 36 |
checkpoint_path=checkpoint,
|
| 37 |
+
config=model_config,
|
| 38 |
vocab_size=tokenizer.vocab_size,
|
| 39 |
)
|
| 40 |
|
|
|
|
| 43 |
text_classifier = TextClassifier(
|
| 44 |
model=model,
|
| 45 |
tokenizer=tokenizer,
|
| 46 |
+
context_length=model_config.context_length,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
scripts/train_classifier.py
CHANGED
|
@@ -13,35 +13,39 @@ from src.task.model import TinyTransformerLM, TinyClassifier
|
|
| 13 |
from src.task.training.classification import train
|
| 14 |
import pandas as pd
|
| 15 |
import numpy as np
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
separator("Helpers")
|
| 25 |
-
|
| 26 |
|
| 27 |
|
| 28 |
# Configs
|
| 29 |
separator("Configuration")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
model_config = ModelConfig()
|
| 34 |
training_config = TrainingConfig()
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Load the corpus
|
| 40 |
separator("Corpus")
|
| 41 |
|
| 42 |
TRAIN_PATH = Path("data/ag-news/train.csv")
|
| 43 |
TEST_PATH = Path("data/ag-news/test.csv")
|
| 44 |
-
checkpoint = "
|
| 45 |
|
| 46 |
if not TRAIN_PATH.exists():
|
| 47 |
raise FileNotFoundError(f"Training file not found: {TRAIN_PATH}")
|
|
|
|
| 13 |
from src.task.training.classification import train
|
| 14 |
import pandas as pd
|
| 15 |
import numpy as np
|
| 16 |
+
from src.task.training.util import TrainingLogger, count_parameters, separator
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
+
# Custom Logger
|
| 21 |
+
logger = TrainingLogger(
|
| 22 |
+
"logs/text_classifier.log"
|
| 23 |
+
)
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
# Configs
|
| 27 |
separator("Configuration")
|
| 28 |
|
| 29 |
+
torch.manual_seed(42)
|
| 30 |
+
# Configs
|
| 31 |
model_config = ModelConfig()
|
| 32 |
training_config = TrainingConfig()
|
| 33 |
|
| 34 |
+
# Overrides
|
| 35 |
+
model_config.context_length = 384
|
| 36 |
+
training_config.num_steps = 10
|
| 37 |
+
training_config.eval_interval = 1
|
| 38 |
+
training_config.num_train_samples = None
|
| 39 |
+
|
| 40 |
+
logger.log(model_config)
|
| 41 |
+
logger.log(training_config)
|
| 42 |
|
| 43 |
# Load the corpus
|
| 44 |
separator("Corpus")
|
| 45 |
|
| 46 |
TRAIN_PATH = Path("data/ag-news/train.csv")
|
| 47 |
TEST_PATH = Path("data/ag-news/test.csv")
|
| 48 |
+
checkpoint = "tiny-text-classifier.pt"
|
| 49 |
|
| 50 |
if not TRAIN_PATH.exists():
|
| 51 |
raise FileNotFoundError(f"Training file not found: {TRAIN_PATH}")
|
scripts/train_generator.py
CHANGED
|
@@ -10,16 +10,12 @@ from src.task.config import ModelConfig,TrainingConfig
|
|
| 10 |
|
| 11 |
from src.task.model import TinyTransformerLM
|
| 12 |
from src.task.training.autoregressive import train, evaluate
|
| 13 |
-
from src.task.training.util import TrainingLogger, count_parameters
|
| 14 |
|
| 15 |
-
def separator(title: str) -> None:
|
| 16 |
-
print("\n" + "=" * 60)
|
| 17 |
-
print(title)
|
| 18 |
-
print("=" * 60)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
logger = TrainingLogger(
|
| 22 |
-
"logs/text_generator.
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
@@ -32,10 +28,10 @@ separator("Configuration")
|
|
| 32 |
|
| 33 |
torch.manual_seed(42)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
model_config = ModelConfig()
|
| 37 |
training_config = TrainingConfig()
|
| 38 |
-
|
| 39 |
training_config.num_steps = 10000
|
| 40 |
training_config.eval_interval = 1000
|
| 41 |
|
|
|
|
| 10 |
|
| 11 |
from src.task.model import TinyTransformerLM
|
| 12 |
from src.task.training.autoregressive import train, evaluate
|
| 13 |
+
from src.task.training.util import TrainingLogger, count_parameters, separator
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Custom Logger
|
| 17 |
logger = TrainingLogger(
|
| 18 |
+
"logs/text_generator.log"
|
| 19 |
)
|
| 20 |
|
| 21 |
|
|
|
|
| 28 |
|
| 29 |
torch.manual_seed(42)
|
| 30 |
|
| 31 |
+
# Configs
|
| 32 |
model_config = ModelConfig()
|
| 33 |
training_config = TrainingConfig()
|
| 34 |
+
# Overrides
|
| 35 |
training_config.num_steps = 10000
|
| 36 |
training_config.eval_interval = 1000
|
| 37 |
|
src/task/training/util.py
CHANGED
|
@@ -31,4 +31,11 @@ def count_parameters(model):
|
|
| 31 |
print(
|
| 32 |
f"Parameters: "
|
| 33 |
f"{count_parameters(model):,}"
|
| 34 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
print(
|
| 32 |
f"Parameters: "
|
| 33 |
f"{count_parameters(model):,}"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def separator(title: str) -> None:
|
| 39 |
+
print("\n" + "=" * 60)
|
| 40 |
+
print(title)
|
| 41 |
+
print("=" * 60)
|