def forward_hook(module, input, output, module_name=None): # Compose module id module_iter = counter[module_name] module_id = (module_name, module_iter) # Check if the module has to be altered if module_id in dirs: # TODO: handle convolutional layers # TODO: use canonical basis if basis[module_id] is None: basis[module_id] = torch.identity(output) # Change basis output = output @ basis[module_id] # Zero out the directions mask = torch.ones_like(output) mask[:, dirs[module_id]] = 0 output = output * mask # Replace the directions with constant mu mask = torch.zeros_like(output) mask[:, dirs[module_id]] = mu output = output + mask # Update counter counter[module_name] += 1 return output
def Likelihood_Y_Z(MuZ,SigmaZ,H,Y,beta): MuY=H@MuZ (d,N)=MuY.size() totalTerm=0 #logdetP=float(0) for i in range(N): yi=Y[:,i] mui=MuY[:,i] deviation=yi-mui outerProduct=torch.gres(deviation,deviation.transpose(0,1)) Sigmai=torch.diag(SigmaZ[:,i]) Covi=(H@Sigmai)@H.transpose(0,1)+(1/beta)*torch.identity(d) Pr=torch.inverse(Covi) expTerm=torch.sum((outerProduct*Pr)) logdet=torch.log(torch.det(Pr)) #print('Determinant is:', determin) #print(Covi) #prod*=1/(math.exp(0.5*expTerm)*math.sqrt(lin.det(Covi))) totalTerm=totalTerm+0.5*(logdet-expTerm) return totalTerm
def random_flip(input, decision): identity = torch.identity() f1 = identity(input) f2 = torch.flip(input, dim=3) return torch.where(torch.less(decision, 0.5), f2, f1)
def batchnorm3d_to_instancenorm3d(model): conversion_count = 0 for name, module in reversed(model._modules.items()): if len(list(module.children())) > 0: # recurse model._modules[ name], num_converted = batchnorm3d_to_instancenorm3d(module) conversion_count += num_converted if isinstance(module, nn.BatchNorm3d): layer_new = nn.InstanceNorm3d(module.num_features, eps=module.eps, momentum=module.momentum, affine=module.affine, track_running_stats=False) layer_new.weight = module.weight layer_new.bias = module.bias model._modules[name] = layer_new conversion_count += 1 return model, conversion_count if __name__ == '__main__': model = torch.identity() model2 = deepcopy(model).cpu() new_model, count = batchnorm3d_to_instancenorm3d(model2, nn.BatchNorm3d, nn.InstanceNorm3d)