Lukasz
Lukasz

Reputation: 2606

Matrix Multiplication TypeError

I'm attempting to write a backpropagation algorithm and I'm encountering an error when attempting to perform a matrix multiplication.

I've created the following simple example to work with

# necessary functions for this example
def sigmoid(z):
    return 1.0/(1.0+np.exp(-z))

def prime(z):
    return sigmoid(z) * (1-sigmoid(z))

def cost_derivative(output_activations, y):
    return (output_activations-y)

# Mock weight and bias matrices
weights = [np.array([[ 1, 0, 2], 
                     [2, -1, 0], 
                     [4, -1, 0], 
                     [1, 3, -2],
                     [0, 0, -1]]), 
           np.array([2, 0, -1, -1, 2])]

biases = [np.array([-1, 2, 0, 0, 4]), np.array([-2])]

# The mock training example
q = [(np.array([1, -2, 3]), np.array([0])), 
     (np.array([2, -3, 5]), np.array([1])),
     (np.array([3, 6, -1]), np.array([1])),
     (np.array([4, -1, -1]), np.array([0]))]

for x, y in q:
        activation = x
        activations = [x]
        zs = []
        for w, b in zip(weights, biases): 
            z = np.dot(w, activation) + b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)

delta = cost_derivative(activations[-1], y) * prime(zs[-1])
print(np.dot(np.transpose(weights[-1])), delta)

I get the following error:

TypeError: Required argument 'b' (pos 2) not found

I've printed the outputs of both the weights transposed which is a 5x2 matrix and delta is a 2x1. The outputs are:

np.transpose(weights[-1]) = [[ 2 -3]
                             [ 0  2]
                             [-1  0]
                             [-1  1]
                             [ 2 -1]]

and

delta = [-0.14342712 -0.03761959]

so the multiplication should work and produce a 5x1 matrix

Upvotes: 0

Views: 1635

Answers (1)

Alicia Garcia-Raboso
Alicia Garcia-Raboso

Reputation: 13913

There is a misplaced parenthesis on your last line. It should be

print(np.dot(np.transpose(weights[-1]), delta))

instead of

print(np.dot(np.transpose(weights[-1])), delta)

Upvotes: 2

Related Questions