Appendix A — Section Learning Objectives

Machine Learning Frameworks - Explained

Tip
  • Introduction to Pytorch
  • Introduction to Keras/Tensorflow

In this chapter we cover the two main ML frameworks Pytorch abd Keras.

A.1 A Simple Example using PyTorch

# Source
# https://www.youtube.com/watch?v=c36lUUr864M

import torch
import numpy as np

# Print Pytorch version
print(torch.__version__)

if torch.cuda.is_available():
    print("CUDA - GPU available")
    device = torch.device("cuda")
    x = torch.ones(5, device=device)
    y = torch.ones(5)
    y = y.to(device)
    z = x + y
else:
    print("CUDA - GPU is NOT available")


x = torch.randn(3, requires_grad=True)
print(x)

y = x + 2
z = y*y*2

z = z.mean()
print(z)


z.backward() # dz/dx
print(x.grad)
2.0.1+cu117
CUDA - GPU available
tensor([-0.3804, -0.1803, -0.6960], requires_grad=True)
tensor(5.0899, grad_fn=<MeanBackward0>)
tensor([2.1594, 2.4263, 1.7386])
import tensorflow as tf

# Print TensorFlow version
print(tf.__version__)


if tf.test.is_built_with_cuda():
    print("CUDA - GPU available")
    tf.config.list_physical_devices('GPU')
else:
    print("CUDA - GPU is NOT available")
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 1
----> 1 import tensorflow as tf
      3 # Print TensorFlow version
      4 print(tf.__version__)

ModuleNotFoundError: No module named 'tensorflow'
Key Concepts and Summary

Machine learning

  • The basic
  • Some central

A.2 ToDo

  • Why is regression analysis machine learning. What type of machine learning is it?