提交 0040a973 编写于 作者: W weishengyu

add save res back

上级 2197820c
...@@ -42,29 +42,29 @@ class TheseusLayer(nn.Layer): ...@@ -42,29 +42,29 @@ class TheseusLayer(nn.Layer):
for layer_i in self._sub_layers: for layer_i in self._sub_layers:
layer_name = self._sub_layers[layer_i].full_name() layer_name = self._sub_layers[layer_i].full_name()
for return_pattern in return_layers: for return_pattern in return_layers:
if return_layers is not None and re.match(return_pattern, if return_layers is not None and re.match(return_pattern, layer_name):
layer_name):
self._sub_layers[layer_i].register_forward_post_hook( self._sub_layers[layer_i].register_forward_post_hook(
self._save_sub_res_hook) self._save_sub_res_hook)
def replace_sub(self, layer_name_pattern, replace_function, def _save_sub_res_hook(self, layer, input, output):
recursive=True): self.res_dict[layer.full_name()] = output
for k in self._sub_layers.keys():
layer_name = self._sub_layers[k].full_name() def _disconnect_res_dict_hook(self, input, output):
self.res_dict = None
def replace_sub(self, layer_name_pattern, replace_function, recursive=True):
for layer_i in self._sub_layers:
layer_name = self._sub_layers[layer_i].full_name()
if re.match(layer_name_pattern, layer_name): if re.match(layer_name_pattern, layer_name):
self._sub_layers[k] = replace_function(self._sub_layers[k]) self._sub_layers[layer_i] = replace_function(self._sub_layers[layer_i])
if recursive: if recursive:
if isinstance(self._sub_layers[k], TheseusLayer): if isinstance(self._sub_layers[layer_i], TheseusLayer):
self._sub_layers[k].replace_sub( self._sub_layers[layer_i].replace_sub(
layer_name_pattern, replace_function, recursive) layer_name_pattern, replace_function, recursive)
elif isinstance(self._sub_layers[k], elif isinstance(self._sub_layers[layer_i], (nn.Sequential, nn.LayerList)):
nn.Sequential) or isinstance( for layer_j in self._sub_layers[layer_i]._sub_layers:
self._sub_layers[k], nn.LayerList): self._sub_layers[layer_i]._sub_layers[layer_j].replace_sub(
for kk in self._sub_layers[k]._sub_layers.keys():
self._sub_layers[k]._sub_layers[kk].replace_sub(
layer_name_pattern, replace_function, recursive) layer_name_pattern, replace_function, recursive)
else:
pass
''' '''
example of replace function: example of replace function:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册