-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain_twec.py
82 lines (72 loc) · 2.2 KB
/
train_twec.py
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
import os
import time
import numpy as np
import streamlit as st
from twec.twec import TWEC
def train(
data_dir="./data/",
embedding_size=300,
skipgram=False,
siter=10,
diter=10,
negative_samples=10,
window_size=5,
output_path="./model",
overwrite_compass=True,
streamlit=False,
component=None,
):
if streamlit and component is None:
raise ValueError("`component` cannot be `None` when `streamlit` is `True`.")
aligner = TWEC(
size=embedding_size,
sg=int(skipgram),
siter=siter,
diter=diter,
workers=4,
ns=negative_samples,
window=window_size,
opath=output_path,
)
if streamlit:
component.write("Training")
progress = 0.0
progress_bar = component.progress(progress)
output = component.beta_expander("Output")
all_files = sorted(os.listdir(data_dir))
num_files = len(all_files)
start = time.time()
# train the compass: the text should be the concatenation of the text from the slices
aligner.train_compass(
os.path.join(data_dir, "compass.txt"), overwrite=overwrite_compass
)
# keep an eye on the overwrite behaviour
end = time.time()
compass_out = f"Time Taken for TWEC Pre-Training: {(end - start)} ms"
if not streamlit:
print(compass_out)
else:
progress += 1 / num_files
progress_bar.progress(np.round(progress, decimals=1))
with output:
st.write(compass_out)
slices = {}
for file in all_files:
if file != "compass.txt":
start = time.time()
slices[file.split(".")[0]] = aligner.train_slice(
os.path.join(data_dir, file), save=True
)
end = time.time()
year_out = f"Time Taken for TWEC Fine-tuning for {file.split('.')[0]}: {(end - start)} ms"
if not streamlit:
print(year_out)
else:
progress += 1 / num_files
if progress > 1.0:
progress = 1.0
progress_bar.progress(progress)
with output:
st.write(year_out)
if __name__ == "__main__":
train()