Sentence Pair Classification
In sentence-pair classification, each example in a dataset has two sentences along with the appropriate target variable. E.g. Sentence similarity, entailment, etc.
Sentence pairs are supported in all classification subtasks.
Note: Input dataframes must contain the three columns, text_a
, text_b
, and labels
. See Sentence-Pair Data Format.
Note: The predict()
function expects a list of lists. A single sample input should also be a list of lists like [[text_a, text_b]]. See Sentence-Pair Data Format.
Tip: Refer to ClassificationModel for details on configuring a classification model.
Minimal Start
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
from simpletransformers.classification import (
ClassificationModel, ClassificationArgs
)
import pandas as pd
import logging
logging.basicConfig(level=logging.INFO)
transformers_logger = logging.getLogger("transformers")
transformers_logger.setLevel(logging.WARNING)
# Preparing train data
train_data = [
[
"Aragorn was the heir of Isildur",
"Gimli fought with a battle axe",
1,
],
[
"Frodo was the heir of Isildur",
"Legolas was an expert archer",
0,
],
]
train_df = pd.DataFrame(train_data)
train_df.columns = ["text_a", "text_b", "labels"]
# Preparing eval data
eval_data = [
[
"Theoden was the king of Rohan",
"Gimli's preferred weapon was a battle axe",
1,
],
[
"Merry was the king of Rohan",
"Legolas was taller than Gimli",
0,
],
]
eval_df = pd.DataFrame(eval_data)
eval_df.columns = ["text_a", "text_b", "labels"]
# Optional model configuration
model_args = ClassificationArgs(num_train_epochs=1)
# Create a ClassificationModel
model = ClassificationModel("roberta", "roberta-base")
# Train the model
model.train_model(train_df)
# Evaluate the model
result, model_outputs, wrong_predictions = model.eval_model(
eval_df
)
# Make predictions with the model
predictions, raw_outputs = model.predict(
[
[
"Legolas was an expert archer",
"Legolas was taller than Gimli",
]
]
)