I have a CSV file with 20 columns and 785 rows. The 785th row for each column is a label describing the encoded image. The encoded image is either 3 or 5. So 1-784 row is the encoded image and 785th row is the label that names the image.

I loaded the CSV file 3_5_small.csv and segregated the labels from the encoded data.

which as you see is the image of number 3.

Now, I decided to use logistic regression to predict the images from the encoded data. I used Stochastic Gradient Descent as explained in the machine learning course by Andrew NG. But I do not think, I got it right. Before following the code, here are the steps I did:

  • Transformed train_labels_3_5 which contained only 3 and 5 to 1 and 0 respectively. So I want to predict the image of 3. If the output probability is < 0.5, it will be 5 and > 0.5 will be 3.
  • Randomly shuffled the train_data_3_5 and train_labels_3_5 to the same degree.
  • Randomly generated the theta vector
  • Passed the theta vector and X vector into the hypothesis function
  • Updated the theta vector.

This is all I did. Here is the code to what I have done.

train <- function(data, labels, alpha = 0.001) {

  #Initialize the theta vector
  theta <- seq(from = 0, to = 1, length.out = nrow(data))

  number_of_iterations = 10
  for(noi in 1:number_of_iterations) {
    for(i in seq(1:ncol(data))) {
      x = as.vector(data[,i]) #Create a x vector
      h = hypothesis(x, theta) #Call the hypothesis function to get the probability
      y = labels[1,i]
      theta <- theta - (alpha * ((h - y) * x))

But on the test data and even on the training data, this does not predict correct at all. I do not know where have I gone wrong. I have revisited the algorithm, the lecture but cannot figure out, what am I doing incorrectly. It always predicts 1 no matter I pass the vector for 3 or 5!

  • $\begingroup$ This seems to be a cross-posting. Two cents: try to initialize the weight vector by small, random values. What is the shape of labels? $\endgroup$
    – Michael M
    Commented Apr 3, 2018 at 18:40

1 Answer 1


My network does always predict the same class. What is the problem?

I had this a couple of times. Although I'm currently too lazy to go through your code, I think I can give some general hints which might also help others who have the same symptom but probably different underlying problems.

Debugging Neural Networks

Fitting one item datasets

For every class i the network should be able to predict, try the following:

  1. Create a dataset of only one data point of class i.
  2. Fit the network to this dataset.
  3. Does the network learn to predict "class i"?

If this doesn't work, there are four possible error sources:

  1. Buggy training algorithm: Try a smaller model, print a lot of values which are calculated in between and see if those match your expectation.
    1. Dividing by 0: Add a small number to the denominator
    2. Logarithm of 0 / negativ number: Like dividing by 0
  2. Data: It is possible that your data has the wrong type. For example, it might be necessary that your data is of type float32 but actually is an integer.
  3. Model: It is also possible that you just created a model which cannot possibly predict what you want. This should be revealed when you try simpler models.
  4. Initialization / Optimization: Depending on the model, your initialization and your optimization algorithm might play a crucial role. For beginners who use standard stochastic gradient descent, I would say it is mainly important to initialize the weights randomly (each weight a different value). - see also: this question / answer

Learning Curve

See sklearn for details.

enter image description here

The idea is to start with a tiny training dataset (probably only one item). Then the model should be able to fit the data perfectly. If this works, you make a slightly larger dataset. Your training error should slightly go up at some point. This reveals your models capacity to model the data.

Data analysis

Check how often the other class(es) appear. If one class dominates the others (e.g. one class is 99.9% of the data), this is a problem. Look for "outlier detection" techniques.


  • Learning rate: If your network doesn't improve and get only slightly better than random chance, try reducing the learning rate. For computer vision, a learning rate of 0.001 is often used / working. This is also relevant if you use Adam as an optimizer.
  • Preprocessing: Make sure you use the same preprocessing for training and testing. You might see differences in the confusion matrix (see this question)

Common Mistakes

This is inspired by reddit:

  • You forgot to apply preprocessing
  • Dying ReLU
  • To small / to big learning rate
  • Wrong activation function in final layer:
    • Your targets are not in sum one? -> Don't use softmax
    • Single elements of your targets are negative -> Don't use Softmax, ReLU, Sigmoid. tanh might be an option
  • Too deep network: You fail to train. Try a simpler neural network first.
  • $\begingroup$ Out of the 2 data classes, one class is 46% of another. Could this be cause of problem? $\endgroup$
    – Jatt
    Commented Apr 2, 2018 at 9:33
  • $\begingroup$ Like there is a difference of 710 data points for one class. $\endgroup$
    – Jatt
    Commented Apr 2, 2018 at 9:38
  • $\begingroup$ Check if the classes are similar spread in the training set. And, no, 46% and 54% is by far not a problem $\endgroup$ Commented Apr 2, 2018 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.