Shaashwat05 / mnist_GA

An MNIST handwritten dataset classifier using a genetically optimized CNN model.
https://medium.com/@shaas2000/mnist-classifier-using-genetic-cnn-e1e860ecc2e9
6 stars 3 forks source link

The mnist dataset label comes without one hot key #1

Open Nileshmk opened 3 years ago

Nileshmk commented 3 years ago

as I mentioned in the title so as the model is using softmax you need to convert the y_train and y_test to one hot key.

def dense_to_one_hot(labels_dense, num_classes=10):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels_dense.shape[0]
  index_offset = numpy.arange(num_labels) * num_classes
  labels_one_hot = numpy.zeros((num_labels, num_classes))
  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
  return labels_one_hot

I don't know that previously the dataset came with one hot key and now it's coming with simple number 0-9 but while I was running the code i got into trouble of this and got valueError. By using the function the error got solved. Plz update the code if this error is occuring to you as well

Shaashwat05 commented 3 years ago

as I mentioned in the title so as the model is using softmax you need to convert the y_train and y_test to one hot key.

def dense_to_one_hot(labels_dense, num_classes=10):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels_dense.shape[0]
  index_offset = numpy.arange(num_labels) * num_classes
  labels_one_hot = numpy.zeros((num_labels, num_classes))
  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
  return labels_one_hot

I don't know that previously the dataset came with one hot key and now it's coming with simple number 0-9 but while I was running the code i got into trouble of this and got valueError. By using the function the error got solved. Plz update the code if this error is occuring to you as well

Thank you for your support, I will check this out and update the code accordingly.

jsfinesse commented 2 years ago

as I mentioned in the title so as the model is using softmax you need to convert the y_train and y_test to one hot key.

def dense_to_one_hot(labels_dense, num_classes=10):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels_dense.shape[0]
  index_offset = numpy.arange(num_labels) * num_classes
  labels_one_hot = numpy.zeros((num_labels, num_classes))
  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
  return labels_one_hot

I don't know that previously the dataset came with one hot key and now it's coming with simple number 0-9 but while I was running the code i got into trouble of this and got valueError. By using the function the error got solved. Plz update the code if this error is occuring to you as well

Hey where do you put this code? I am running into the same valueError.

Edit - If someone is still wondering, you need to do this before defining the model

def dense_to_one_hot(labels_dense, num_classes=10):
  """Convert class labels from scalars to one-hot vectors."""
  num_labels = labels_dense.shape[0]
  index_offset = numpy.arange(num_labels) * num_classes
  labels_one_hot = numpy.zeros((num_labels, num_classes))
  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
  return labels_one_hot

y_train = dense_to_one_hot(y_train)
y_test = dense_to_one_hot(y_test)