In [2]:
import numpy as np
a = np.array([1,2,3,4])
print(a)
In [10]:
import time
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
c = np.dot(a,b)
toc = time.time()
print(c)
print("vectorized version:" + str(1000*(toc-tic)) + "ms")
c = 0
tic = time.time()
for i in range(1000000):
c += a[i]*b[i]
toc = time.time()
print(c)
print("for loop version:" + str(1000*(toc-tic)) + "ms")
In [12]:
import numpy as np
A = np.array([[56.0, 0.0, 4.4, 68.0],
[1.2,104.0,52.0,8.0],
[1.8,135.0,99.0,0.9]])
print(A)
In [13]:
cal = A.sum(axis=0)
print(cal)
In [14]:
percentage = 100*A/cal.reshape(1,4)
print(percentage)
In [1]:
import numpy as np
a = np.random.randn(5)
In [2]:
print(a)
In [3]:
print(a.shape)
In [4]:
print(a.T)
In [5]:
print(np.dot(a,a.T))
In [6]:
a = np.random.randn(5,1)
print(a)
In [7]:
print(a.T)
In [8]:
print(np.dot(a,a.T))
In [10]:
A = np.random.randn(4,3)
B = np.sum(A, axis = 1, keepdims = True)
print(B.shape)
In [ ]:
forward
input: $a^{[l-1]}$
output: $a^{[l]}, cache: z^{[l]}, w^{[l]}, b^{[l]}$
vectorized:
$$Z^{[l]} = W^{[l]} · A^{[l-1]} + b^{[l}]$$$$A^{[l]} = g^{[l]}(Z^{[l]})$$backward
input: $da^{[l]}$
output: $da^{[l-1]}, dW^{[l]}, db^{[l]}$
vectorized:
$$ dZ^{[l]} = dA^{[l]} * g^{[l] \prime}(Z^{[l]}) $$$$dW^{[l]} = \frac{1}{m} dZ^{[l]} · A^{[l-1]T}$$$$db^{[l]} = \frac{1}{m} np.sum(dZ^{[l]}, axis = 1, keepdims = True)$$$$dA^{[l-1]} = W^{[l]T} · dZ^{[l]}$$
In [ ]: