-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Conversation
docs/tutorials/gluon/naming.md
Outdated
dense0_ | ||
|
||
|
||
When you create more Blocks of the same kind, they will be named differetly to avoid collision: |
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.
differently
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.
Would it make sense to mention that number appended to the name would be incremented to avoid collision ?
|
||
|
||
```python | ||
model0 = Model() |
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.
should we use names like zeroth_model
and first_model
to get the point across.
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.
I think model0 is fine. It matches the prefix
docs/tutorials/gluon/naming.md
Outdated
``` | ||
|
||
Dense(None -> 100, linear) alexnet0_dense3_ | ||
|
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.
can you please add <!-- INSERT SOURCE DOWNLOAD BUTTONS -->
to enable the notebook download of your tutorial?
docs/tutorials/gluon/naming.md
Outdated
|
||
For example, the alexnet in model zoo has 1000 output dimensions, but maybe you only have 100 classes in your application. | ||
|
||
To see how to do this, we first load an pretrained alexnet. |
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.
a pretrained*
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.
maybe alexnet
if you want to refer to it now as an object instead of a model from the zoo, where it would be AlexNet.
|
||
```python | ||
with alexnet.name_scope(): | ||
alexnet.output = gluon.nn.Dense(100) |
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.
Maybe it is worth putting here that this works only because the models in model zoo are inheriting HybridBlock
and have been built in a way so that their final output layer is associated with the .output
property.
I can see people building their own network usingHybridSequential
for example and trying to update the output layer by calling output
on their network.
Great tutorial, it was much needed and clarifies what is going on under the hood with the naming scopes 👍
|
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.
I have some minor changes requested and some suggestions for clarity.
docs/tutorials/gluon/naming.md
Outdated
|
||
To manage the names of nested Blocks, each Block has a `name_scope` attached to it. All Blocks created within a name scope will have its parent Block's prefix prepended to its name. | ||
|
||
Let's demonstrate this by first define a simple neural net: |
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.
defining
docs/tutorials/gluon/naming.md
Outdated
model0 = Model() | ||
model0.initialize() | ||
model0(mx.nd.zeros((1, 20))) | ||
print(model0.prefix, model0.dense0.prefix, model0.dense1.prefix, model0.mydense.prefix) |
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.
This print all runs together, and... maybe a I'm a little dense ;) but I'm not quite catching the point. Plus I have a hard time breaking it up when the Jupyter font uses an 'l' that looks like a '1', so I see mode10, not model0, and run together it's incomprehensible.
docs/tutorials/gluon/naming.md
Outdated
model1_ model1_dense0_ model1_dense1_ model1_mydense_ | ||
|
||
|
||
**It is recommended that you manually specify prefix for the top level Block (i.e. `model = Model(prefix='mymodel_')`) to avoid potential confusions in naming** |
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.
specify a prefix
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.
add a period at the end
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.
And why not show that code sample if it's so important?
docs/tutorials/gluon/naming.md
Outdated
|
||
**It is recommended that you manually specify prefix for the top level Block (i.e. `model = Model(prefix='mymodel_')`) to avoid potential confusions in naming** | ||
|
||
The same principle also applies to container blocks like Sequantial. `name_scope` can be used inside `__init__` as well as out side of `__init__`: |
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.
spelling... Sequential
|
||
|
||
```python | ||
mydense = gluon.nn.Dense(100, prefix='mydense_') |
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 also choose to not use an underscore and name it what you want. Maybe explain why you want to use an underscore.
docs/tutorials/gluon/naming.md
Outdated
) | ||
|
||
|
||
As a result if you try to save parameters from model0 and load it with model1, you'll get an error due to unmatching names: |
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.
result,
docs/tutorials/gluon/naming.md
Outdated
model0.collect_params().save('model.params') | ||
try: | ||
model1.collect_params().load('model.params', mx.cpu()) | ||
except Exception, e: |
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.
Not python 3 compatible syntax , --> as
|
||
Sometimes you may want to load a pretrained model, and replace certain Blocks in it for fine-tuning. | ||
|
||
For example, the alexnet in model zoo has 1000 output dimensions, but maybe you only have 100 classes in your application. |
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.
the AlexNet model from the model zoo (add link?)
1,000
docs/tutorials/gluon/naming.md
Outdated
|
||
For example, the alexnet in model zoo has 1000 output dimensions, but maybe you only have 100 classes in your application. | ||
|
||
To see how to do this, we first load an pretrained alexnet. |
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.
maybe alexnet
if you want to refer to it now as an object instead of a model from the zoo, where it would be AlexNet.
docs/tutorials/gluon/naming.md
Outdated
|
||
To change the output to 100 dimension, we replace it with a new block. | ||
|
||
- Note that it's important to do this in alexnet's name_scope, otherwise you will have unmatching names when you try to save and load your model. |
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.
I ran into similar issues and just blanked out the prefix. Here's an example that I used.
https://gist.github.com/aaronmarkham/1017664fe683596c614961112a867145#gistcomment-2336981
Can you explain a bit more? This is all really helpful, and I wonder how you'd explain what I ran into and better ways to fix it.
Like how I used ignore_extra
to get around the errors but was still worried that I hadn't really loaded the model properly.
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.
make a fix so that it won't be a problem anymore
name=name, type1=type(existing), type2=type(value))) | ||
|
||
if isinstance(value, Block): | ||
self.register_child(value, name) |
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.
Removing the block update logic can cause having stale blocks in _children
.
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.
_children is OrderedDict now
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.
Just noticed. This means it's finally possible to print the model repr
in order.
Still have problem save and load. #10544 |
* add naming tutorial * fix * Update naming.md * Update index.md * fix save load * fix * fix * fix * fix
* add naming tutorial * fix * Update naming.md * Update index.md * fix save load * fix * fix * fix * fix
Description
(Brief description on what this PR is about)
Checklist
Essentials
Please feel free to remove inapplicable items for your PR.
Changes
Comments