This file contains 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 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) | |
This file contains 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
#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 |
This file contains 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
#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 |
This file contains 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
#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 |
This file contains 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 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)) |
This file contains 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 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 |
This file contains 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
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) |
This file contains 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 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 |
This file contains 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
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 |
This file contains 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
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) |