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

Question:

I’m trying to make an ANN from scratch and I’ve run into a big problem…

from math import *
from random import random
import numpy as np


training = np.array([[0,0], [0,1], [1,0], [1,1]])
trainingLabels = np.array([0, 1, 1, 0])

testing = np.array([[0,1], [0,0], [1,0], [1,1], [0,1]])
testingLabels = np.array([1, 0, 1, 0, 1])


hiddenLayers = [2, 2]
outputs = 1


weights = []

weights.append(np.random.rand(len(training), hiddenLayers[0]) - 0.5)

if len(hiddenLayers) > 1:
    for i in range(len(hiddenLayers) - 1):
        weights.append(np.random.rand(hiddenLayers[i+1], hiddenLayers[i]) - 0.5)

weights.append(np.random.rand(outputs, hiddenLayers[-1]) - 0.5)


biases = []

biases.append(np.random.rand(1, hiddenLayers[0]) - 0.5)
if len(hiddenLayers) > 1:
    for i in range(len(hiddenLayers) - 1):
        biases.append(np.random.rand(1, hiddenLayers[i + 1]) - 0.5)
biases.append(np.random.rand(1, outputs) - 0.5)


forward = []

forward.append(np.dot(weights[0], training) + biases[0])

This is the output I got from running that:

Traceback (most recent call last):
  File "c:UserskailaDesktopANNANN.py", line 80, in <module>
    forward.append(np.dot(weights[0], training) + biases[0])
  File "<__array_function__ internals>", line 5, in dot
ValueError: shapes (4,2) and (4,2) not aligned: 2 (dim 1) != 4 (dim 0)

I tried printing different things to see if anything was wrong, but everything was as I intended.

print(np.shape(training))
print(np.shape(weights[0]))
print(training, "nn")
print(weights[0])

This returned:

(4, 2)
(4, 2)
[[0 0]
 [0 1]
 [1 0]
 [1 1]]


[[ 0.31642805  0.23512315]
 [ 0.07491602 -0.27518716]
 [ 0.47279068  0.12803371]
 [ 0.48695467 -0.07876347]]

Any ideas? Even if it isn’t the full fix, anything is appreciated.

Asked By: TiltedGamer

||

Answers:

I think that I understood your problem.

Remember that when taking the dot product of two vectors with equal shape, the one on the left should be transposed, so:

np.dot(weights[0].T, training)

Should work, with .T transposing the tensor.

Answered By: Caridorc