This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
NewerOlder