Kaggle Competition: Digit Recognizer -MNIST with tf.Keras
This is a tutorial on how to join a “Getting Started” Kaggle competition — Digit Recognizer — classify digits with tf.Keras. In this tutorial we will use a Kaggle Kernel to create a CNN with tf.Keras to classify the hand-written digits from MNIST. Then we create an output file from the kernel for submission to competition. The following topics are covered:
- How to join a competion
- How to fork or create a new kernel
- How to generate output file
- How to submit predictions
I included a link to my kernel (MNIST with tf.Keras) with example code. Note: the main goal of this tutorial is to go over the process of joining the competition, instead of how to win a competition.

Join the competition
First go to Kaggle competitions, scroll through the competition list and you will see the competition Digit Recognizer. Select the competition and read through the Description, Evaluation, Tutorial and Rules to better stand the requirements. Since this competition is for learning only, it’s an on-going competition without deadline.
Click on “Join Competition”. Note: you must be signed in to Kaggle in order to see the “Join Competition” button.
Create a kernel
A Kaggle Kernel is an in-browser computational environment fully integrated with most competition datasets on Kaggle. It’s preloaded with most data science packages and libraries. It supports scripts, Jupyter Notebooks in R and Python, as well as RMarkdown reports.
You can use kernels to create submission files or explore the competition data. While you don’t have to use Kaggle kernels for writing your code, it’s a great option that is seamlessly integrated with the datasets, free GPU and easy submission to competitions.
To create a kernel you can either fork an existing kernel someone else published or create a new one on you own.
Fork an existing notebook
Under the Digit Recognizer competition Tutorial section, you will see a list of kernels suggested for you to try out. Click on “Fork Notebook” will create your own copy of the kernel to explore code someone else wrote.
Create a new kernel
Here is how to create a new kernel:
- In the Digit Recognizer competition, click on the Kernels tab,
- Click on New Kernel, choose Notebook option. A new notebook will be created for you, default to Python language.
- The new notebook will have some stub code such as import numpy, pandas and os. Note all the input files are under “../input/” directory and output in “../working/” directory
import os# list files from input directory
print(os.listdir("../input"))
You will see these 3 files listed under the input directory:

The sample_submission.cvs file contains the format of the data that you are expected to submit.
Take a look at the kernel that I created — click on “Fork Notebook” to execute the cells in the notebook. Note I used tf.Keras to create a CNN to classify the digits.
Protip: you can turn on GPU for your kernel by going to Settings and toggle from GPU off to GPU on. For a simple dataset such as MNIST you don’t necessarily need to turn on GPU but it’s good to know it’s available when you need it.
Make kernel public
In order to generate an output file, you will need to make sure your kernel is public. Making your kernel public also allows others to learn from your code.
There are two options to make your kernel public:
- On the Kernel overview page, click on “Make Public” to toggle public/private setting.
- In your notebook, go to Settings and click on “Sharing” to toggle to public.

Either way, you will be prompted and click on “Ok, make public” which will make your kernel publicly accessible to others.

Generate output file
These lines of code at the end of the notebook will generate an output file:
predictions = model.predict_classes(x_test, verbose=0)
submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)), "Label": predictions})# Generate csv file
submissions.to_csv("submission.csv", index=False, header=True)
Once you click on “Commit” it will saves a version of your code and also runs your code from top to bottom. There is a URL to each version which people can use to view your kernel. By default the latest version is visible to others once your kernel is made public.
Submit predictions
To see the output file generated by your notebook, click on the << icon (back to kernel page) on the top left corner of your notebook. Click on the “Output” tab and you will see the output file there. Click on the button “Submit to Competition” and our output file will be submitted. You will then be taking to the leaderboard to see your ranking.
References:

