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 libraries | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import tensorflow as tf | |
from tensorflow.python.framework import ops | |
from tensorflow.examples.tutorials.mnist import input_data | |
# Import Fashion MNIST | |
fashion_mnist = input_data.read_data_sets('input/data', one_hot=True) |
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
# Shapes of training set | |
print("Training set (images) shape: {shape}".format(shape=fashion_mnist.train.images.shape)) | |
print("Training set (labels) shape: {shape}".format(shape=fashion_mnist.train.labels.shape)) | |
# Shapes of test set | |
print("Test set (images) shape: {shape}".format(shape=fashion_mnist.test.images.shape)) | |
print("Test set (labels) shape: {shape}".format(shape=fashion_mnist.test.labels.shape)) | |
''' | |
Output: |
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 dictionary of target classes | |
label_dict = { | |
0: ‘T-shirt/top’, | |
1: ‘Trouser’, | |
2: ‘Pullover’, | |
3: ‘Dress’, | |
4: ‘Coat’, | |
5: ‘Sandal’, | |
6: ‘Shirt’, | |
7: ‘Sneaker’, |
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
# Sample 1 | |
# Get 28x28 image | |
sample_1 = fashion_mnist.train.images[47].reshape(28,28) | |
# Get corresponding integer label from one-hot encoded data | |
sample_label_1 = np.where(fashion_mnist.train.labels[47] == 1)[0][0] | |
# Plot sample | |
print("y = {label_index} ({label})".format(label_index=sample_label_1, label=label_dict[sample_label_1])) | |
plt.imshow(sample_1, cmap='Greys') |
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 |
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
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
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 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 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 |
OlderNewer