Update mathrandom-progressive.py
Browse files- mathrandom-progressive.py +134 -23
mathrandom-progressive.py
CHANGED
|
@@ -1,46 +1,157 @@
|
|
| 1 |
import random
|
| 2 |
import json
|
| 3 |
import operator
|
|
|
|
| 4 |
|
| 5 |
num_samples = [20000, 50000, 230000, 500000]
|
| 6 |
min_values = [-9.99, -19.99, -99.99, -99.99]
|
| 7 |
max_values = [9.99, 19.99, 99.99, 99.99]
|
| 8 |
vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"]
|
| 9 |
result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"]
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
'+': operator.add,
|
| 12 |
'-': operator.sub,
|
| 13 |
'*': operator.mul,
|
| 14 |
-
'/': operator.truediv
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
|
|
|
|
| 17 |
def safe_division(numerator, denominator):
|
| 18 |
return numerator if denominator == 0 else numerator / denominator
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
data = []
|
| 22 |
-
for _ in range(num_samples
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
if
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
else:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
data.append({'instruction': expression, 'output': output})
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
out_file = f'arithmetic-float-complex_{idx}.json'
|
| 45 |
with open(out_file, 'w') as f:
|
| 46 |
-
json.dump(data, f)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
import json
|
| 3 |
import operator
|
| 4 |
+
import math
|
| 5 |
|
| 6 |
num_samples = [20000, 50000, 230000, 500000]
|
| 7 |
min_values = [-9.99, -19.99, -99.99, -99.99]
|
| 8 |
max_values = [9.99, 19.99, 99.99, 99.99]
|
| 9 |
vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"]
|
| 10 |
result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"]
|
| 11 |
+
seed = 42
|
| 12 |
+
|
| 13 |
+
random.seed(seed)
|
| 14 |
+
|
| 15 |
+
# Binary operations
|
| 16 |
+
binary_operations = {
|
| 17 |
'+': operator.add,
|
| 18 |
'-': operator.sub,
|
| 19 |
'*': operator.mul,
|
| 20 |
+
'/': operator.truediv,
|
| 21 |
+
#'**': operator.pow, # it makes tons of troubles, but if u wanna contribute with a functional fix (L)
|
| 22 |
+
'%': operator.mod
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# Safe division to avoid division by zero
|
| 26 |
def safe_division(numerator, denominator):
|
| 27 |
return numerator if denominator == 0 else numerator / denominator
|
| 28 |
|
| 29 |
+
# Unary operations
|
| 30 |
+
def safe_sqrt(x):
|
| 31 |
+
return math.sqrt(x) if x >= 0 else 0
|
| 32 |
+
|
| 33 |
+
def safe_log(x):
|
| 34 |
+
return math.log(x) if x > 0 else 0
|
| 35 |
+
|
| 36 |
+
unary_operations = {
|
| 37 |
+
'sqrt': safe_sqrt,
|
| 38 |
+
'sin': math.sin,
|
| 39 |
+
'cos': math.cos,
|
| 40 |
+
'log': safe_log
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
def format_result(result, format_spec):
|
| 44 |
+
if isinstance(result, complex):
|
| 45 |
+
real_part = format_spec % result.real
|
| 46 |
+
imag_part = format_spec % result.imag
|
| 47 |
+
return f"{real_part} + {imag_part}j"
|
| 48 |
+
else:
|
| 49 |
+
return format_spec % result
|
| 50 |
+
|
| 51 |
+
def safe_modulo(numerator, denominator):
|
| 52 |
+
return 0 if denominator == 0 else numerator % denominator
|
| 53 |
+
|
| 54 |
+
def safe_power(base, exponent):
|
| 55 |
+
if base == 0 and exponent < 0:
|
| 56 |
+
return 0 # or some other value that signifies an undefined operation
|
| 57 |
+
else:
|
| 58 |
+
return base ** exponent
|
| 59 |
+
|
| 60 |
+
def generate_sub_expression(min_value, max_value, val_float):
|
| 61 |
+
num1 = float(val_float % random.uniform(min_value, max_value))
|
| 62 |
+
num2 = float(val_float % random.uniform(min_value, max_value))
|
| 63 |
+
operation = random.choice(list(binary_operations.keys()))
|
| 64 |
+
|
| 65 |
+
if operation == '/':
|
| 66 |
+
result = safe_division(num1, num2)
|
| 67 |
+
elif operation == '%':
|
| 68 |
+
result = safe_modulo(num1, num2)
|
| 69 |
+
else:
|
| 70 |
+
result = binary_operations[operation](num1, num2)
|
| 71 |
+
|
| 72 |
+
expression = f"({num1} {operation} {num2})"
|
| 73 |
+
return expression, result
|
| 74 |
+
|
| 75 |
+
def generate_expression(num_samples, min_value, max_value, val_float, result_float):
|
| 76 |
data = []
|
| 77 |
+
for _ in range(num_samples):
|
| 78 |
+
sub_ops_count = random.randint(0, 2) # Number of sub-operations (0, 1, or 2), the code is not tailored for more cases, but feel free to improve it.
|
| 79 |
+
sub_expressions = [generate_sub_expression(min_value, max_value, val_float) for _ in range(sub_ops_count)]
|
| 80 |
+
|
| 81 |
+
# Main operation
|
| 82 |
+
main_num1 = float(val_float % random.uniform(min_value, max_value))
|
| 83 |
+
main_num2 = float(val_float % random.uniform(min_value, max_value))
|
| 84 |
+
main_operation = random.choice(list(binary_operations.keys()))
|
| 85 |
+
|
| 86 |
+
# Embed sub-expressions into main expression
|
| 87 |
+
if sub_expressions:
|
| 88 |
+
if len(sub_expressions) == 1:
|
| 89 |
+
sub_expr, sub_result = sub_expressions[0]
|
| 90 |
+
if random.choice([True, False]):
|
| 91 |
+
main_expression = f"{sub_expr} {main_operation} {main_num2}"
|
| 92 |
+
if main_operation == '/':
|
| 93 |
+
main_result = safe_division(sub_result, main_num2)
|
| 94 |
+
elif main_operation == '%':
|
| 95 |
+
main_result = safe_modulo(sub_result, main_num2)
|
| 96 |
+
else:
|
| 97 |
+
main_result = binary_operations[main_operation](sub_result, main_num2)
|
| 98 |
+
else:
|
| 99 |
+
main_expression = f"{main_num1} {main_operation} {sub_expr}"
|
| 100 |
+
if main_operation == '/':
|
| 101 |
+
main_result = safe_division(main_num1, sub_result)
|
| 102 |
+
elif main_operation == '%':
|
| 103 |
+
main_result = safe_modulo(main_num1, sub_result)
|
| 104 |
+
else:
|
| 105 |
+
main_result = binary_operations[main_operation](main_num1, sub_result)
|
| 106 |
+
else:
|
| 107 |
+
sub_expr1, sub_result1 = sub_expressions[0]
|
| 108 |
+
sub_expr2, sub_result2 = sub_expressions[1]
|
| 109 |
+
main_expression = f"{sub_expr1} {main_operation} {sub_expr2}"
|
| 110 |
+
if main_operation == '/':
|
| 111 |
+
main_result = safe_division(sub_result1, sub_result2)
|
| 112 |
+
elif main_operation == '%':
|
| 113 |
+
main_result = safe_modulo(sub_result1, sub_result2)
|
| 114 |
+
else:
|
| 115 |
+
main_result = binary_operations[main_operation](sub_result1, sub_result2)
|
| 116 |
else:
|
| 117 |
+
main_expression = f"{main_num1} {main_operation} {main_num2}"
|
| 118 |
+
if main_operation == '/':
|
| 119 |
+
main_result = safe_division(main_num1, main_num2)
|
| 120 |
+
elif main_operation == '%':
|
| 121 |
+
main_result = safe_modulo(main_num1, main_num2)
|
| 122 |
+
else:
|
| 123 |
+
main_result = binary_operations[main_operation](main_num1, main_num2)
|
| 124 |
+
|
| 125 |
+
output = format_result(main_result, result_float)
|
| 126 |
+
data.append({'instruction': main_expression, 'output': output})
|
| 127 |
|
| 128 |
+
return data
|
|
|
|
| 129 |
|
| 130 |
+
|
| 131 |
+
# DPO (Follows Ultrabinarized Format)
|
| 132 |
+
def make_prompt(expression, output):
|
| 133 |
+
prompt = {
|
| 134 |
+
'prompt': 'What is the result of the following expression?',
|
| 135 |
+
'chosen': [],
|
| 136 |
+
'rejected': [],
|
| 137 |
+
'messages': []
|
| 138 |
+
}
|
| 139 |
+
prompt['chosen'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'})
|
| 140 |
+
prompt['chosen'].append({'content': output, 'role': 'assistant'})
|
| 141 |
+
prompt['rejected'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'})
|
| 142 |
+
prompt['rejected'].append({'content': output[0:2], 'role': 'assistant'})
|
| 143 |
+
prompt['messages'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'})
|
| 144 |
+
prompt['messages'].append({'content': output, 'role': 'assistant'})
|
| 145 |
+
return prompt
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# Generate and write data for each set of parameters
|
| 149 |
+
for idx, sample_count in enumerate(num_samples):
|
| 150 |
+
data = generate_expression(sample_count, min_values[idx], max_values[idx], vals_floats[idx], result_floats[idx])
|
| 151 |
+
dpo_data = [make_prompt(elem['instruction'], elem['output']) for elem in data]
|
| 152 |
out_file = f'arithmetic-float-complex_{idx}.json'
|
| 153 |
with open(out_file, 'w') as f:
|
| 154 |
+
json.dump(data, f)
|
| 155 |
+
out_file = f'arithmetic-float-complex_DPO_{idx}.json'
|
| 156 |
+
with open(out_file, 'w') as f:
|
| 157 |
+
json.dump(dpo_data, f)
|