JackyChunKit commited on
Commit
48c4afc
·
verified ·
1 Parent(s): 9919ebb

Upload gsm8k.py

Browse files
Files changed (1) hide show
  1. gsm8k.py +65 -0
gsm8k.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2024 Bytedance Ltd. and/or its affiliates
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import re
16
+
17
+
18
+ def extract_solution(solution_str, method='strict'):
19
+ assert method in ['strict', 'flexible']
20
+
21
+ if method == 'strict':
22
+ # this also tests the formatting of the model
23
+ solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str)
24
+ if solution is None:
25
+ final_answer = None
26
+ else:
27
+ final_answer = solution.group(0)
28
+ final_answer = final_answer.split('#### ')[1].replace(',', '').replace('$', '')
29
+ elif method == 'flexible':
30
+ answer = re.findall("(\\-?[0-9\\.\\,]+)", solution_str)
31
+ final_answer = None
32
+ if len(answer) == 0:
33
+ # no reward is there is no answer
34
+ pass
35
+ else:
36
+ invalid_str = ['', '.']
37
+ # find the last number that is not '.'
38
+ for final_answer in reversed(answer):
39
+ if final_answer not in invalid_str:
40
+ break
41
+ return final_answer
42
+
43
+
44
+ def compute_score(solution_str, ground_truth, method='strict', format_score=0.1, score=1.):
45
+ """The scoring function for GSM8k.
46
+
47
+ Reference: Trung, Luong, et al. "Reft: Reasoning with reinforced fine-tuning." Proceedings of the 62nd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). 2024.
48
+
49
+ Args:
50
+ solution_str: the solution text
51
+ ground_truth: the ground truth
52
+ method: the method to extract the solution, choices are 'strict' and 'flexible'
53
+ format_score: the score for the format
54
+ score: the score for the correct answer
55
+ """
56
+ answer = extract_solution(solution_str=solution_str, method=method)
57
+ if answer is None:
58
+ return 0
59
+ else:
60
+ if answer == ground_truth:
61
+ return 1.0
62
+ elif type(answer) != type(ground_truth):
63
+ print("...Data type is not correc.....")
64
+ else:
65
+ return 0