def test_007_is_no_numerical_spec(self): """ Should not classify '007' as numerical spec, because it has leading zeros. """ self.assertIs(plan_check.is_numerical_spec('007'), False)
def test_negative_one_is_no_numerical_spec(self): """ Should not classify '-1' as numerical spec. """ self.assertIs(plan_check.is_numerical_spec('-1'), False)
def test_42_is_numerical_spec(self): """ Should classify '42' as numerical spec. """ self.assertIs(plan_check.is_numerical_spec('42'), True)
def test_zero_is_no_numerical_spec(self): """ Should not classify '0' as numerical spec. """ self.assertIs(plan_check.is_numerical_spec('0'), False)
def __init__(self, net_name: NetNames, dataset_name: DatasetNames, plan_conv: list, plan_fc: list): super(Net, self).__init__() assert (net_name == NetNames.CONV) or (net_name == NetNames.LENET), \ f"Could not initialize net, because the given name {net_name} is invalid." self.init_weight_count_net = dict([('conv', 0), ('fc', 0)]) self.net_name = net_name self.dataset_name = dataset_name self.plan_conv = plan_conv self.plan_fc = plan_fc # create and initialize layers with Gaussian Glorot conv_layers = [] fc_layers = [] sample_shape = get_sample_shape(dataset_name) filters = sample_shape[0] pooling_count = 0 for spec in plan_conv: if spec == 'A': conv_layers.append(nn.AvgPool2d(2)) pooling_count += 1 elif spec == 'M': conv_layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) pooling_count += 1 elif is_batch_norm_spec(spec): spec_number = get_number_from_batch_norm_spec(spec) conv_layers.append( nn.Conv2d(filters, spec_number, kernel_size=3, padding=1)) conv_layers.append(nn.BatchNorm2d(spec_number)) conv_layers.append(get_activation(net_name)) self.init_weight_count_net['conv'] += filters * spec_number * 9 filters = spec_number elif is_numerical_spec(spec): spec_number = get_number_from_numerical_spec(spec) conv_layers.append( nn.Conv2d(filters, spec_number, kernel_size=3, padding=1)) conv_layers.append(get_activation(net_name)) self.init_weight_count_net['conv'] += filters * spec_number * 9 filters = spec_number else: raise AssertionError( f"{spec} from plan_conv is an invalid spec.") # each Pooling-layer quarters the input size (height * width) filters = filters * round( (sample_shape[1] * sample_shape[2]) / (4**pooling_count)) for spec in plan_fc: assert is_numerical_spec( spec), f"{spec} from plan_fc is not a numerical spec." spec_number = get_number_from_numerical_spec(spec) fc_layers.append(nn.Linear(filters, spec_number)) fc_layers.append(get_activation(net_name)) self.init_weight_count_net['fc'] += filters * spec_number filters = spec_number self.conv = nn.Sequential(*conv_layers) self.fc = nn.Sequential(*fc_layers) self.out = nn.Linear(filters, 10) self.init_weight_count_net['fc'] += filters * 10 self.criterion = nn.CrossEntropyLoss() self.apply(gaussian_glorot) self.store_initial_weights() self.apply(setup_masks)