Is there a way to vectorize a class/function like numpy.vectorize?

I am trying to so something similar to code below:

class MyClass(nn.Module):

def __init__(self):
	for i in range(10):
		self.MySubClassArray.append(MySubClass())

def forward(self, X_Array):
            #X_Array is a list of 10 elements,
	Y_Array = map(lambda MySubClass, x: MySubClass(x), self.MySubClassArray, X_Array)
	return Y_Array

In forward() call, all 10 calls to MySubClass() are executed sequentially (as far as I know, correct me here if I am wrong). However can we make it a parallel execution? It should be possible as each instance of MySubClass() is independent and different than the other.

2 Likes

Hi,

if you indeed have an X_array, for many use-cases (where the items have same size), you can just glue your enumeration dimension with the batch dimension using .view:

y = MySubClass(X_Array.view((X_Array.size(0)*X_Array.size(1),X_Array.size(2)))
return y.view(X_Array.size(0), X_Array.size(1),y.size(2))

or so.

Of course, if the items are all of different shapes, that won’t work.

Best regards

Thomas

1 Like

There are different instances of MySubClass () to be applied on X_Array(0), X_Array(1) etc. So It cant be done that way.

def init(self):
    for i in range(10):
    self.MySubClassArray.append(MySubClass())

I have collection of different MySubClass() instances which are then applied on elements of X_Array() using map function whicb I intend to parallelize.