mirror of
https://github.com/open-thought/reasoning-gym.git
synced 2026-04-26 17:13:17 +00:00
Feat/curr adj (#394)
This commit is contained in:
parent
2c52f33c3a
commit
43c739cb3e
26 changed files with 152390 additions and 453 deletions
|
|
@ -86,7 +86,7 @@ class LetterCountingCurriculum(BaseCurriculum):
|
|||
self._define_attributes(
|
||||
RangeAttributeDefinition(
|
||||
name="words",
|
||||
levels=[10, 50, 100, 1000],
|
||||
levels=list(range(5, 20, 2)),
|
||||
description="Number of words in the span",
|
||||
lower_field_name="min_words",
|
||||
upper_field_name="max_words",
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ from dataclasses import dataclass
|
|||
from random import Random
|
||||
from typing import Any, Optional
|
||||
|
||||
import numpy as np
|
||||
|
||||
from ..coaching import BaseCurriculum, RangeAttributeDefinition
|
||||
from ..factory import ProceduralDataset, register_dataset
|
||||
|
||||
|
|
@ -44,12 +46,6 @@ Please follow the instruction below:
|
|||
## 2. Convert all numbers in the square brackets as strings. For example, ['-69', '-13', '1', '7', '11', '43', '59', '61']
|
||||
"""
|
||||
|
||||
def _format_number(self, num: float, decimals: int) -> str:
|
||||
"""Format number with specified decimal places"""
|
||||
formatted = f"{num:.{decimals}f}"
|
||||
# Reparse to ensure exact decimal representation
|
||||
return f"{float(formatted):.{decimals}f}"
|
||||
|
||||
def _generate_numbers(self, rng: Random, count: int) -> tuple[list[float], list[str]]:
|
||||
"""Generate list of numbers and their string representations"""
|
||||
numbers = []
|
||||
|
|
@ -58,11 +54,9 @@ Please follow the instruction below:
|
|||
for _ in range(count):
|
||||
num = rng.uniform(self.config.min_value, self.config.max_value)
|
||||
decimals = rng.randint(self.config.min_decimals, self.config.max_decimals)
|
||||
num_str = self._format_number(num, decimals)
|
||||
# Reparse to ensure exact value
|
||||
num = float(num_str)
|
||||
num = np.round(num, decimals)
|
||||
numbers.append(num)
|
||||
number_strs.append(num_str)
|
||||
number_strs.append(str(num))
|
||||
|
||||
return numbers, number_strs
|
||||
|
||||
|
|
@ -78,9 +72,8 @@ Please follow the instruction below:
|
|||
desc_numbers = sorted(numbers, reverse=True)
|
||||
|
||||
# Format answers as string lists
|
||||
decimals = len(number_strs[0].split(".")[-1]) if "." in number_strs[0] else 0
|
||||
asc_answer = [self._format_number(n, decimals) for n in asc_numbers]
|
||||
desc_answer = [self._format_number(n, decimals) for n in desc_numbers]
|
||||
asc_answer = [str(n) for n in asc_numbers]
|
||||
desc_answer = [str(n) for n in desc_numbers]
|
||||
|
||||
# Randomly choose ascending or descending
|
||||
is_ascending = rng.choice([True, False])
|
||||
|
|
@ -158,7 +151,7 @@ Please follow the instruction below:
|
|||
return 0.0
|
||||
|
||||
# Check if the values are close enough (allowing for small rounding differences)
|
||||
tolerance = 0.1 # Increased tolerance to handle decimal differences
|
||||
tolerance = 1 # Increased tolerance to handle decimal differences
|
||||
for i in range(len(user_floats)):
|
||||
if abs(user_floats[i] - expected_floats[i]) > tolerance:
|
||||
return 0.0
|
||||
|
|
@ -177,7 +170,7 @@ class NumberSortingCurriculum(BaseCurriculum):
|
|||
self._define_attributes(
|
||||
RangeAttributeDefinition(
|
||||
name="numbers",
|
||||
levels=[10, 100, 500, 1000],
|
||||
levels=list(range(5, 20, 2)),
|
||||
description="How many numbers to sort",
|
||||
lower_field_name="min_numbers",
|
||||
upper_field_name="max_numbers",
|
||||
|
|
@ -185,7 +178,7 @@ class NumberSortingCurriculum(BaseCurriculum):
|
|||
),
|
||||
RangeAttributeDefinition(
|
||||
name="decimals",
|
||||
levels=[0, 2, 4, 6],
|
||||
levels=list(range(0, 8)),
|
||||
description="Number of decimal places",
|
||||
lower_field_name="min_decimals",
|
||||
upper_field_name="max_decimals",
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ class SpellBackwardConfig:
|
|||
"""Configuration for spelling words backward task generation"""
|
||||
|
||||
min_word_len: int = 3 # Minimum word length
|
||||
max_word_len: int = 20 # Maximum word length
|
||||
max_word_len: int = 10 # Maximum word length
|
||||
seed: Optional[int] = None
|
||||
data_file: str = "words3to10.txt"
|
||||
size: int = 500 # Virtual dataset size
|
||||
|
||||
def validate(self) -> None:
|
||||
|
|
@ -34,12 +35,11 @@ class SpellBackwardDataset(ProceduralDataset):
|
|||
super().__init__(config=config, seed=config.seed, size=config.size)
|
||||
|
||||
# Load and preprocess text
|
||||
text = read_data_file("in_the_year_2889.txt")
|
||||
# Extract words and clean them to contain only alphanumeric characters
|
||||
text = read_data_file(self.config.data_file)
|
||||
self.words = [
|
||||
word
|
||||
for word in re.findall(r"\b\w+\b", text)
|
||||
if word.isalnum() and config.min_word_len <= len(word) <= config.max_word_len
|
||||
word.strip()
|
||||
for word in text.splitlines()
|
||||
if word.strip().isalnum() and config.min_word_len <= len(word.strip()) <= config.max_word_len
|
||||
]
|
||||
|
||||
def __getitem__(self, idx: int) -> dict:
|
||||
|
|
@ -69,10 +69,22 @@ class SpellBackwardDataset(ProceduralDataset):
|
|||
expected_answer = entry["answer"]
|
||||
if isinstance(answer, str):
|
||||
try:
|
||||
if expected_answer.lower() == answer.lower():
|
||||
reward = 1.0
|
||||
expected_answer = expected_answer.lower()
|
||||
answer = answer.lower()
|
||||
if expected_answer == answer:
|
||||
return 1.0
|
||||
else:
|
||||
reward = 0.05
|
||||
answer_len = len(expected_answer)
|
||||
for i in range(len(expected_answer)):
|
||||
if i < len(expected_answer) and i < len(answer):
|
||||
if expected_answer[i] == answer[i]:
|
||||
reward += 1 / answer_len
|
||||
else:
|
||||
continue
|
||||
else:
|
||||
break
|
||||
if reward == 1.0:
|
||||
reward -= 0.2
|
||||
except:
|
||||
reward = 0.0
|
||||
return reward
|
||||
|
|
@ -86,11 +98,11 @@ class SpellBackwardCurriculum(BaseCurriculum):
|
|||
self._define_attributes(
|
||||
RangeAttributeDefinition(
|
||||
name="word_len",
|
||||
levels=[5, 10, 20, 30],
|
||||
levels=list(range(3, 11, 1)),
|
||||
description="Word length",
|
||||
lower_field_name="min_word_len",
|
||||
upper_field_name="max_word_len",
|
||||
ensure_interval=True,
|
||||
ensure_interval=False,
|
||||
),
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -125,14 +125,25 @@ class WordSortingDataset(ProceduralDataset):
|
|||
|
||||
def score_answer(self, answer: Optional[str], entry: dict[str, Any]) -> float:
|
||||
oracle_answer = entry["metadata"]["sorted_words"]
|
||||
if answer is not None and len(answer) > 0:
|
||||
parsed_answer = [word.strip() for word in re.split(r",\s*", answer)]
|
||||
if parsed_answer == oracle_answer:
|
||||
return 1.0
|
||||
elif sorted(parsed_answer) == oracle_answer:
|
||||
return 0.2
|
||||
|
||||
return 0.0
|
||||
if not answer:
|
||||
return 0.0
|
||||
|
||||
parsed_answer = [word.strip() for word in re.split(r",\s*", answer)]
|
||||
|
||||
if parsed_answer == oracle_answer:
|
||||
return 1.0
|
||||
|
||||
correct_positions = sum(
|
||||
1 for i, word in enumerate(parsed_answer) if i < len(oracle_answer) and word == oracle_answer[i]
|
||||
)
|
||||
|
||||
partial_score = correct_positions / len(oracle_answer)
|
||||
|
||||
if sorted(parsed_answer) == sorted(oracle_answer):
|
||||
partial_score = max(partial_score, 0.2)
|
||||
|
||||
return partial_score
|
||||
|
||||
|
||||
class WordSortingCurriculum(BaseCurriculum):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue