kitkatdafu commited on
Commit
84c740d
1 Parent(s): 37b44d4

Upload jester_embedding.py

Browse files
Files changed (1) hide show
  1. jester_embedding.py +92 -0
jester_embedding.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import datasets
3
+ from sklearn.model_selection import train_test_split
4
+
5
+ _CITATION = "N/A"
6
+ _DESCRIPTION = "Embeddings for the jokes in Jester jokes dataset"
7
+ _HOMEPAGE = "N/A"
8
+ _LICENSE = "apache-2.0"
9
+
10
+ _URLS = {
11
+ "mistral": "jester-salesforce-sfr-embedding-mistral.pt",
12
+ "instructor-xl": "jester-hkunlp-instructor-xl.pt",
13
+ "all-MiniLM-L6-v2": "jester-sentence-transformers-all-MiniLM-L6-v2.pt",
14
+ "all-mpnet-base-v2": "jester-sentence-transformers-all-mpnet-base-v2.pt",
15
+ }
16
+
17
+
18
+ _DIMS = {
19
+ "mistral": 4096,
20
+ "instructor-xl": 768,
21
+ "all-MiniLM-L6-v2": 384,
22
+ "all-mpnet-base-v2": 768,
23
+ }
24
+
25
+
26
+ class JesterEmbedding(datasets.GeneratorBasedBuilder):
27
+
28
+ VERSION = datasets.Version("0.0.1")
29
+
30
+ BUILDER_CONFIGS = [
31
+ datasets.BuilderConfig(name="mistral", version=VERSION, description="SFR-Embedding by Salesforce Research."),
32
+ datasets.BuilderConfig(name="instructor-xl", version=VERSION, description="Instructor embedding"),
33
+ datasets.BuilderConfig(name="all-MiniLM-L6-v2", version=VERSION, description="All-round model embedding tuned for many use-cases"),
34
+ datasets.BuilderConfig(name="all-mpnet-base-v2", version=VERSION, description="All-round model embedding tuned for many use-cases"),
35
+ ]
36
+
37
+ DEFAULT_CONFIG_NAME = "mistral" # It's not mandatory to have a default configuration. Just use one if it make sense.
38
+
39
+ def _info(self):
40
+ features = datasets.Features({"x": datasets.Array2D(shape=_DIMS[self.config.name], dtype="float32")})
41
+ return datasets.DatasetInfo(
42
+ description=_DESCRIPTION,
43
+ features=features,
44
+ homepage=_HOMEPAGE,
45
+ license=_LICENSE,
46
+ citation=_CITATION,
47
+ )
48
+
49
+ def _split_generators(self, dl_manager):
50
+ urls = _URLS[self.config.name]
51
+ data_dir = dl_manager.download_and_extract(urls)
52
+ return [
53
+ datasets.SplitGenerator(
54
+ name=datasets.Split.TRAIN,
55
+ # These kwargs will be passed to _generate_examples
56
+ gen_kwargs={
57
+ "filepath": data_dir,
58
+ "split": "train",
59
+ },
60
+ ),
61
+ datasets.SplitGenerator(
62
+ name=datasets.Split.VALIDATION,
63
+ # These kwargs will be passed to _generate_examples
64
+ gen_kwargs={
65
+ "filepath": data_dir,
66
+ "split": "dev",
67
+ },
68
+ ),
69
+ datasets.SplitGenerator(
70
+ name=datasets.Split.TEST,
71
+ # These kwargs will be passed to _generate_examples
72
+ gen_kwargs={
73
+ "filepath": data_dir,
74
+ "split": "test"
75
+ },
76
+ ),
77
+ ]
78
+
79
+ def _generate_examples(self, filepath, split):
80
+ embeddings = torch.load(f=filepath, map_location="cpu")
81
+ train, test = train_test_split(embeddings, test_size=0.2, random_state=42)
82
+ train, val = train_test_split(train, test_size=0.2, random_state=42)
83
+
84
+ if split == "train":
85
+ for _id, x in enumerate(train):
86
+ yield _id, {"x": x}
87
+ elif split == "test":
88
+ for _id, x in enumerate(test):
89
+ yield _id, {"x": x}
90
+ else:
91
+ for _id, x in enumerate(val):
92
+ yield _id, {"x": x}