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

Class TemporarilySeededRandom

utils.py:156–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

154
155
156class TemporarilySeededRandom:
157 def __init__(self, seed):
158 """Temporarily set the random seed, and then restore it when exiting the context."""
159 self.seed = seed
160 self.stored_state = None
161 self.stored_np_state = None
162
163 def __enter__(self):
164 # Store the current random state
165 self.stored_state = random.getstate()
166 self.stored_np_state = np.random.get_state()
167
168 # Set the random seed
169 random.seed(self.seed)
170 np.random.seed(self.seed)
171
172 def __exit__(self, exc_type, exc_value, traceback):
173 # Restore the random state
174 random.setstate(self.stored_state)
175 np.random.set_state(self.stored_np_state)

Callers 1

get_batch_iteratorFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected