Skip to content

Instantly share code, notes, and snippets.

Midterm Notes & Questions

1. Discriminative vs Generative models: what are the differences? What are the advantages and disadvantages of each?

Discriminative Models ($P(Y|X)$):

  • Concept: They learn the boundary between classes. They care about "what differentiates a cat from a dog", not "what makes a dog a dog".
  • Goal: Map input $X$ to label $Y$ directly.
  • Examples: Logistic Regression, SVM, Neural Nets (standard classifiers).
  • Advantages:
  • Generally higher accuracy for classification tasks because they focus purely on the decision boundary.
@bdsaglam
bdsaglam / insert-datetime-karabiner.json
Created February 7, 2021 16:54
Insert datetime with Karabiner
rules: [
{
"description": "hyper + d inserts datetime",
"manipulators": [
{
"from": {
"key_code": "d",
"modifiers": {
"mandatory": [
"left_shift",
@bdsaglam
bdsaglam / gsd.sh
Created September 16, 2020 20:22
Get script directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
@bdsaglam
bdsaglam / ccl.py
Last active August 11, 2020 06:42
Connected component labelling in Python
import numpy as np
# Time complexity: O(n), where n is the number of pixels in image
# Space complexity: O(n)
def label_connected_components(image: np.uint8) -> np.int:
nrow = image.shape[0]
ncol = image.shape[1]
component_map = np.full((nrow, ncol), -1, dtype=np.int)
current_comp_id = 1
for r in range(nrow):
@bdsaglam
bdsaglam / isSorted.swift
Last active August 10, 2020 16:02
Swift isSorted
//
// SequenceExtensions.swift
// Swift6006
//
// Created by Barış Deniz Sağlam on 10.08.2020.
// Copyright © 2020 BDS. All rights reserved.
//
import Foundation
@bdsaglam
bdsaglam / pulverizer.py
Created July 28, 2020 20:03
Pulverizer algorithm to find smallest linear combination of two positive numbers
def quotient_remainder(a, b):
# returns quotient and remainder of a division b
# a = q*b + r
q = 0
while a >= b:
q += 1
a -= b
return q, a
@bdsaglam
bdsaglam / expm.py
Created July 14, 2020 09:50
n-order Taylor approximation of matrix exponentiation for PyTorch
def expm(x, order=10):
'''
nth-order Taylor approximation of matrix exponential
'''
if order < 1:
raise ValueError("order cannot be smaller than 1")
I = torch.eye(x.shape[-1], dtype=x.dtype, device=x.device)
result = I
nom = I
@bdsaglam
bdsaglam / log.py
Last active June 18, 2020 17:02
Python logging example
# Credits: https://gist.github.com/nguyenkims
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
FORMATTER = logging.Formatter("[%(asctime)s][%(name)s][%(levelname)s] - %(message)s")
LOG_FILE = "app.log"
def make_console_handler():
@bdsaglam
bdsaglam / hydra_in_jupyter.py
Last active December 31, 2024 09:00
Use Hydra in Jupyter notebook
import pathlib
import hydra
hydra._internal.hydra.GlobalHydra().clear()
config_dir = pathlib.Path('/path/to/configs/')
hydra.experimental.initialize(config_dir=config_dir)
cfg = hydra.experimental.compose(config_file='config.yaml', overrides=[])
print(cfg.pretty())
@bdsaglam
bdsaglam / pycharm.gitignore
Created June 13, 2020 09:40
gitignore file for PyCharm
data/
# Jupyter notebook checkpoints
notebooks/
*.ipynb_checkpoints/
# PyTorch Lightning
*lightning_logs/
# PyCharm files