Standalone Deep Learning

KAIST Idea factory YouTube lecture


Theory

Linear Regression

  • Even within Regression, a distinction is made between Linear/Nonlinear depending on whether the hypothesis is linear or nonlinear.
  • As practical knowledge, it was useful to learn that although theoretically the coefficient \(W\) comes first, because of the hassle of having to transpose it, in actual code the multiplication ends up being done in the form of XW. Since I have no experience implementing models, I think I’ll pick up a lot of these vaguely curious little points through the lectures.

💡

I’m curious about what difference it makes whether or not a bias is used, and exactly why a bias is used.

Session Why is it called Regression? → It originated from the use of the term regression in the life sciences.

  • Given data → identifying trends → regression of predicted values

⇒ The use of Bias? Consider it from the perspective of a single perceptron layer in a biological sense.

At the next neuron, if this signal exceeds a threshold it propagates to the next neuron, and if it doesn’t, signal transmission stops there. Each neuron has a criterion for whether to pass a ‘value’ through.

⇒ Bias is created as a way of adjusting the threshold to 0.

Classification

  • The key point is the decision boundary
  • Loss
    • Binary classification: Cross Entropy
    • Multiclass classification: Softmax

MLP Regression

  • A single-layer perceptron can perform AND and OR operations, but for the XOR operation the layers must be stacked into multiple layers.
  • Computing Backpropagation and Gradient descent: it’s efficient to represent them as a computational graph.
  • ReLU
    • Due to the nature of sigmoid and tanh, which converge as \(-\infty, \infty\), the gradient vanishing problem occurs.

      ⇒ This can be mitigated with ReLU.

Practice

Linear Regression

💡

In the practice code for Linear Regression, the model actually implemented used an MLP. Could a Linear Model reduce the loss even further?

Session

  • In the Lab of #5, the Data Set itself is nonlinear. It was visibly clear that a linear model could not predict it properly.
  • Polynomial regression and multiple linear regression are different things!

After Session

  • Out of curiosity, I used sklearn’s built-in Linear Regression with PolynomialFeatures to try a 4th-degree polynomial form.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline

# 다항 회귀 모델 생성 및 학습
degree = 4
model = make_pipeline(PolynomialFeatures(degree), LinearRegression())
model.fit(train_X, train_y)

# 훈련 데이터와 검증 데이터에 대한 예측 수행
train_pred = model.predict(train_X)
val_pred = model.predict(val_X)

# 모델 평가
print("Training score:", model.score(train_X, train_y))
print("Validation score:", model.score(val_X, val_y))

Figure

Figure

  • I could see it followed the nonlinear data distribution a bit more closely than plain linear regression (I didn’t do the data visualization well. While using the scikit-learn model, I couldn’t figure out how to log per epoch).

Classification

  • I kept not understanding why the target of nn.CrossEntropyLoss() is one-dimensional, so it took me some time looking it up. In the end, the issue came down to whether I accurately understood the concepts of Batch Size, sample, class, logit, etc., so I re-established the concepts.
    • A Sample is each data point, and a Class is, put another way, something like a Label. For example, if you have to classify the digits 0,…,9, each of these digits is a Class. A Logit is the Score value before probability conversion. In the lecture, the instructor said to be careful whether there are any issues in the training process and model evaluation process when the input does not sum to 1, but it seems that the input to nn.CrossEntropyLoss() can actually be logits. Since there is a process of taking softmax inside this function anyway, it receives logit values and computes within the function.
    • If the input has a size of Batch Size x Number of Classes, then the target has a size of Batch Size.
    • The Batch Size was the entire dataset in the practices of #5 and #7, so it is Full-Batch. However, usually as the dataset size grows, a Mini-Batch is used… so the target size will be matched to the mini-batch.
    • Taking the code used for testing in the middle as an example,

       import torch
       import torch.nn as nn
            
       cls_loss = nn.CrossEntropyLoss()
            
       test_pred_y = torch.Tensor([[2,0.1],[0,1]])
       test_true_y1 = torch.Tensor([1,0]).long()
       test_true_y2 = torch.Tensor([0,1]).long()
      

      Here the input pred_y is a 2x2 matrix, so there are 2 samples and 2 classes. For sample 1, the scores for each class are 2 and 0.1, and for sample 2, the scores for each class are 0 and 1. And looking only at the target true_y1, the answer class of sample 1 can be thought of as the first element and the answer class of sample 2 as the second element.

💡

Code questions

[71] pred_y = model(test_x).detach().max(dim=1)[1].numpy()
  • .detach(): used in a test situation to ensure that the tensor in question is no longer included in the gradient computation via backpropagation
  • .max(dim=1)[1]: finds the maximum value along a specific dimension of the tensor, and via (dim=1) is the part for finding the maximum value column-wise (per sample). Using .max() returns both the return value and the index, and [1] is used to take only the index.
  • numpy(): tensor → array conversion for subsequent visualization

Session

  • Is Dropout useful for both overfitting and underfitting?
  • Review on PCA again - eigenvalues?
    • PCA is ML; it’s still widely used in industry (because it’s cheaper than DL while having similar effects)
    • Learn it mathematically
  • Why is it called Regression? → It originated from the use of the term regression in the life sciences.
    • Given data → identifying trends → regression of predicted values

    ⇒ The use of Bias? Consider it from the perspective of a single perceptron layer in a biological sense.

    At the next neuron, if this signal exceeds a threshold it propagates to the next neuron, and if it doesn’t, signal transmission stops there. Each neuron has a criterion for whether to pass a ‘value’ through.

    ⇒ Bias is created as a way of adjusting the threshold to 0.

  • What exactly is a hyperparameter?
  • Train, validation, test set / Checking whether learning is progressing on the Validation set. Then what about the Test set?

    ⇒ Hold out: Train Set // Test set

    ⇒ Cross Validation: split into k-folds to divide Train/Validation

  • Difference between Objective vs. Metric?
    • Objective: the ‘performance’ the model wants → the objective function for that
    • Metric: the methodology for numerically confirming performance after testing