This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dmlc/master
merge dmlc/master
- Loading branch information
Showing
13 changed files
with
191 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# coding: utf-8 | ||
|
||
"""NDArray interface of mxnet""" | ||
from __future__ import absolute_import | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# coding: utf-8 | ||
"""Automatic naming support for symbolic API.""" | ||
from __future__ import absolute_import | ||
|
||
class NameManager(object): | ||
"""NameManager to do automatic naming. | ||
User can also inheritate this object to change naming behavior. | ||
""" | ||
current = None | ||
|
||
def __init__(self): | ||
self._counter = {} | ||
self._old_manager = None | ||
|
||
def get(self, name, hint): | ||
"""Get the canonical name for a symbol. | ||
This is default implementation. | ||
When user specified a name, | ||
the user specified name will be used. | ||
When user did not, we will automatically generate a | ||
name based on hint string. | ||
Parameters | ||
---------- | ||
name : str or None | ||
The name user specified. | ||
hint : str | ||
A hint string, which can be used to generate name. | ||
Returns | ||
------- | ||
full_name : str | ||
A canonical name for the user. | ||
""" | ||
if name: | ||
return name | ||
if hint not in self._counter: | ||
self._counter[hint] = 0 | ||
name = '%s%d' % (hint, self._counter[hint]) | ||
self._counter[hint] += 1 | ||
return name | ||
|
||
def __enter__(self): | ||
self._old_manager = NameManager.current | ||
NameManager.current = self | ||
return self | ||
|
||
def __exit__(self, ptype, value, trace): | ||
assert self._old_manager | ||
NameManager.current = self._old_manager | ||
|
||
|
||
class Prefix(NameManager): | ||
"""A name manager that always attach a prefix to all names. | ||
Examples | ||
-------- | ||
>>> import mxnet as mx | ||
>>> data = mx.symbol.Variable('data') | ||
>>> with mx.name.Prefix('mynet_'): | ||
net = mx.symbol.FullyConnected(data, num_hidden=10, name='fc1') | ||
>>> net.list_arguments() | ||
['data', 'mynet_fc1_weight', 'mynet_fc1_bias'] | ||
""" | ||
def __init__(self, prefix): | ||
super(Prefix, self).__init__() | ||
self._prefix = prefix | ||
|
||
def get(self, name, hint): | ||
name = super(Prefix, self).get(name, hint) | ||
return self._prefix + name | ||
|
||
# initialize the default name manager | ||
NameManager.current = NameManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.