In [1]:
# Problem 1 - Implement Min-Max scaling for grayscale image data
def normalize_grayscale(image_data):
"""
Normalize the image data with Min-Max scaling to a range of [0.1, 0.9]
:param image_data: The image data to be normalized
:return: Normalized image data
"""
a = 0.1
b = 0.9
grayscale_min = 0
grayscale_max = 255
return a + ( ( (image_data - grayscale_min)*(b - a) )/( grayscale_max - grayscale_min ) )
In [2]:
import numpy as np
%timeit normalize_grayscale(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 255]))
In [3]:
def normalize_grayscale_no_zero_ops(image_data):
"""
Normalize the image data with Min-Max scaling to a range of [0.1, 0.9]
:param image_data: The image data to be normalized
:return: Normalized image data
"""
a = 0.1
b = 0.9
# grayscale_min = 0
grayscale_max = 255
# return a + ( ( (image_data - grayscale_min)*(b - a) )/( grayscale_max - grayscale_min ) )
return a + ( ( (image_data )*(b - a) )/( grayscale_max ) )
In [4]:
%timeit normalize_grayscale_no_zero_ops(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 255]))
In [5]:
def normalize_grayscale_no_a_minus_b_op(image_data):
"""
Normalize the image data with Min-Max scaling to a range of [0.1, 0.9]
:param image_data: The image data to be normalized
:return: Normalized image data
"""
# a = 0.1
# b = 0.9
# b_minus_a = 0.8
# grayscale_min = 0
# grayscale_max = 255
# return a + ( ( (image_data - grayscale_min)*(b - a) )/( grayscale_max - grayscale_min ) )
return 0.1 + ( ( (image_data )*(0.8) )/( 255 ) )
In [6]:
%timeit normalize_grayscale_no_a_minus_b_op(np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 255]))
features and labels since they are the inputs to the model.features and labels must also be float32.weights and biases to be modified.weights must be the dimensions of features by labels. The number of features is the size of the image, 28*28=784. The size of labels is 10.biases must be the dimensions of the labels, which is 10.
In [ ]:
features_count = 784
labels_count = 10
# Problem 2 - Set the features and labels tensors
features = tf.placeholder(tf.float32)
labels = tf.placeholder(tf.float32)
# Problem 2 - Set the weights and biases tensors
weights = tf.Variable(tf.truncated_normal((features_count, labels_count)))
biases = tf.Variable(tf.zeros(labels_count))