def clone_get_equiv(self, check_integrity=True, attach_feature=True): """Clone the graph and get a memo( a dict )that map old node to new node ---------------------------- Parameters: check_integrity - { bool } Whether to check integrity. Default is True. attach_feature - { bool } Whether to attach feature of origin graph to cloned graph. Default is True. ---------------------------- Returns: e - { FunctionGraph } Cloned fgraph. Every node in cloned graph is cloned. equiv - { dict } A dict that map old node to new node. """ equiv = graph.clone_get_equiv(self.inputs, self.outputs) if check_integrity: self.check_integrity() e = FunctionGraph([equiv[i] for i in self.inputs], [equiv[o] for o in self.outputs], clone=False) if check_integrity: e.check_integrity() if attach_feature: for feature in self._features: e.attach_feature(feature) return e, equiv
def clone_get_equiv(self, check_integrity=True, attach_feature=True): """Clone the graph and get a dict that maps old nodes to new ones Parameters: check_integrity: bool Whether to check integrity. Default is True. attach_feature: bool Whether to attach feature of origin graph to cloned graph. Default is True. Returns: e: FunctionGraph Cloned fgraph. Every node in cloned graph is cloned. equiv: dict A dict that map old node to new node. """ equiv = clone_get_equiv(self.inputs, self.outputs) if check_integrity: self.check_integrity() e = FunctionGraph( [equiv[i] for i in self.inputs], [equiv[o] for o in self.outputs], clone=False, ) if check_integrity: e.check_integrity() if attach_feature: for feature in self._features: e.attach_feature(feature) return e, equiv
def clone_get_equiv(self): """WRITEME""" equiv = graph.clone_get_equiv(self.inputs, self.outputs) self.check_integrity() e = FunctionGraph([equiv[i] for i in self.inputs], [equiv[o] for o in self.outputs]) e.check_integrity() for feature in self._features: e.attach_feature(feature) return e, equiv
def clone_get_equiv(self): """WRITEME""" equiv = graph.clone_get_equiv(self.inputs, self.outputs) self.check_integrity() e = self.__class__([equiv[i] for i in self.inputs], [equiv[o] for o in self.outputs]) e.check_integrity() for feature in self._features: e.extend(feature) return e, equiv
def std_interactive_env(inputs, outputs, clone_inputs_and_orphans=True): features = [] features.append(toolbox.ReplaceValidate()) features.append(Newest()) features.append(destroyhandler.DestroyHandler()) features.append(toolbox.PreserveNames()) equiv = graph.clone_get_equiv(inputs, outputs, copy_inputs_and_orphans=clone_inputs_and_orphans) rval = InteractiveEnv(equiv, features) rval.add_inputs([equiv[v] for v in inputs]) if not clone_inputs_and_orphans: for v in inputs: assert equiv[v] is v assert rval.inputs == inputs rval.add_outputs([equiv[v] for v in outputs]) for k, v in equiv.iteritems(): assert v.env is rval return rval
def optimize_graph(x, optimization, return_graph=None, in_place=False): """Easily optimize Theano graphs. Apply an optimization to either the graph formed by a Theano variable or an existing graph and return the resulting optimized graph. When given an existing `FunctionGraph`, the optimization is performed without side-effects (i.e. won't change the given graph). """ if not isinstance(x, tt_FunctionGraph): inputs = tt_inputs([x]) outputs = [x] model_memo = clone_get_equiv(inputs, outputs, copy_orphans=False) cloned_inputs = [ model_memo[i] for i in inputs if not isinstance(i, tt.Constant) ] cloned_outputs = [model_memo[i] for i in outputs] x_graph = FunctionGraph(cloned_inputs, cloned_outputs, clone=False) x_graph.memo = model_memo if return_graph is None: return_graph = False else: x_graph = x if return_graph is None: return_graph = True x_graph_opt = x_graph if in_place else x_graph.clone() _ = optimization.optimize(x_graph_opt) if return_graph: res = x_graph_opt else: res = x_graph_opt.outputs x_graph_opt.disown() if len(res) == 1: (res, ) = res return res