0%

Vectorization || Neural Networks and Deep Learning

Vectorization is an important stage in Creating a robust machien Learning model. Without Vectorizaation the model can be incomplete. Real More to know the practical aspects of vectorization.

Note - These are my notes on DeepLearning Specialization Part:
Python Basics With Numpy

Table of Contents

Vectorization

1
import numpy as np
1
2
3
a = np.random.rand(1000000)
b = np.random.rand(1000000)

1
%time c = np.dot(a, b)
CPU times: user 1.62 ms, sys: 727 µs, total: 2.34 ms
Wall time: 1.06 ms
1
2
3
4
def loop():
c = 0
for i in range(1000000):
c += a[i] * b[i]
1
%time loop()
CPU times: user 316 ms, sys: 2.56 ms, total: 319 ms
Wall time: 317 ms

Vectorizing Logistic Regression

Vectorizing Logistic Regression’s Gradient Output

A note on python/numpy vectors

1
2
a = np.random.randn(5)
a
array([-0.91796822, -0.53903443,  1.00289266,  0.22272871, -0.35617949])
1
a.shape
(5,)
1
a.T
array([-0.91796822, -0.53903443,  1.00289266,  0.22272871, -0.35617949])
1
np.dot(a, a.T)
2.3154893533786054
1
2
a = np.random.randn(5, 1)
a
array([[-1.26834861],
       [-0.254855  ],
       [-1.37786229],
       [ 0.18718574],
       [-1.31341244]])
1
a.shape
(5, 1)
1
a.T
array([[-1.26834861, -0.254855  , -1.37786229,  0.18718574, -1.31341244]])
1
np.dot(a, a.T)
array([[ 1.60870819,  0.32324498,  1.74760972, -0.23741677,  1.66586484],
       [ 0.32324498,  0.06495107,  0.35115509, -0.04770522,  0.33472972],
       [ 1.74760972,  0.35115509,  1.8985045 , -0.25791618,  1.80970147],
       [-0.23741677, -0.04770522, -0.25791618,  0.0350385 , -0.24585208],
       [ 1.66586484,  0.33472972,  1.80970147, -0.24585208,  1.72505223]])

1
2
%load_ext version_information
%version_information numpy
SoftwareVersion
Python3.6.6 64bit [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
IPython7.0.1
OSDarwin 17.7.0 x86_64 i386 64bit
numpy1.15.1
Sun Oct 14 19:41:16 2018 MDT

Credits to the teacher