Views
No views yet
1from tensorflow.keras.models import load_model
2from huggingface_hub import from_pretrained_keras
3import tensorflow as tf
4from transformers import TFAutoModel, AutoTokenizer
5
6
7class BERTForClassification(tf.keras.Model):
8 def __init__(self, bert_model, num_classes):
9 super(BERTForClassification, self).__init__()
10 self.bert = bert_model
11 self.fc = tf.keras.layers.Dense(num_classes, activation='softmax')
12
13 def call(self, inputs):
14 x = self.bert(inputs)[1]
15 return self.fc(x)
16
17bert_model = TFAutoModel.from_pretrained("bert-base-uncased")
18custom_objects = {
19 'BERTForClassification': BERTForClassification(bert_model, num_classes=2)
20}
21
22model = from_pretrained_keras("jeanong2/finetuned-bert-aita-classifier", custom_objects=custom_objects)
23tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
24
25# Inference
26def inference_analysis(model, text):
27 encoding = tokenizer(text, padding='max_length', truncation=True, max_length=512, return_tensors="tf")
28 inputs = {
29 'input_ids': encoding['input_ids'],
30 'attention_mask': encoding['attention_mask']
31 }
32 if 'token_type_ids' in encoding:
33 inputs['token_type_ids'] = encoding['token_type_ids']
34 test_dataset = tf.data.Dataset.from_tensor_slices((inputs))
35 test_dataset = test_dataset.batch(1)
36 predictions = model.predict(test_dataset)
37 print("Probabilities for 0 and 1 :")
38 print(predictions)
39
40text = '''AITA for making out with this dude's ex in front of him? | I play rugby with the guy in question (let's say, "Mark") but I don't usually hang out with him outside of matches and practice.
41He broke up with a woman (Jia) that I'm rather attracted to last week. For the sake of propriety, I had no real intention to make moves or anything.
42But last night I'm at a bar, and both Mark and Jia are there. I was at a table with some friends, and he was a couple tables over.
43Jia's with her friends as well but after a time comes over to my table and sits next to me, starts chatting me up.
44We flirt, and eventually she leans in and kisses me, and I reciprocate. I tend to think that PDA of that kind is a bit trashy so after a few seconds I get up with her and we go outside,
45but I can see that Mark has been watching the entire time. He makes a rude comment to both of us as we pass.
46Today at practice he picked a fight with me that would have come to blows if the other guys on the team hadn't held him back.
47He's steaming mad. I feel a little sorry for him, but at the moment I can't actually bring myself to feel bad about hooking up with Jia, or the fact that he was there for it. AITA here?'''
48
49inference_analysis(model, text)
50| Hyperparameters | Value |
|---|---|
| name | Adam |
| weight_decay | None |
| clipnorm | None |
| global_clipnorm | None |
| clipvalue | None |
| use_ema | False |
| ema_momentum | 0.99 |
| ema_overwrite_frequency | None |
| jit_compile | True |
| is_legacy_optimizer | False |
| learning_rate | 9.999999747378752e-06 |
| beta_1 | 0.9 |
| beta_2 | 0.999 |
| epsilon | 1e-07 |
| amsgrad | False |
| training_precision | float32 |