-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Init partial network parameters from another saved model. #2664
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c986582
Support to init partial network parameters from the tar file.
qingqing01 6dce72f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
qingqing01 555540f
fix typo
qingqing01 11a8dfe
Use Parameters.from_tar for static method.
qingqing01 7c9b53c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
qingqing01 23d6c59
add comments
qingqing01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ class Parameters(object): | |
def __init__(self): | ||
self.__param_conf__ = dict() | ||
self.__gradient_machines__ = [] | ||
self.__tmp_params__ = [] | ||
self.__tmp_params__ = dict() | ||
|
||
def __append_config__(self, param_conf): | ||
""" | ||
|
@@ -128,13 +128,10 @@ def __getitem__(self, key): | |
|
||
if len(self.__gradient_machines__) == 0: | ||
# create new parameter in python numpy. | ||
if len(self.__tmp_params__) != 0: | ||
ret_list = [ | ||
mat for name, mat in self.__tmp_params__ if name == key | ||
] | ||
if len(ret_list) == 1: | ||
return ret_list[0] | ||
return np.ndarray(shape=shape, dtype=np.float32) | ||
if key in self.__tmp_params__: | ||
return self.__tmp_params__[key] | ||
else: | ||
return np.ndarray(shape=shape, dtype=np.float32) | ||
else: | ||
for each_gradient_machine in self.__gradient_machines__: | ||
param = __get_parameter_in_gradient_machine__( | ||
|
@@ -187,7 +184,7 @@ def __setitem__(self, key, value): | |
(shape, value.shape)) | ||
|
||
if len(self.__gradient_machines__) == 0: | ||
self.__tmp_params__.append((key, value)) | ||
self.__tmp_params__[key] = value | ||
else: | ||
for each_gradient_machine in self.__gradient_machines__: | ||
__copy_parameter_to_gradient_machine__(each_gradient_machine, | ||
|
@@ -231,7 +228,7 @@ def append_gradient_machine(self, gradient_machine): | |
raise ValueError("gradient_machine should be api.GradientMachine") | ||
|
||
if len(self.__tmp_params__) != 0: | ||
for name, val in self.__tmp_params__: | ||
for name, val in self.__tmp_params__.iteritems(): | ||
try: | ||
__copy_parameter_to_gradient_machine__(gradient_machine, | ||
name, val) | ||
|
@@ -287,6 +284,18 @@ def to_tar(self, f): | |
|
||
@staticmethod | ||
def from_tar(f): | ||
""" | ||
Create a `Parameters` object from the given file. And | ||
the `Parameters` only contains the parameters in this | ||
file. It is adapted the parameters are same in the | ||
defined network and the given file. For example, it | ||
can be used in the inference. | ||
|
||
:param f: the initialized model file. | ||
:type f: tar file | ||
:return: A Parameters object. | ||
:rtype: Parameters. | ||
""" | ||
params = Parameters() | ||
tar = tarfile.TarFile(fileobj=f, mode='r') | ||
for finfo in tar: | ||
|
@@ -302,6 +311,21 @@ def from_tar(f): | |
params.deserialize(param_name, f) | ||
return params | ||
|
||
def init_from_tar(self, f): | ||
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. Because 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. Done. |
||
""" | ||
Different from `from_tar`, this interface can be used to | ||
init partial network parameters from another saved model. | ||
|
||
:param f: the initialized model file. | ||
:type f: tar file | ||
:return: Nothing. | ||
""" | ||
|
||
tar_param = Parameters.from_tar(f) | ||
for pname in tar_param.names(): | ||
if pname in self.names(): | ||
self.set(pname, tar_param.get(pname)) | ||
|
||
|
||
def __get_parameter_in_gradient_machine__(gradient_machine, name): | ||
""" | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
append
could lead the repeated key inself.__tmp_params__
. This is a bug.