Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Deep Learning from the Basics
  • Table Of Contents Toc
Deep Learning from the Basics

Deep Learning from the Basics

By : Koki Saitoh, Shigeo Yushita
4.5 (15)
close
close
Deep Learning from the Basics

Deep Learning from the Basics

4.5 (15)
By: Koki Saitoh, Shigeo Yushita

Overview of this book

Deep learning is rapidly becoming the most preferred way of solving data problems. This is thanks, in part, to its huge variety of mathematical algorithms and their ability to find patterns that are otherwise invisible to us. Deep Learning from the Basics begins with a fast-paced introduction to deep learning with Python, its definition, characteristics, and applications. You’ll learn how to use the Python interpreter and the script files in your applications, and utilize NumPy and Matplotlib in your deep learning models. As you progress through the book, you’ll discover backpropagation—an efficient way to calculate the gradients of weight parameters—and study multilayer perceptrons and their limitations, before, finally, implementing a three-layer neural network and calculating multidimensional arrays. By the end of the book, you’ll have the knowledge to apply the relevant technologies in deep learning.
Table of Contents (11 chapters)
close
close

NumPy

When implementing deep learning, arrays and matrices are often calculated. The array class of NumPy (numpy.array) provides many convenient methods that are used to implement deep learning. This section provides a brief description of NumPy, which we will use later.

Importing NumPy

NumPy is an external library. The word external here means that NumPy is not included in standard Python. So, you must load (import) the NumPy library first:

>>> import numpy as np

In Python, an import statement is used to import a library. Here, import numpy as np means that numpy is loaded as np. Thus, you can now reference a method for NumPy as np.

Creating a NumPy Array

You can use the np.array() method to create a NumPy array. np.array() takes a Python list as an argument to create an array for NumPy—that is, numpy.ndarray:

>>> x = np.array([1.0, 2.0, 3.0])
>>> print(x)
[ 1. 2. 3.]
>>> type(x)
<class 'numpy.ndarray'>

Mathematical Operations in NumPy

The following are a few sample mathematical operations involving NumPy arrays:

>>> x = np.array([1.0, 2.0, 3.0])
>>> y = np.array([2.0, 4.0, 6.0])
>>> x + y # Add arrays
array([ 3., 6., 9.])
>>> x - y
array([ -1., -2., -3.])
>>> x * y # element-wise product
array([ 2.,	8., 18.])
>>> x / y
array([ 0.5, 0.5, 0.5])

Take note that the numbers of elements of arrays x and y are the same (both are one-dimensional arrays with three elements). When the numbers of elements of x and y are the same, mathematical operations are conducted for each element. If the numbers of elements are different, an error occurs. So, it is important that they are the same. "For each element" is also called element-wise, and the "product of each element" is called an element-wise product.

In addition to element-wise calculations, mathematical operations of a NumPy array and a single number (scalar value) are also available. In that case, calculations are performed between each element of the NumPy array and the scalar value. This feature is called broadcasting (more details on this will be provided later):

>>> x = np.array([1.0, 2.0, 3.0])
>>> x / 2.0
array([ 0.5, 1. , 1.5])

N-Dimensional NumPy Arrays

In NumPy, you can create multi-dimensional arrays as well as one-dimensional arrays (linear arrays). For example, you can create a two-dimensional array (matrix) as follows:

>>> A = np.array([[1, 2], [3, 4]])
>>> print(A)
[[1 2]
[3 4]]
>>> A.shape
(2, 2)
>>> A.dtype
dtype('int64')

Here, a 2x2 matrix, A, was created. You can use shape to check the shape of the matrix, A, and use dtype to check the type of its elements. Here are the mathematical operations of matrices:

>>> B = np.array([[3, 0],[0, 6]])
>>> A + B
array([[ 4, 2],
[ 3, 10]])
>>> A * B
array([[ 3, 0],
[ 0, 24]])

As in arrays, matrices are calculated element by element if they have the same shape. Mathematical operations between a matrix and a scalar (single number) are also available. This is also conducted by broadcasting:

>>> print(A)
[[1 2]
[3 4]]
>>> A * 10
array([[ 10, 20],
[ 30, 40]])

A NumPy array (np.array) can be an N-dimensional array. You can create arrays of any number of dimensions, such as one-, two-, three-, ... dimensional arrays. In mathematics, a one-dimensional array is called a vector, and a two-dimensional array is called a matrix. Generalizing a vector and a matrix is called a tensor. In this book, we will call a two-dimensional array a matrix, and an array of three or more dimensions a tensor or a multi-dimensional array.

Broadcasting

In NumPy, you can also do mathematical operations between arrays with different shapes. In the preceding example, the 2x2 matrix, A, was multiplied by a scalar value of s. Figure 1.1 shows what is done during this operation: a scalar value of 10 is expanded to 2x2 elements for the operation. This smart feature is called broadcasting:

Figure 1.1: Sample broadcasting – a scalar value of 10 is treated as a 2x2 matrix
Figure 1.1: Sample broadcasting – a scalar value of 10 is treated as a 2x2 matrix

Here is a calculation for another broadcasting sample:

>>> A = np.array([[1, 2], [3, 4]])
>>> B = np.array([10, 20])
>>> A * B
array([[ 10, 40],
[ 30, 80]])

Here (as shown in Figure 1.2), the one-dimensional array B is transformed so that it has the same shape as the two-dimensional array A, and they are calculated element by element.

Thus, NumPy can use broadcasting to do operations between arrays with different shapes:

Figure 1.2: Sample broadcasting
Figure 1.2: Sample broadcasting

Accessing Elements

The index of an element starts from 0 (as usual). You can access each element as follows:

>>> X = np.array([[51, 55], [14, 19], [0, 4]])
>>> print(X)
[[51 55]
[14 19]
[ 0 4]]
>>> X[0]  # 0th row
array([51, 55])
>>> X[0][1] # Element at (0,1)
55

Use a for statement to access each element:

>>> for row in X:
...    print(row)
...
[51 55]
[14 19]
[0 4]

In addition to the index operations described so far, NumPy can also use arrays to access each element:

>>> X = X.flatten( ) # Convert X into a one-dimensional array
>>> print(X)
[51 55 14 19 0 4]
>>> X[np.array([0, 2, 4])] # Obtain the elements of the 0th, 2nd, and 4th indices
array([51, 14, 0])

Use this notation to obtain only the elements that meet certain conditions. For example, the following statement extracts values that are larger than 15 from X:

>>> X > 15
array([ True, True, False, True, False, False], dtype=bool)
>>> X[X>15]
array([51, 55, 19])

An inequality sign used with a NumPy array (X > 15, in the preceding example) returns a Boolean array. Here, the Boolean array is used to extract each element in the array, extracting elements that are True.

Note

It is said that dynamic languages, such as Python, are slower in terms of processing than static languages (compiler languages), such as C and C++. In fact, you should write programs in C/C++ to handle heavy processing. When performance is required in Python, the content of a process is implemented in C/C++. In that case, Python serves as a mediator for calling programs written in C/C++. In NumPy, the main processes are implemented by C and C++. So, you can use convenient Python syntax without reducing performance.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Deep Learning from the Basics
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon