Skip to content

Instantly share code, notes, and snippets.

View Niranjankumar-c's full-sized avatar
:octocat:
Focusing

Niranjan Kumar Niranjankumar-c

:octocat:
Focusing
View GitHub Profile
@Niranjankumar-c
Niranjankumar-c / imshow.py
Created October 9, 2019 16:12
Function to display image in the data
def imshow(img, title):
"""Custom function to display the image using matplotlib"""
#define std correction to be made
std_correction = np.asarray([0.229, 0.224, 0.225]).reshape(3, 1, 1)
#define mean correction to be made
mean_correction = np.asarray([0.485, 0.456, 0.406]).reshape(3, 1, 1)
@Niranjankumar-c
Niranjankumar-c / imports.py
Last active October 3, 2019 14:09
CNN Visualizing imports
#required libraries
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import torch
import torch.nn as nn
import torchvision
@Niranjankumar-c
Niranjankumar-c / ffnetwork_data_pytorch.py
Last active June 19, 2019 16:38
generating dummy data for ffnetwork using pytorch
#generate data using make_blobs function from sklearn.
#centers = 4 indicates different types of classes
data, labels = make_blobs(n_samples=1000, centers=4, n_features=2, random_state=0)
print(data.shape, labels.shape)
#visualize the data
plt.scatter(data[:,0], data[:,1], c=labels, cmap=my_cmap)
plt.show()
#splitting the data into train and test
@Niranjankumar-c
Niranjankumar-c / ffnetwork_pytorch.py
Created June 19, 2019 16:21
Importing libraries required for ffnetwork using Pytorch
#required libraries
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.colors
import time
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, mean_squared_error, log_loss
from tqdm import tqdm_notebook
@Niranjankumar-c
Niranjankumar-c / activationfunction_trainmodel.py
Created May 7, 2019 16:42
Training a neural network for activation functions
def post_process(scatter_plot=False, gradient_plot=True, plot_scale=0.1):
#Method for evaluating the network performance and plot the graphs
Y_pred_train = model.predict(X_train)
Y_pred_train = np.argmax(Y_pred_train,1)
Y_pred_val = model.predict(X_val)
Y_pred_val = np.argmax(Y_pred_val,1)
#computing the accuracy for test data and train data
accuracy_train = accuracy_score(Y_pred_train, Y_train)
accuracy_val = accuracy_score(Y_pred_val, Y_val)
print("Training accuracy", round(accuracy_train, 4))
@Niranjankumar-c
Niranjankumar-c / activationfunction_ffnetwork.py
Created May 5, 2019 16:15
ff network with different activation and weight initialization methods
class FFNetwork:
def __init__(self, init_method = 'random', activation_function = 'sigmoid', leaky_slope = 0.1):
self.params={}
self.params_h = []
self.num_layers=2
self.layer_sizes = [2, 2, 4]
self.activation_function = activation_function
self.leaky_slope = leaky_slope
@Niranjankumar-c
Niranjankumar-c / activationfunction_generatedata.py
Created May 5, 2019 15:51
generate non-linearly separable data
data, labels = make_blobs(n_samples=1000, centers=4, n_features=2, random_state=0)
print(data.shape, labels.shape)
#plot graph
plt.scatter(data[:,0], data[:,1], c=labels, cmap=my_cmap)
plt.show()
#convert 4 class to 2 class data
labels_orig = labels
labels = np.mod(labels_orig, 2)
@Niranjankumar-c
Niranjankumar-c / activationfunction_import.py
Created May 5, 2019 14:37
Imports for creating activation functions
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, mean_squared_error, log_loss
from tqdm import tqdm_notebook
import seaborn as sns
import imageio
import time
from IPython.display import HTML
@Niranjankumar-c
Niranjankumar-c / optimization_gradientdescent.py
Created April 22, 2019 18:38
gradient descent algorithm settings
X = np.asarray([0.5, 2.5])
Y = np.asarray([0.2, 0.9])
algo = 'GD'
w_init = -2
b_init = -2
w_min = -7
w_max = 5
b_min = -7
b_max = 5
@Niranjankumar-c
Niranjankumar-c / optimization_2dplotting.py
Created April 22, 2019 18:14
Create 2D plots for animating the gradient descent rule
if plot_2d:
W = np.linspace(w_min, w_max, 256)
b = np.linspace(b_min, b_max, 256)
WW, BB = np.meshgrid(W, b)
Z = sn.error(X, Y, WW, BB)
fig = plt.figure(dpi=100)
ax = plt.subplot(111)
ax.set_xlabel('w')
ax.set_xlim(w_min - 1, w_max + 1)