Aureliano commited on
Commit
77e4d25
1 Parent(s): 7aa36eb
README.md CHANGED
@@ -1,3 +1,74 @@
1
  ---
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
  ---
4
+
5
+ ## How to use the discriminator in `transformers` on a custom dataset
6
+ (Heavily based on: https://github.com/huggingface/notebooks/blob/master/examples/text_classification-tf.ipynb)
7
+
8
+ ```python
9
+ import math
10
+
11
+ import tensorflow as tf
12
+ from datasets import Dataset, ClassLabel, Features, Value
13
+ from transformers import TFAutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding, create_optimizer
14
+
15
+ # This example shows how this model can be used:
16
+ # you should finetune the model of your specific corpus if commands, bogger than this
17
+ dict_train = {
18
+ "idx": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"],
19
+ "sentence": ["e", "get pen", "drop book", "x paper", "i", "south", "get paper", "drop pen", "x book", "inventory",
20
+ "n", "get book", "drop paper", "examine Pen", "inv", "w"],
21
+ "label": ["v01835496", "v01214265", "v01977701", "v02131279", "v02472495", "v01835496", "v01214265", "v01977701",
22
+ "v02131279", "v02472495", "v01835496", "v01214265", "v01977701", "v02131279", "v02472495", "v01835496"]
23
+ }
24
+
25
+ num_labels = len(set(dict_train["label"]))
26
+ features = Features({'idx': Value('uint32'), 'sentence': Value('string'),
27
+ 'label': ClassLabel(names=list(set(dict_train["label"])))})
28
+
29
+ raw_train_dataset = Dataset.from_dict(dict_train, features=features)
30
+
31
+ discriminator = TFAutoModelForSequenceClassification.from_pretrained("Aureliano/distilbert-base-uncased-if", num_labels=num_labels)
32
+ tokenizer = AutoTokenizer.from_pretrained("Aureliano/distilbert-base-uncased-if")
33
+
34
+ tokenize_function = lambda example: tokenizer(example["sentence"], truncation=True)
35
+
36
+ pre_tokenizer_columns = set(raw_train_dataset.features)
37
+ train_dataset = raw_train_dataset.map(tokenize_function, batched=True)
38
+ tokenizer_columns = list(set(train_dataset.features) - pre_tokenizer_columns)
39
+
40
+ data_collator = DataCollatorWithPadding(tokenizer=tokenizer, return_tensors="tf")
41
+
42
+ batch_size = 16
43
+ tf_train_dataset = train_dataset.to_tf_dataset(
44
+ columns=tokenizer_columns,
45
+ label_cols=["labels"],
46
+ shuffle=True,
47
+ batch_size=batch_size,
48
+ collate_fn=data_collator
49
+ )
50
+
51
+ loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
52
+ num_epochs = 100
53
+ batches_per_epoch = math.ceil(len(train_dataset) / batch_size)
54
+ total_train_steps = int(batches_per_epoch * num_epochs)
55
+
56
+ optimizer, schedule = create_optimizer(
57
+ init_lr=1e-5, num_warmup_steps=1, num_train_steps=total_train_steps
58
+ )
59
+
60
+ discriminator.compile(optimizer=optimizer, loss=loss)
61
+ discriminator.fit(
62
+ tf_train_dataset,
63
+ epochs=num_epochs
64
+ )
65
+
66
+ text = "get lamp"
67
+ encoded_input = tokenizer(text, return_tensors='tf')
68
+ output = discriminator(encoded_input)
69
+ prediction = tf.nn.softmax(output["logits"][0], -1)
70
+ label = dict_train["label"][tf.math.argmax(prediction)]
71
+ print(text, ":", label)
72
+ # ideally [v01214265 -> take.v.04 -> "get into one's hands, take physically"], but probably only with a better dataset
73
+
74
+ ```
config.json ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "wn_full_classifier-trainer",
3
+ "activation": "gelu",
4
+ "architectures": [
5
+ "DistilBertModel"
6
+ ],
7
+ "attention_dropout": 0.1,
8
+ "dim": 768,
9
+ "dropout": 0.1,
10
+ "hidden_dim": 3072,
11
+ "id2label": {
12
+ "0": "LABEL_0",
13
+ "1": "LABEL_1",
14
+ "2": "LABEL_2",
15
+ "3": "LABEL_3",
16
+ "4": "LABEL_4",
17
+ "5": "LABEL_5",
18
+ "6": "LABEL_6",
19
+ "7": "LABEL_7",
20
+ "8": "LABEL_8",
21
+ "9": "LABEL_9",
22
+ "10": "LABEL_10",
23
+ "11": "LABEL_11",
24
+ "12": "LABEL_12",
25
+ "13": "LABEL_13",
26
+ "14": "LABEL_14",
27
+ "15": "LABEL_15",
28
+ "16": "LABEL_16",
29
+ "17": "LABEL_17",
30
+ "18": "LABEL_18",
31
+ "19": "LABEL_19",
32
+ "20": "LABEL_20",
33
+ "21": "LABEL_21",
34
+ "22": "LABEL_22",
35
+ "23": "LABEL_23",
36
+ "24": "LABEL_24",
37
+ "25": "LABEL_25",
38
+ "26": "LABEL_26",
39
+ "27": "LABEL_27",
40
+ "28": "LABEL_28",
41
+ "29": "LABEL_29",
42
+ "30": "LABEL_30",
43
+ "31": "LABEL_31",
44
+ "32": "LABEL_32",
45
+ "33": "LABEL_33",
46
+ "34": "LABEL_34",
47
+ "35": "LABEL_35",
48
+ "36": "LABEL_36",
49
+ "37": "LABEL_37",
50
+ "38": "LABEL_38",
51
+ "39": "LABEL_39",
52
+ "40": "LABEL_40",
53
+ "41": "LABEL_41",
54
+ "42": "LABEL_42",
55
+ "43": "LABEL_43",
56
+ "44": "LABEL_44",
57
+ "45": "LABEL_45",
58
+ "46": "LABEL_46",
59
+ "47": "LABEL_47",
60
+ "48": "LABEL_48",
61
+ "49": "LABEL_49",
62
+ "50": "LABEL_50",
63
+ "51": "LABEL_51",
64
+ "52": "LABEL_52",
65
+ "53": "LABEL_53",
66
+ "54": "LABEL_54",
67
+ "55": "LABEL_55",
68
+ "56": "LABEL_56",
69
+ "57": "LABEL_57",
70
+ "58": "LABEL_58",
71
+ "59": "LABEL_59",
72
+ "60": "LABEL_60",
73
+ "61": "LABEL_61",
74
+ "62": "LABEL_62",
75
+ "63": "LABEL_63",
76
+ "64": "LABEL_64",
77
+ "65": "LABEL_65",
78
+ "66": "LABEL_66",
79
+ "67": "LABEL_67",
80
+ "68": "LABEL_68",
81
+ "69": "LABEL_69",
82
+ "70": "LABEL_70",
83
+ "71": "LABEL_71",
84
+ "72": "LABEL_72",
85
+ "73": "LABEL_73",
86
+ "74": "LABEL_74",
87
+ "75": "LABEL_75",
88
+ "76": "LABEL_76",
89
+ "77": "LABEL_77",
90
+ "78": "LABEL_78",
91
+ "79": "LABEL_79",
92
+ "80": "LABEL_80",
93
+ "81": "LABEL_81",
94
+ "82": "LABEL_82",
95
+ "83": "LABEL_83"
96
+ },
97
+ "initializer_range": 0.02,
98
+ "label2id": {
99
+ "LABEL_0": 0,
100
+ "LABEL_1": 1,
101
+ "LABEL_10": 10,
102
+ "LABEL_11": 11,
103
+ "LABEL_12": 12,
104
+ "LABEL_13": 13,
105
+ "LABEL_14": 14,
106
+ "LABEL_15": 15,
107
+ "LABEL_16": 16,
108
+ "LABEL_17": 17,
109
+ "LABEL_18": 18,
110
+ "LABEL_19": 19,
111
+ "LABEL_2": 2,
112
+ "LABEL_20": 20,
113
+ "LABEL_21": 21,
114
+ "LABEL_22": 22,
115
+ "LABEL_23": 23,
116
+ "LABEL_24": 24,
117
+ "LABEL_25": 25,
118
+ "LABEL_26": 26,
119
+ "LABEL_27": 27,
120
+ "LABEL_28": 28,
121
+ "LABEL_29": 29,
122
+ "LABEL_3": 3,
123
+ "LABEL_30": 30,
124
+ "LABEL_31": 31,
125
+ "LABEL_32": 32,
126
+ "LABEL_33": 33,
127
+ "LABEL_34": 34,
128
+ "LABEL_35": 35,
129
+ "LABEL_36": 36,
130
+ "LABEL_37": 37,
131
+ "LABEL_38": 38,
132
+ "LABEL_39": 39,
133
+ "LABEL_4": 4,
134
+ "LABEL_40": 40,
135
+ "LABEL_41": 41,
136
+ "LABEL_42": 42,
137
+ "LABEL_43": 43,
138
+ "LABEL_44": 44,
139
+ "LABEL_45": 45,
140
+ "LABEL_46": 46,
141
+ "LABEL_47": 47,
142
+ "LABEL_48": 48,
143
+ "LABEL_49": 49,
144
+ "LABEL_5": 5,
145
+ "LABEL_50": 50,
146
+ "LABEL_51": 51,
147
+ "LABEL_52": 52,
148
+ "LABEL_53": 53,
149
+ "LABEL_54": 54,
150
+ "LABEL_55": 55,
151
+ "LABEL_56": 56,
152
+ "LABEL_57": 57,
153
+ "LABEL_58": 58,
154
+ "LABEL_59": 59,
155
+ "LABEL_6": 6,
156
+ "LABEL_60": 60,
157
+ "LABEL_61": 61,
158
+ "LABEL_62": 62,
159
+ "LABEL_63": 63,
160
+ "LABEL_64": 64,
161
+ "LABEL_65": 65,
162
+ "LABEL_66": 66,
163
+ "LABEL_67": 67,
164
+ "LABEL_68": 68,
165
+ "LABEL_69": 69,
166
+ "LABEL_7": 7,
167
+ "LABEL_70": 70,
168
+ "LABEL_71": 71,
169
+ "LABEL_72": 72,
170
+ "LABEL_73": 73,
171
+ "LABEL_74": 74,
172
+ "LABEL_75": 75,
173
+ "LABEL_76": 76,
174
+ "LABEL_77": 77,
175
+ "LABEL_78": 78,
176
+ "LABEL_79": 79,
177
+ "LABEL_8": 8,
178
+ "LABEL_80": 80,
179
+ "LABEL_81": 81,
180
+ "LABEL_82": 82,
181
+ "LABEL_83": 83,
182
+ "LABEL_9": 9
183
+ },
184
+ "max_position_embeddings": 512,
185
+ "model_type": "distilbert",
186
+ "n_heads": 12,
187
+ "n_layers": 6,
188
+ "pad_token_id": 0,
189
+ "problem_type": "single_label_classification",
190
+ "qa_dropout": 0.1,
191
+ "seq_classif_dropout": 0.2,
192
+ "sinusoidal_pos_embds": false,
193
+ "tie_weights_": true,
194
+ "torch_dtype": "float32",
195
+ "transformers_version": "4.17.0",
196
+ "vocab_size": 30522
197
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:12eaf2d9db88163a36fbbc5e45331658bc35d26421cb061f05c473c7708a30d4
3
+ size 265487161
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tf_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d789b0b93a1504985b62a35e46433ea14548042e4bdb2e9710619198d6a7b62e
3
+ size 265571752
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"do_lower_case": true, "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "tokenize_chinese_chars": true, "strip_accents": null, "model_max_length": 512, "special_tokens_map_file": null, "name_or_path": "distilbert-base-uncased", "tokenizer_class": "DistilBertTokenizer"}
vocab.txt ADDED
The diff for this file is too large to render. See raw diff