Skip to content

Instantly share code, notes, and snippets.

View archydeberker's full-sized avatar

Archy de Berker archydeberker

View GitHub Profile
@archydeberker
archydeberker / client_injection.py
Last active January 21, 2025 15:38
Demonstrate the repository pattern for session mgmt in FastAPI
from typing import List
from uuid import UUID
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session
from pydantic import BaseModel
class User(BaseModel):
# This is a DB model - in SQLModel you can return the ORM model directly bc it's Pydantic under the hood
n_searches = 20
n_epochs = 15
n_val = 500
for train_size in dataset_size:
print('Training with subset %1.4f, which is %d images'%(train_size, train_size*total_train))
test_acc[train_size] = []
test_loss[train_size] = []
val_acc[train_size] = []
def random_hyperparamters():
""" Returns randomly drawn hyperparamters for our CNN"
hyperparam_dict = {}
hyperparam_dict['lr'] = 10 ** np.random.uniform(-6, -1)
hyperparam_dict['weight_decay'] = 10 ** np.random.uniform(-6, -3)
hyperparam_dict['momentum'] = 10 ** np.random.uniform(-1, 0)
hyperparam_dict['conv1_size'] = int(np.random.uniform(10,100))
test_acc = {}
val_acc = {}
train_acc = {}
test_loss = {}
for train_size in dataset_size:
print('Training with subset %1.4f, which is %d images'%(train_size, train_size*total_train))
net = Net()
# Train model with an early stopping criterion - terminates after 4 epochs of non-improving val loss
net, loss_list, val_list = train_model(net, trainset_loaders[train_size], valloader, 1000, n_epochs=10)
@archydeberker
archydeberker / CIFAR_Data_Subsampled.py
Last active April 14, 2019 21:30
Subsample data for the purpose of training a CIFAR classifier with varying dataset size
def get_dataset_size(start=0.5, end=100, base=2):
""" Returns exponentially distributed dataset size vector"""
dataset_size=[start]
while True:
dataset_size.append(dataset_size[-1]*base)
if dataset_size[-1] > end:
dataset_size[-1] = end
break
return dataset_size
@archydeberker
archydeberker / SimpleCNN.py
Created March 4, 2018 21:37
A simple CNN in Pytorch
class Net(nn.Module):
""" A simple 5 layer CNN, configurable by passing a hyperparameter dictionary at initialization.
Based upon the one outlined in the Pytorch intro tutorial
(http://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html#define-the-network)
"""
def __init__(self, hyperparam_dict=None):
super(Net, self).__init__()
if not hyperparam_dict :