class_docstr: Retain temporary inputs
  
  Function nodes manage inputs variable nodes using weak reference. When
  variable is made as temporary value, exporter cannot get the corresponded
  variable from the variable node because the reference is collected. To
  resolve it, retain all inputs and will use when make computational graph.
  
  To reduce memory size, this hook retains only variables not showed in link
  inputs. To enable this feature, links are required to use ``forward``, not
  ``__call__``.
class_docstr_tok: ["Retain","temporary","inputs","Function","nodes","manage","inputs","variable","nodes","using","weak","reference",".","When","variable","is","made","as","temporary","value",",","exporter","can","not","get","the","corresponded","variable","from","the","variable","node","because","the","reference","is","collected",".","To","resolve","it",",","retain","all","inputs","and","will","use","when","make","computational","graph",".","To","reduce","memory","size",",","this","hook","retains","only","variables","not","showed","in","link","inputs",".","To","enable","this","feature",",","links","are","required","to","use","``","forward","``",",","not","``","__call__","``","."]
code: def __init__(self):
      self.link_inputs = set()
      self.retain_inputs = []
      self.replaced_inputs = []
  
      self.org_apply = chainer.function_node.FunctionNode.apply
  
      def hooked_apply(_self, inputs):
          ret = self.org_apply(_self, inputs)
          func_inodes = list(_self.inputs)
          for i, inode in enumerate(func_inodes):
              referenced_var = inode.get_variable_or_none()
              if referenced_var is None:
                  # This variable is created within function node and weakref
                  # is lost. Make temporary variable and retain it.
                  temp_var = chainer.as_variable(inputs[i])
                  func_inodes[i] = temp_var.node
                  self.retain_inputs.append(temp_var)
              else:
                  if id(referenced_var) not in self.link_inputs:
                      # This variable is created within link forward, outside
                      # of function node. To avoid to lose reference out
                      # of the forward, retain the variable.
                      self.retain_inputs.append(referenced_var)
          self.replaced_inputs.append((_self, _self.inputs))
          _self.inputs = tuple(func_inodes)
          return ret
  
      self.hooked_apply = hooked_apply
  
file_path: chainer/onnx_chainer/export.py
imports: from __future__ import print_function
  from collections import OrderedDict
  import warnings
  import chainer
label_desc: Use image node_id 1 to create a new RetainInputHook object from inherited base classes: chainer with example: obj = RetainInputHook()