Lab 1 - Introduction
Lab 1 - Introduction
Tools/Software Requirement
Google Colab
Description
Follow the lab manual step by step.
print("Hello, World!")
b) To execute the code you've written, you can do one of the following:
i. Click the "Play" button (a triangle icon) located to the left of the code cell.
ii. Use the keyboard shortcut Shift+Enter (press Shift and Enter simultaneously).
c) After running the code cell, you will see the output displayed below the cell. It should show
"Hello, World!" as the output.
NumPy is the main package for scientific computing in Python. It is maintained by a large community
(www.numpy.org). It provides a high-performance multidimensional array object, and tools for working
with these arrays. In this exercise you will learn several key NumPy functions that are useful in machine
learning and deep learning.
NumPy Arrays
A NumPy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers.
The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the
size of the array along each dimension.
We can initialize NumPy arrays from nested Python lists, and access elements using square brackets:
import NumPy as np
import NumPy as np
NumPy offers several ways to index into arrays. Like Python lists, NumPy arrays can be sliced. Since
arrays may be multidimensional, you must specify a slice for each dimension of the array:
import NumPy as np
You can also mix integer indexing with slice indexing. However, doing so will yield an array of lower rank
than the original array:
import NumPy as np
# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower
rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"
Integer array indexing: When you index into NumPy arrays using slicing, the resulting array view will
always be a subarray of the original array. In contrast, integer array indexing allows you to construct
arbitrary arrays using the data from another array. Here is an example:
import NumPy as np
# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]]) # Prints "[2 2]"
One useful trick with integer array indexing is selecting or mutating one element from each row of a
matrix:
import NumPy as np
Boolean array indexing: Boolean array indexing lets you pick out arbitrary elements of an array.
Frequently this type of indexing is used to select the elements of an array that satisfy some condition.
Here is an example:
import NumPy as np
Every NumPy array is a grid of elements of the same type. NumPy provides a large set of numeric
datatypes that you can use to construct arrays. NumPy tries to guess a datatype when you create an
array, but functions that construct arrays usually also include an optional argument to explicitly specify
the datatype. Here is an example:
import NumPy as np
Basic mathematical functions operate elementwise on arrays, and are available both as operator
overloads and as functions in the NumPy module:
import NumPy as np
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
Note that * is elementwise multiplication, not matrix multiplication. We instead use the dot function
to compute inner products of vectors, to multiply a vector by a matrix, and to multiply matrices. dot is
available both as a function in the NumPy module and as an instance method of array objects:
import NumPy as np
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))
NumPy provides many useful functions for performing computations on arrays; one of the most useful
is sum :
import NumPy as np
x = np.array([[1,2],[3,4]])
Apart from computing mathematical functions using arrays, we frequently need to reshape or otherwise
manipulate data in arrays. The simplest example of this type of operation is transposing a matrix; to
transpose a matrix, simply use the T attribute of an array object:
import NumPy as np
x = np.array([[1,2], [3,4]])
print(x) # Prints "[[1 2]
# [3 4]]"
print(x.T) # Prints "[[1 3]
# [2 4]]"
Broadcasting is a powerful mechanism that allows NumPy to work with arrays of different shapes when
performing arithmetic operations. Frequently we have a smaller array and a larger array, and we want to
use the smaller array multiple times to perform some operation on the larger array. For example,
suppose that we want to add a constant vector to each row of a matrix. We could do it like this:
import NumPy as np
This works; however when the matrix x is very large, computing an explicit loop in Python could be
slow. Note that adding the vector v to each row of the matrix x is equivalent to forming a matrix vv by
stacking multiple copies of v vertically, then performing elementwise summation of x and vv . We
could implement this approach like this:
import NumPy as np
NumPy broadcasting allows us to perform this computation without actually creating multiple copies
of v . Consider this version, using broadcasting:
import NumPy as np
The line y = x + v works even though x has shape (4, 3) and v has shape (3,) due to broadcasting; this
line works as if v actually had shape (4, 3) , where each row was a copy of v , and the sum was
performed elementwise.
1. If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s
until both shapes have the same length.
2. The two arrays are said to be compatible in a dimension if they have the same size in the
dimension, or if one of the arrays has size 1 in that dimension.
3. The arrays can be broadcast together if they are compatible in all dimensions.
4. After broadcasting, each array behaves as if it had shape equal to the elementwise maximum
of shapes of the two input arrays.
5. In any dimension where one array had size 1 and the other array had size greater than 1, the
first array behaves as if it were copied along that dimension
import NumPy as np
Assessment Exercises:
x1 x2 xn
e e e
softmax ( x )=softmax ( [ x 1 x 2 … x n ] ) =[ … ]
∑ ex ∑ ex j j
∑ ex j
j j j
i=0
Create a function that takes two vectors in the form of standard python lists and returns the L2
loss according to the above formula.
d) Now create another function that returns L2 loss but uses NumPy arrays instead of standard
python list. Compare the two approaches.