ValueError: shapes (3,4) and (5,2) not aligned: 4 (dim 1) != 5 (dim 0)

i'm getting an error while running this code ValueError: shapes (3,4) and (5,2) not aligned: 4 (dim 1) != 5 (dim 0)

import numpy as np
np.random.seed(0)
X = [[1, 2, 3, 2.5],
     [2.0, 5.0, -1.0, 2.0],
     [-1.5, 2.7, 3.3, -0.8]]
class Layer_Dense:
    def __init__(self,n_inputs, n_neurons):
        self.weights = 0.10 * np.random.randn(n_inputs, n_neurons )
        self.biases = np.zeros((1, n_neurons))
    def forward(self, inputs):
        self.output = np.dot(inputs, self.weights) + self.biases
        
layer1 = Layer_Dense(4,5)
layer1 = Layer_Dense(5,2)

layer1.forward(X)
print(layer1.output)   

Solution 1:

Firstly, you have a mistake in the bottom, you wrote

layer1 = Layer_Dense(4,5)
layer1 = Layer_Dense(5,2)

but you should have written

layer1 = Layer_Dense(4,5)
layer2 = Layer_Dense(5,2)

Then, I think your shapes are not aligned because the first number in your layer1 = Layer_Dense(4,5) which is 4, is referred to your inputs meaning it can display as many inputs as the X has, which is 4 in one array.

import numpy as np

np.random.seed(0)

X = [[1, 2, 3, 2.5],
 [2.0, 5.0, -1.0, 2.0],
 [-1.5, 2.7, 3.3, -0.8]]


 class Layer_Dense:
   def __init__(self, n_inputs, n_neurons):
    self.weights = 0.10 * np.random.randn(n_inputs, n_neurons)
    self.biases = np.zeros((1, n_neurons))

   def forward(self, inputs):
    self.output = np.dot(inputs, self.weights) + self.biases


layer1 = Layer_Dense(4, 5)
layer2 = Layer_Dense(3, 4)

layer1.forward(X)
print(layer1.output)

if this doesn't help, then try changing places of a variable with inputs as a parameter, like self.output = np.dot(inputs, self.weights) and make it self.output = np.dot(self.weights, inputs) or change self.weights = 0.10 * np.random.randn(n_inputs, n_neurons) to self.weights = 0.10 * np.random.randn(n_neurons, n_inputs) or def __init__(self, n_inputs, n_neurons) to def __init__(self, n_neurons n_inputs). Also don't forget to change the parameters of the layer you are printing. That made layer1 working, but not layer2 for some reason...

2 DAYS LATER... I noticed that my code could print layer1, but when I wanted to print(layer2.output), it would say error(shape). My mistake was that i was printing the inputs in layer one, and then the same inputs in layer 2! So I had to forward inputs with layer1 and then forward this layer1.output into layer2's inputs, and print that as layer2.output like this :

layer1 = Layer_Dense(4, 6)
layer2 = Layer_Dense(6, 7)

layer1.forward(X)
print(layer1.output)

layer2.forward(layer1.output)
print(layer2.output)

Remember, the first parameter of layer1 is from X inputs, and teh second parameter is how many neurons this layer will have, which is how many you like, so i put 6 here. The first parameter of layer2 is from layer1.outputs which is in my case 6, and the second parameter is again how many neurons this layer will have, which is how many you like, so i put here 7. That way, I'm more sure your code will work and you can move to the activation function tutorial :D