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.
Python library that adds support for multi-dimensional arrays and matrices, along with many high-level mathematical functions to operate on these arrays.
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
ndarray
is an N-dimensional array.
type(numpyarr)
= <class 'numpy.ndarray'>
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)
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))
An array containing 1-D elements is a 2-D Array. These are often used to represent matrix or 2nd order tensors.
An array containing matrices (2-D arrays) as its elements is a 3-D array. These often represent a 3rd order tensor.
NumPy arrays have an ndim
attribute, that can be used as such:
arr = np.array([[1, 2], [3, 4]])
print(arr.ndim)
stdout:
2
You can set the number of dimensions while initialising a NumPy array, as such:
arr = np.array([1, 2, 3], ndim=5)
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
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://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 are similar to NumPy ndarray
s, 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)
https://machinelearningmastery.com/introduction-to-tensors-for-machine-learning/