def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][ P.KEY_DS_NUM_CLASSES] self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5) # Here we define the layers of our network # Seventh convolutional layer self.conv7 = nn.Conv2d( self.get_input_shape()[0], 384, 3) # 256 input channels, 384 output channels, 3x3 convolutions self.bn7 = nn.BatchNorm2d(384) # Batch Norm layer # Eightth convolutional layer self.conv8 = nn.Conv2d( 384, 512, 3) # 384 input channels, 512 output channels, 3x3 convolutions self.bn8 = nn.BatchNorm2d(512) # Batch Norm layer self.CONV_OUTPUT_SIZE = utils.shape2size( utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT])) # FC Layers self.fc9 = nn.Linear( self.CONV_OUTPUT_SIZE, 4096 ) # conv_output_size-dimensional input, 4096-dimensional output self.bn9 = nn.BatchNorm1d(4096) # Batch Norm layer self.fc10 = nn.Linear( 4096, self.NUM_CLASSES ) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][ P.KEY_DS_NUM_CLASSES] self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5) # Here we define the layers of our network # Third convolutional layer self.conv3 = nn.Conv2d( self.get_input_shape()[0], 192, 3) # 128 input channels, 192 output channels, 3x3 convolutions self.bn3 = nn.BatchNorm2d(192) # Batch Norm layer # Fourth convolutional layer self.conv4 = nn.Conv2d( 192, 256, 3) # 192 input channels, 256 output channels, 3x3 convolutions self.bn4 = nn.BatchNorm2d(256) # Batch Norm layer self.CONV_OUTPUT_SIZE = utils.shape2size( utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT])) # FC Layers self.fc5 = nn.Linear( self.CONV_OUTPUT_SIZE, 4096 ) # conv_output_size-dimensional input, 4096-dimensional output self.bn5 = nn.BatchNorm1d(4096) # Batch Norm layer self.fc6 = nn.Linear( 4096, self.NUM_CLASSES ) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][ P.KEY_DS_NUM_CLASSES] self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5) # Here we define the layers of our network # First convolutional layer self.conv1 = nn.Conv2d( 3, 96, 7) # 3 input channels, 96 output channels, 7x7 convolutions self.bn1 = nn.BatchNorm2d(96) # Batch Norm layer # Second convolutional layer self.conv2 = nn.Conv2d( 96, 128, 3) # 96 input channels, 128 output channels, 3x3 convolutions self.bn2 = nn.BatchNorm2d(128) # Batch Norm layer # Third convolutional layer self.conv3 = nn.Conv2d( 128, 192, 3) # 128 input channels, 192 output channels, 3x3 convolutions self.bn3 = nn.BatchNorm2d(192) # Batch Norm layer # Fourth convolutional layer self.conv4 = nn.Conv2d( 192, 192, 3) # 192 input channels, 192 output channels, 3x3 convolutions self.bn4 = nn.BatchNorm2d(192) # Batch Norm layer # Fifth convolutional layer self.conv5 = nn.Conv2d( 192, 256, 3) # 192 input channels, 256 output channels, 3x3 convolutions self.bn5 = nn.BatchNorm2d(256) # Batch Norm layer # Sixth convolutional layer self.conv6 = nn.Conv2d( 256, 256, 3) # 256 input channels, 256 output channels, 3x3 convolutions self.bn6 = nn.BatchNorm2d(256) # Batch Norm layer # Seventh convolutional layer self.conv7 = nn.Conv2d( 256, 384, 3) # 256 input channels, 384 output channels, 3x3 convolutions self.bn7 = nn.BatchNorm2d(384) # Batch Norm layer # Eightth convolutional layer self.conv8 = nn.Conv2d( 384, 512, 3) # 384 input channels, 512 output channels, 3x3 convolutions self.bn8 = nn.BatchNorm2d(512) # Batch Norm layer self.CONV_OUTPUT_SIZE = utils.shape2size( utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT])) # FC Layers self.fc9 = nn.Linear( self.CONV_OUTPUT_SIZE, 4096 ) # conv_output_size-dimensional input, 4096-dimensional output self.bn9 = nn.BatchNorm1d(4096) # Batch Norm layer self.fc10 = nn.Linear( 4096, self.NUM_CLASSES ) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][P.KEY_DS_NUM_CLASSES] self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5) # Here we define the layers of our network # First convolutional layer self.conv1 = nn.Conv2d(3, 96, 5) # 3 input channels, 96 output channels, 5x5 convolutions self.bn1 = nn.BatchNorm2d(96) # Batch Norm layer self.CONV_OUTPUT_SIZE = utils.shape2size(utils.tens2shape(self.get_dummy_fmap()[self.CONV_OUTPUT])) # FC Layers self.fc2 = nn.Linear(self.CONV_OUTPUT_SIZE, self.NUM_CLASSES) # conv_output_shape-dimensional input, 10-dimensional output (one per class)
def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][ P.KEY_DS_NUM_CLASSES] self.DEEP_TEACHER_SIGNAL = config.CONFIG_OPTIONS.get( P.KEY_DEEP_TEACHER_SIGNAL, False) LRN_SIM = config.CONFIG_OPTIONS.get(PP.KEY_LRN_SIM, None) LRN_ACT = config.CONFIG_OPTIONS.get(PP.KEY_LRN_ACT, None) OUT_SIM = config.CONFIG_OPTIONS.get(PP.KEY_OUT_SIM, None) OUT_ACT = config.CONFIG_OPTIONS.get(PP.KEY_OUT_ACT, None) self.lrn_sim = utils.retrieve( LRN_SIM) if LRN_SIM is not None else HF.kernel_mult2d self.lrn_act = utils.retrieve( LRN_ACT) if LRN_ACT is not None else F.relu self.out_sim = utils.retrieve( OUT_SIM) if OUT_SIM is not None else HF.kernel_mult2d self.out_act = utils.retrieve( OUT_ACT) if OUT_ACT is not None else F.relu self.competitive_act = config.CONFIG_OPTIONS.get( PP.KEY_COMPETITIVE_ACT, None) if self.competitive_act is not None: self.competitive_act = utils.retrieve(self.competitive_act) self.K = config.CONFIG_OPTIONS.get(PP.KEY_COMPETITIVE_K, 1) self.LRN_SIM_B = config.CONFIG_OPTIONS.get(PP.KEY_LRN_SIM_B, 0.) self.LRN_SIM_S = config.CONFIG_OPTIONS.get(PP.KEY_LRN_SIM_S, 1.) self.LRN_SIM_P = config.CONFIG_OPTIONS.get(PP.KEY_LRN_SIM_P, 1.) self.LRN_SIM_EXP = config.CONFIG_OPTIONS.get(PP.KEY_LRN_SIM_EXP, None) self.LRN_ACT_SCALE_IN = config.CONFIG_OPTIONS.get( PP.KEY_LRN_ACT_SCALE_IN, 1) self.LRN_ACT_SCALE_OUT = config.CONFIG_OPTIONS.get( PP.KEY_LRN_ACT_SCALE_OUT, 1) self.LRN_ACT_OFFSET_IN = config.CONFIG_OPTIONS.get( PP.KEY_LRN_ACT_OFFSET_IN, 0) self.LRN_ACT_OFFSET_OUT = config.CONFIG_OPTIONS.get( PP.KEY_LRN_ACT_OFFSET_OUT, 0) self.LRN_ACT_P = config.CONFIG_OPTIONS.get(PP.KEY_LRN_ACT_P, 1) self.OUT_SIM_B = config.CONFIG_OPTIONS.get(PP.KEY_OUT_SIM_B, 0.) self.OUT_SIM_S = config.CONFIG_OPTIONS.get(PP.KEY_OUT_SIM_S, 1.) self.OUT_SIM_P = config.CONFIG_OPTIONS.get(PP.KEY_OUT_SIM_P, 1.) self.OUT_SIM_EXP = config.CONFIG_OPTIONS.get(PP.KEY_OUT_SIM_EXP, None) self.OUT_ACT_SCALE_IN = config.CONFIG_OPTIONS.get( PP.KEY_OUT_ACT_SCALE_IN, 1) self.OUT_ACT_SCALE_OUT = config.CONFIG_OPTIONS.get( PP.KEY_OUT_ACT_SCALE_OUT, 1) self.OUT_ACT_OFFSET_IN = config.CONFIG_OPTIONS.get( PP.KEY_OUT_ACT_OFFSET_IN, 0) self.OUT_ACT_OFFSET_OUT = config.CONFIG_OPTIONS.get( PP.KEY_OUT_ACT_OFFSET_OUT, 0) self.OUT_ACT_P = config.CONFIG_OPTIONS.get(PP.KEY_OUT_ACT_P, 1) self.ACT_COMPLEMENT_INIT = None self.ACT_COMPLEMENT_RATIO = 0. self.ACT_COMPLEMENT_ADAPT = None self.ACT_COMPLEMENT_GRP = False self.GATING = H.HebbianConv2d.GATE_HEBB self.UPD_RULE = H.HebbianConv2d.UPD_RECONSTR self.RECONSTR = H.HebbianConv2d.REC_LIN_CMB self.RED = H.HebbianConv2d.RED_AVG self.VAR_ADAPTIVE = False self.LOC_LRN_RULE = config.CONFIG_OPTIONS.get(P.KEY_LOCAL_LRN_RULE, 'hpca') if self.LOC_LRN_RULE in ['hpcat', 'hpcat_ada']: if LRN_ACT is None: self.lrn_act = HF.tanh if OUT_ACT is None: self.out_act = HF.tanh if self.LOC_LRN_RULE == 'hpcat_ada': self.VAR_ADAPTIVE = True if self.LOC_LRN_RULE == 'hwta': if LRN_SIM is None: self.lrn_sim = HF.raised_cos_sim2d self.LRN_SIM_P = config.CONFIG_OPTIONS.get( PP.KEY_LRN_SIM_P, 2. ) # NB: In hwta the default lrn_sim is squared raised cosine if LRN_ACT is None: self.lrn_act = HF.identity if OUT_SIM is None: self.out_sim = HF.vector_proj2d if OUT_ACT is None: self.out_act = F.relu self.GATING = H.HebbianConv2d.GATE_BASE self.RECONSTR = H.HebbianConv2d.REC_QNT_SGN self.RED = H.HebbianConv2d.RED_W_AVG if self.LOC_LRN_RULE in ['ica', 'hica', 'ica_nrm', 'hica_nrm']: if LRN_ACT is None: self.lrn_act = HF.tanh if OUT_ACT is None: self.out_act = HF.tanh self.ACT_COMPLEMENT_INIT = config.CONFIG_OPTIONS.get( PP.KEY_ACT_COMPLEMENT_INIT, None) self.ACT_COMPLEMENT_RATIO = config.CONFIG_OPTIONS.get( PP.KEY_ACT_COMPLEMENT_RATIO, 0.) self.ACT_COMPLEMENT_ADAPT = config.CONFIG_OPTIONS.get( PP.KEY_ACT_COMPLEMENT_ADAPT, None) self.ACT_COMPLEMENT_GRP = config.CONFIG_OPTIONS.get( PP.KEY_ACT_COMPLEMENT_GRP, False) self.UPD_RULE = H.HebbianConv2d.UPD_ICA if self.LOC_LRN_RULE == 'hica': self.UPD_RULE = H.HebbianConv2d.UPD_HICA if self.LOC_LRN_RULE == 'ica_nrm': self.UPD_RULE = H.HebbianConv2d.UPD_ICA_NRM if self.LOC_LRN_RULE == 'hica_nrm': self.UPD_RULE = H.HebbianConv2d.UPD_HICA_NRM if self.LOC_LRN_RULE in ['ica_nrm', 'hica_nrm']: self.VAR_ADAPTIVE = True self.GATING = H.HebbianConv2d.GATE_BASE if self.LRN_SIM_EXP is not None: self.lrn_sim = HF.get_exp_sim( HF.get_affine_sim(self.lrn_sim, p=self.LRN_SIM_EXP), HF.get_pow_nc( utils.retrieve( config.CONFIG_OPTIONS.get(PP.KEY_LRN_SIM_NC, None)), self.LRN_SIM_EXP)) self.lrn_sim = HF.get_affine_sim(self.lrn_sim, self.LRN_SIM_B, self.LRN_SIM_S, self.LRN_SIM_P) self.lrn_act = HF.get_affine_act(self.lrn_act, self.LRN_ACT_SCALE_IN, self.LRN_ACT_SCALE_OUT, self.LRN_ACT_OFFSET_IN, self.LRN_ACT_OFFSET_OUT, self.LRN_ACT_P) if self.OUT_SIM_EXP is not None: self.out_sim = HF.get_exp_sim( HF.get_affine_sim(self.out_sim, p=self.OUT_SIM_EXP), HF.get_pow_nc( utils.retrieve( config.CONFIG_OPTIONS.get(PP.KEY_OUT_SIM_NC, None)), self.OUT_SIM_EXP)) self.out_sim = HF.get_affine_sim(self.out_sim, self.OUT_SIM_B, self.OUT_SIM_S, self.OUT_SIM_P) self.out_act = HF.get_affine_act(self.out_act, self.OUT_ACT_SCALE_IN, self.OUT_ACT_SCALE_OUT, self.OUT_ACT_OFFSET_IN, self.OUT_ACT_OFFSET_OUT, self.OUT_ACT_P) self.ALPHA_L = config.CONFIG_OPTIONS.get(P.KEY_ALPHA_L, 1.) self.ALPHA_G = config.CONFIG_OPTIONS.get(P.KEY_ALPHA_G, 0.) # Here we define the layers of our network # Fourth convolutional layer self.conv4 = H.HebbianConv2d( in_channels=self.get_input_shape()[0], out_channels=256, kernel_size=3, lrn_sim=self.lrn_sim, lrn_act=self.lrn_act, lrn_cmp=True, lrn_t=True, out_sim=self.out_sim, out_act=self.out_act, competitive=H.Competitive(out_size=(16, 16), competitive_act=self.competitive_act, k=self.K), act_complement_init=self.ACT_COMPLEMENT_INIT, act_complement_ratio=self.ACT_COMPLEMENT_RATIO, act_complement_adapt=self.ACT_COMPLEMENT_ADAPT, act_complement_grp=self.ACT_COMPLEMENT_GRP, var_adaptive=self.VAR_ADAPTIVE, gating=self.GATING, upd_rule=self.UPD_RULE, reconstruction=self.RECONSTR, reduction=self.RED, alpha_l=self.ALPHA_L, alpha_g=self.ALPHA_G, ) # 192 input channels, 16x16=256 output channels, 3x3 convolutions self.bn4 = nn.BatchNorm2d(256) # Batch Norm layer self.CONV_OUTPUT_SHAPE = utils.tens2shape( self.get_dummy_fmap()[self.CONV_OUTPUT]) # FC Layers (convolution with kernel size equal to the entire feature map size is like a fc layer) self.fc5 = H.HebbianConv2d( in_channels=self.CONV_OUTPUT_SHAPE[0], out_channels=4096, kernel_size=(self.CONV_OUTPUT_SHAPE[1], self.CONV_OUTPUT_SHAPE[2]), lrn_sim=self.lrn_sim, lrn_act=self.lrn_act, lrn_cmp=True, lrn_t=True, out_sim=self.out_sim, out_act=self.out_act, competitive=H.Competitive(out_size=(64, 64), competitive_act=self.competitive_act, k=self.K), act_complement_init=self.ACT_COMPLEMENT_INIT, act_complement_ratio=self.ACT_COMPLEMENT_RATIO, act_complement_adapt=self.ACT_COMPLEMENT_ADAPT, act_complement_grp=self.ACT_COMPLEMENT_GRP, var_adaptive=self.VAR_ADAPTIVE, gating=self.GATING, upd_rule=self.UPD_RULE, reconstruction=self.RECONSTR, reduction=self.RED, alpha_l=self.ALPHA_L, alpha_g=self.ALPHA_G, ) # conv_output_shape-shaped input, 64x64=4096 output channels self.bn5 = nn.BatchNorm2d(4096) # Batch Norm layer self.fc6 = H.HebbianConv2d( in_channels=4096, out_channels=self.NUM_CLASSES, kernel_size=1, lrn_sim=HF.get_affine_sim(HF.raised_cos_sim2d, p=2), lrn_act=HF.identity, lrn_cmp=True, lrn_t=True, out_sim=HF.vector_proj2d if self.ALPHA_G == 0. else HF.kernel_mult2d, out_act=HF.identity, competitive=H.Competitive(), gating=H.HebbianConv2d.GATE_BASE, upd_rule=H.HebbianConv2d.UPD_RECONSTR if self.ALPHA_G == 0. else None, reconstruction=H.HebbianConv2d.REC_QNT_SGN, reduction=H.HebbianConv2d.RED_W_AVG, alpha_l=self.ALPHA_L, alpha_g=self.ALPHA_G if self.ALPHA_G == 0. else 1., ) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class)
def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][P.KEY_DS_NUM_CLASSES] self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5) self.NUM_LATENT_VARS = config.CONFIG_OPTIONS.get(PP.KEY_VAE_NUM_LATENT_VARS, 256) self.ELBO_BETA = config.CONFIG_OPTIONS.get(P.KEY_ELBO_BETA, 1.) self.ALPHA_L = config.CONFIG_OPTIONS.get(P.KEY_ALPHA_L, 1.) self.ALPHA_G = config.CONFIG_OPTIONS.get(P.KEY_ALPHA_G, 0.) # Here we define the layers of our network and the variables to store internal gradients # First convolutional layer self.conv1 = nn.Conv2d(3, 96, 5) # 3 input chennels, 96 output channels, 5x5 convolutions self.bn1 = nn.BatchNorm2d(96) # Batch Norm layer self.conv1_delta_w = torch.zeros_like(self.conv1.weight) self.conv1_delta_bias = torch.zeros_like(self.conv1.bias) self.bn1_delta_w = torch.zeros_like(self.bn1.weight) self.bn1_delta_bias = torch.zeros_like(self.bn1.bias) # Second convolutional layer self.conv2 = nn.Conv2d(96, 128, 3) # 96 input chennels, 128 output channels, 3x3 convolutions self.bn2 = nn.BatchNorm2d(128) # Batch Norm layer self.conv2_delta_w = torch.zeros_like(self.conv2.weight) self.conv2_delta_bias = torch.zeros_like(self.conv2.bias) self.bn2_delta_w = torch.zeros_like(self.bn2.weight) self.bn2_delta_bias = torch.zeros_like(self.bn2.bias) # Third convolutional layer self.conv3 = nn.Conv2d(128, 192, 3) # 128 input chennels, 192 output channels, 3x3 convolutions self.bn3 = nn.BatchNorm2d(192) # Batch Norm layer self.conv3_delta_w = torch.zeros_like(self.conv3.weight) self.conv3_delta_bias = torch.zeros_like(self.conv3.bias) self.bn3_delta_w = torch.zeros_like(self.bn3.weight) self.bn3_delta_bias = torch.zeros_like(self.bn3.bias) # Fourth convolutional layer self.conv4 = nn.Conv2d(192, 256, 3) # 192 input chennels, 256 output channels, 3x3 convolutions self.bn4 = nn.BatchNorm2d(256) # Batch Norm layer self.conv4_delta_w = torch.zeros_like(self.conv4.weight) self.conv4_delta_bias = torch.zeros_like(self.conv4.bias) self.bn4_delta_w = torch.zeros_like(self.bn4.weight) self.bn4_delta_bias = torch.zeros_like(self.bn4.bias) self.OUTPUT_FMAP_SHAPE = {k: utils.tens2shape(v) for k, v in self.get_dummy_fmap().items() if isinstance(v, torch.Tensor)} self.OUTPUT_FMAP_SIZE = {k: utils.shape2size(self.OUTPUT_FMAP_SHAPE[k]) for k in self.OUTPUT_FMAP_SHAPE.keys()} self.CONV_OUTPUT_SIZE = self.OUTPUT_FMAP_SIZE[self.CONV_OUTPUT] # FC Layers self.fc5 = nn.Linear(self.CONV_OUTPUT_SIZE, 4096) # conv_output_size-dimensional input, 4096-dimensional output self.bn5 = nn.BatchNorm1d(4096) # Batch Norm layer self.fc5_delta_w = torch.zeros_like(self.fc5.weight) self.fc5_delta_bias = torch.zeros_like(self.fc5.bias) self.bn5_delta_w = torch.zeros_like(self.bn5.weight) self.bn5_delta_bias = torch.zeros_like(self.bn5.bias) self.fc6 = nn.Linear(4096, self.NUM_CLASSES) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class) # Latent variable mapping layers self.fc_mu1 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN1], self.NUM_LATENT_VARS) # bn1_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_var1 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN1], self.NUM_LATENT_VARS) # bn1_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_mu1_delta_w = torch.zeros_like(self.fc_mu1.weight) self.fc_mu1_delta_bias = torch.zeros_like(self.fc_mu1.bias) self.fc_var1_delta_w = torch.zeros_like(self.fc_var1.weight) self.fc_var1_delta_bias = torch.zeros_like(self.fc_var1.bias) self.fc_mu2 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN2], self.NUM_LATENT_VARS) # bn2_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_var2 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN2], self.NUM_LATENT_VARS) # bn2_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_mu2_delta_w = torch.zeros_like(self.fc_mu2.weight) self.fc_mu2_delta_bias = torch.zeros_like(self.fc_mu2.bias) self.fc_var2_delta_w = torch.zeros_like(self.fc_var2.weight) self.fc_var2_delta_bias = torch.zeros_like(self.fc_var2.bias) self.fc_mu3 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN3], self.NUM_LATENT_VARS) # bn3_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_var3 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN3], self.NUM_LATENT_VARS) # bn3_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_mu3_delta_w = torch.zeros_like(self.fc_mu3.weight) self.fc_mu3_delta_bias = torch.zeros_like(self.fc_mu3.bias) self.fc_var3_delta_w = torch.zeros_like(self.fc_var3.weight) self.fc_var3_delta_bias = torch.zeros_like(self.fc_var3.bias) self.fc_mu4 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN4], self.NUM_LATENT_VARS) # bn4_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_var4 = nn.Linear(self.OUTPUT_FMAP_SIZE[self.BN4], self.NUM_LATENT_VARS) # bn4_output_size-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_mu4_delta_w = torch.zeros_like(self.fc_mu4.weight) self.fc_mu4_delta_bias = torch.zeros_like(self.fc_mu4.bias) self.fc_var4_delta_w = torch.zeros_like(self.fc_var4.weight) self.fc_var4_delta_bias = torch.zeros_like(self.fc_var4.bias) self.fc_mu5 = nn.Linear(4096, self.NUM_LATENT_VARS) # 4096-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_var5 = nn.Linear(4096, self.NUM_LATENT_VARS) # 4096-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_mu5_delta_w = torch.zeros_like(self.fc_mu5.weight) self.fc_mu5_delta_bias = torch.zeros_like(self.fc_mu5.bias) self.fc_var5_delta_w = torch.zeros_like(self.fc_var5.weight) self.fc_var5_delta_bias = torch.zeros_like(self.fc_var5.bias) # Decoding Layers self.dec_fc0 = nn.Linear(self.NUM_LATENT_VARS, 4096) # NUM_LATENT_VARS-dimensional input, 4096-dimensional output self.dec_bn0 = nn.BatchNorm1d(4096) # Batch Norm layer self.dec_fc1 = nn.Linear(4096, self.CONV_OUTPUT_SIZE) # 4096-dimensional input, CONV_OUTPUT_SIZE-dimensional output self.dec_fc0_delta_w = torch.zeros_like(self.dec_fc0.weight) self.dec_fc0_delta_bias = torch.zeros_like(self.dec_fc0.bias) self.dec_bn0_delta_w = torch.zeros_like(self.dec_bn0.weight) self.dec_bn0_delta_bias = torch.zeros_like(self.dec_bn0.bias) self.dec_fc1_delta_w = torch.zeros_like(self.dec_fc1.weight) self.dec_fc1_delta_bias = torch.zeros_like(self.dec_fc1.bias) self.dec_fc2 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN4]) # NUM_LATENT_VARS-dimensional input, bn4_output_size-dimensional output self.dec_bn2 = nn.BatchNorm2d(256) # Batch Norm layer self.dec_conv2 = nn.ConvTranspose2d(256, 192, 3) # 256 input chennels, 192 output channels, 3x3 transpose convolutions self.dec_fc2_delta_w = torch.zeros_like(self.dec_fc2.weight) self.dec_fc2_delta_bias = torch.zeros_like(self.dec_fc2.bias) self.dec_bn2_delta_w = torch.zeros_like(self.dec_bn2.weight) self.dec_bn2_delta_bias = torch.zeros_like(self.dec_bn2.bias) self.dec_conv2_delta_w = torch.zeros_like(self.dec_conv2.weight) self.dec_conv2_delta_bias = torch.zeros_like(self.dec_conv2.bias) self.dec_fc3 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN3]) # NUM_LATENT_VARS-dimensional input, bn3_output_size-dimensional output self.dec_bn3 = nn.BatchNorm2d(192) # Batch Norm layer self.dec_conv3 = nn.ConvTranspose2d(192, 128, 3) # 192 input chennels, 128 output channels, 3x3 transpose convolutions self.dec_fc3_delta_w = torch.zeros_like(self.dec_fc3.weight) self.dec_fc3_delta_bias = torch.zeros_like(self.dec_fc3.bias) self.dec_bn3_delta_w = torch.zeros_like(self.dec_bn3.weight) self.dec_bn3_delta_bias = torch.zeros_like(self.dec_bn3.bias) self.dec_conv3_delta_w = torch.zeros_like(self.dec_conv3.weight) self.dec_conv3_delta_bias = torch.zeros_like(self.dec_conv3.bias) self.dec_fc4 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN2]) # NUM_LATENT_VARS-dimensional input, bn2_output_size-dimensional output self.dec_bn4 = nn.BatchNorm2d(128) # Batch Norm layer self.dec_conv4 = nn.ConvTranspose2d(128, 96, 3) # 128 input chennels, 96 output channels, 3x3 transpose convolutions self.dec_fc4_delta_w = torch.zeros_like(self.dec_fc4.weight) self.dec_fc4_delta_bias = torch.zeros_like(self.dec_fc4.bias) self.dec_bn4_delta_w = torch.zeros_like(self.dec_bn4.weight) self.dec_bn4_delta_bias = torch.zeros_like(self.dec_bn4.bias) self.dec_conv4_delta_w = torch.zeros_like(self.dec_conv4.weight) self.dec_conv4_delta_bias = torch.zeros_like(self.dec_conv4.bias) self.dec_fc5 = nn.Linear(self.NUM_LATENT_VARS, self.OUTPUT_FMAP_SIZE[self.BN1]) # NUM_LATENT_VARS-dimensional input, bn1_output_size-dimensional output self.dec_bn5 = nn.BatchNorm2d(96) # Batch Norm layer self.dec_conv5 = nn.ConvTranspose2d(96, 3, 5) # 96 input chennels, 3 output channels, 5x5 transpose convolutions self.dec_fc5_delta_w = torch.zeros_like(self.dec_fc5.weight) self.dec_fc5_delta_bias = torch.zeros_like(self.dec_fc5.bias) self.dec_bn5_delta_w = torch.zeros_like(self.dec_bn5.weight) self.dec_bn5_delta_bias = torch.zeros_like(self.dec_bn5.bias) self.dec_conv5_delta_w = torch.zeros_like(self.dec_conv5.weight) self.dec_conv5_delta_bias = torch.zeros_like(self.dec_conv5.bias) # Internal ELBO loss function self.loss = ELBOMetric(self.ELBO_BETA)
def __init__(self, config, input_shape=None): super(Net, self).__init__(config, input_shape) self.NUM_CLASSES = P.GLB_PARAMS[P.KEY_DATASET_METADATA][ P.KEY_DS_NUM_CLASSES] self.DROPOUT_P = config.CONFIG_OPTIONS.get(P.KEY_DROPOUT_P, 0.5) self.NUM_LATENT_VARS = config.CONFIG_OPTIONS.get( PP.KEY_VAE_NUM_LATENT_VARS, 256) # Here we define the layers of our network # First convolutional layer self.conv1 = nn.Conv2d( 3, 96, 5) # 3 input channels, 96 output channels, 5x5 convolutions self.bn1 = nn.BatchNorm2d(96) # Batch Norm layer # Second convolutional layer self.conv2 = nn.Conv2d( 96, 128, 3) # 96 input channels, 128 output channels, 3x3 convolutions self.bn2 = nn.BatchNorm2d(128) # Batch Norm layer # Third convolutional layer self.conv3 = nn.Conv2d( 128, 192, 3) # 128 input channels, 192 output channels, 3x3 convolutions self.bn3 = nn.BatchNorm2d(192) # Batch Norm layer # Fourth convolutional layer self.conv4 = nn.Conv2d( 192, 256, 3) # 192 input channels, 256 output channels, 3x3 convolutions self.bn4 = nn.BatchNorm2d(256) # Batch Norm layer self.CONV_OUTPUT_SHAPE = utils.tens2shape( self.get_dummy_fmap()[self.CONV_OUTPUT]) self.CONV_OUTPUT_SIZE = utils.shape2size(self.CONV_OUTPUT_SHAPE) # FC Layers self.fc5 = nn.Linear( self.CONV_OUTPUT_SIZE, 4096 ) # conv_output_size-dimensional input, 4096-dimensional output self.bn5 = nn.BatchNorm1d(4096) # Batch Norm layer self.fc6 = nn.Linear( 4096, self.NUM_CLASSES ) # 4096-dimensional input, NUM_CLASSES-dimensional output (one per class) self.fc_mu = nn.Linear( 4096, self.NUM_LATENT_VARS ) # 4096-dimensional input, NUM_LATENT_VARS-dimensional output self.fc_var = nn.Linear( 4096, self.NUM_LATENT_VARS ) # 4096-dimensional input, NUM_LATENT_VARS-dimensional output # Decoding Layers self.dec_fc0 = nn.Linear( self.NUM_LATENT_VARS, 4096) # NUM_LATENT_VARS-dimensional input, 4096-dimensional output self.dec_bn0 = nn.BatchNorm1d(4096) # Batch Norm layer self.dec_fc1 = nn.Linear( 4096, self.CONV_OUTPUT_SIZE ) # 4096-dimensional input, CONV_OUTPUT_SIZE-dimensional output self.dec_bn1 = nn.BatchNorm1d( self.CONV_OUTPUT_SIZE) # Batch Norm layer self.dec_conv2 = nn.ConvTranspose2d( 256, 192, 3 ) # 256 input channels, 192 output channels, 3x3 transpose convolutions self.dec_bn2 = nn.BatchNorm2d(192) # Batch Norm layer self.dec_conv3 = nn.ConvTranspose2d( 192, 128, 3 ) # 192 input channels, 128 output channels, 3x3 transpose convolutions self.dec_bn3 = nn.BatchNorm2d(128) # Batch Norm layer self.dec_conv4 = nn.ConvTranspose2d( 128, 96, 3 ) # 128 input channels, 96 output channels, 3x3 transpose convolutions self.dec_bn4 = nn.BatchNorm2d(96) # Batch Norm layer self.dec_conv5 = nn.ConvTranspose2d( 96, 3, 5 ) # 96 input channels, 3 output channels, 5x5 transpose convolutions self.dec_bn5 = nn.BatchNorm2d(3) # Batch Norm layer