我正在尝试使用model.clone()替换图中的某些节点。我通过调用以下函数得到了我想要替换的节点: times_nodes = find_all_with_name(model,name,-1) 有问题的节点在图中不在深度0处。我注意到当我按如下方式调用clone时: model = model.clone(' clone',substitutions = subst) 实际上没有任何节点被替换。有没有办法做到这一点,还是这种行为?
答案 0 :(得分:0)
目前,作为块功能的节点不能将其内容替换为克隆。我们可能会在克隆中添加这种操作模式。现在,您可以尝试展平图表。以下代码可能足以压缩大多数网络。我没有彻底测试过它:
def break_a_block(root):
blocks = C.logging.graph.depth_first_search(root, lambda node: isinstance(node, C.cntk_py.Function) and node.is_block, depth=-1)
if len(blocks) == 0:
return False, root
block = blocks[0]
composite = C.as_composite(block.block_root)
output_dict = dict(zip(block.outputs, composite.outputs))
mapping = dict(block.block_arguments_mapping)
items = list(mapping.items())
owners = set(C.as_composite(arg.owner) for _, arg in items if arg.is_output)
for owner in owners:
clone = owner.clone('share', output_dict)
out = dict(zip(owner.outputs,clone.outputs))
for p,a in items:
if mapping[p] in out:
mapping[p] = out[mapping[p]]
composite.replace_placeholders(mapping)
return True, root.clone('share',output_dict)
def flatten(root):
changed, root = break_a_block(root)
while changed:
changed, root = break_a_block(root)
return root