Views
No views yet
1from transformers import pipeline
2pipe = pipeline("text-classification", model="dipawidia/xlnet-base-cased-product-review-sentiment-analysis")
3pipe("This shoes is awesome")[{'label': 'Positive', 'score': 0.9995703101158142}]1from transformers import XLNetTokenizer, TFXLNetForSequenceClassification
2import tensorflow as tf
3import numpy as np
4
5tokenizer = XLNetTokenizer.from_pretrained("dipawidia/xlnet-base-cased-product-review-sentiment-analysis")
6model = TFXLNetForSequenceClassification.from_pretrained("dipawidia/xlnet-base-cased-product-review-sentiment-analysis")
7
8def get_sentimen(text):
9 tokenize_text = tokenizer(text, return_tensors = 'tf')
10 preds = model.predict(dict(tokenize_text))['logits']
11 class_preds = np.argmax(tf.keras.layers.Softmax()(preds))
12 if class_preds == 1:
13 label = 'Positive'
14 else:
15 label = 'Negative'
16 return(label)
17
18get_sentimen('i hate this product')Negative| Train Loss | Train Accuracy | Validation Loss | Validation Accuracy | Epoch |
|---|---|---|---|---|
| 0.3417 | 0.8491 | 0.1568 | 0.9449 | 0 |
| 0.1943 | 0.9235 | 0.1504 | 0.9466 | 1 |
| 0.1569 | 0.9404 | 0.1612 | 0.9466 | 2 |
| 0.1238 | 0.9572 | 0.1748 | 0.9475 | 3 |
| 0.1085 | 0.9617 | 0.1910 | 0.9414 | 4 |