Why input is scaled in tf.nn.dropout in tensorflow?

前端 未结 4 1957
自闭症患者
自闭症患者 2021-01-30 13:31

I can\'t understand why dropout works like this in tensorflow. The blog of CS231n says that, \"dropout is implemented by only keeping a neuron active with some probability

相关标签:
4条回答
  • 2021-01-30 13:50

    This scaling enables the same network to be used for training (with keep_prob < 1.0) and evaluation (with keep_prob == 1.0). From the Dropout paper:

    The idea is to use a single neural net at test time without dropout. The weights of this network are scaled-down versions of the trained weights. If a unit is retained with probability p during training, the outgoing weights of that unit are multiplied by p at test time as shown in Figure 2.

    Rather than adding ops to scale down the weights by keep_prob at test time, the TensorFlow implementation adds an op to scale up the weights by 1. / keep_prob at training time. The effect on performance is negligible, and the code is simpler (because we use the same graph and treat keep_prob as a tf.placeholder() that is fed a different value depending on whether we are training or evaluating the network).

    0 讨论(0)
  • 2021-01-30 13:51

    Here is a quick experiment to disperse any remaining confusion.

    Statistically the weights of a NN-layer follow a distribution that is usually close to normal (but not necessarily), but even in the case when trying to sample a perfect normal distribution in practice, there are always computational errors.

    Then consider the following experiment:

    DIM = 1_000_000                      # set our dims for weights and input
    x = np.ones((DIM,1))                 # our input vector
    #x = np.random.rand(DIM,1)*2-1.0     # or could also be a more realistic normalized input
    
    probs = [1.0, 0.7, 0.5, 0.3]         # define dropout probs
    
    W = np.random.normal(size=(DIM,1))   # sample normally distributed weights
    print("W-mean = ", W.mean())         # note the mean is not perfect --> sampling error!
    
    # DO THE DRILL
    h = defaultdict(list)
    for i in range(1000):
      for p in probs:
        M = np.random.rand(DIM,1)
        M = (M < p).astype(int)
        Wp = W * M
        a = np.dot(Wp.T, x)
        h[str(p)].append(a)
    
    for k,v in h.items():
      print("For drop-out prob %r the average linear activation is %r (unscaled) and %r (scaled)" % (k, np.mean(v), np.mean(v)/float(k)))
    

    Sample output:

    x-mean =  1.0
    W-mean =  -0.001003985674840264
    For drop-out prob '1.0' the average linear activation is -1003.985674840258 (unscaled) and -1003.985674840258 (scaled)
    For drop-out prob '0.7' the average linear activation is -700.6128015029908 (unscaled) and -1000.8754307185584 (scaled)
    For drop-out prob '0.5' the average linear activation is -512.1602655283492 (unscaled) and -1024.3205310566984 (scaled)
    For drop-out prob '0.3' the average linear activation is -303.21194422742315 (unscaled) and -1010.7064807580772 (scaled)
    

    Notice that the unscaled activations diminish due to the statistically imperfect normal distribution.

    Can you spot an obvious correlation between the W-mean and the average linear activation means?

    0 讨论(0)
  • 2021-01-30 13:52

    If you keep reading in cs231n, the difference between dropout and inverted dropout is explained.

    Since we want to leave the forward pass at test time untouched (and tweak our network just during training), tf.nn.dropout directly implements inverted dropout, scaling the values.

    0 讨论(0)
  • 2021-01-30 14:03

    Let's say the network had n neurons and we applied dropout rate 1/2

    Training phase, we would be left with n/2 neurons. So if you were expecting output x with all the neurons, now you will get on x/2. So for every batch, the network weights are trained according to this x/2

    Testing/Inference/Validation phase, we dont apply any dropout so the output is x. So, in this case, the output would be with x and not x/2, which would give you the incorrect result. So what you can do is scale it to x/2 during testing.

    Rather than the above scaling specific to Testing phase. What Tensorflow's dropout layer does is that whether it is with dropout or without (Training or testing), it scales the output so that the sum is constant.

    0 讨论(0)
提交回复
热议问题