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

Method update

domainbed/algorithms.py:1034–1102  ·  view source on GitHub ↗
(self, minibatches, unlabeled=None)

Source from the content-addressed store, hash-verified

1032 )
1033
1034 def update(self, minibatches, unlabeled=None):
1035
1036 all_x = torch.cat([x for x, y in minibatches])
1037 all_y = torch.cat([y for _, y in minibatches])
1038
1039 lam = np.random.beta(0.5, 0.5)
1040
1041 batch_size = all_y.size()[0]
1042
1043 # cluster and order features into same-class group
1044 with torch.no_grad():
1045 sorted_y, indices = torch.sort(all_y)
1046 sorted_x = torch.zeros_like(all_x)
1047 for idx, order in enumerate(indices):
1048 sorted_x[idx] = all_x[order]
1049 intervals = []
1050 ex = 0
1051 for idx, val in enumerate(sorted_y):
1052 if ex == val:
1053 continue
1054 intervals.append(idx)
1055 ex = val
1056 intervals.append(batch_size)
1057
1058 all_x = sorted_x
1059 all_y = sorted_y
1060
1061 feat = self.featurizer(all_x)
1062 proj = self.cdpl(feat)
1063
1064 output = self.classifier(feat)
1065
1066 # shuffle
1067 output_2 = torch.zeros_like(output)
1068 feat_2 = torch.zeros_like(proj)
1069 output_3 = torch.zeros_like(output)
1070 feat_3 = torch.zeros_like(proj)
1071 ex = 0
1072 for end in intervals:
1073 shuffle_indices = torch.randperm(end - ex) + ex
1074 shuffle_indices2 = torch.randperm(end - ex) + ex
1075 for idx in range(end - ex):
1076 output_2[idx + ex] = output[shuffle_indices[idx]]
1077 feat_2[idx + ex] = proj[shuffle_indices[idx]]
1078 output_3[idx + ex] = output[shuffle_indices2[idx]]
1079 feat_3[idx + ex] = proj[shuffle_indices2[idx]]
1080 ex = end
1081
1082 # mixup
1083 output_3 = lam * output_2 + (1 - lam) * output_3
1084 feat_3 = lam * feat_2 + (1 - lam) * feat_3
1085
1086 # regularization
1087 L_ind_logit = self.MSEloss(output, output_2)
1088 L_hdl_logit = self.MSEloss(output, output_3)
1089 L_ind_feat = 0.3 * self.MSEloss(feat, feat_2)
1090 L_hdl_feat = 0.3 * self.MSEloss(feat, feat_3)
1091

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected