{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7f22bb91853a35b363e7cbfbf23ee4f7c03fb5c"},"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer/train.csv')\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c362c44fa33c8c021e37d435900ba57fa5daa6c0"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e99fe2ef161f8389f6df1581c835b94e2bd9021"},"cell_type":"code","source":"train_df = df.sample(frac=0.8,random_state=200)\ntest_df = df.drop(train_df.index)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be812250e39d4ac5835cb84e3dba3e5bce724c43"},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a244875358f3df68cbfa9460d3ab9401e8d1234f"},"cell_type":"code","source":"import math\ndef random_mini_batches(X, Y, mini_batch_size = 128, seed = 0):\n\n    m = X.shape[0]                  # number of training examples\n    mini_batches = []\n    np.random.seed(seed)\n    \n    # Step 1: Shuffle (X, Y)\n    permutation = list(np.random.permutation(m))\n    shuffled_X = X[permutation,:]\n    shuffled_Y = Y[permutation,:]\n\n    # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.\n    num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning\n    for k in range(0, num_complete_minibatches):\n        mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:]\n        mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:]\n        mini_batch = (mini_batch_X, mini_batch_Y)\n        mini_batches.append(mini_batch)\n    \n    # Handling the end case (last mini-batch < mini_batch_size)\n    if m % mini_batch_size != 0:\n        mini_batch_X = shuffled_X[num_complete_minibatches * mini_batch_size : m,:]\n        mini_batch_Y = shuffled_Y[num_complete_minibatches * mini_batch_size : m,:]\n        mini_batch = (mini_batch_X, mini_batch_Y)\n        mini_batches.append(mini_batch)\n    \n    return mini_batches\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f3cd05c016f31a545420f5fe102ad3c9def86dc"},"cell_type":"code","source":"import tensorflow as tf \nlabel_train = train_df['label'].values\ntrain_df = train_df.drop(['label'],axis=1)\ntrain_x = train_df.as_matrix()/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d11a621b377a179aab4e64f524ea19f9034d6a0"},"cell_type":"code","source":"label_test = test_df['label'].values\ntest_df = test_df.drop(['label'],axis=1)\ntest_x = test_df.as_matrix()/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"985a9d3046f30622f58a724f72667cccf711229c"},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nenc = OneHotEncoder(sparse=False)\ntrain_y = enc.fit_transform(label_train.reshape(-1,1),)\ntrain_y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"eaf6cc6445ef196d6b62d0763a564ee08fdfd10d"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom scipy.misc import imread,imresize\nimg = []\nimg.append(imread('../input/digit1/digit1.jpg'))\nimg.append(imread('../input/digit1/digit2.jpg'))\nimg.append(imread('../input/digit1/digit3.jpg'))\nfor i in range(3):\n    img[i] = imresize(img[i],(28,28))\n    img[i] = (255 - np.mean(img[i],axis=2))/255\nplt.subplot(131)\nplt.imshow(img[0],cmap='gray')\nplt.subplot(132)\nplt.imshow(img[1],cmap='gray')\nplt.subplot(133)\nplt.imshow(img[2],cmap='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89bedcc1218d672c906373a24773881ca2d76754"},"cell_type":"code","source":"def rbf(x,y,sigma):\n    z = -np.sum((x-y)*(x-y))/(2*sigma)\n    return np.exp(z)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb00d6190bbb84b6d953b307e361346d551527f0"},"cell_type":"code","source":"cl_cen = train_x[np.random.choice(33600,784,replace=False)]\ncl_cen.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e3927fa4e28e7f64f4d1be40b6fdfd0138aa3e4"},"cell_type":"code","source":"rbf(train_x[0],cl_cen[0],100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"94f4b21f96603a8783e5fc1acc730b0a391cfb5d","_kg_hide-output":true},"cell_type":"code","source":"train_rbf = np.zeros([33600,784])\nfor i in range(33600):\n    for j in range(784):\n        train_rbf[i,j] = rbf(train_x[i],cl_cen[j],100)\n    print(i)\ntrain_rbf","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"38ad9325e01acb25ef9fc7e89292b9aef533f732"},"cell_type":"code","source":"test_rbf = np.zeros([8400,784])\nfor i in range(8400):\n    for j in range(784):\n        test_rbf[i,j] = rbf(test_x[i],cl_cen[j],100)\n    print(i)\ntest_rbf","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"# Initialize placeholders \nX = tf.placeholder(dtype = tf.float32, shape = [None,784])\ny = tf.placeholder(dtype = tf.int32, shape = [None,10])\n\n\n# the output layer or NN\noutput_layer = tf.contrib.layers.fully_connected(X,10,tf.nn.softmax)\npred_layer = tf.argmax(output_layer,1)\n\n# cost function \ncost = tf.reduce_mean(tf.losses.softmax_cross_entropy(y,output_layer))\n\n# optimizer\noptimizer = tf.train.AdamOptimizer().minimize(cost)\n\ninit = tf.global_variables_initializer()\n\nwith tf.Session() as sess:\n    sess.run(init)\n    total_cost = 0\n    \n    minibatches = random_mini_batches(train_rbf,train_y)\n    for epochs in range(150):\n        for minibatch in minibatches:\n            (mini_x,mini_y) = minibatch\n            mini_cost,_ = sess.run([cost,optimizer],feed_dict={X:mini_x,y:mini_y})\n            total_cost = total_cost+mini_cost\n        print('epoch ',epochs,' : ',total_cost/((epochs+1)*len(minibatches)))\n    \n    pred_train = pred_layer.eval({X:train_rbf})\n    train_acc = np.sum(pred_train==label_train)/label_train.shape[0]\n    print('train accuracy : '+str(train_acc))\n    \n    pred_test = pred_layer.eval({X:test_rbf})\n    test_acc = np.sum(pred_test==label_test)/label_test.shape[0]\n    print('test accuracy : '+str(test_acc))\n    \n    pred = []\n    for i in range(3):\n        pred.append(pred_layer.eval({X:img[i].reshape(1,-1)}))\n    print(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"d31f1b3554b1036d80bede2249d17da8e380b16f"},"cell_type":"code","source":"plt.subplot(131)\nplt.imshow(img[0],cmap='gray')\nplt.subplot(132)\nplt.imshow(img[1],cmap='gray')\nplt.subplot(133)\nplt.imshow(img[2],cmap='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"46176306dd8120ac6fedfbee3573ec1519a3496e"},"cell_type":"code","source":"pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b0aab5d0bfcbfa1cdd8d7ca865a657662e40cd4","collapsed":true},"cell_type":"code","source":"img[0][img[0]>25] = 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33d3aa2c9a47ed91ade4fdf7b0b08c1e581e05f5","collapsed":true},"cell_type":"code","source":"plt.imshow(img[0].reshape(28,28),cmap='gray')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fab583b423f347984c32c6ac9eabbe7094fe9ad4"},"cell_type":"code","source":"np.save('train_rbf.npy',train_rbf)\nnp.save('test_rbf.npy',test_rbf)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdc69f5bfcb942726692bd0d59f065e2e85df790","collapsed":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b451c32c2d3dbbe026472f2424f52ebad13a230"},"cell_type":"code","source":"","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}