def td_conv(inputs,
            filters,
            kernel_size,
            targeting_count,
            targeting_fn,
            keep_prob,
            is_training,
            do_prune=True,
            strides=(1, 1),
            padding="valid",
            data_format="channels_last",
            dilation_rate=(1, 1),
            activation=None,
            use_bias=True,
            kernel_initializer=None,
            bias_initializer=tf.zeros_initializer(),
            name=None,
            reuse=None):
  """Apply targeted dropout to the weights of a convolution."""
  with tf.variable_scope(name, default_name="td_conv", reuse=reuse):
    nhwc = data_format == "channels_last"
    in_dim = shape_list(inputs)[-1] if nhwc else shape_list(inputs)[1]

    kernel_shape = [kernel_size, kernel_size, in_dim, filters]
    w = tf.get_variable(
        "DW", shape=kernel_shape, initializer=kernel_initializer)
    if use_bias:
      b = tf.get_variable("b", shape=[filters], initializer=bias_initializer)

    if keep_prob < 1.0:
      w = targeted_dropout(
          w,
          targeting_count,
          keep_prob,
          targeting_fn,
          is_training,
          do_prune=do_prune)

    if isinstance(strides, int):
      strides = [strides, strides]
    if isinstance(dilation_rate, int):
      dilation_rate = [dilation_rate, dilation_rate]

    if nhwc:
      strides = [1, strides[0], strides[1], 1]
      dilation_rate = [1, dilation_rate[0], dilation_rate[1], 1]
    else:
      strides = [1, 1, strides[0], strides[1]]
      dilation_rate = [1, 1, dilation_rate[0], dilation_rate[1]]

    y = tf.nn.conv2d(
        inputs,
        w,
        strides,
        padding,
        data_format="NHWC" if nhwc else "NCHW",
        dilations=dilation_rate,
        name=None)

    if use_bias:
      y += b

    if activation:
      y = activation(y)

    return y
def td_conv ( inputs , filters , kernel_size , targeting_count , targeting_fn , keep_prob , is_training , do_prune = True , strides = ( 1 , 1 ) , padding = "valid" , data_format = "channels_last" , dilation_rate = ( 1 , 1 ) , activation = None , use_bias = True , kernel_initializer = None , bias_initializer = tf . zeros_initializer ( ) , name = None , reuse = None ) : with tf . variable_scope ( name , default_name = "td_conv" , reuse = reuse ) : nhwc = data_format == "channels_last" in_dim = shape_list ( inputs ) [ - 1 ] if nhwc else shape_list ( inputs ) [ 1 ] kernel_shape = [ kernel_size , kernel_size , in_dim , filters ] w = tf . get_variable ( "DW" , shape = kernel_shape , initializer = kernel_initializer ) if use_bias : b = tf . get_variable ( "b" , shape = [ filters ] , initializer = bias_initializer ) if keep_prob < 1.0 : w = targeted_dropout ( w , targeting_count , keep_prob , targeting_fn , is_training , do_prune = do_prune ) if isinstance ( strides , int ) : strides = [ strides , strides ] if isinstance ( dilation_rate , int ) : dilation_rate = [ dilation_rate , dilation_rate ] if nhwc : strides = [ 1 , strides [ 0 ] , strides [ 1 ] , 1 ] dilation_rate = [ 1 , dilation_rate [ 0 ] , dilation_rate [ 1 ] , 1 ] else : strides = [ 1 , 1 , strides [ 0 ] , strides [ 1 ] ] dilation_rate = [ 1 , 1 , dilation_rate [ 0 ] , dilation_rate [ 1 ] ] y = tf . nn . conv2d ( inputs , w , strides , padding , data_format = "NHWC" if nhwc else "NCHW" , dilations = dilation_rate , name = None ) if use_bias : y += b if activation : y = activation ( y ) return y
Apply targeted dropout to the weights of a convolution.