-
Notifications
You must be signed in to change notification settings - Fork 3
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
adding dropout-by row #8
base: dropout_schedule
Are you sure you want to change the base?
Changes from 3 commits
6548b55
23ae730
614a868
c1d1ad1
14662b6
1d22219
5b8b98b
4137c9d
d721e59
1e2adab
463a4dc
d0290c3
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 |
---|---|---|
|
@@ -87,27 +87,39 @@ void PnormComponent::Write(std::ostream &os, bool binary) const { | |
} | ||
|
||
|
||
void DropoutComponent::Init(int32 dim, BaseFloat dropout_proportion) { | ||
void DropoutComponent::Init(int32 dim, BaseFloat dropout_proportion, bool dropout_per_frame) { | ||
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. please watch line length (80-char limit) |
||
dropout_proportion_ = dropout_proportion; | ||
dropout_per_frame_ = dropout_per_frame; | ||
dim_ = dim; | ||
} | ||
|
||
void DropoutComponent::InitFromConfig(ConfigLine *cfl) { | ||
int32 dim = 0; | ||
BaseFloat dropout_proportion = 0.0; | ||
bool dropout_per_frame = false; | ||
bool ok = cfl->GetValue("dim", &dim) && | ||
cfl->GetValue("dropout-proportion", &dropout_proportion); | ||
bool ok2 = cfl->GetValue("dropout-per-frame", &dropout_per_frame); | ||
if (!ok || cfl->HasUnusedValues() || dim <= 0 || | ||
dropout_proportion < 0.0 || dropout_proportion > 1.0) | ||
KALDI_ERR << "Invalid initializer for layer of type " | ||
<< Type() << ": \"" << cfl->WholeLine() << "\""; | ||
Init(dim, dropout_proportion); | ||
if( ! ok2 ) | ||
{ | ||
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. you don't need a branch here because dropout_per_frame defaults to false if not set (that's how you |
||
dropout_per_frame = false; | ||
Init(dim, dropout_proportion, dropout_per_frame); | ||
} | ||
else | ||
{ | ||
Init(dim, dropout_proportion, dropout_per_frame); | ||
} | ||
} | ||
|
||
std::string DropoutComponent::Info() const { | ||
std::ostringstream stream; | ||
stream << Type() << ", dim=" << dim_ | ||
<< ", dropout-proportion=" << dropout_proportion_; | ||
<< ", dropout-proportion=" << dropout_proportion_ | ||
<< ", dropout-per-frame=" << dropout_per_frame_; | ||
return stream.str(); | ||
} | ||
|
||
|
@@ -119,16 +131,36 @@ void DropoutComponent::Propagate(const ComponentPrecomputedIndexes *indexes, | |
|
||
BaseFloat dropout = dropout_proportion_; | ||
KALDI_ASSERT(dropout >= 0.0 && dropout <= 1.0); | ||
if(dropout_per_frame_ == true) | ||
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.
|
||
{ | ||
// This const_cast is only safe assuming you don't attempt | ||
// to use multi-threaded code with the GPU. | ||
const_cast<CuRand<BaseFloat>&>(random_generator_).RandUniform(out); | ||
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. Here, you'll want to create a temporary vector with dimension equal to the num-rows in your 'in'/'out' matrices, and do the rand stuff on that, then you'll need CopyColsFromVec(). |
||
|
||
// This const_cast is only safe assuming you don't attempt | ||
// to use multi-threaded code with the GPU. | ||
const_cast<CuRand<BaseFloat>&>(random_generator_).RandUniform(out); | ||
out->Add(-dropout); // now, a proportion "dropout" will be <0.0 | ||
out->ApplyHeaviside(); // apply the function (x>0?1:0). Now, a proportion "dropout" will | ||
// be zero and (1 - dropout) will be 1.0. | ||
|
||
out->Add(-dropout); // now, a proportion "dropout" will be <0.0 | ||
out->ApplyHeaviside(); // apply the function (x>0?1:0). Now, a proportion "dropout" will | ||
// be zero and (1 - dropout) will be 1.0. | ||
out->MulElements(in); | ||
} | ||
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.
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. Change every other place too. 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. @danpovey using 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. very slow... |
||
else | ||
{ | ||
|
||
out->MulElements(in); | ||
// This const_cast is only safe assuming you don't attempt | ||
// to use multi-threaded code with the GPU. | ||
const_cast<CuRand<BaseFloat>&>(random_generator_).RandUniform(out); | ||
out->Add(-dropout); // now, a proportion "dropout" will be <0.0 | ||
out->ApplyHeaviside(); // apply the function (x>0?1:0). Now, a proportion "dropout" will | ||
// be zero and (1 - dropout) will be 1.0. | ||
CuVector<BaseFloat> *random_drop_vector = new CuVector<BaseFloat>(in.NumRows(), kSetZero); | ||
MatrixIndexT i = 0; | ||
random_drop_vector->CopyColFromMat(*out, i); | ||
for (MatrixIndexT i = 0; i < in.NumCols(); i++) | ||
{ | ||
out->CopyColFromVec(*random_drop_vector, i); | ||
} | ||
out->MulElements(in); | ||
} | ||
} | ||
|
||
|
||
|
@@ -154,6 +186,8 @@ void DropoutComponent::Read(std::istream &is, bool binary) { | |
ReadBasicType(is, binary, &dim_); | ||
ExpectToken(is, binary, "<DropoutProportion>"); | ||
ReadBasicType(is, binary, &dropout_proportion_); | ||
ExpectToken(is, binary, "<DropoutPerFrame>"); | ||
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. Make this backcompatible. Change this to ReadToken and then add an if condition to check which token is present. 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. @GaofengCheng, you need to understand what Vimal was saying here- there needs to be back compatibility code for the old format. Search for ReadToken() in the file for examples. However, the reason for your error is that you need to recompile in 'chainbin/' (and possibly chain/'). 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. .. and, of course, make this back compatible. |
||
ReadBasicType(is, binary, &dropout_per_frame_); | ||
ExpectToken(is, binary, "</DropoutComponent>"); | ||
} | ||
|
||
|
@@ -163,6 +197,8 @@ void DropoutComponent::Write(std::ostream &os, bool binary) const { | |
WriteBasicType(os, binary, dim_); | ||
WriteToken(os, binary, "<DropoutProportion>"); | ||
WriteBasicType(os, binary, dropout_proportion_); | ||
WriteToken(os, binary, "<DropoutPerFrame>"); | ||
WriteBasicType(os, binary, dropout_per_frame_); | ||
WriteToken(os, binary, "</DropoutComponent>"); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,11 +87,11 @@ class PnormComponent: public Component { | |
// "Dropout: A Simple Way to Prevent Neural Networks from Overfitting". | ||
class DropoutComponent : public RandomComponent { | ||
public: | ||
void Init(int32 dim, BaseFloat dropout_proportion = 0.0); | ||
void Init(int32 dim, BaseFloat dropout_proportion = 0.0, bool dropout_per_frame = false); | ||
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. please watch line length. 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. line too long |
||
|
||
DropoutComponent(int32 dim, BaseFloat dropout = 0.0) { Init(dim, dropout); } | ||
DropoutComponent(int32 dim, BaseFloat dropout = 0.0, bool dropout_per_frame = false) { Init(dim, dropout, dropout_per_frame); } | ||
|
||
DropoutComponent(): dim_(0), dropout_proportion_(0.0) { } | ||
DropoutComponent(): dim_(0), dropout_proportion_(0.0), dropout_per_frame_(false) { } | ||
|
||
virtual int32 Properties() const { | ||
return kLinearInInput|kBackpropInPlace|kSimpleComponent|kBackpropNeedsInput|kBackpropNeedsOutput; | ||
|
@@ -120,17 +120,21 @@ class DropoutComponent : public RandomComponent { | |
Component *to_update, | ||
CuMatrixBase<BaseFloat> *in_deriv) const; | ||
virtual Component* Copy() const { return new DropoutComponent(dim_, | ||
dropout_proportion_); } | ||
dropout_proportion_, | ||
dropout_per_frame_); } | ||
virtual std::string Info() const; | ||
|
||
void SetDropoutProportion(BaseFloat dropout_proportion) { dropout_proportion_ = dropout_proportion; } | ||
void SetDropoutProportion(BaseFloat dropout_proportion, bool dropout_per_frame) { | ||
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. .. remove dropout_per_frame.. |
||
dropout_proportion_ = dropout_proportion; | ||
dropout_per_frame_ = dropout_per_frame; | ||
} | ||
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. Odd indentation. Make sure you are not introducing tabs into the file.
You can find 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. @danpovey updating the new version, sorry for incorrect format, I'm using visual studio code/sublime, and so far I haven't found a format tool that could keep exactly the same as the way we are using in Kaldi (I have tried to format nnet-simple-component.cc, it changes a lot to the existing code, though it's also google style, ). I PR the |
||
|
||
private: | ||
int32 dim_; | ||
/// dropout-proportion is the proportion that is dropped out, | ||
/// e.g. if 0.1, we set 10% to zero value. | ||
BaseFloat dropout_proportion_; | ||
|
||
bool dropout_per_frame_; | ||
}; | ||
|
||
class ElementwiseProductComponent: public Component { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -524,12 +524,14 @@ std::string NnetInfo(const Nnet &nnet) { | |
} | ||
|
||
void SetDropoutProportion(BaseFloat dropout_proportion, | ||
bool dropout_per_frame , | ||
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. No space before |
||
Nnet *nnet) { | ||
dropout_per_frame = false; | ||
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. Why is the input to the function ignored? |
||
for (int32 c = 0; c < nnet->NumComponents(); c++) { | ||
Component *comp = nnet->GetComponent(c); | ||
DropoutComponent *dc = dynamic_cast<DropoutComponent*>(comp); | ||
if (dc != NULL) | ||
dc->SetDropoutProportion(dropout_proportion); | ||
dc->SetDropoutProportion(dropout_proportion, dropout_per_frame); | ||
} | ||
} | ||
|
||
|
@@ -694,18 +696,22 @@ void ReadEditConfig(std::istream &edit_config_is, Nnet *nnet) { | |
// matches names of components, not nodes. | ||
config_line.GetValue("name", &name_pattern); | ||
BaseFloat proportion = -1; | ||
bool perframe = false; | ||
if (!config_line.GetValue("proportion", &proportion)) { | ||
KALDI_ERR << "In edits-config, expected proportion to be set in line: " | ||
<< config_line.WholeLine(); | ||
} | ||
if (!config_line.GetValue("perframe", &perframe)) { | ||
perframe = false; | ||
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. per_frame is more appropriate name |
||
} | ||
DropoutComponent *component = NULL; | ||
int32 num_dropout_proportions_set = 0; | ||
for (int32 c = 0; c < nnet->NumComponents(); c++) { | ||
if (NameMatchesPattern(nnet->GetComponentName(c).c_str(), | ||
name_pattern.c_str()) && | ||
(component = | ||
dynamic_cast<DropoutComponent*>(nnet->GetComponent(c)))) { | ||
component->SetDropoutProportion(proportion); | ||
component->SetDropoutProportion(proportion, perframe); | ||
num_dropout_proportions_set++; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,7 +182,7 @@ std::string NnetInfo(const Nnet &nnet); | |
|
||
/// This function sets the dropout proportion in all dropout component to | ||
/// dropout_proportion value. | ||
void SetDropoutProportion(BaseFloat dropout_proportion, Nnet *nnet); | ||
void SetDropoutProportion(BaseFloat dropout_proportion, bool dropout_per_frame, Nnet *nnet); | ||
|
||
/// This function finds a list of components that are never used, and outputs | ||
/// the integer comopnent indexes (you can use these to index | ||
|
@@ -233,7 +233,7 @@ void FindOrphanNodes(const Nnet &nnet, std::vector<int32> *nodes); | |
remove internal nodes directly; instead you should use the command | ||
'remove-orphans'. | ||
|
||
set-dropout-proportion [name=<name-pattern>] proportion=<dropout-proportion> | ||
set-dropout-proportion [name=<name-pattern>] proportion=<dropout-proportion> perframe=<perframe> | ||
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. This should probably be made per-frame. |
||
Sets the dropout rates for any components of type DropoutComponent whose | ||
names match the given <name-pattern> (e.g. lstm*). <name-pattern> defaults to "*". | ||
\endverbatim | ||
|
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 can remove this 'false' argument from the function... just make it a fixed property of the component that can't be changed after you initialize.