Skip to content

Instantly share code, notes, and snippets.

@XinyueZ
Last active January 15, 2022 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XinyueZ/debcff1838abb8e00143d7ecf99283c9 to your computer and use it in GitHub Desktop.
Save XinyueZ/debcff1838abb8e00143d7ecf99283c9 to your computer and use it in GitHub Desktop.
Dropout (inverted dropout)
disp("Dropout, overcome the overfitting in NN....");
function [X] = dropout(X, keep_prob)
% Dropout some units from X.
% (1 - keep_prob) of units will be dropped out.
sz = size(X);
mask = rand(sz);
mask = mask < 0.8; % Element of mask will be set to 1 or 0 with probability π‘˜π‘’π‘’π‘_π‘π‘Ÿπ‘œπ‘
mask
X = X .* mask;
% The element size of X (input) has been reduced by keep_prob from mask (a percentage of elements have been dropped out by mask),
% thus the value of X (output) is also gonna be reduced, so to compensate this roughly we shall invert the change by dividing
% keep_prob to make sure the value of X (output) won't be impacted mostly.
X = X ./ keep_prob;
endfunction
L = randi(35, 3, 4);
keep_prob = 0.85;
L
printf("sum(L): %f\n", sum(L, 2));
L = dropout(L, keep_prob);
printf("sum(L): %f\n", sum(L, 2));
% Output like:
% L =
% 33 8 33 18
% 6 27 32 24
% 23 26 27 31
% sum(L):92.000000
% sum(L):89.000000
% sum(L):107.000000
% mask =
% 1 1 1 1
% 1 0 1 1
% 1 1 1 0
% sum(L):108.235294
% sum(L):72.941176
% sum(L):89.411765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment