Views
No views yet

1train_val_data, test_data = train_test_split(data, test_size=0.1, random_state=42)
2# Then split train+validation into train and validation
3train_data, val_data = train_test_split(train_val_data, test_size=0.1, random_state=42)1 num_train_epochs=1, # Number of training epochs
2 per_device_train_batch_size=32, # Batch size per device during training
3 per_device_eval_batch_size=64, # Batch size per device during evaluation
4 learning_rate=0.00001,
5 warmup_steps=100, # Number of warmup steps for learning rate scheduler
6 weight_decay=0.01, # Strength of weight decay
7 evaluation_strategy="steps", # Evaluate every 'eval_steps'
8 eval_steps=50, # Evaluation frequency in steps
9 logging_steps=50, # Log every eval_steps
10 save_steps=150, # Save model every 500 steps
11 save_total_limit=2,
12 load_best_model_at_end=True, # Load the best model when finished training
13 metric_for_best_model="eval_loss", # Metric to use for best model selection| Model | Test Set of Synthetic Dataset | Benchmark 1 (WTQ Validation Set) | Benchmark 2 (TabFact) | Benchmark 3 (SQA) |
|---|---|---|---|---|
| google/tapas-base-finetuned-wtq (before Fine-tuning) | 0.2933 | 0.3405 | 0.5005 | 0.2512 |
| google/tapas-base-finetuned-wtq (Fine-tuned) | 0.4667 | 0.3405 | 0.5005 | 0.2525 |
| mistralai/Mistral-7B-Instruct-v0.3 | 0 | Exact Match: 0.0346 / Fuzzy Match: 0.4744 | 0.4995 | 0.0296 |
| meta-llama/Llama-3.2-1B | 0.0133 | Exact Match: 0.0593 / Fuzzy Match: 0.2769 | 0.4995 | 0.0238 |
1saved_path = "am5uc/ServiceNow_Table_Question_Answering"
2tokenizer = TapasTokenizer.from_pretrained(saved_path)
3model = TapasForQuestionAnswering.from_pretrained(saved_path)
4
5question = "How many Hardware Upgrade changes are still pending?"
6table_df = pd.DataFrame({
7 "change_id": [
8 "CHG3000",
9 "CHG3001",
10 "CHG3002",
11 "CHG3003"
12 ],
13 "category": [
14 "Security Patch",
15 "Software Update",
16 "Hardware Upgrade",
17 "Software Update"
18 ],
19 "status": [
20 "Rejected",
21 "In Progress",
22 "In Progress",
23 "Completed"
24 ],
25 "approved_by": [
26 "",
27 "Manager2",
28 "",
29 "Admin1"
30 ],
31 "implementation_date": [
32 "",
33 "",
34 "",
35 "2023-05-30"
36 ]
37})
38
39# Tokenize both Question and Table together
40inputs = tokenizer(table=table_df, queries=[question], padding='max_length', return_tensors='pt')
41
42# Model prediction
43# --- Helper function ---
44def get_final_answer(model, tokenizer, inputs, table_df):
45 outputs = model(**inputs)
46
47 logits = outputs.logits
48 logits_agg = outputs.logits_aggregation
49
50 predicted_answer_coordinates, predicted_aggregation_indices = tokenizer.convert_logits_to_predictions(
51 inputs,
52 logits.detach(),
53 logits_agg=logits_agg.detach()
54 )
55
56 aggregation_operators = ["NONE", "SUM", "AVERAGE", "COUNT"]
57
58 agg_op_idx = predicted_aggregation_indices[0] if predicted_aggregation_indices else 0
59 agg_op = aggregation_operators[agg_op_idx]
60
61 predicted_cells = []
62 for coord in predicted_answer_coordinates[0]:
63 cell_value = table_df.iat[coord[0], coord[1]]
64 predicted_cells.append(cell_value)
65
66 if agg_op == "COUNT":
67 answer = len(predicted_cells)
68 elif agg_op == "SUM":
69 try:
70 answer = sum(float(cell) for cell in predicted_cells)
71 except ValueError:
72 answer = "Could not SUM non-numeric cells"
73 elif agg_op == "AVERAGE":
74 try:
75 answer = sum(float(cell) for cell in predicted_cells) / len(predicted_cells)
76 except ValueError:
77 answer = "Could not AVERAGE non-numeric cells"
78 else: # NONE
79 answer = predicted_cells
80
81 return agg_op, answer, predicted_cells
82
83_, answer, _ = get_final_answer(model, tokenizer, inputs, table_df)
84
85print(answer)
861question = "How many Hardware Upgrade changes are still pending?"
2table_df = pd.DataFrame({
3 "change_id": [
4 "CHG3000",
5 "CHG3001",
6 "CHG3002",
7 "CHG3003"
8 ],
9 "category": [
10 "Security Patch",
11 "Software Update",
12 "Hardware Upgrade",
13 "Software Update"
14 ],
15 "status": [
16 "Rejected",
17 "In Progress",
18 "In Progress",
19 "Completed"
20 ],
21 "approved_by": [
22 "",
23 "Manager2",
24 "",
25 "Admin1"
26 ],
27 "implementation_date": [
28 "",
29 "",
30 "",
31 "2023-05-30"
32 ]
33})
34
35inputs = tokenizer(table=table_df, queries=[question], padding='max_length', return_tensors='pt')
361# Tokenize both Question and Table together
2inputs = tokenizer(table=table_df, queries=[question], padding='max_length', return_tensors='pt')
3
4# Model prediction
5 ##--- Helper function ---
6
7def get_final_answer(model, tokenizer, inputs, table_df):
8 outputs = model(**inputs)
9
10 logits = outputs.logits
11 logits_agg = outputs.logits_aggregation
12
13 predicted_answer_coordinates, predicted_aggregation_indices = tokenizer.convert_logits_to_predictions(
14 inputs,
15 logits.detach(),
16 logits_agg=logits_agg.detach()
17 )
18
19 aggregation_operators = ["NONE", "SUM", "AVERAGE", "COUNT"]
20
21 agg_op_idx = predicted_aggregation_indices[0] if predicted_aggregation_indices else 0
22 agg_op = aggregation_operators[agg_op_idx]
23
24 predicted_cells = []
25 for coord in predicted_answer_coordinates[0]:
26 cell_value = table_df.iat[coord[0], coord[1]]
27 predicted_cells.append(cell_value)
28
29 if agg_op == "COUNT":
30 answer = len(predicted_cells)
31 elif agg_op == "SUM":
32 try:
33 answer = sum(float(cell) for cell in predicted_cells)
34 except ValueError:
35 answer = "Could not SUM non-numeric cells"
36 elif agg_op == "AVERAGE":
37 try:
38 answer = sum(float(cell) for cell in predicted_cells) / len(predicted_cells)
39 except ValueError:
40 answer = "Could not AVERAGE non-numeric cells"
41 else: # NONE
42 answer = predicted_cells
43
44 return agg_op, answer, predicted_cells
45
46_, answer, _ = get_final_answer(model, tokenizer, inputs, table_df)
47print(answer)