I am trying to know the chance of fire based on sensors x1 and x2.
y=1
For this, I am trying to generate random vectors and matrices for weights and bias but I get an error.
import numpy as np
np.random.seed(seed=123)
w1 = np.random.rand(4,2)
b1 = 4*1
x = np.array([0.4, 0.32])
z1 = np.dot(w1,x) + b1
a1 = 1 / (1+np.exp(-z1))
np.random.seed(seed=123)
w2 = np.random.rand(1,4)
b2 = 1*1
z2 = np.dot(w2,x) + b2
a2 = 1 /(1+np.exp(-z2))
But I get the error below:
----> 1 z2 = np.dot(w2,x) + b2
2 a2 = np.tanh(Z1)
3 print(a2)
ValueError: shapes (2,4) and (2,) not aligned: 4 (dim 1) != 2 (dim 0)
I am not able to figure out how to solve this.
The answer is in the error - you are trying to multiply the matrices w2
and x
, which have invalid dimensions to be multiplied.
Matrix w2
has 1 row and 4 columns:
>>> w2 = np.random.rand(1,4)
>>> w2.shape
(1, 4)
Matrix x
has 2 entries:
>>> x = np.array([0.4, 0.32])
>>> x.shape
(2,)
Therefore, you cannot multiply these matrices together - matrices can only be multiplied if and only if the number of columns in the first matrix, w2
, is equal to the number of rows in the second, x
. Here, as the error says, 4 (dim 1) != 2 (dim 0)
.
You can solve this by either giving x
four rows, or w2
two columns.
Hope this helps.