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 / punct_blogmining.py
Last active March 15, 2020 13:43
Removing punctuation of the text
# 4. Remove punctuation
main_content_string = main_content_string.translate(str.maketrans('', '', string.punctuation))
main_content_string = main_content_string.replace("‘", '').replace("’", '').replace("'", '')
main_content_string[:300]
@Niranjankumar-c
Niranjankumar-c / import_blogmining.py
Created March 15, 2020 12:58
Imports required for mining text data
#import required packages
from nltk.tokenize import word_tokenize
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
from collections import Counter
import seaborn as sns
from nltk.corpus import stopwords
import re
import string
@Niranjankumar-c
Niranjankumar-c / batchnorm2d_model.py
Created October 20, 2019 16:31
batch norm 2d for visualizing batch norm
class CNN_BN(nn.Module):
def __init__(self):
super(MyNetBN, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 3, 5), # (N, 1, 28, 28) -> (N, 3, 24, 24)
nn.ReLU(),
nn.AvgPool2d(2, stride=2), # (N, 3, 24, 24) -> (N, 3, 12, 12)
nn.Conv2d(3, 6, 3),
nn.BatchNorm2d(6) # (N, 3, 12, 12) -> (N, 6, 10, 10)
)
@Niranjankumar-c
Niranjankumar-c / batchnorm_model.py
Created October 20, 2019 13:53
network to visualize the batch norm
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.classifier = nn.Sequential(
nn.Linear(784, 48), # 28 x 28 = 784 flatten the input image
nn.ReLU(),
nn.Linear(48, 24),
nn.ReLU(),
nn.Linear(24, 10)
)
@Niranjankumar-c
Niranjankumar-c / dnn_dropout.py
Created October 20, 2019 06:35
model for visualizing dropout
#create a neural network with out dropout
N_h = 100 #hidden nodes
model = torch.nn.Sequential(
nn.Linear(1, N_h),
nn.ReLU(),
nn.Linear(N_h, N_h),
nn.ReLU(),
nn.Linear(N_h, 1)
)
@Niranjankumar-c
Niranjankumar-c / generate_data.py
Created October 20, 2019 06:29
data for visualizing the dropout
N = 50 #number of data points
noise = 0.3
#generate the train data
X_train = torch.unsqueeze(torch.linspace(-1, 1, N),1)
Y_train = X_train + noise * torch.normal(torch.zeros(N,1), torch.ones(N,1))
#generate the test data
X_test = torch.unsqueeze(torch.linspace(-1,1,N),1)
Y_test = X_test + noise * torch.normal(torch.zeros(N,1), torch.ones(N,1))
@Niranjankumar-c
Niranjankumar-c / generate_data.py
Created October 20, 2019 06:29
data for visualizing the dropout
N = 50 #number of data points
noise = 0.3
#generate the train data
X_train = torch.unsqueeze(torch.linspace(-1, 1, N),1)
Y_train = X_train + noise * torch.normal(torch.zeros(N,1), torch.ones(N,1))
#generate the test data
X_test = torch.unsqueeze(torch.linspace(-1,1,N),1)
Y_test = X_test + noise * torch.normal(torch.zeros(N,1), torch.ones(N,1))
@Niranjankumar-c
Niranjankumar-c / displayheatmap.py
Created October 11, 2019 17:36
display occlusion heatmap
#compute occlusion heatmap
heatmap = occlusion(model, images, pred[0].item(), 32, 14)
#displaying the image using seaborn heatmap and also setting the maximum value of gradient to probability
imgplot = sns.heatmap(heatmap, xticklabels=False, yticklabels=False, vmax=prob_no_occ)
figure = imgplot.get_figure()
@Niranjankumar-c
Niranjankumar-c / occlusion.py
Created October 11, 2019 17:12
function for occlusion analysis
#custom function to conduct occlusion experiments
def occlusion(model, image, label, occ_size = 50, occ_stride = 50, occ_pixel = 0.5):
#get the width and height of the image
width, height = image.shape[-2], image.shape[-1]
#setting the output image width and height
output_height = int(np.ceil((height-occ_size)/occ_stride))
output_width = int(np.ceil((width-occ_size)/occ_stride))
@Niranjankumar-c
Niranjankumar-c / plotweights.py
Created October 11, 2019 06:24
plotting weights
def plot_weights(model, layer_num, single_channel = True, collated = False):
#extracting the model features at the particular layer number
layer = model.features[layer_num]
#checking whether the layer is convolution layer or not
if isinstance(layer, nn.Conv2d):
#getting the weight tensor data
weight_tensor = model.features[layer_num].weight.data