-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseline_w_gpt_embed.py
146 lines (123 loc) · 5.49 KB
/
baseline_w_gpt_embed.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
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
import os
import numpy as np
from tqdm import tqdm
from torch.utils.data import DataLoader
from openai import OpenAI
from pyod.models.auto_encoder import AutoEncoder
from pyod.models.deep_svdd import DeepSVDD
from pyod.models.ecod import ECOD
from pyod.models.iforest import IForest
from pyod.models.lof import LOF
from pyod.models.lunar import LUNAR
from pyod.models.so_gaal_new import SO_GAAL
from pyod.models.vae import VAE
from utils import SynthDataset, TextDataset, evaluate
from config import DefaultConfig, PrivacyConfig
default_config = DefaultConfig()
privacy_config = PrivacyConfig()
baseline_map = {
"autoencoder": AutoEncoder,
"deepsvdd": DeepSVDD,
"ecod": ECOD,
"iforest": IForest,
"lof": LOF,
"lunar": LUNAR,
"so_gaal": SO_GAAL,
"vae": VAE
}
# parameter setting follows the default values in NLP-ADBench
params_map = {
"autoencoder": {"batch_size": 4, "epoch_num": 30, "contamination": 0.1},
"deepsvdd": {"batch_size": 4, "use_ae": False,
"epochs": 5, "contamination": 0.1,
"random_state": 10},
"ecod": {},
"iforest": {},
"lof": {},
"lunar": {},
"so_gaal": {"epoch_num": 30, "contamination": 0.1, "verbose": 2},
"vae": {"batch_size": 4, "epoch_num": 30, "contamination": 0.1,
"beta": 0.8, "capacity": 0.2}
}
def init_gpt():
client = OpenAI(
organization="org-hl3WFJbV0CMfCWTIVmS7JbiA",
project="proj_5GhbbJZ4xGLQCxfrHyyJU299",
api_key=privacy_config.gpt_api_key
)
return client
def generate_embeddings(gpt_client, dataloader):
embeddings = []
for text_batch in tqdm(dataloader):
response = gpt_client.embeddings.create(
model="text-embedding-3-large",
input=text_batch,
)
embeddings.append([item.embedding for item in response.data])
embeddings = np.vstack(embeddings)
return embeddings
def main(baseline_name="lunar"):
# available baselines:
# ["autoencoder", "deepsvdd", "ecod", "iforest", "lof", "lunar", "so_gaal", "vae"]
if baseline_name not in baseline_map:
raise ValueError(f"Invalid baseline name: {baseline_name}")
print(f"Baseline: {baseline_name}")
gpt_client = init_gpt()
test_dataset = TextDataset(default_config.data_name, model_name="gpt")
test_X = test_dataset.get_X()
test_dataloader = DataLoader(test_X, batch_size=default_config.batch_size,
shuffle=False, drop_last=False)
test_gt = test_dataset.get_labels()
test_gt = np.array(test_gt)
use_desc = ""
if default_config._use_desc:
use_desc = "_use_desc"
cur_dir = os.path.dirname(__file__)
data_dir = os.path.join(cur_dir, 'data')
data_name = default_config.data_name
part_file_path = os.path.join(data_dir, data_name, f"{data_name}_gpt_part_embeddings.npy")
part_and_synth_file_path = os.path.join(data_dir, data_name, f"{data_name}_gpt_part_and_synth_embeddings{use_desc}.npy")
test_file_path = os.path.join(data_dir, data_name, f"{data_name}_test_embeddings.npy")
if not os.path.exists(part_file_path):
part_X = SynthDataset(default_config.data_name, mode=0, model_name="gpt")
part_dataloader = DataLoader(part_X, batch_size=default_config.batch_size,
shuffle=True, drop_last=False)
part_embeddings = generate_embeddings(gpt_client, part_dataloader)
np.save(part_file_path, part_embeddings)
else:
part_embeddings = np.load(part_file_path)
if not os.path.exists(part_and_synth_file_path):
part_and_synth_X = SynthDataset(default_config.data_name, mode=1, model_name="gpt")
part_and_synth_dataloader = DataLoader(part_and_synth_X, batch_size=default_config.batch_size,
shuffle=True, drop_last=False)
part_and_synth_embeddings = generate_embeddings(gpt_client, part_and_synth_dataloader)
np.save(part_and_synth_file_path, part_and_synth_embeddings)
else:
part_and_synth_embeddings = np.load(part_and_synth_file_path)
if not os.path.exists(test_file_path):
test_embeddings = generate_embeddings(gpt_client, test_dataloader)
np.save(test_file_path, test_embeddings)
else:
test_embeddings = np.load(test_file_path)
print(f"Part embeddings shape: {part_embeddings.shape}")
print(f"Part and synth embeddings shape: {part_and_synth_embeddings.shape}")
print(f"Test embeddings shape: {test_embeddings.shape}")
if baseline_name == "deepsvdd":
detector_part = DeepSVDD(n_features=part_embeddings.shape[1],
**params_map[baseline_name])
detector_part_and_synth = DeepSVDD(n_features=part_and_synth_embeddings.shape[1],
**params_map[baseline_name])
else:
detector_part = baseline_map[baseline_name](**params_map[baseline_name])
detector_part_and_synth = baseline_map[baseline_name](**params_map[baseline_name])
detector_part.fit(part_embeddings)
detector_part_and_synth.fit(part_and_synth_embeddings)
test_score_part = detector_part.predict_proba(test_embeddings)[:, -1]
test_score_part_and_synth = detector_part_and_synth.predict_proba(test_embeddings)[:, -1]
print("without synthetic data:")
evaluate(test_gt, test_score_part)
print("********************************")
print("Part and synth:")
evaluate(test_gt, test_score_part_and_synth)
if __name__ == "__main__":
main(baseline_name="lunar")