[Numpy-discussion] question about optimizing
Anne Archibald
peridot.faceted at gmail.com
Sat May 17 15:18:15 EDT 2008
2008/5/17 Brian Blais <bblais at bryant.edu>:
> at least for me, that was the motivation. I am trying to build a simulation
> framework for part of the brain, which requires connected layers of nodes.
> A layer is either a 1D or 2D structure of nodes, with each node a
> relatively complex beast. Rather than reinvent the indexing (1D, 2D,
> slicing, etc...), I just inherited from ndarray. I thought, after the fact,
> that some numpy functions on arrays would help speed up the code, which
> consists mostly of calling an update function on all nodes, passing each
> them an input vector. I wasn't sure if there would be any speed up for
> this, compared to
> for n in self.flat:
> n.update(input_vector)
> From the response, the answer seems to be no, and that I should stick with
> the python loops for clarity. But also, the words of Anne Archibald, makes
> me think that I have made a bad choice by inheriting from ndarray, although
> I am not sure what a convenient alternative would be.
Well, it doesn't exist yet, but a handy tool would be a factory
function "ArrayOf"; you would pass it a class, and it would produce a
subclass of ndarray designed to contain that class. That is, the
underlying storage would be a record array, but the getitem and
setitem would automatically handle conversion to and from the class
you supplied it, where appropriate.
myarray = ArrayOf(Node,dtype=...)
A = myarray.array([Node(...), Node(...), Node(...)])
n = A[1]
A[2] = Node(...)
A.C.update() # python-loop-based update of all elements
You could also design it so that it was easy to derive a class from
it, since that's probably the best way to handle vectorized methods:
class myarray(ArrayOf(Node, dtype=...)):
def update(self):
self.underlying["node_attribute"] += 1
I should say, if you can get away with treating your nodes more like C
structures and writing (possibly vectorized) functions to act on them,
you can avoid all this mumbo jumbo:
node_dtype = [("node_attribute",np.int),("weight", np.float)]
A = np.zeros(10,dtype=node_dtype)
def nodes_update(A):
A["node_attribute"] += 1
Anne
More information about the NumPy-Discussion
mailing list