@@ -0,0 +1,1338 @@
+import numpy as np%0A%0A%0Aclass ParameterEditor():%0A  %22%22%22Conveniently edit the parameters of a lucid model.%0A%0A  Example usage:%0A%0A    model = models.InceptionV1()%0A    param = ParameterEditor(model.graph_def)%0A    # Flip weights of first channel of conv2d0%0A    param%5B%22conv2d0_w%22, :, :, :, 0%5D *= -1%0A%0A  %22%22%22%0A%0A  def __init__(self, graph_def):%0A    self.nodes = %7B%7D%0A    for node in graph_def.node:%0A      if %22value%22 in node.attr:%0A        self.nodes%5Bstr(node.name)%5D = node%0A%0A  def __getitem__(self, key):%0A    name = key%5B0%5D if isinstance(key, tuple) else key%0A    tensor = self.nodes%5Bname%5D.attr%5B%22value%22%5D.tensor%0A    shape = %5Bint(d.size) for d in tensor.tensor_shape.dim%5D%0A    array = np.frombuffer(tensor.tensor_content, dtype=%22float32%22).reshape(shape).copy()%0A    return array%5Bkey%5B1:%5D%5D if isinstance(key, tuple) else array%0A%0A  def __setitem__(self, key, new_value):%0A    name = key%5B0%5D if isinstance(key, tuple) else key%0A    tensor = self.nodes%5Bname%5D.attr%5B%22value%22%5D.tensor%0A    node_shape = %5Bint(d.size) for d in tensor.tensor_shape.dim%5D%0A    if isinstance(key, tuple):%0A      array = np.frombuffer(tensor.tensor_content, dtype=%22float32%22)%0A      array = array.reshape(node_shape).copy()%0A      array%5Bkey%5B1:%5D%5D = new_value%0A      tensor.tensor_content = array.tostring()%0A    else:%0A      assert new_value.shape == node_shape%0A      tensor.tensor_content = new_value.tostring()%0A

Add ParameterEditor to scratch