<jupyter_start><jupyter_text>TOC __Chapter 4 - Fundamentals of machine learning__1. [Import](Import)1. [Supervised learning](Supervised-learning)1. [Unsupervised learning](Unsupervised-learning)1. [Reinforcement learning](Reinforcement-learning)1. [Overfitting](Overfitting)    1. [Reducing the size of the network](Reducing-the-size-of-the-network)    1. [Applying weight regularization](Applying-weight-regularization)    1. [Dropout](Dropout)1. [Learning rate picking strategies](Learning-rate-picking-strategies)  Import<jupyter_code># standard libary and settings
import os
import sys
import importlib
import itertools
from PIL import Image
from glob import glob
import warnings

warnings.simplefilter("ignore")
from IPython.core.display import display, HTML

display(HTML("<style>.container { width:95% !important; }</style>"))

# data extensions and settings
import numpy as np

np.set_printoptions(threshold=np.inf, suppress=True)
import pandas as pd

pd.set_option("display.max_rows", 500)
pd.set_option("display.max_columns", 500)
pd.options.display.float_format = "{:,.6f}".format

# pytorch tools
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torch.autograd import Variable
from torchvision import datasets, models, transforms

# visualization extensions and settings
import seaborn as sns
import matplotlib.pyplot as plt

%matplotlib inline

# custom extensions and settings
sys.path.append("/home/mlmachine") if "/home/mlmachine" not in sys.path else None
sys.path.append("/home/prettierplot") if "/home/prettierplot" not in sys.path else None

import mlmachine as mlm
from prettierplot.plotter import PrettierPlot
import prettierplot.style as style<jupyter_output><empty_output><jupyter_text>Supervised learningPyTorch can be applied to supervised learning problems just like popular machine learning libraries such as scikit-learn. Here is a brief list of problem types and generic examples:-__Classification__ - classifying dogs and cats based on images.-__Regression__ - predicting stock prices. -__Image segmentation__ - pixel-classification. as a specific example, it is important for a self-driving car to identify what each pixel belongs to so that it can identify where various objects exist/begin/end, such as other cars, pedestrians, trees, and so on.-__Speech recognition__ - Alexa, Siri, OK Google.-__Language translation__ - translating speech form one language to another language.   Unsupervised learningWhen there are no labels in the data, the task is considered to be unsuperivsed. two commonly-used techniques are:- __Clustering__ - groups similar data points / observations together- __Dimensionality reduction__ - reduces the number of dimensions so that we can evaluate / visualize high-dimensional data to discover any hidden patterns.   Reinforcement learningAn application of reinforcement learning that received mainstream news coverage recently is Google's DeepMind project called AlphaGo that defeated the Go world champion. This topic will not be covered in this book.   OverfittingOverfitting occurs when a model performs well on a training dataset but generalizes poorly to unseen data. Neural networks can be adjusted in several differt ways to attmpt addressing overfitting issues.   Reducing the size of the networkRemoving certain layers from an unnecessarily deep network can prevent the network from memorizing the training data. Training data memorization typically results in poor performance on validation/unseen data.<jupyter_code># excessively deep network
class Architecture1(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(Architecture1, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, num_classes)
        self.relu = nn.ReLU()
        self.fc3 = nn.Linear(hidden_size, num_classes)

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        out = self.relu(out)
        out = self.fc3(out)
        return out


# same network as above, but one ReLU layer and one linear layer has been removed, which may be less likely to overfit
class Architecture2(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(Architecture2, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, num_classes)

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out<jupyter_output><empty_output><jupyter_text>Applying weight regularizationRegularization reduces the complexity of model by penalizing large weights. PyTorch provides an easy way to utilize l2/ridge regularization through the weight_decay parameter in the optimizer. by default weight_decay is set to 0.```pythonmodel = Architecture1(10, 20, 2)optimizer = torch.optim.Adam(model.parameters(), lr = 1e-4, weight_decay = le-5)```<jupyter_code># instantiate model and create optimizer
model = Architecture1(10, 20, 2)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07680665909950814, 'fraction_numerical': 0.01021566401816118, 'mean_word_length': 4.697198275862069, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '612328', 'license': 'MIT', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1440, 'n_words': 539, 'path': 'textbooks/DeepLearningWithPyTorch/ch04_Fundamentals_of_machine_learning.ipynb', 'repo_name': 'sudhu26/data-science-portfolio', 'size': '5286'}