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
# 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) |
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 parse(expr): | |
expr = expr.replace('(', ' ( ').replace(')', ' ) ') | |
brac_just_opened = False | |
def _listify(iter): | |
nonlocal brac_just_opened | |
items = [] | |
for item in iter: | |
if item == '(': |
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
# -*- 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 |
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
# 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 |
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 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 |
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 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 |
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 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: |
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 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] | |
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
# 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: |
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
# 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 |
NewerOlder