Skip to content

Instantly share code, notes, and snippets.

View vrjkmr's full-sized avatar

Vivian Rajkumar vrjkmr

  • Available for hire
  • Chennai, India
View GitHub Profile
@vrjkmr
vrjkmr / decision_boundary_2d.py
Last active September 1, 2020 18:46
Plotting decision boundaries
# imports
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# set seed
seed = 1
np.random.seed(seed)
@vrjkmr
vrjkmr / boolean_parser.py
Last active September 2, 2018 00:31
Boolean text which returns a list prepared for a binary tree structure.
def parse(expr):
expr = expr.replace('(', ' ( ').replace(')', ' ) ')
brac_just_opened = False
def _listify(iter):
nonlocal brac_just_opened
items = []
for item in iter:
if item == '(':
@vrjkmr
vrjkmr / text_cleaner.py
Last active August 28, 2018 03:00
Code snippet to preprocess and clean text.
# -*- coding: utf-8 -*-
# imports
import re
from nltk.corpus import stopwords as sw
# stopwords : a list/set of strings
stopwords = set(sw.words('english'))
# TextCleaner : cleans text
# Running our model
train = fashion_mnist.train
test = fashion_mnist.test
parameters = model(train, test, learning_rate=0.0005)
'''
Output:
Cost after epoch 0: 0.5206283370382022
Cost after epoch 1: 0.3820550605650376
def model(train, test, learning_rate=0.0001, num_epochs=16, minibatch_size=32, print_cost=True, graph_filename='costs'):
'''
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
Arguments:
train -- training set
test -- test set
learning_rate -- learning rate of the optimization
num_epochs -- number of epochs of the optimization loop
minibatch_size -- size of a minibatch
def compute_cost(Z3, Y):
'''
Computes the cost
Arguments:
Z3 -- output of forward propagation (output of the last LINEAR unit), of shape (10, number_of_examples)
Y -- "true" labels vector placeholder, same shape as Z3
Returns:
cost - Tensor of the cost function
def forward_propagation(X, parameters):
'''
Implements the forward propagation for the model:
LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX
Arguments:
X -- input dataset placeholder, of shape (input size, number of examples)
parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
the shapes are given in initialize_parameters
Returns:
def initialize_parameters():
'''
Initializes parameters to build a neural network with tensorflow. The shapes are:
W1 : [n_hidden_1, n_input]
b1 : [n_hidden_1, 1]
W2 : [n_hidden_2, n_hidden_1]
b2 : [n_hidden_2, 1]
W3 : [n_classes, n_hidden_2]
b3 : [n_classes, 1]
# Create placeholders
def create_placeholders(n_x, n_y):
'''
Creates the placeholders for the tensorflow session.
Arguments:
n_x -- scalar, size of an image vector (28*28 = 784)
n_y -- scalar, number of classes (10)
Returns:
# Network parameters
n_hidden_1 = 128 # Units in first hidden layer
n_hidden_2 = 128 # Units in second hidden layer
n_input = 784 # Fashion MNIST data input (img shape: 28*28)
n_classes = 10 # Fashion MNIST total classes (0–9 digits)
n_samples = fashion_mnist.train.num_examples # Number of examples in training set