You are on page 1of 7

Project 2

Advanced Deep Learning:


(ADL)

Submitted by:
Khawar Abbas Khan
MS CS Semester 1

Submitted to :
Dr M Shamim Baig

Muslim Youth University Islamabad


Task 1:
Algebraically Compute the following expression:-
Y= A.(B + kC) where A, B, C are vectors of fixed size=4.
(a): input vectors are filled by you, & output vectors are initialized to all zeroes.
(b): input vectors are filled with random numbers in range (o-r) you, & output
vectors are initialized to all zeroes.
Give Python Code to implement above expression.
Use for-loop with index pointer for computation &
Print vectors (horizontally with name) using index pointer.

Python code :

A=[]
B=[]
C=[]
Y=[]
k=10
for i in range(4):
A.append(int(input("Enter Value in Array 1")))
B.append(int(input("Enter Value in Array 2")))
C.append(int(input("Enter Value in Array 3")))
Y.append(0)
#Y= A.(B + kC) >> A.B+A.C.k
for i in range(4):
Y[i] = A[i]*B[i]+A[i]*C[i]*k
print(Y)
import random
random.seed(32)
A=[]
B=[]
C=[]
k=1
Y=[]
for i in range(4):
A.append(random.randrange(10))
B.append(random.randrange(12))
C.append(random.randrange(9))
Y.append(0)
for i in range(4):
Y[i] = A[i]*(B[i]+(k*C[i]))

print("Array A",A)
print("Array B",B)
print("Array C",C)
print("Result Y",Y)
Task 2:
(a) Give Python code to Compute the following expression:-
Y= A . (B + kC)
Initially A, B, C are empty vectors of variable size= n (user input)
& are dynamically filled with random numbers in range (0r).
Print all the vectors w/o using index pointer.
(b) What are the two options to initialize intermediate & Final output
vectors, discuss advantages/ disadvantages of each option.

Python Code :

A = []
B = []
C = []
k=1
Y = []
for i in range(int(input("Enter The Value of Array Range"))):
A.append(random.randrange(10))
B.append(random.randrange(12))
C.append(random.randrange(9))
Y.append(0)
for i in range(4):
Y[i] = A[i]*(B[i]+(k*C[i]))

print("Array A",A)
print("Array B",B)
print("Array C",C)
print("Result Y",Y)

What are the two options to initialize intermediate & Final output vectors, discuss
advantages/ disadvantages of each option.
Python code can be accelerated without the need for loops by employing
vectorization. Making effective use of such a function can assist reduce the amount of
time that code runs? Vectors can be subjected to a variety of operations, including
element-wise multiplication, which produces elements with the same indexes and
keeps the matrix's dimension unchanged, outer products, which yield a square matrix
with a dimension equal to the length X length of the vectors, and the dot product of
vectors, also known as the scalar product because it yields a single output.
By calculating their processing times, we will observe that the traditional approaches
take longer than utilizing some standard function.

Dot Product:

Dot product is an algebraic operation in which two equal length vectors are being
multiplied such that it produces a single number. Dot Product often called as inner
product. This product results in a scalar number. Let’s consider two matrix a and b of
same length, the dot product is done by taking the transpose of first matrix and then
mathematical matrix multiplication of a’(transpose of a) and b is followed as shown in
the figure below.

Pictorial representation of dot product:

Example :

# Dot product
import time
import numpy
import array

# 8 bytes size int


a = array.array('q')
for i in range(100000):
a.append(i);

b = array.array('q')
for i in range(100000, 200000):
b.append(i)

# classic dot product of vectors implementation


tic = time.process_time()
dot = 0.0;

for i in range(len(a)):
dot += a[i] * b[i]

toc = time.process_time()

print("dot_product = "+ str(dot));


print("Computation time = " + str(1000*(toc - tic )) + "ms")

n_tic = time.process_time()
n_dot_product = numpy.dot(a, b)
n_toc = time.process_time()

print("\nn_dot_product = "+str(n_dot_product))
print("Computation time = "+str(1000*(n_toc - n_tic ))+"ms")

Output:

dot_product = 833323333350000.0
Computation time = 35.59449199999999ms

n_dot_product = 833323333350000
Computation time = 0.1559900000000225ms

You might also like