Skip to content

Instantly share code, notes, and snippets.

View srikarplus's full-sized avatar

Srikar Tadiparthi srikarplus

View GitHub Profile
@srikarplus
srikarplus / stratified_sample.py
Created December 1, 2018 08:17
Stratified Sampling in Pytorch
def make_weights_for_balanced_classes(images, nclasses):
count = [0] * nclasses
for item in images:
count[item[1]] += 1
weight_per_class = [0.] * nclasses
N = float(sum(count))
for i in range(nclasses):
weight_per_class[i] = N/float(count[i])
weight = [0] * len(images)
for idx, val in enumerate(images):
@srikarplus
srikarplus / train_valid_loader.py
Last active May 18, 2022 10:17
Train and Validation Split for Pytorch torchvision Datasets
import torch
import numpy as np
from utils import plot_images
from torchvision import datasets
from torchvision import transforms
from torch.utils.data.sampler import SubsetRandomSampler
def get_train_valid_loader(data_dir,
def randInitializeWeights(L_in, L_out):
epsilon = 0.12
return np.random.rand(L_out, L_in+1) * 2 * epsilon - epsilon
initial_theta1 = randInitializeWeights(input_layer_size, hidden_layer_size)
initial_theta2 = randInitializeWeights(hidden_layer_size, num_labels)
# unrolling parameters into a single column vector
nn_initial_params = np.hstack((initial_theta1.ravel(order='F'), initial_theta2.ravel(order='F')))
@srikarplus
srikarplus / predict.py
Created October 9, 2018 02:24
neural network prediction
def predict(theta1, theta2, X, y):
m = len(y)
ones = np.ones((m,1))
a1 = np.hstack((ones, X))
a2 = sigmoid(a1 @ theta1.T)
a2 = np.hstack((ones, a2))
h = sigmoid(a2 @ theta2.T)
return np.argmax(h, axis = 1) + 1
theta_opt = opt.fmin_cg(maxiter = 50, f = nnCostFunc, x0 = nn_initial_params, fprime = nnGrad, \
args = (input_layer_size, hidden_layer_size, num_labels, X, y.flatten(), lmbda))
theta1_opt = np.reshape(theta_opt[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F')
theta2_opt = np.reshape(theta_opt[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F')
@srikarplus
srikarplus / check_grad.py
Created October 9, 2018 01:53
gradient checking
def checkGradient(nn_initial_params,nn_backprop_Params,input_layer_size, hidden_layer_size, num_labels,myX,myy,mylambda=0.):
myeps = 0.0001
flattened = nn_initial_params
flattenedDs = nn_backprop_Params
n_elems = len(flattened)
#Pick ten random elements, compute numerical gradient, compare to respective D's
for i in range(10):
x = int(np.random.rand()*n_elems)
epsvec = np.zeros((n_elems,1))
epsvec[x] = myeps
@srikarplus
srikarplus / nnGrad.py
Created October 9, 2018 01:51
gradient for neural network
def nnGrad(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda):
initial_theta1 = np.reshape(nn_params[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F')
initial_theta2 = np.reshape(nn_params[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F')
y_d = pd.get_dummies(y.flatten())
delta1 = np.zeros(initial_theta1.shape)
delta2 = np.zeros(initial_theta2.shape)
m = len(y)
for i in range(X.shape[0]):
@srikarplus
srikarplus / nnCostFunc.py
Created October 9, 2018 01:48
cost function for neural network
def nnCostFunc(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda):
theta1 = np.reshape(nn_params[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F')
theta2 = np.reshape(nn_params[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F')
m = len(y)
ones = np.ones((m,1))
a1 = np.hstack((ones, X))
a2 = sigmoid(a1 @ theta1.T)
a2 = np.hstack((ones, a2))
h = sigmoid(a2 @ theta2.T)