Custom dropout layer implementationKeras Maxpooling2d layer gives ValueErrorUsing make_template() in TensorFlowdropout with relu activationsDropout setting layer weights array to emptyKeras Dropout with noise_shapePredict for multiple rows for single/multiple timesteps lstmAbout correctly using dropout in RNNs (Keras)Tensorflow compute_output_shape() Not Working For Custom LayerKERAS: Get a SLICE of RNN timesteps with return_sequence = TrueInput 0 is incompatible with layer flatten_5: expected min_ndim=3, found ndim=2

Was Self-modifying-code possible just using BASIC?

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

Solving ‘Null geometry…’ error during distance matrix operation?

Are polynomials with the same roots identical?

Ordinal analysis and proofs of consistency

I've been given a project I can't complete, what should I do?

Grep Match and extract

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

Sci-fi novel: ark ship from Earth is sent into space to another planet, one man woken early from cryosleep paints a giant mural

Origin of "boor"

Getting UPS Power from One Room to Another

Is it safe to change the harddrive power feature so that it never turns off?

Can a human be transformed into a Mind Flayer?

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

Should I put programming books I wrote a few years ago on my resume?

Is using 'echo' to display attacker-controlled data on the terminal dangerous?

What STL algorithm can determine if exactly one item in a container satisfies a predicate?

Is it possible to fly backward if you have really strong headwind?

Teaching a class likely meant to inflate the GPA of student athletes

Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?

How does the Around command at zero work?

Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?

How can I remove material from this wood beam?

What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?



Custom dropout layer implementation


Keras Maxpooling2d layer gives ValueErrorUsing make_template() in TensorFlowdropout with relu activationsDropout setting layer weights array to emptyKeras Dropout with noise_shapePredict for multiple rows for single/multiple timesteps lstmAbout correctly using dropout in RNNs (Keras)Tensorflow compute_output_shape() Not Working For Custom LayerKERAS: Get a SLICE of RNN timesteps with return_sequence = TrueInput 0 is incompatible with layer flatten_5: expected min_ndim=3, found ndim=2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to implement custom dropout layer. During forward propagation, I'd like my inputs to pass as it is without any dropout. During backward pass, I'd like to update gradient of only some inputs while freezing gradient of others. This would be based on a probability which decides what gradients to update and what to freeze.



I have implemented a custom layer, however as the modification is subtle, it is difficult to verify if it is correct. It is possible to get reasonable output with incorrect implementation. I have modified existing dropout function in Keras.



class MyDropout(Layer):
"""Applies Dropout to the input.
Dropout consists in randomly setting
a fraction `rate` of input units to 0 at each update during training time,
which helps prevent overfitting.
# Arguments
rate: float between 0 and 1. Fraction of the input units to drop.
noise_shape: 1D integer tensor representing the shape of the
binary dropout mask that will be multiplied with the input.
For instance, if your inputs have shape
`(batch_size, timesteps, features)` and
you want the dropout mask to be the same for all timesteps,
you can use `noise_shape=(batch_size, 1, features)`.
seed: A Python integer to use as random seed.
# References
- [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](
http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf)
"""
def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
super(MyDropout, self).__init__(**kwargs)
self.rate = min(1., max(0., rate))
self.noise_shape = noise_shape
self.seed = seed
self.supports_masking = True

def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape

symbolic_shape = keras.backend.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)


def call(self, inputs, training=None):
if 0. < self.rate < 1.:
noise_shape = self._get_noise_shape(inputs)

# generate random number of same shape as input
uniform_random_number = keras.backend.random_normal(shape=keras.backend.shape(inputs))
# check where the random number if greater than the dropout rate
indices_greater_than = tf.greater(uniform_random_number,self.rate,name = 'stoppedGradientLocations')
indices_greater_than = tf.cast(indices_greater_than,dtype=tf.float32)
inputs_copy = tf.identity(inputs)
out1 = tf.stop_gradient(inputs_copy*indices_greater_than)
indices_less_than= 1 - indices_greater_than
out2 = inputs*indices_less_than
out_total = out1 + out2


return out_total

def get_config(self):
config = 'rate': self.rate,
'noise_shape': self.noise_shape,
'seed': self.seed
base_config = super(Dropout, self).get_config()
return dict(list(base_config.items()) + list(config.items()))

def compute_output_shape(self, input_shape):
return input_shape


What is the best way to verify my implementation - is the code working as intended?










share|improve this question






























    1















    I am trying to implement custom dropout layer. During forward propagation, I'd like my inputs to pass as it is without any dropout. During backward pass, I'd like to update gradient of only some inputs while freezing gradient of others. This would be based on a probability which decides what gradients to update and what to freeze.



    I have implemented a custom layer, however as the modification is subtle, it is difficult to verify if it is correct. It is possible to get reasonable output with incorrect implementation. I have modified existing dropout function in Keras.



    class MyDropout(Layer):
    """Applies Dropout to the input.
    Dropout consists in randomly setting
    a fraction `rate` of input units to 0 at each update during training time,
    which helps prevent overfitting.
    # Arguments
    rate: float between 0 and 1. Fraction of the input units to drop.
    noise_shape: 1D integer tensor representing the shape of the
    binary dropout mask that will be multiplied with the input.
    For instance, if your inputs have shape
    `(batch_size, timesteps, features)` and
    you want the dropout mask to be the same for all timesteps,
    you can use `noise_shape=(batch_size, 1, features)`.
    seed: A Python integer to use as random seed.
    # References
    - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](
    http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf)
    """
    def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
    super(MyDropout, self).__init__(**kwargs)
    self.rate = min(1., max(0., rate))
    self.noise_shape = noise_shape
    self.seed = seed
    self.supports_masking = True

    def _get_noise_shape(self, inputs):
    if self.noise_shape is None:
    return self.noise_shape

    symbolic_shape = keras.backend.shape(inputs)
    noise_shape = [symbolic_shape[axis] if shape is None else shape
    for axis, shape in enumerate(self.noise_shape)]
    return tuple(noise_shape)


    def call(self, inputs, training=None):
    if 0. < self.rate < 1.:
    noise_shape = self._get_noise_shape(inputs)

    # generate random number of same shape as input
    uniform_random_number = keras.backend.random_normal(shape=keras.backend.shape(inputs))
    # check where the random number if greater than the dropout rate
    indices_greater_than = tf.greater(uniform_random_number,self.rate,name = 'stoppedGradientLocations')
    indices_greater_than = tf.cast(indices_greater_than,dtype=tf.float32)
    inputs_copy = tf.identity(inputs)
    out1 = tf.stop_gradient(inputs_copy*indices_greater_than)
    indices_less_than= 1 - indices_greater_than
    out2 = inputs*indices_less_than
    out_total = out1 + out2


    return out_total

    def get_config(self):
    config = 'rate': self.rate,
    'noise_shape': self.noise_shape,
    'seed': self.seed
    base_config = super(Dropout, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))

    def compute_output_shape(self, input_shape):
    return input_shape


    What is the best way to verify my implementation - is the code working as intended?










    share|improve this question


























      1












      1








      1


      1






      I am trying to implement custom dropout layer. During forward propagation, I'd like my inputs to pass as it is without any dropout. During backward pass, I'd like to update gradient of only some inputs while freezing gradient of others. This would be based on a probability which decides what gradients to update and what to freeze.



      I have implemented a custom layer, however as the modification is subtle, it is difficult to verify if it is correct. It is possible to get reasonable output with incorrect implementation. I have modified existing dropout function in Keras.



      class MyDropout(Layer):
      """Applies Dropout to the input.
      Dropout consists in randomly setting
      a fraction `rate` of input units to 0 at each update during training time,
      which helps prevent overfitting.
      # Arguments
      rate: float between 0 and 1. Fraction of the input units to drop.
      noise_shape: 1D integer tensor representing the shape of the
      binary dropout mask that will be multiplied with the input.
      For instance, if your inputs have shape
      `(batch_size, timesteps, features)` and
      you want the dropout mask to be the same for all timesteps,
      you can use `noise_shape=(batch_size, 1, features)`.
      seed: A Python integer to use as random seed.
      # References
      - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](
      http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf)
      """
      def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
      super(MyDropout, self).__init__(**kwargs)
      self.rate = min(1., max(0., rate))
      self.noise_shape = noise_shape
      self.seed = seed
      self.supports_masking = True

      def _get_noise_shape(self, inputs):
      if self.noise_shape is None:
      return self.noise_shape

      symbolic_shape = keras.backend.shape(inputs)
      noise_shape = [symbolic_shape[axis] if shape is None else shape
      for axis, shape in enumerate(self.noise_shape)]
      return tuple(noise_shape)


      def call(self, inputs, training=None):
      if 0. < self.rate < 1.:
      noise_shape = self._get_noise_shape(inputs)

      # generate random number of same shape as input
      uniform_random_number = keras.backend.random_normal(shape=keras.backend.shape(inputs))
      # check where the random number if greater than the dropout rate
      indices_greater_than = tf.greater(uniform_random_number,self.rate,name = 'stoppedGradientLocations')
      indices_greater_than = tf.cast(indices_greater_than,dtype=tf.float32)
      inputs_copy = tf.identity(inputs)
      out1 = tf.stop_gradient(inputs_copy*indices_greater_than)
      indices_less_than= 1 - indices_greater_than
      out2 = inputs*indices_less_than
      out_total = out1 + out2


      return out_total

      def get_config(self):
      config = 'rate': self.rate,
      'noise_shape': self.noise_shape,
      'seed': self.seed
      base_config = super(Dropout, self).get_config()
      return dict(list(base_config.items()) + list(config.items()))

      def compute_output_shape(self, input_shape):
      return input_shape


      What is the best way to verify my implementation - is the code working as intended?










      share|improve this question
















      I am trying to implement custom dropout layer. During forward propagation, I'd like my inputs to pass as it is without any dropout. During backward pass, I'd like to update gradient of only some inputs while freezing gradient of others. This would be based on a probability which decides what gradients to update and what to freeze.



      I have implemented a custom layer, however as the modification is subtle, it is difficult to verify if it is correct. It is possible to get reasonable output with incorrect implementation. I have modified existing dropout function in Keras.



      class MyDropout(Layer):
      """Applies Dropout to the input.
      Dropout consists in randomly setting
      a fraction `rate` of input units to 0 at each update during training time,
      which helps prevent overfitting.
      # Arguments
      rate: float between 0 and 1. Fraction of the input units to drop.
      noise_shape: 1D integer tensor representing the shape of the
      binary dropout mask that will be multiplied with the input.
      For instance, if your inputs have shape
      `(batch_size, timesteps, features)` and
      you want the dropout mask to be the same for all timesteps,
      you can use `noise_shape=(batch_size, 1, features)`.
      seed: A Python integer to use as random seed.
      # References
      - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](
      http://www.jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf)
      """
      def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
      super(MyDropout, self).__init__(**kwargs)
      self.rate = min(1., max(0., rate))
      self.noise_shape = noise_shape
      self.seed = seed
      self.supports_masking = True

      def _get_noise_shape(self, inputs):
      if self.noise_shape is None:
      return self.noise_shape

      symbolic_shape = keras.backend.shape(inputs)
      noise_shape = [symbolic_shape[axis] if shape is None else shape
      for axis, shape in enumerate(self.noise_shape)]
      return tuple(noise_shape)


      def call(self, inputs, training=None):
      if 0. < self.rate < 1.:
      noise_shape = self._get_noise_shape(inputs)

      # generate random number of same shape as input
      uniform_random_number = keras.backend.random_normal(shape=keras.backend.shape(inputs))
      # check where the random number if greater than the dropout rate
      indices_greater_than = tf.greater(uniform_random_number,self.rate,name = 'stoppedGradientLocations')
      indices_greater_than = tf.cast(indices_greater_than,dtype=tf.float32)
      inputs_copy = tf.identity(inputs)
      out1 = tf.stop_gradient(inputs_copy*indices_greater_than)
      indices_less_than= 1 - indices_greater_than
      out2 = inputs*indices_less_than
      out_total = out1 + out2


      return out_total

      def get_config(self):
      config = 'rate': self.rate,
      'noise_shape': self.noise_shape,
      'seed': self.seed
      base_config = super(Dropout, self).get_config()
      return dict(list(base_config.items()) + list(config.items()))

      def compute_output_shape(self, input_shape):
      return input_shape


      What is the best way to verify my implementation - is the code working as intended?







      python-3.x tensorflow keras






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 at 21:02







      Terwayp

















      asked Mar 24 at 20:27









      TerwaypTerwayp

      213




      213






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328246%2fcustom-dropout-layer-implementation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328246%2fcustom-dropout-layer-implementation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해