Google ML Bootcamp(181)
-
19. A Note on Python/Numpy Vectors
import numpy as np a = np.random.randn(5) a.shape - (5,) : 1순위 배열이라고 부름. - 이는 행 벡터나 열 벡터로 일관되게 동작하지 않음. 열벡터도 행벡터도 아님. 중요한것은 1순위 배열을 사용하지 않는 것. a = np.random.randn(5,1) or a = np.random.randn(1,5)로 선언하여 사용. a.shape - (5,1) or (1,5) 행렬 차원을 구체화할 것.
2023.09.08 -
18. Broadcasting in Python
python matrix operation. 더하기,빼기,곱하기, 나누기(+ - * / ) : element-wise. **중요 곱하기조차 행렬곱이 아닌 element-wise로 진행됨, np.dot(a,b)의 경우 행렬곱으로 진행** - but (3,4) shape matrix + (1,4) shape matrix = (3,4) shape matrix - 이때는 (1,4)를 행방향으로 복사하여 (3,4) matrix로 만든 후 element-wise 연산을 수행한다고 생각. - (3,4) shape matrix + (3,1) shape matrix = (3,4) shape matrix - 이때는 (3,1)를 열방향으로 복사하여 (3,4) matrix로 만든 후 element-wise 연산을 수행한다고 ..
2023.09.08 -
17. Vectorizing Logistic Regression's Gradient Output
a 는 sigmoid(z)의 결과값으로 y* dz(i) = a(i) - y(i) , 즉 loss를 의미. 따라서 dZ = A - Y - dZ = [a(1)-y(1), a(2)-y(2), ... , a(m)-y(m)] 기존 for m for w(nx) 에서 for m dw+= x * dz 로 for문 제거. 에서 또 한번 for문을 제거 결국 dW = 1/m * X * dZ(T) dB = 1/m * np.sum(db) 로 초기화함으로써 for문 하나 더 제거. 즉 dW에는 m개의 훈련 예제가 x * dz를 통해 x(i)당 nx개의 기울기 누적합이 존재. - 그걸 m개 평균을 내서 한번에 update 진행. 기존에는 1부터 m까지 한번씩 dw+=를 통해 진행. 그걸 한번에 sum(dw)로 바꾼거.
2023.09.08 -
16. Vectorizing Logistic Regression 2023.09.08
-
15. More Vectorization Examples
dw를 벡터화할 경우, dw+=x*dz가 되는데, 이는 x가 J에 미치는 영향(기울기)의 평균을 구하는 과정이므로. dw = dw + x(i)dz(i)이면 결국 누적합 + i시점 기울기 이때 벡터끼리 덧셈은 element-wise형태로 이루어지므로 for 문을 없앨 수 있다.
2023.09.08 -
14. Vectorization
벡터화 : 코드에서 for문을 명시적으로 제거하는 기술. import time import numpy as np 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 :'+str(1000*(toc-tic)) +'ms') 확인해보면 대략 300배 차이나는 것을 확인할 수..
2023.09.08