Skip to content

[C++ API] nn::Module::as#9149

Closed
goldsborough wants to merge 1 commit intopytorch:masterfrom
goldsborough:cpp-as
Closed

[C++ API] nn::Module::as#9149
goldsborough wants to merge 1 commit intopytorch:masterfrom
goldsborough:cpp-as

Conversation

@goldsborough
Copy link
Contributor

@goldsborough goldsborough commented Jul 3, 2018

Added a way to dynamic_cast an nn::Module and get a pointer to it. nn::Module::is<T> just checked if the return value of the dynamic_cast was nullptr, so I got rid of is<T> since it's equivalent to as<T> != nullptr(or just as<T> due to boolean conversion).

We're now at

if (auto* conv = module.as<nn::Conv2d>()) {
  conv->weight.data().normal_(0.0, 0.02);
} else if (auto* bn = module.as<nn::BatchNorm>()) {
  bn->weight.data().normal_(1.0, 0.02);
  bn->bias.data().fill_(0);
}

@ezyang @apaszke @ebetica

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goldsborough has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

goodlux pushed a commit to goodlux/pytorch that referenced this pull request Aug 15, 2018
Summary:
Added a way to `dynamic_cast` an `nn::Module` and get a pointer to it. `nn::Module::is<T>` just checked if the return value of the `dynamic_cast` was nullptr, so I got rid of `is<T>` since it's equivalent to `as<T> != nullptr`(or just `as<T>` due to boolean conversion).

We're now at

```
if (auto* conv = module.as<nn::Conv2d>()) {
  conv->weight.data().normal_(0.0, 0.02);
} else if (auto* bn = module.as<nn::BatchNorm>()) {
  bn->weight.data().normal_(1.0, 0.02);
  bn->bias.data().fill_(0);
}
```

ezyang apaszke ebetica
Closes pytorch#9149

Differential Revision: D8735954

Pulled By: goldsborough

fbshipit-source-id: e2b8f6f0cea16a621f8bc0807a33cc7651d25154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants