Spaces:
Build error
Build error
File size: 12,496 Bytes
b11ac48 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
"""
Learn to classify the manually annotated CDA attributes (frames, 'riferimento', orientation)
"""
import sys
import torch
from allennlp.data.vocabulary import Vocabulary
from allennlp.data import DatasetReader, TokenIndexer, Instance, Token
from allennlp.data.fields import TextField, LabelField
from allennlp.data.token_indexers.pretrained_transformer_indexer import (
PretrainedTransformerIndexer,
)
from allennlp.data.tokenizers.pretrained_transformer_tokenizer import (
PretrainedTransformerTokenizer,
)
from allennlp.models import BasicClassifier
from allennlp.modules.text_field_embedders.basic_text_field_embedder import (
BasicTextFieldEmbedder,
)
from allennlp.modules.token_embedders.pretrained_transformer_embedder import (
PretrainedTransformerEmbedder,
)
from allennlp.modules.seq2vec_encoders.bert_pooler import BertPooler
from allennlp.training.checkpointer import Checkpointer
from allennlp.training.gradient_descent_trainer import GradientDescentTrainer
from allennlp.data.data_loaders.simple_data_loader import SimpleDataLoader
from allennlp.training.optimizers import AdamOptimizer
from allennlp.predictors.text_classifier import TextClassifierPredictor
from sklearn.svm import SVC
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import precision_recall_fscore_support
from sklearn.tree import DecisionTreeClassifier
from sklearn.dummy import DummyClassifier
import pandas as pd
import numpy as np
import spacy
import json
import os
from typing import Dict, Iterable
class MigrationReader(DatasetReader):
def __init__(self, token_indexers, tokenizer):
self.token_indexers = token_indexers
self.tokenizer = tokenizer
def text_to_instance(self, sentence, label=None) -> Instance:
text_field = TextField(self.tokenizer.tokenize(sentence), self.token_indexers)
fields = {"tokens": text_field}
if label is not None:
label_field = LabelField(label)
fields["label"] = label_field
return Instance(fields)
def read_instances(
self, text: pd.Series, labels: pd.Series
) -> Iterable[Instance]:
for sentence, label in zip(text, labels):
instance = self.text_to_instance(sentence, label)
yield instance
def train(attrib, use_gpu=False):
assert attrib in ["cda_frame", "riferimento", "orientation", "fake"]
# load data
print("Loading data...")
x_train, y_train, x_dev, y_dev = load_data(attrib)
print(f"\t\ttrain size: {len(x_train)}")
print(f"\t\tdev size: {len(x_dev)}")
# try different setups
print("Running training setups...")
scores = []
setups = [
# defaults: remove_punct=True, lowercase=True, lemmatize=False, remove_stop=False
# ({}, {}, {"type": "svm", "options": {"kernel": "linear", "C": 1.0}}),
(
{},
{},
{
"type": "bert",
"options": {"transformer": "Musixmatch/umberto-commoncrawl-cased-v1"},
},
),
# ({"lemmatize": True, "remove_stop": True}, {}, {"type": "svm", "options": {"kernel": "linear", "C": 0.8}}),
# ({"lemmatize": True, "remove_stop": True}, {"embed": False}, {"type": "svm", "options": {"kernel": "linear", "C": 0.8}}),
# ({"lemmatize": True, "remove_stop": True}, {"embed": False}, {"type": "dummy", "options": {}}),
# ({"lemmatize": True, "remove_stop": True}, {"embed": False}, {"type": "tree", "options": {}}),
# ({"lemmatize": True, "remove_stop": True}, {}, SVC(kernel='linear')),
# ({"lemmatize": True, "remove_stop": True}, {"min_freq": 5}, SVC(kernel='linear')),
# ({"lemmatize": True, "remove_stop": True}, {"min_freq": 5, "max_freq": .70}, SVC(kernel='linear')),
# ({"lemmatize": True, "remove_stop": True}, {}, SVC(kernel='linear', C=0.6)),
# ({"lemmatize": True, "remove_stop": True}, {}, SVC(kernel='linear', C=0.7)),
# ({"lemmatize": True, "remove_stop": True}, {}, SVC(kernel='linear', C=0.8)),
# ({"lemmatize": True, "remove_stop": True}, {"ngram_range": (1,2)}, SVC(kernel='linear', C=0.8)),
# ({"lemmatize": True, "remove_stop": True}, {}, SVC(kernel="rbf")),
]
nlp = spacy.load("it_core_news_md")
for s_idx, (text_options, vect_options, model_info) in enumerate(setups):
if model_info["type"] == "bert":
print("\t\tPreparing BERT model...")
# cuda_device = 0 if torch.cuda.is_available() else -1
cuda_device = None if use_gpu and torch.cuda.is_available() else -1
transformer = model_info["options"]["transformer"]
token_indexers = {"tokens": PretrainedTransformerIndexer(transformer)}
tokenizer = PretrainedTransformerTokenizer(transformer)
reader = MigrationReader(token_indexers, tokenizer)
train_instances = list(
reader.read_instances(x_train, y_train)
)
dev_instances = list(
reader.read_instances(x_dev, y_dev)
)
vocab = Vocabulary.from_instances(train_instances + dev_instances)
print(vocab.get_vocab_size("tags"))
embedder = BasicTextFieldEmbedder(
{"tokens": PretrainedTransformerEmbedder(transformer)}
)
seq2vec = BertPooler(transformer)
model = BasicClassifier(vocab, embedder, seq2vec, namespace="tags")
if use_gpu:
model = model.cuda(cuda_device)
checkpoint_dir = f"/scratch/p289731/cda_classify/model_{attrib}/checkpoints/"
serialization_dir = f"/scratch/p289731/cda_classify/model_{attrib}/serialize/"
os.makedirs(checkpoint_dir)
os.makedirs(serialization_dir)
checkpointer = Checkpointer(checkpoint_dir)
optimizer = AdamOptimizer(
[(n, p) for n, p in model.named_parameters() if p.requires_grad],
lr=1e-6
)
train_loader = SimpleDataLoader(train_instances, batch_size=8, shuffle=True)
dev_loader = SimpleDataLoader(dev_instances, batch_size=8, shuffle=False)
train_loader.index_with(vocab)
dev_loader.index_with(vocab)
print("\t\tTraining BERT model")
trainer = GradientDescentTrainer(
model,
optimizer,
train_loader,
validation_data_loader=dev_loader,
patience=32,
checkpointer=checkpointer,
cuda_device=cuda_device,
serialization_dir=serialization_dir
)
trainer.train()
print("\t\tProducing predictions...")
predictor = TextClassifierPredictor(model, reader)
predictions = [predictor.predict(sentence) for sentence in x_dev]
y_dev_pred = [p["label"] for p in predictions]
class_labels = list(vocab.get_token_to_index_vocabulary("labels").keys())
elif model_info["type"] in ["svm", "tree", "dummy"]:
# extract features
print("\t\tExtracting features...")
x_train_fts, vectorizer = extract_features(
x_train, nlp, text_options, **vect_options
)
x_dev_fts, _ = extract_features(
x_dev, nlp, text_options, **vect_options, vectorizer=vectorizer
)
if not vect_options["embed"]:
print(f"\t\t\tnum features: {len(vectorizer.vocabulary_)}")
else:
assert model_info["type"] != "tree", "Decision tree does not support embedding input"
print("\t\tTraining the model...")
if model_info["type"] == "svm":
model = SVC(**model_info["options"])
elif model_info["type"] == "tree":
model = DecisionTreeClassifier()
else:
model = DummyClassifier()
model.fit(x_train_fts, y_train)
# evaluate on dev
print("\t\tValidating the model...")
y_dev_pred = model.predict(x_dev_fts)
class_labels = model.classes_
p_micro, r_micro, f_micro, _ = precision_recall_fscore_support(
y_dev, y_dev_pred, average="micro"
)
p_classes, r_classes, f_classes, _ = precision_recall_fscore_support(
y_dev, y_dev_pred, average=None, labels=class_labels, zero_division=0
)
print(
f"\t\t\tOverall scores (micro-averaged):\tP={p_micro}\tR={r_micro}\tF={f_micro}"
)
scores.append(
{
"micro": {"p": p_micro, "r": r_micro, "f": f_micro},
"classes": {
"p": list(zip(class_labels, p_classes)),
"r": list(zip(class_labels, r_classes)),
"f": list(zip(class_labels, f_classes)),
},
}
)
prediction_df = pd.DataFrame(
zip(x_dev, y_dev, y_dev_pred), columns=["headline", "gold", "prediction"]
)
prediction_df.to_csv(
f"output/migration/cda_classify/predictions_{attrib}_{s_idx:02}.csv"
)
with open(
f"output/migration/cda_classify/scores_{attrib}.json", "w", encoding="utf-8"
) as f_scores:
json.dump(scores, f_scores, indent=4)
def load_data(attrib):
train_data = pd.read_csv("output/migration/preprocess/annotations_train.csv")
dev_data = pd.read_csv("output/migration/preprocess/annotations_dev.csv")
x_train = train_data["Titolo"]
x_dev = dev_data["Titolo"]
if attrib == "cda_frame":
y_train = train_data["frame"]
y_dev = dev_data["frame"]
elif attrib == "riferimento":
y_train = train_data["riferimento"]
y_dev = dev_data["riferimento"]
elif attrib == "orientation":
y_train = train_data["orientation"]
y_dev = dev_data["orientation"]
# fake task to test setup
else:
y_train = pd.Series(["true" if "rifugiato" in exa else "false" for exa in x_train])
y_dev = pd.Series(["true" if "rifugiato" in exa else "false" for exa in x_dev])
return x_train, y_train, x_dev, y_dev
def extract_features(
headlines,
nlp,
text_options,
embed=False,
min_freq=1,
max_freq=1.0,
ngram_range=(1, 1),
vectorizer=None,
):
if embed:
vectorized = np.array(
[vec for vec in process_text(headlines, nlp, embed=True, **text_options)]
)
else:
tokenized = [
" ".join(sent) for sent in process_text(headlines, nlp, **text_options)
]
if vectorizer is None:
vectorizer = CountVectorizer(
lowercase=False,
analyzer="word",
min_df=min_freq,
max_df=max_freq,
ngram_range=ngram_range,
)
vectorized = vectorizer.fit_transform(tokenized)
else:
vectorized = vectorizer.transform(tokenized)
return vectorized, vectorizer
def process_text(
headlines,
nlp,
embed=False,
remove_punct=True,
lowercase=True,
lemmatize=False,
remove_stop=False,
):
for sent in headlines:
doc = nlp(sent)
tokens = (
t
for t in doc
if (not remove_stop or not t.is_stop)
and (not remove_punct or t.pos_ not in ["PUNCT", "SYM", "X"])
)
if embed:
if lemmatize:
tokens = (t.vocab[t.lemma].vector for t in tokens)
else:
tokens = (t.vector for t in tokens if t.has_vector)
else:
if lemmatize:
tokens = (t.lemma_ for t in tokens)
else:
tokens = (t.text for t in tokens)
if lowercase:
tokens = (t.lower() for t in tokens)
if embed:
token_arr = np.array([t for t in tokens])
if len(token_arr) == 0:
yield np.random.rand(300)
else:
yield np.mean(token_arr, axis=0)
else:
yield list(tokens)
if __name__ == "__main__":
use_gpu = True if sys.argv[1] == "gpu" else False
# train(attrib="fake", use_gpu=use_gpu)
train(attrib="cda_frame", use_gpu=use_gpu)
# train(attrib="riferimento")
# train(attrib="orientation")
|