| | import random |
| | import json |
| | import operator |
| | import math |
| |
|
| | num_samples = [20000, 50000, 230000, 500000] |
| | min_values = [-9.99, -19.99, -99.99, -99.99] |
| | max_values = [9.99, 19.99, 99.99, 99.99] |
| | vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"] |
| | result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"] |
| | seed = 42 |
| |
|
| | random.seed(seed) |
| |
|
| | |
| | binary_operations = { |
| | '+': operator.add, |
| | '-': operator.sub, |
| | '*': operator.mul, |
| | '/': operator.truediv, |
| | |
| | '%': operator.mod |
| | } |
| |
|
| | |
| | def safe_division(numerator, denominator): |
| | return numerator if denominator == 0 else numerator / denominator |
| |
|
| | |
| | def safe_sqrt(x): |
| | return math.sqrt(x) if x >= 0 else 0 |
| |
|
| | def safe_log(x): |
| | return math.log(x) if x > 0 else 0 |
| |
|
| | unary_operations = { |
| | 'sqrt': safe_sqrt, |
| | 'sin': math.sin, |
| | 'cos': math.cos, |
| | 'log': safe_log |
| | } |
| |
|
| | def format_result(result, format_spec): |
| | if isinstance(result, complex): |
| | real_part = format_spec % result.real |
| | imag_part = format_spec % result.imag |
| | return f"{real_part} + {imag_part}j" |
| | else: |
| | return format_spec % result |
| |
|
| | def safe_modulo(numerator, denominator): |
| | return 0 if denominator == 0 else numerator % denominator |
| |
|
| | def safe_power(base, exponent): |
| | if base == 0 and exponent < 0: |
| | return 0 |
| | else: |
| | return base ** exponent |
| |
|
| | def generate_sub_expression(min_value, max_value, val_float): |
| | num1 = float(val_float % random.uniform(min_value, max_value)) |
| | num2 = float(val_float % random.uniform(min_value, max_value)) |
| | operation = random.choice(list(binary_operations.keys())) |
| |
|
| | if operation == '/': |
| | result = safe_division(num1, num2) |
| | elif operation == '%': |
| | result = safe_modulo(num1, num2) |
| | else: |
| | result = binary_operations[operation](num1, num2) |
| |
|
| | expression = f"({num1} {operation} {num2})" |
| | return expression, result |
| |
|
| | def generate_expression(num_samples, min_value, max_value, val_float, result_float): |
| | data = [] |
| | for _ in range(num_samples): |
| | sub_ops_count = random.randint(0, 2) |
| | sub_expressions = [generate_sub_expression(min_value, max_value, val_float) for _ in range(sub_ops_count)] |
| |
|
| | |
| | main_num1 = float(val_float % random.uniform(min_value, max_value)) |
| | main_num2 = float(val_float % random.uniform(min_value, max_value)) |
| | main_operation = random.choice(list(binary_operations.keys())) |
| |
|
| | |
| | if sub_expressions: |
| | if len(sub_expressions) == 1: |
| | sub_expr, sub_result = sub_expressions[0] |
| | if random.choice([True, False]): |
| | main_expression = f"{sub_expr} {main_operation} {main_num2}" |
| | if main_operation == '/': |
| | main_result = safe_division(sub_result, main_num2) |
| | elif main_operation == '%': |
| | main_result = safe_modulo(sub_result, main_num2) |
| | else: |
| | main_result = binary_operations[main_operation](sub_result, main_num2) |
| | else: |
| | main_expression = f"{main_num1} {main_operation} {sub_expr}" |
| | if main_operation == '/': |
| | main_result = safe_division(main_num1, sub_result) |
| | elif main_operation == '%': |
| | main_result = safe_modulo(main_num1, sub_result) |
| | else: |
| | main_result = binary_operations[main_operation](main_num1, sub_result) |
| | else: |
| | sub_expr1, sub_result1 = sub_expressions[0] |
| | sub_expr2, sub_result2 = sub_expressions[1] |
| | main_expression = f"{sub_expr1} {main_operation} {sub_expr2}" |
| | if main_operation == '/': |
| | main_result = safe_division(sub_result1, sub_result2) |
| | elif main_operation == '%': |
| | main_result = safe_modulo(sub_result1, sub_result2) |
| | else: |
| | main_result = binary_operations[main_operation](sub_result1, sub_result2) |
| | else: |
| | main_expression = f"{main_num1} {main_operation} {main_num2}" |
| | if main_operation == '/': |
| | main_result = safe_division(main_num1, main_num2) |
| | elif main_operation == '%': |
| | main_result = safe_modulo(main_num1, main_num2) |
| | else: |
| | main_result = binary_operations[main_operation](main_num1, main_num2) |
| |
|
| | output = format_result(main_result, result_float) |
| | data.append({'instruction': main_expression, 'output': output}) |
| |
|
| | return data |
| |
|
| |
|
| | |
| | def make_prompt(expression, output): |
| | prompt = { |
| | 'prompt': 'What is the result of the following expression?', |
| | 'chosen': [], |
| | 'rejected': [], |
| | 'messages': [] |
| | } |
| | prompt['chosen'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'}) |
| | prompt['chosen'].append({'content': output, 'role': 'assistant'}) |
| | prompt['rejected'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'}) |
| | prompt['rejected'].append({'content': output[0:2], 'role': 'assistant'}) |
| | prompt['messages'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'}) |
| | prompt['messages'].append({'content': output, 'role': 'assistant'}) |
| | return prompt |
| |
|
| |
|
| | |
| | for idx, sample_count in enumerate(num_samples): |
| | data = generate_expression(sample_count, min_values[idx], max_values[idx], vals_floats[idx], result_floats[idx]) |
| | dpo_data = [make_prompt(elem['instruction'], elem['output']) for elem in data] |
| | out_file = f'arithmetic-float-complex_{idx}.json' |
| | with open(out_file, 'w') as f: |
| | json.dump(data, f) |
| | out_file = f'arithmetic-float-complex_DPO_{idx}.json' |
| | with open(out_file, 'w') as f: |
| | json.dump(dpo_data, f) |