first
Browse files- batch_finetune_danish_xl.sh +12 -0
- finetune_classification_xl.gin +40 -0
- interference.sh +16 -0
- my_metrics.py +7 -0
- tasks.py +67 -0
batch_finetune_danish_xl.sh
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PROJECT_DIR=${HOME}"/models/danish-hate-classification"
|
| 2 |
+
export PYTHONPATH=${PROJECT_DIR}
|
| 3 |
+
INITIAL_CHECKPOINT_PATH=\"gs://north-t5x/pretrained_models/xl/scandinavian3k_t5x_xl/checkpoint_3000000\"
|
| 4 |
+
TRAIN_STEPS=3003000
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
python3 ../../t5x/t5x/train.py --gin_search_paths="./" --gin.TRAIN_STEPS=${TRAIN_STEPS} --gin_file="finetune_classification_xl.gin" --gin.INITIAL_CHECKPOINT_PATH=${INITIAL_CHECKPOINT_PATH} --gin.MIXTURE_OR_TASK_NAME=\"classify_hate\" --gin.MODEL_DIR=\"gs://north-t5x/finetuned/danish_hate/calassification_v1\" &&
|
| 8 |
+
python3 ../../t5x/t5x/train.py --gin_search_paths="./" --gin.TRAIN_STEPS=${TRAIN_STEPS} --gin_file="finetune_classification_xl.gin" --gin.INITIAL_CHECKPOINT_PATH=${INITIAL_CHECKPOINT_PATH} --gin.MIXTURE_OR_TASK_NAME=\"classify_hate\" --gin.MODEL_DIR=\"gs://north-t5x/finetuned/danish_hate/calassification_v2\" &&
|
| 9 |
+
python3 ../../t5x/t5x/train.py --gin_search_paths="./" --gin.TRAIN_STEPS=${TRAIN_STEPS} --gin_file="finetune_classification_xl.gin" --gin.INITIAL_CHECKPOINT_PATH=${INITIAL_CHECKPOINT_PATH} --gin.MIXTURE_OR_TASK_NAME=\"classify_hate\" --gin.MODEL_DIR=\"gs://north-t5x/finetuned/danish_hate/calassification_v3\" &&
|
| 10 |
+
python3 ../../t5x/t5x/train.py --gin_search_paths="./" --gin.TRAIN_STEPS=${TRAIN_STEPS} --gin_file="finetune_classification_xl.gin" --gin.INITIAL_CHECKPOINT_PATH=${INITIAL_CHECKPOINT_PATH} --gin.MIXTURE_OR_TASK_NAME=\"classify_hate\" --gin.MODEL_DIR=\"gs://north-t5x/finetuned/danish_hate/calassification_v4\" &&
|
| 11 |
+
python3 ../../t5x/t5x/train.py --gin_search_paths="./" --gin.TRAIN_STEPS=${TRAIN_STEPS} --gin_file="finetune_classification_xl.gin" --gin.INITIAL_CHECKPOINT_PATH=${INITIAL_CHECKPOINT_PATH} --gin.MIXTURE_OR_TASK_NAME=\"classify_hate\" --gin.MODEL_DIR=\"gs://north-t5x/finetuned/danish_hate/calassification_v5\"
|
| 12 |
+
|
finetune_classification_xl.gin
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __gin__ import dynamic_registration
|
| 2 |
+
import tasks
|
| 3 |
+
|
| 4 |
+
import __main__ as train_script
|
| 5 |
+
from t5.data import mixtures
|
| 6 |
+
from t5x import models
|
| 7 |
+
from t5x import partitioning
|
| 8 |
+
from t5x import utils
|
| 9 |
+
|
| 10 |
+
include "t5x/examples/t5/mt5/xl.gin"
|
| 11 |
+
include "t5x/configs/runs/finetune.gin"
|
| 12 |
+
|
| 13 |
+
MIXTURE_OR_TASK_NAME = %gin.REQUIRED
|
| 14 |
+
TASK_FEATURE_LENGTHS = {"inputs": 512, "targets": 512}
|
| 15 |
+
INITIAL_CHECKPOINT_PATH = %gin.REQUIRED
|
| 16 |
+
TRAIN_STEPS = %gin.REQUIRED # 1000000 pre-trained steps + 10000 fine-tuning steps.
|
| 17 |
+
USE_CACHED_TASKS = False
|
| 18 |
+
DROPOUT_RATE = 0.1
|
| 19 |
+
RANDOM_SEED = 0
|
| 20 |
+
BATCH_SIZE = 32
|
| 21 |
+
|
| 22 |
+
#Fixing a small error
|
| 23 |
+
infer_eval/utils.DatasetConfig:
|
| 24 |
+
task_feature_lengths = %TASK_FEATURE_LENGTHS
|
| 25 |
+
|
| 26 |
+
#Saving every 1000 steps
|
| 27 |
+
utils.SaveCheckpointConfig:
|
| 28 |
+
period = 1000
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Pere: Only necessary if we load a t5 model. We can start with an t5x model here
|
| 32 |
+
# `LOSS_NORMALIZING_FACTOR`: When fine-tuning a model that was pre-trained
|
| 33 |
+
# using Mesh Tensorflow (e.g. the public T5 / mT5 / ByT5 models), this should be
|
| 34 |
+
# set to `pretraining batch_size` * `target_token_length`. For T5 and T5.1.1:
|
| 35 |
+
# `2048 * 114`. For mT5: `1024 * 229`. For ByT5: `1024 * 189`.
|
| 36 |
+
# LOSS_NORMALIZING_FACTOR = 234496
|
| 37 |
+
|
| 38 |
+
# Might have to ba changed based on architecture
|
| 39 |
+
# partitioning.PjitPartitioner.num_partitions = 1
|
| 40 |
+
|
interference.sh
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
INFER_OUTPUT_DIR="output2" # directory to write infer output
|
| 2 |
+
T5X_DIR="../../t5x" # directory where the t5x is cloned, e.g., ${HOME}"/t5x".
|
| 3 |
+
TFDS_DATA_DIR="gs://nb-t5x/corpus_multi_sentencefix_mt5/"
|
| 4 |
+
CHECKPOINT_PATH="gs://nb-t5x-us-central2/model_mT5X_large_16_e/checkpoint_1140000"
|
| 5 |
+
PROJECT_DIR=${HOME}"/models/multi-sentencefix-mt5"
|
| 6 |
+
export PYTHONPATH=${PROJECT_DIR}
|
| 7 |
+
|
| 8 |
+
python3 ${T5X_DIR}/t5x/infer.py \
|
| 9 |
+
--gin_search_paths=${PROJECT_DIR} \
|
| 10 |
+
--gin_file="large_wmt_infer.gin" \
|
| 11 |
+
--gin.CHECKPOINT_PATH=\"${CHECKPOINT_PATH}\" \
|
| 12 |
+
--gin.INFER_OUTPUT_DIR=\"${INFER_OUTPUT_DIR}\" \
|
| 13 |
+
--tfds_data_dir=${TFDS_DATA_DIR}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
my_metrics.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sklearn.metrics
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def f1_macro(targets, predictions):
|
| 5 |
+
targets, predictions = np.asarray(targets).astype(str), np.asarray(predictions).astype(str)
|
| 6 |
+
return {"f1_macro": 100*sklearn.metrics.f1_score(targets, predictions, average='macro')}
|
| 7 |
+
|
tasks.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /home/perk/mymodel/categorisation-mt5x/tasks.py
|
| 2 |
+
|
| 3 |
+
import functools
|
| 4 |
+
import seqio
|
| 5 |
+
import tensorflow_datasets as tfds
|
| 6 |
+
from t5.evaluation import metrics
|
| 7 |
+
import my_metrics
|
| 8 |
+
from t5.data import preprocessors
|
| 9 |
+
import t5
|
| 10 |
+
import tensorflow.compat.v1 as tf
|
| 11 |
+
|
| 12 |
+
tsv_path = {
|
| 13 |
+
"train": "gs://north-t5x/corpus/danish_hate/train.tsv",
|
| 14 |
+
"validation": "gs://north-t5x/corpus/danish_hate/eval.tsv",
|
| 15 |
+
"test": "gs://north-t5x/corpus/danish_hate/test.tsv"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
vocabulary = seqio.SentencePieceVocabulary(
|
| 19 |
+
'gs://t5-data/vocabs/mc4.250000.100extra/sentencepiece.model', extra_ids=0)
|
| 20 |
+
|
| 21 |
+
DEFAULT_OUTPUT_FEATURES = {
|
| 22 |
+
"inputs":
|
| 23 |
+
seqio.Feature(
|
| 24 |
+
vocabulary=vocabulary, add_eos=True),
|
| 25 |
+
"targets":
|
| 26 |
+
seqio.Feature(
|
| 27 |
+
vocabulary=vocabulary, add_eos=True)
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
def categorise_preprocessor(ds):
|
| 31 |
+
def normalize_text(text):
|
| 32 |
+
"""Lowercase and remove quotes from a TensorFlow string."""
|
| 33 |
+
text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
|
| 34 |
+
return text
|
| 35 |
+
|
| 36 |
+
def to_inputs_and_targets(ex):
|
| 37 |
+
"""Map {"source": ..., "source": ...}->{"target": ..., "target": ...}."""
|
| 38 |
+
return {
|
| 39 |
+
"inputs":
|
| 40 |
+
tf.strings.join(
|
| 41 |
+
[normalize_text(ex["source"])]),
|
| 42 |
+
"targets":
|
| 43 |
+
tf.strings.join(
|
| 44 |
+
[normalize_text(ex["target"])]),
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
return ds.map(to_inputs_and_targets,
|
| 48 |
+
num_parallel_calls=tf.data.experimental.AUTOTUNE)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
seqio.TaskRegistry.add(
|
| 52 |
+
"classify_hate",
|
| 53 |
+
source=seqio.TextLineDataSource(
|
| 54 |
+
split_to_filepattern=tsv_path,
|
| 55 |
+
#num_input_examples=num_nq_examples
|
| 56 |
+
),
|
| 57 |
+
preprocessors=[
|
| 58 |
+
functools.partial(
|
| 59 |
+
t5.data.preprocessors.parse_tsv,
|
| 60 |
+
field_names=["id","source","target"]),
|
| 61 |
+
categorise_preprocessor,
|
| 62 |
+
seqio.preprocessors.tokenize_and_append_eos,
|
| 63 |
+
],
|
| 64 |
+
metric_fns=[metrics.accuracy,my_metrics.f1_macro],
|
| 65 |
+
output_features=DEFAULT_OUTPUT_FEATURES,
|
| 66 |
+
)
|
| 67 |
+
|