I'm trying to create np.array of np.arrays of different shape. I need it because in the next part I will sum up this large np.array with some delta_array that has the same shape.
Like matrices sum
self.weights += delta_weights
And I will be working with each array inside it separately as well. Or for example I will need to multiply element wise all arrays by some number.
Can't figure out what's wrong :( Please, help me. Code creates list of random np.arrays of different shape. So here is my code:
weights = []
for i in range(1, len(self.layers)):
weights.append(np.random.rand(self.layers[i-1] + 1, self.layers[i]))
print(type(weights))
print([(type(w), w.shape) for w in weights])
#error here with layers = [2,2,1] or [3,3,1] etc
self.weights = np.array(weights)
Output: For self.layers=[2, 2, 1]
<class 'list'>
[(<class 'numpy.ndarray'>, (3, 2)), (<class 'numpy.ndarray'>, (3, 1))]
Traceback (most recent call last):
line 20, in <module>
run()
line 8, in run
net.init_weights()
line 71, in init_weights
self.weights = np.array(weights)
ValueError: could not broadcast input array from shape (3,2) into shape (3)
For [2, 3, 1] everything is okay:
<class 'list'>
[(<class 'numpy.ndarray'>, (3, 3)), (<class 'numpy.ndarray'>, (4, 1))]
For [3, 3, 1] same story as for [2, 2, 1] - error
For [3, 7, 1] or [3, 2, 1] everything is okay.
//It's matrices of weights for gradient descent for machine learning.