{"cells":[{"metadata":{"trusted":true,"_uuid":"7ed6973d3d2d1e860b28c1b2142d3f1e1322f237"},"cell_type":"code","source":"# Import stuff\nfrom __future__ import print_function\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten\nfrom keras import backend as K\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt #for plotting","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"da7dd3f51b395dc49ac217c94eac320be3915fe0"},"cell_type":"markdown","source":"### Difference between batch & epoch\n![Batch vs Epoch](https://i.imgur.com/Zsb38ZL.png)\nRead more at [StackOverflow - Epoch vs Iteration when training neural networks](https://stackoverflow.com/a/31842945)"},{"metadata":{"trusted":true,"_uuid":"bd0fa6e2c7ce32152e609f58e41d33ce2d033fd0"},"cell_type":"code","source":"# How many images to send to GPU?\nbatch_size = 128\n\n# How many target classes are in the dataset? (10 number, 0 - 9)\nnum_classes = 10\n\n# Epoch = neural network have seen all training examples\nepochs = 5\n\n# input image dimensions\nimg_rows, img_cols = 28, 28","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a182195f6d4669ef6acced3dd91dc81e57570644"},"cell_type":"code","source":"# loading the training dataset\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c667896b84e4766709ff79a7807ede05fb98c85c"},"cell_type":"code","source":"# the data\nx_train = (train.iloc[:,1:].values).astype('float32') # all pixel values\ny_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\nx_test = test.values.astype('float32') # this is not a validation set. We don't have labels for this!","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc6d2e1bd606d1fc5f861ec5549401332b4bd316"},"cell_type":"code","source":"# preview the images\nplt.figure(figsize=(12,10))\nplot_cols, plot_rows = 10, 4\nfor i in range(40):\n    plt.subplot(plot_rows, plot_cols, i+1)\n    plt.imshow(x_train[i+50, :].reshape((28,28)),interpolation='nearest')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85883ec4b1476a8ca290d44c64281b63e690e840"},"cell_type":"code","source":"# Pixel values are from range 0 - 255. We must normalize to 0-1 (this is super important)!\nx_train = x_train / 255.0\nx_test = x_test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e1b6f5e134743d949b793e1b22a8cf4515ca088"},"cell_type":"code","source":"# Different Keras backends can have different index ordering!\nif K.image_data_format() == 'channels_first':\n    # array indexing is x_train[image_index, color, y, x]\n    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)\n    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)\n    input_shape = (1, img_rows, img_cols)\nelse:\n    # array indexing is x_train[image_index, y, x, color]\n    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n    input_shape = (img_rows, img_cols, 1)\n\nprint('x_train shape:', x_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a30887bc70aa28319cce903d31a436107d79301b"},"cell_type":"code","source":"# convert class vectors to binary class matrices \n# this means that: (2 -> [0,0,1,0,0,0,0,0,0,0])\n# in other words: we want network to put probability 100% (value 1.0) on index associated with number 2\ny_train_matrix = keras.utils.to_categorical(y_train, num_classes)\ny_train_matrix[0, :]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9532db5bf00548f4dac2727a7e3e3c635ec0250e"},"cell_type":"code","source":"# split to train and validation set (remember, x_test is not validation set, but Kaggle test dataset without labels!)\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train_matrix, test_size = 0.1, random_state=42)\nprint(X_train.shape, X_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de3bc0a5c89b0b72235e50657a5cc2cfa597367b"},"cell_type":"code","source":"model = Sequential()\n\n# https://keras.io/layers/core/\nmodel.add(TODO ADD LAYERS (please use even number of neurons!))\n\n# https://keras.io/losses/\n# https://keras.io/optimizers/\nmodel.compile(loss='TODO_CHOOSE_LOSS',\n              optimizer=TODO_CHOOSE_OPTIMIZER,\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4657216fa0ce4e97aa22c3a578be1371f363ae72","_kg_hide-output":false},"cell_type":"code","source":"model.fit(X_train, Y_train,\n          batch_size=batch_size,\n          epochs=epochs,\n          verbose=1,\n          validation_data=(X_val, Y_val))\n\nscore = model.evaluate(X_val, Y_val, verbose=0)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74b42f2ecfad8ada335b422632eda3a97ed817ac"},"cell_type":"code","source":"# show N-th layer neuron weights (this is going to be nice espeically for network without hidden layer!)\nLAYER_INDEX = 1\nweights = model.get_layer(name=Dense, index=LAYER_INDEX).get_weights()\n\nx, y = 6, int(weights[0].shape[1] / 6)\nfor i in range(weights[0].shape[1]):  \n    plt.subplot(y, x, i+1)\n    plt.imshow(weights[0][:,i].reshape(28,28))\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a1920b9743ee8ea0e4addad432a428fa2b13890"},"cell_type":"code","source":"## Kaggle stuff\n\n#get the predictions for the test data\npredicted_classes = model.predict_classes(x_test)\n\n# create submission file\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(predicted_classes)+1)),\n                         \"Label\": predicted_classes})\n# save results\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)\n\n# save network\nmodel.save('my_awesome_model.h5')\njson_string = model.to_json()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}