Removing comps list from numpy code

I am in the middle of building a geometric neural network, and I am facing a problem with vectorization. Basically, there is a lambda function that I defined that really should work on every sample provided as input. The problem is that it would be most convenient to pass the inputs in the form of an array with the last axis of this array serving as the "sample axis" (the axis where each index is a complete sample).

I have a solution that works, which basically only does this in listcomp, and then converts it back to a numpy array for the rest of the calculations. (Let me know if you want to see any of the functions defined, but I don't think they are very relevant)

class GeometricNeuralNet(object):

    def __init__(self, c, weight_domain=math.log(2)):
        """
        Dimensions of c should be a tuple that indicates the size of each layer.

        First number should be the number of input units, and the last should be the number of output units.
        Other entries should be the sizes of hidden layers.
        """
        weight_matrix = lambda a, b: np.exp(np.random.uniform(-weight_domain, weight_domain, [a,b]))
        self.weights = [weight_matrix(c[i], c[i+1]) for i in range(len(c) - 1)]
        self.predict = lambda input_vector, end=None: reduce(transfer_function, [input_vector] + self.weights[:end])

    def train(self, samples, outputs, learning_rate):
        # Forward Pass
        true_inputs = np.array([self.predict(sample, -1) for sample in samples])
        print true_inputs.shape

- true_inputs. ? np.vectorize np.frompyfunc, , , .

EDIT:

transfer_function.

def transfer_function(x, y):
    return gmean(np.power(x, y.T), axis=1)
+3
1

numpy apply_along_axis: http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html

>>> def my_func(a):
...     """Average first and last element of a 1-D array"""
...     return (a[0] + a[-1]) * 0.5
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.apply_along_axis(my_func, 0, b)
array([ 4.,  5.,  6.])
>>> np.apply_along_axis(my_func, 1, b)
array([ 2.,  5.,  8.])
+1

All Articles