Day30: A working CNN

Posted by csiu on March 26, 2017 | with: 100daysofcode, Machine Learning

Yesterday’s failure brings today’s success. Sometimes you need to stop, take a look at the problem from another angle, and refer back to a working solution. The working solution I referred to is craffel@github’s Lasagne Tutorial.

Debugging yesterday’s error

In getting a convolutional neural network (CNN) to run, I realized the format of my input data was incorrectly specified. Originally, the data was formatted as a matrix (features along the columns and samples along the rows). The correct format is a nested data structure: dataset[sample][channel][row][column].

# Here is an example of the first sample in the correct format
# Data is found in only 1 "channel"
# (eg. notice the 2x square brackets before the first list of data)

array([[ [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         ...,
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.],
         [ 0.,  0.,  0., ...,  0.,  0.,  0.]  ]], dtype=float32)

Another problem I discovered was that when specifying the input layer’s shape, an error is returned if I specify the number of examples (n_examples), but no error is returned when None is used.

l_in = lasagne.layers.InputLayer(
  shape=(n_examples, n_channels, width, height))

Script: 5 main sections to make & run a CNN

  1. Load the data
  2. Define the CNN network structure
  3. Define the learning
    (eg. what the error function is and how the network gets updated)
  4. Define the classification/get output function
    (for computing the network’s output given an input)
  5. Do the training