Skip to content

coding-wiki/learn-machine-learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

learn-machine-learning

Machine learning for dummies. Assuming minimal to practically no knowledge of Python. We will be referring to PyTorch primarily. In the future, this guide should be framework agnostic and framework specific information should be moved over to separate TensorFlow and PyTorch guides.

NumPy

What is NumPy?

Python library that adds support for multi-dimensional arrays and matrices, along with many high-level mathematical functions to operate on these arrays.

Code

Initialise a NumPy array

import numpy as np

arr = numpy.arr((1, 2, 3, 4, 5))

print(arr)

stdout: [1 2 3 4 5]

We have just created a 1-D array.

We can create a 0-D (zero dimensional) array with arr = numpy.arr(1)

stdout: 1

NumPy Arrays

ndarray is an N-dimensional array.

type(numpyarr) = <class 'numpy.ndarray'>

0-D Array

A 0-D Array are Scalars, they are the elements in array. Each value in an array is a 0-D array.

arr = numpy.arr(1)

1-D Array

An array that contains 0-D elements is a 1-D Array, a uni-dimensional array.

arr = numpy.arr([1, 2, 3, 4, 5])
# OR
arr = numpy.arr((1, 2, 3, 4, 5))

2-D Array

An array containing 1-D elements is a 2-D Array. These are often used to represent matrix or 2nd order tensors.

3-D Array

An array containing matrices (2-D arrays) as its elements is a 3-D array. These often represent a 3rd order tensor.

How do I Check the Dimension of an Array?

NumPy arrays have an ndim attribute, that can be used as such:

arr = np.array([[1, 2], [3, 4]])

print(arr.ndim)

stdout: 2

Setting Number of Dimensions

You can set the number of dimensions while initialising a NumPy array, as such:

arr = np.array([1, 2, 3], ndim=5)

Tensors

What is a Tensor?

Tensors are a specialised data structure very similar to arrays and matrices. Machine learning frameworks use tensors to encode the inputs and outputs of a model, as well as the model’s parameters.

Introduction: https://www.grc.nasa.gov/WWW/k-12/Numbers/Math/documents/Tensors_TM2002211716.pdf

https://www.kdnuggets.com/2018/05/wtf-tensor.html

How do I implement a Tensor in C?

Is this something that can be done, or would it only be used in the context of a library? Seems to be library specific, and an array would be used for interoperability (or data initialisation.

https://stackoverflow.com/questions/56841794/implement-tensor-multiplication-using-c-in-correspondence-to-mat-mul-operatio

What differentiates a tensor from an array?

https://thecleverprogrammer.com/2021/03/19/difference-between-tensors-and-arrays/

An array is a grid of values that contains raw data and we use it to locate and interpret elements in the raw data.

Tensors are backed by the accelerator memory like GPU and they are immutable, unlike NumPy arrays.

Tensors in PyTorch

Tensors are similar to NumPy ndarrays, except that tensors can run on GPUs or other hardware accelerators (i.e. M1 neural cores?). Tensors and NumPy arrays can often share the same underlying memory, eliminating the need to copy data.

Example tensor initialisation

data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)

Todo - Sort

https://machinelearningmastery.com/introduction-to-tensors-for-machine-learning/

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published