New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation - Find and Fill Method for Dropout Layer #3684
base: master
Are you sure you want to change the base?
Changes from 4 commits
f2660a7
cf6aa30
65e9914
7511561
20cf02e
862c05e
52b6e29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,20 @@ class DropoutType : public Layer<MatType> | |
* @param output Resulting output activation. | ||
*/ | ||
void Forward(const MatType& input, MatType& output); | ||
|
||
|
||
/** | ||
* Implementation of the forward pass of the dropout layer. | ||
* | ||
* @param input Input data used for evaluating the specified function. | ||
* @param output Resulting output activation. | ||
*/ | ||
void ForwardImpl(const MatType& input, MatType& output); | ||
|
||
#ifdef MLPACK_HAS_COOT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we actually need this guard here: we want to use the optimized implementation when we have an Armadillo matrix, and the general implementation otherwise. So long as the second implementation of |
||
void ForwardImpl(const MatType& input, MatType& output); | ||
#endif | ||
|
||
|
||
/** | ||
* Ordinary feed backward pass of the dropout layer. | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -74,10 +74,18 @@ DropoutType<MatType>::operator=(DropoutType&& other) | |||||
return *this; | ||||||
} | ||||||
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need for an extra line 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this one is still missing. |
||||||
template<typename MatType> | ||||||
void DropoutType<MatType>::Forward(const MatType& input, MatType& output) | ||||||
{ | ||||||
// The dropout mask will not be multiplied in testing mode. | ||||||
ForwardImpl(input, output); | ||||||
} | ||||||
|
||||||
template<typename MatType> | ||||||
void DropoutType<MatType>::ForwardImpl(const MatType& input, | ||||||
MatType& output) | ||||||
{ | ||||||
if (!this->training) | ||||||
{ | ||||||
output = input; | ||||||
|
@@ -92,6 +100,28 @@ void DropoutType<MatType>::Forward(const MatType& input, MatType& output) | |||||
} | ||||||
} | ||||||
|
||||||
#ifdef MLPACK_HAS_COOT | ||||||
|
||||||
template<typename MatType> | ||||||
void DropoutType<MatType>::ForwardImpl(const MatType& input, | ||||||
MatType& output) | ||||||
{ | ||||||
if (!this->training) | ||||||
{ | ||||||
output = input; | ||||||
} | ||||||
else | ||||||
{ | ||||||
mask.randu(input.n_rows, input.n_cols); | ||||||
arma::uvec indices = arma::find(mask > ratio); | ||||||
mask.zeros(); | ||||||
mask.elem(indices).fill(1); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Would this work? It avoids the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rcurtin You can take a look at my implementation in the latest commit. It should be a bit faster and is fully functional. I avoided using arma:: to keep it more general. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, it looks good to me! Thanks for doing that. |
||||||
output = input % mask * scale; | ||||||
} | ||||||
} | ||||||
|
||||||
#endif | ||||||
|
||||||
template<typename MatType> | ||||||
void DropoutType<MatType>::Backward( | ||||||
const MatType& /* input */, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll have to use SFINAE here, e.g. add an argument like
const typename std::enable_if_t<arma::is_arma_type<MatType>::value>::type* = 0
(I just did that from memory, it may not be exactly right).