0

I'm working on my backpropagation for a basic neural network, and for each example I must calculate the new weights. I save my weights in a 2D numpy array called weights looking like:

 [[0.09719335 0.03077288 0.84256845 0.78993436]
 [0.87452232 0.9833483  0.803617   0.46675746]
 [0.77805488 0.11567956 0.63747511 0.14045771]]

For the new weights, I need the average of each weight between a pair of neurons. My idea was to calculate it for all the data items in my training set and then calculate the mean. For this i wanted to make an array of zeros with np.zeros, with the size of the upper array times the amount of data items I have in my set. I tried it like this

newWeights = np.zeros((2,(weights.shape)))

But this didn't work. is there even a way to initialize an array like this or is there another way I could do this easier (I thought about np.append but couldn't figure this out)

3 Answers 3

1

weights.shape is a tuple, so you can't include it as it is as the dimensions must be integers. You can use * to unpack the tuple:

newWeights = np.zeros((2, *weights.shape))

This essentially unpacks weights.shape so it's the equivalent of (2, x, y) for the dimensions.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this

import numpy as np
arr = np.array( [[0.09719335, 0.03077288, 0.84256845, 0.78993436],
 [0.87452232, 0.9833483,  0.803617,   0.46675746],
 [0.77805488, 0.11567956, 0.63747511, 0.14045771]])

arr3D = np.zeros((2,*arr.shape))

Then you save a single 2D array in the 3D array like this:

arr3D[0,:,:] = arr

The calculation of the mean array works like this:

mean_arr = arr3D.mean(axis=0)

Comments

1

Assuming you're okay with modifying your weights-array in-place, np.ndarray.resize will resize your array to (2, 3, 4) and fill the new values with 0:

import numpy as np

weights = np.asarray([[0.09719335, 0.03077288, 0.84256845, 0.78993436], [0.87452232, 0.9833483, 0.803617, 0.46675746],
                [0.77805488, 0.11567956, 0.63747511, 0.14045771]])
print(weights.shape) # (3, 4)

weights.resize((2, *weights.shape), refcheck=False)
print(weights.shape) # (2, 3, 4)
[[[0.09719335 0.03077288 0.84256845 0.78993436]
  [0.87452232 0.9833483  0.803617   0.46675746]
  [0.77805488 0.11567956 0.63747511 0.14045771]]

 [[0.         0.         0.         0.        ]
  [0.         0.         0.         0.        ]
  [0.         0.         0.         0.        ]]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.