-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain-bert.py
More file actions
171 lines (133 loc) · 4.98 KB
/
train-bert.py
File metadata and controls
171 lines (133 loc) · 4.98 KB
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
from dotenv import load_dotenv
import os
from sqlalchemy import create_engine, text as sql_text
import pandas as pd
from tqdm.auto import tqdm
from transformers import TrainingArguments, Trainer, EarlyStoppingCallback
import transformers as tfm
import numpy as np
import evaluate
import logging
from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer
from datasets import Dataset, Features, Value, ClassLabel, DatasetDict
from sklearn.model_selection import train_test_split
# Get the root logger
logger = logging.getLogger()
# Set the logging level
logger.setLevel(logging.INFO)
# Create handlers for both console and file
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler("output.log")
# Create a formatter
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S"
)
# Set the formatter for both handlers
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# Add both handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
load_dotenv()
logger.info("Connecting to database")
conn = create_engine(
f"postgresql://{os.getenv('PG_USER')}:{os.getenv('PG_PASSWORD')}@{os.getenv('PG_HOST')}/{os.getenv('PG_DBNAME')}"
).connect()
logger.info("Reading data from database")
temp_readings = pd.read_sql_query(sql_text("SELECT * FROM public.temp_readings"), conn)
fire_alerts = pd.read_sql_query(sql_text("SELECT * FROM public.fire_alerts"), conn)
tweets = pd.read_sql_query(sql_text("SELECT * FROM public.tweets"), conn)
# output all tweets that occur at the date and location of a fire event
# first, get all the dates and locations of fire events
logger.info("Processing fire events and tweets")
fires = []
for i, row in fire_alerts.iterrows():
fires.append((row["event_day"], row["xy"]))
# then, get all the tweets that occur at the date and location of a fire event
tqdm.pandas()
fire_tweets = tweets.progress_apply(
lambda row: (row["day"], row["xy"]) in fires, axis=1
)
fire_samples = tweets.loc[fire_tweets]
not_fire_samples = tweets.loc[~fire_tweets]
tweet_samples_x = (
fire_samples["content"].values.tolist()
+ not_fire_samples["content"].values.tolist()
)
tweet_samples_y = [1] * len(fire_samples) + [0] * len(not_fire_samples)
# preprocess our data
# we need to split our data into train and test sets first
# then we need to correct any class imbalance
logger.info("Preprocessing data and splitting into train, test, and validation sets")
train_x, test_x, train_y, test_y = train_test_split(
tweet_samples_x, tweet_samples_y, test_size=0.2, stratify=tweet_samples_y
)
train_x, val_x, train_y, val_y = train_test_split(
train_x, train_y, test_size=0.2, stratify=train_y
)
features = Features(
{
"text": Value("string"),
"label": ClassLabel(num_classes=2, names=["no_fire", "yes_fire"]),
}
)
dataset = DatasetDict(
{
"train": Dataset.from_dict(
{"text": train_x, "label": train_y}, features=features
),
"test": Dataset.from_dict({"text": test_x, "label": test_y}, features=features),
"validation": Dataset.from_dict(
{"text": val_x, "label": val_y}, features=features
),
}
)
logger.info("Tokenizing datasets")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
def tokenize_function(examples):
return tokenizer(
examples["text"], padding="max_length", truncation=True, max_length=300
)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
tokenized_datasets = tokenized_datasets.remove_columns("text")
id2label = {0: "no_fire", 1: "yes_fire"}
label2id = {"no_fire": 0, "yes_fire": 1}
logger.info("Initializing model")
model = AutoModelForSequenceClassification.from_pretrained(
"bert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id
)
def compute_metrics(eval_pred):
logits, labels = eval_pred
predictions = np.argmax(logits, axis=-1)
return metric.compute(predictions=predictions, references=labels)
tfm.logging.set_verbosity_info()
logger.info("Setting up training arguments")
training_args = TrainingArguments(
output_dir="test_trainer",
evaluation_strategy="steps",
save_strategy="steps",
num_train_epochs=3,
load_best_model_at_end=True,
auto_find_batch_size=True,
report_to="none",
per_device_train_batch_size=128,
per_device_eval_batch_size=128,
)
metric = evaluate.load("accuracy")
logger.info("Creating Trainer and beginning training")
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"].shuffle(seed=42),
eval_dataset=tokenized_datasets["validation"].shuffle(seed=42),
compute_metrics=compute_metrics,
tokenizer=tokenizer,
callbacks=[EarlyStoppingCallback(early_stopping_patience=3)],
)
trainer.train()
# evaluate the model
logger.info("Evaluating the model")
trainer.evaluate(tokenized_datasets["test"])
logger.info("Saving the model")
trainer.save_model()