'Make an array of matrices in Python (numpy)

I'm trying to create an undefined length array of matrices for a neural network, but, when i append the second matrix to the array, the format is messed up.

    def createRandomWeights(X):
    initialW = generateWeights(X.shape[1], S[0]) # first weight matrix
    w = np.array([initialW])              # array of weight matrices
    for i in range(0, L - 1):
        layerW = np.random.uniform(-1, 1, (S[i], S[i + 1]))
        w = np.append(w, [layerW])
    return w

The function generateWeights only creates an NxM size np.matrix of random numbers between -1 and 1. S is an array of numbers L is the lenght of S

Example:

S = [2,3]
L = len(s)
X = [[1,1,1],[1,-1,1],[-1,1,1],[-1,-1,1]]

Expected output example (random numbers wrote as 'rn'):

matrix1 = [[rn, rn],[rn, rn],[rn, rn]] # 3x2 matrix
matrix2 = [[rn, rn, rn],[rn, rn, rn]] # 2x3 matrix
output = [matrix1, matrix2] # 2 matrix elements array

Real output:

output = [rn, rn, rn, rn, rn...] #12 times


Solution 1:[1]

The problem is that you are using np.append instead of using the append method for list in Python.

def createRandomWeights(X):
    initialW = generateWeights(X.shape[1], S[0]) # first weight matrix
    w = np.array([initialW])              # array of weight matrices
    for i in range(0, L - 1):
        layerW = np.random.uniform(-1, 1, (S[i], S[i + 1]))
        w.append(layerW)
    return w

The code above should do the job. If you check the docs on np.append you will see that it will turn the arguments into a 1 dimensional array if no other params are specified.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mtzd