MCPcopy Create free account
hub / github.com/alexrame/fishr / LeaveOneOutSelectionMethod

Class LeaveOneOutSelectionMethod

domainbed/model_selection.py:102–141  ·  view source on GitHub ↗

Picks (hparams, step) by leave-one-out cross validation.

Source from the content-addressed store, hash-verified

100 return test_records.map(self._step_acc).argmax('val_acc')
101
102class LeaveOneOutSelectionMethod(SelectionMethod):
103 """Picks (hparams, step) by leave-one-out cross validation."""
104 name = "leave-one-domain-out cross-validation"
105
106 @classmethod
107 def _step_acc(self, records):
108 """Return the {val_acc, test_acc} for a group of records corresponding
109 to a single step."""
110 test_records = get_test_records(records)
111 if len(test_records) != 1:
112 return None
113
114 test_env = test_records[0]['args']['test_envs'][0]
115 n_envs = 0
116 for i in itertools.count():
117 if f'env{i}_out_acc' not in records[0]:
118 break
119 n_envs += 1
120 val_accs = np.zeros(n_envs) - 1
121 for r in records.filter(lambda r: len(r['args']['test_envs']) == 2):
122 val_env = (set(r['args']['test_envs']) - set([test_env])).pop()
123 val_accs[val_env] = r['env{}_in_acc'.format(val_env)]
124 val_accs = list(val_accs[:test_env]) + list(val_accs[test_env+1:])
125 if any([v==-1 for v in val_accs]):
126 return None
127 val_acc = np.sum(val_accs) / (n_envs-1)
128 return {
129 'val_acc': val_acc,
130 'test_acc': test_records[0]['env{}_in_acc'.format(test_env)]
131 }
132
133 @classmethod
134 def run_acc(self, records):
135 step_accs = records.group('step').map(lambda step, step_records:
136 self._step_acc(step_records)
137 ).filter_not_none()
138 if len(step_accs):
139 return step_accs.argmax('val_acc')
140 else:
141 return None

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected