MCPcopy Create free account
hub / github.com/eric-mitchell/direct-preference-optimization / get_shp

Function get_shp

preference_datasets.py:85–117  ·  view source on GitHub ↗

Load the Stanford Human Preferences dataset from Huggingface and convert it to the necessary format. See hh for the format. We filter preference pairs to only keep pairs where the score ratio is at least 2. For this dataset, the sft_target is the response with the highest score.

(split: str, silent: bool = False, cache_dir: str = None)

Source from the content-addressed store, hash-verified

83 return data
84
85def get_shp(split: str, silent: bool = False, cache_dir: str = None) -> Dict[str, Dict[str, Union[List[Tuple[int, int]], List[str], str]]]:
86 """Load the Stanford Human Preferences dataset from Huggingface and convert it to the necessary format. See hh for the format.
87
88 We filter preference pairs to only keep pairs where the score ratio is at least 2.
89 For this dataset, the sft_target is the response with the highest score.
90 """
91 print(f'Loading SHP dataset ({split} split) from Huggingface...')
92 dataset = datasets.load_dataset('stanfordnlp/SHP', split=split, cache_dir=cache_dir)
93 print('done')
94
95 data = defaultdict(lambda: defaultdict(list))
96 for row in tqdm.tqdm(dataset, desc='Processing SHP', disable=silent):
97 prompt = '\n\nHuman: ' + row['history'] + '\n\nAssistant:'
98 responses = [' ' + row['human_ref_A'], ' ' + row['human_ref_B']]
99 scores = [row['score_A'], row['score_B']]
100 if prompt in data:
101 n_responses = len(data[prompt]['responses'])
102 else:
103 n_responses = 0
104 score_ratio = max(scores[0] / scores[1], scores[1] / scores[0])
105 if score_ratio < 2:
106 continue
107
108 # according to https://huggingface.co/datasets/stanfordnlp/SHP
109 data[prompt]['pairs'].append((n_responses, n_responses + 1) if row['labels'] == 1 else (n_responses + 1, n_responses))
110 data[prompt]['responses'].extend(responses)
111 data[prompt]['scores'].extend(scores)
112
113 for prompt in data:
114 data[prompt]['sft_target'] = max(data[prompt]['responses'], key=lambda x: data[prompt]['scores'][data[prompt]['responses'].index(x)])
115 del data[prompt]['scores']
116
117 return data
118
119
120def get_hh(split: str, silent: bool = False, cache_dir: str = None) -> Dict[str, Dict[str, Union[List[Tuple[int, int]], List[str], str]]]:

Callers 1

get_datasetFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected