paleo/layers/conv.py
def gradients(self, wrt='inputs'):
        """Returns a conv layer that is equivalent to calculating the gradient
        on this layer.

        Args:
            wrt: inputs or filters
        """

        layer = self

        def _compute_padding(layer):
            # Reference: TensorFlow ConvBackpropExtractAndVerifyDimension()
            # Convolution of inputs with padded output grads and filters.
            expanded_output_h = (layer.outputs[1] - 1) * layer.strides[1] + 1
            expanded_output_w = (layer.outputs[2] - 1) * layer.strides[2] + 1

            padded_out_h = layer.inputs[1] + layer.filters[0] - 1
            padded_out_w = layer.inputs[2] + layer.filters[1] - 1

            # Number of padding elements to be added before/after this
            # dimension of input when computing Conv2DBackpropInput.
            pad_before_h = layer.filters[0] - 1 - layer._pad_h
            pad_before_w = layer.filters[1] - 1 - layer._pad_w
            pad_after_h = padded_out_h - expanded_output_h - pad_before_h
            pad_after_w = padded_out_w - expanded_output_w - pad_before_w

            # Add one when padding is odd.
            # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/conv_grad_filter_ops.cc#L471
            if abs(pad_before_h - pad_after_h) == 1:
                expanded_output_h += 1
            if abs(pad_before_w - pad_after_w) == 1:
                expanded_output_w += 1
            p_h = min(pad_before_h, pad_after_h)
            p_w = min(pad_before_w, pad_after_w)
            return (expanded_output_h, expanded_output_w, p_h, p_w)

        expanded_output_h, expanded_output_w, pad_h, pad_w = _compute_padding(
            layer)

        holes = (expanded_output_h * expanded_output_w - self.outputs[1] *
                 self.outputs[2])
        percent_holes = (holes / expanded_output_h / expanded_output_w)
        #  print('gradient wrt: {}'.format(wrt))
        #  print('expanded outputs: {} {}'.format(expanded_output_h,
        #                                         expanded_output_w))
        #  print('padding: {} {}'.format(pad_h, pad_h))
        #  print('holes: {} ({})'.format(holes, percent_holes))

        if wrt == 'inputs':
            dummy_layer = Conv2d(
                name="dummy_layer",
                inputs=[
                    layer.outputs[0], expanded_output_h, expanded_output_w,
                    layer.outputs[3]
                ],
                filters=[
                    layer.filters[0], layer.filters[1], layer.filters[3],
                    layer.filters[2]
                ],
                strides=[1, 1, 1, 1],
                padding=[pad_h, pad_w],
                percent_holes=percent_holes,
                hole_position='inputs')
            # FIXME: distinguish holes in input and filter

        elif wrt == 'filters':
            if layer.padding == 'VALID':
                _p = "VALID"
            else:
                _p = [pad_h, pad_w]
            # Convolution of inputs with inputs and output grads.
            dummy_layer = Conv2d(
                name="dummy_layer",
                inputs=[
                    layer.inputs[3], layer.inputs[1], layer.inputs[2],
                    layer.inputs[0]
                ],
                filters=[
                    expanded_output_h, expanded_output_w, layer.outputs[0],
                    layer.outputs[3]
                ],
                strides=[1, 1, 1, 1],
                padding=_p,
                percent_holes=percent_holes,
                hole_position='filters')

        return dummy_layer
Returns a conv layer that is equivalent to calculating the gradient
        on this layer.

        Args:
            wrt: inputs or filters
        
Returns a conv layer that is equivalent to calculating the gradient
on this layer.
Returns a conv layer that is equivalent to calculating the gradient on this layer .
def gradients(self, wrt='inputs'):
        layer = self
        def _compute_padding(layer):
            expanded_output_h = (layer.outputs[1] - 1) * layer.strides[1] + 1
            expanded_output_w = (layer.outputs[2] - 1) * layer.strides[2] + 1
            padded_out_h = layer.inputs[1] + layer.filters[0] - 1
            padded_out_w = layer.inputs[2] + layer.filters[1] - 1
            pad_before_h = layer.filters[0] - 1 - layer._pad_h
            pad_before_w = layer.filters[1] - 1 - layer._pad_w
            pad_after_h = padded_out_h - expanded_output_h - pad_before_h
            pad_after_w = padded_out_w - expanded_output_w - pad_before_w
            if abs(pad_before_h - pad_after_h) == 1:
                expanded_output_h += 1
            if abs(pad_before_w - pad_after_w) == 1:
                expanded_output_w += 1
            p_h = min(pad_before_h, pad_after_h)
            p_w = min(pad_before_w, pad_after_w)
            return (expanded_output_h, expanded_output_w, p_h, p_w)
        expanded_output_h, expanded_output_w, pad_h, pad_w = _compute_padding(
            layer)
        holes = (expanded_output_h * expanded_output_w - self.outputs[1] *
                 self.outputs[2])
        percent_holes = (holes / expanded_output_h / expanded_output_w)
        if wrt == 'inputs':
            dummy_layer = Conv2d(
                name="dummy_layer",
                inputs=[
                    layer.outputs[0], expanded_output_h, expanded_output_w,
                    layer.outputs[3]
                ],
                filters=[
                    layer.filters[0], layer.filters[1], layer.filters[3],
                    layer.filters[2]
                ],
                strides=[1, 1, 1, 1],
                padding=[pad_h, pad_w],
                percent_holes=percent_holes,
                hole_position='inputs')
        elif wrt == 'filters':
            if layer.padding == 'VALID':
                _p = "VALID"
            else:
                _p = [pad_h, pad_w]
            dummy_layer = Conv2d(
                name="dummy_layer",
                inputs=[
                    layer.inputs[3], layer.inputs[1], layer.inputs[2],
                    layer.inputs[0]
                ],
                filters=[
                    expanded_output_h, expanded_output_w, layer.outputs[0],
                    layer.outputs[3]
                ],
                strides=[1, 1, 1, 1],
                padding=_p,
                percent_holes=percent_holes,
                hole_position='filters')
        return dummy_layer
def gradients ( self , wrt = 'inputs' ) : layer = self def _compute_padding ( layer ) : expanded_output_h = ( layer . outputs [ 1 ] - 1 ) * layer . strides [ 1 ] + 1 expanded_output_w = ( layer . outputs [ 2 ] - 1 ) * layer . strides [ 2 ] + 1 padded_out_h = layer . inputs [ 1 ] + layer . filters [ 0 ] - 1 padded_out_w = layer . inputs [ 2 ] + layer . filters [ 1 ] - 1 pad_before_h = layer . filters [ 0 ] - 1 - layer . _pad_h pad_before_w = layer . filters [ 1 ] - 1 - layer . _pad_w pad_after_h = padded_out_h - expanded_output_h - pad_before_h pad_after_w = padded_out_w - expanded_output_w - pad_before_w if abs ( pad_before_h - pad_after_h ) == 1 : expanded_output_h += 1 if abs ( pad_before_w - pad_after_w ) == 1 : expanded_output_w += 1 p_h = min ( pad_before_h , pad_after_h ) p_w = min ( pad_before_w , pad_after_w ) return ( expanded_output_h , expanded_output_w , p_h , p_w ) expanded_output_h , expanded_output_w , pad_h , pad_w = _compute_padding ( layer ) holes = ( expanded_output_h * expanded_output_w - self . outputs [ 1 ] * self . outputs [ 2 ] ) percent_holes = ( holes / expanded_output_h / expanded_output_w ) if wrt == 'inputs' : dummy_layer = Conv2d ( name = "dummy_layer" , inputs = [ layer . outputs [ 0 ] , expanded_output_h , expanded_output_w , layer . outputs [ 3 ] ] , filters = [ layer . filters [ 0 ] , layer . filters [ 1 ] , layer . filters [ 3 ] , layer . filters [ 2 ] ] , strides = [ 1 , 1 , 1 , 1 ] , padding = [ pad_h , pad_w ] , percent_holes = percent_holes , hole_position = 'inputs' ) elif wrt == 'filters' : if layer . padding == 'VALID' : _p = "VALID" else : _p = [ pad_h , pad_w ] dummy_layer = Conv2d ( name = "dummy_layer" , inputs = [ layer . inputs [ 3 ] , layer . inputs [ 1 ] , layer . inputs [ 2 ] , layer . inputs [ 0 ] ] , filters = [ expanded_output_h , expanded_output_w , layer . outputs [ 0 ] , layer . outputs [ 3 ] ] , strides = [ 1 , 1 , 1 , 1 ] , padding = _p , percent_holes = percent_holes , hole_position = 'filters' ) return dummy_layer
Returns a conv layer that is equivalent to calculating the gradient
on this layer.
Returns a conv layer that is equivalent to calculating the gradient on this layer .
"""Returns a conv layer that is equivalent to calculating the gradient
        on this layer.

        Args:
            wrt: inputs or filters
        """ # Reference: TensorFlow ConvBackpropExtractAndVerifyDimension() # Convolution of inputs with padded output grads and filters. # Number of padding elements to be added before/after this # dimension of input when computing Conv2DBackpropInput. # Add one when padding is odd. # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/conv_grad_filter_ops.cc#L471 #  print('gradient wrt: {}'.format(wrt)) #  print('expanded outputs: {} {}'.format(expanded_output_h, #                                         expanded_output_w)) #  print('padding: {} {}'.format(pad_h, pad_h)) #  print('holes: {} ({})'.format(holes, percent_holes)) # FIXME: distinguish holes in input and filter # Convolution of inputs with inputs and output grads.
self
wrt
inputs or filters
inputs or filters