Implementing Planar Classifier with Keras


There is an example in the third week of first course in which we implement a Shallow Neural Network for classification of planar data. I wanted to implement this also using Keras. I used the code below to do that. Thelearning rate is the same as we used in the example for planar classification. I had to take transpose of the input and output because the array X is (2,400) whereas Y is (1,400), that’s why, If I use them without transposing I got the following error.

ValueError: Data cardinality is ambiguous:
x sizes: 2
y sizes: 1
Please provide data which shares the same first dimension.

I may have a mistake doing this transposing thing but I think the accuracy should be more, In the example of week 3, with 5 hidden neurons we get %90 accuracy. However, I got %34 here. Is there a mistake in my implementation for this network which caused a lower accuracy or it is just about some parameters I didn’t tune ?

It is great that you are trying experiments like this. You always learn something useful.

You’ll have to do some more experimentation to make progress here. Here are a couple of suggestions for things to consider:

  1. This is a binary classification, right? So you probably want binary cross entropy as your loss function, instead of “categorical”. Although categorical with 2 categories is probably equivalent. Also most people use from_logits = True mode, so you won’t need the sigmoid activation on the output layer.
  2. Unless you know the internals of the Keras SGD optimizer, you really have no idea what a good learning rate is. In other words, if you’re not doing exactly the same version of gradient descent that was done in the exercise, you have no a priori knowledge of a good learning rate. Or to put it another way: learning rates are not “portable”.
  3. You’re only doing 500 Epochs of training. In the notebook, notice that we did 10k iterations.
  4. It’s worth trying a more sophisticated optimizer like Adam, rather than just going with SGD.

Let us know if you get better results based on your further experiments! Science! :nerd_face: