Solutions

Problem 1

Implement the Min-Max scaling function ($X'=a+{\frac {\left(X-X_{\min }\right)\left(b-a\right)}{X_{\max }-X_{\min }}}$) with the parameters:

$X_{\min }=0$

$X_{\max }=255$

$a=0.1$

$b=0.9$


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]))


The slowest run took 8.66 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 19.8 µs per loop

Alternative (no zero ops) solution


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]))


The slowest run took 4.07 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 17.4 µs per loop

Alternative 2 (no a - b op) solution


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]))


100000 loops, best of 3: 17.3 µs per loop

Problem 2

  • Use tf.placeholder() for features and labels since they are the inputs to the model.
  • Any math operations must have the same type on both sides of the operator. The weights are float32, so the features and labels must also be float32.
  • Use tf.Variable() to allow weights and biases to be modified.
  • The 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.
  • The 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))

Problem 3

Configuration 1

  • Epochs: 1
  • Learning Rate: 0.1

Configuration 2

  • Epochs: 4 or 5
  • Learning Rate: 0.2