Exemplo n.º 1
0
    def build(
            self,
            rng,
            n_fms_in,
            n_fms_out,
            conv_kernel_dims,
            pool_prms,  # Can be []
            conv_w_init_method,
            conv_pad_mode,
            use_bn,
            moving_avg_length,  #If this is <= 0, we are not using BatchNormalization, even if above is True.
            activ_func="relu",
            dropout_rate=0.0):
        """
        param rng: numpy.random.RandomState used to initialize weights
        param inputToLayer: tensor5 of shape inputToLayerShape
        param filterShape: (number of filters, num input feature maps,
                            filter height, filter width, filter depth)
        param inputToLayerShape: (batch size, num input feature maps,
                            image height, image width, filter depth)
        use_bn: True of False. Used to not allow doing BN on first layers straight on image, even if rollingAvForBnOverThayManyBatches > 0.
        """
        self._n_fms_in = n_fms_in
        self._n_fms_out = n_fms_out

        #  Order of what is applied, ala He et al "Identity Mappings in Deep Residual Networks" 2016
        #  Input -> [ BatchNorm OR biases applied] -> NonLinearity -> DropOut -> Pooling --> Conv ]

        #------------------ Batch Normalization ------------------
        if use_bn and moving_avg_length > 0:
            self._bn_l = dm_layers.BatchNormLayer(moving_avg_length,
                                                  n_channels=n_fms_in)
            self._layers.append(self._bn_l)
        else:  #Not using batch normalization
            #make the bias terms and apply them. Like the old days before BN's own learnt bias terms.
            bias_l = dm_layers.BiasLayer(n_fms_in)
            self._layers.append(bias_l)

        #------------ Apply Activation/ non-linearity -----------
        act_l = dm_layers.get_act_layer(activ_func, n_fms_in)
        self._layers.append(act_l)

        #------------- Dropout --------------
        dropout_l = dm_layers.DropoutLayer(dropout_rate, rng)
        self._layers.append(dropout_l)

        #-----------  Pooling ----------------------------------
        if len(pool_prms) > 0:  #Max pooling is actually happening here...
            # if len == 0, pool_prms == [], no max pooling before this conv.
            pooling_l = PoolingLayer(pool_prms[0], pool_prms[1], pool_prms[2],
                                     pool_prms[3])
            self._layers.append(pooling_l)

        # --------- Convolution ---------------------------------
        conv_l = self._create_conv_layer(n_fms_in, n_fms_out, conv_kernel_dims,
                                         conv_w_init_method, conv_pad_mode,
                                         rng)
        self._layers.append(conv_l)
Exemplo n.º 2
0
    def build(self, rng, n_fms, t=1):
        # t: temperature. Scalar

        self._n_fms_in = n_fms
        self._n_fms_out = n_fms
        self._temperature = t

        self._bias_l = dm_layers.BiasLayer(n_fms)
        self._layers.append(self._bias_l)