def dot_prod(x,w):
if not ( x.shape[1]==w.shape[0]):
raise Exception( 'The number of columns of the first matrix does not match the number of rows of the second matrix ')
dot_product = np.zeros((x.shape[0], w.shape[1]), dtype=np.dtype('O'))
for i1,a in enumerate(x):
for i2,y in enumerate(w.T):
dot_product[i1,i2]= np.sum(a*y)
return dot_product
Выход:
>x = np.random.rand(5,3)
>w = np.random.rand(3,2)
>dot_prod(x,w)
array([[1.0216453677132162, 1.0520242959212602],
[0.7139675035454871, 0.7616075739263084],
[0.9126062852861008, 0.9864445729083398],
[0.42673040494581216, 0.4203998986679549],
[0.9638211885773351, 1.0142282080627387]], dtype=object)
>x.dot(w)
array([[1.02164537, 1.0520243 ],
[0.7139675 , 0.76160757],
[0.91260629, 0.98644457],
[0.4267304 , 0.4203999 ],
[0.96382119, 1.01422821]])
>x = np.random.rand(5,3)
>w = np.random.rand(2,2)
>dot_prod(x,w)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/alperen/Projects/tmp.py", line 8, in dot_prod
raise Exception( 'The number of columns of the first matrix does not match the number of rows of the second matrix ')
Exception: The number of columns of the first matrix does not match the number of rows of the second matrix