def test_determinist_init(): seed1 = set_and_print_random_seed() DetLin = nn.Linear(32*7*7,10) BayLin = GaussianLinear(1, 32 * 7 * 7, 10) set_and_print_random_seed(seed1) BayLin.reset_parameters() assert torch.sum(torch.abs(BayLin.mu - DetLin.weight)) == 0 assert torch.sum(torch.abs(BayLin.mu_bias - DetLin.bias)) == 0
def test_determinist_init(): seed1 = set_and_print_random_seed() DetCNN = nn.Conv2d(1, 16, 3) BayCNN = GaussianCNN(1, 1, 16, 3) set_and_print_random_seed(seed1) BayCNN.reset_parameters() assert torch.sum(torch.abs(BayCNN.mu - DetCNN.weight)) == 0 assert torch.sum(torch.abs(BayCNN.mu_bias - DetCNN.bias)) == 0
def test_determinist_forward_and_init(): seed1 = set_and_print_random_seed() DetLin = nn.Linear(32*7*7, 10) BayLin = GaussianLinear('determinist', 32 * 7 * 7, 10) set_and_print_random_seed(seed1) BayLin.reset_parameters() image = torch.rand(1, 32*7*7) output1 = BayLin(image) output2 = DetLin(image) assert torch.sum(torch.abs(output1 - output2)) == 0
def test_determinist_forward_and_init(): seed1 = set_and_print_random_seed() DetCNN = nn.Conv2d(1, 16, 3) BayCNN = GaussianCNN('determinist', 1, 16, 3) set_and_print_random_seed(seed1) BayCNN.reset_parameters() image = torch.rand(1, 1, 28, 28) output1 = BayCNN(image) output2 = DetCNN(image) assert torch.sum(torch.abs(output1 - output2)) == 0
def eval_random(model, batch_size, img_channels, img_dim, number_of_tests, random_seed=None, verbose=False, device='cpu'): """ Args: model (torch.nn.Module child): the model we evaluate batch_size (int): The size of the random sample img_channels (int): dimension of the random sample img_dim (int): dimension of the random sample number_of_tests (int): the number of times we do a forward for each input random_seed (int): the seed of the random generation, for reproducibility verbose (Bool): whether we want a progress bar or not device (torch.device || str): which device to compute on (either on GPU or CPU). Either torch.device type or specific string 'cpu' or 'gpu'. Returns: torch.Tensor: size (batch_size): the variation-ratio uncertainty torch.Tensor: size (batch_size): the predictive entropy uncertainty torch.Tensor: size (batch_size): the mutual information uncertainty int: the seed of the random generation, for reproducibility """ number_of_classes = model.number_of_classes seed = set_and_print_random_seed(random_seed) random_noise = torch.randn(batch_size, img_channels, img_dim, img_dim).to(device) output_random = torch.Tensor(number_of_tests, batch_size, number_of_classes) if verbose: iterator = tqdm(range(number_of_tests)) else: iterator = range(number_of_tests) for test_idx in iterator: output_random[test_idx] = model(random_noise).detach() return output_random, seed
else: device = 'cpu' device = torch.device(device) if trainset == 'mnist': trainloader, valloader, evalloader = get_mnist(train_labels=range(10), eval_labels=range(10), batch_size=batch_size) dim_input = 28 dim_channels = 1 if trainset == 'cifar10': trainloader, evalloader = get_cifar10(batch_size=batch_size) dim_input = 32 dim_channels = 3 seed_model = set_and_print_random_seed() bay_net = GaussianClassifier(rho=rho, stds_prior=stds_prior, dim_input=dim_input, number_of_classes=10, dim_channels=dim_channels) bay_net.to(device) criterion = CrossEntropyLoss() if loss_type == 'uniform': step_function = uniform loss = BBBLoss(bay_net, criterion, step_function) elif loss_type == 'exp': def step_function(batch_idx, number_of_batches): return 2**(number_of_batches - batch_idx) / (2**number_of_batches - 1)