{"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 all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"import pandas as pd\nsample_submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Code Essence Provided by professor Sungwoo Kim of SNU\n\nimport numpy as np\n\nclass perceptron:\n    def __init__(self, input_dim, hidden_dim, output_dim, lr=0.01):\n        self.w1 = np.random.normal(0.0, pow(hidden_dim, -0.5), (input_dim, hidden_dim))\n        self.w2 = np.random.normal(0.0, pow(hidden_dim, -0.5), (hidden_dim, output_dim))\n        self.h = np.zeros((1, hidden_dim))\n        self.lr = lr\n        self.input_dim = input_dim\n        self.hidden_dim = hidden_dim\n        self.output_dim = output_dim\n        self.theta = 0\n\n    def softmax(self, x):\n        e_x = np.exp(x-np.max(x))\n        return e_x / e_x.sum(axis=0)\n\n    def sigmoid(self, x):\n        return 1 / (1+np.exp(-x))\n\n    def relu(self, x):\n        return x * (x > 0)\n\n    def relu_prime(self, x):\n        return (x>0).astype(x.dtype)\n\n    def tanh(self, x):\n        return (np.exp(x)-np.exp(-x)) / (np.exp(x) + np.exp(-x))\n\n    def feedforward(self, x):\n        a = x.astype(float)\n        b = self.w1.astype(float)\n        self.h = self.relu(np.dot(a,b))\n        return self.relu(np.dot(self.h, self.w2))\n\n\n    def bprop_w2(self, g, y):\n        q = (-2)*(g-y)*self.relu_prime(np.dot(self.h, self.w2))\n        return np.dot(self.h.reshape(self.hidden_dim, 1), q.reshape(1, self.output_dim))\n\n    def bprop_w1(self, g, y, x):\n        q1 = (-2)*(g-y)*self.relu_prime(np.dot(self.h, self.w2))\n        q2 = np.dot(self.w2, q1)\n        q3 = q2*self.relu_prime(np.dot(x, self.w1))\n        return np.dot(x.reshape(self.input_dim, 1), q3.reshape(1, self.hidden_dim))\n    \n    def training(self, input, target):\n        x = np.array(input).T\n        y = self.feedforward(x)\n        g = np.array(target).T\n\n        self.w1 = self.w1 - self.lr * self.bprop_w1(g, y, x)    #### w1 먼저 update 해주어야 함!!!\n        self.w2 = self.w2 - self.lr * self.bprop_w2(g, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#### Hyperparameter ####\n\ninput_dim = 784\nhidden_dim = 300\noutput_dim = 10\nepoch = 20\n\ntrain_label = train['label']\ntrain_pixel = train.drop(columns=['label'])\n\ndef input_norm(x):\n    return x/255.0*0.99 + 0.01\n\ntrain_targets = pd.get_dummies(train_label)\ntrain_targets = train_targets.replace(0, 0.01).replace(1, 0.99)\n\ntrain_pixel = train_pixel.applymap(input_norm)\n\ntrain_targets.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_pixel.sample(3)[['pixel200', 'pixel300', 'pixel400', 'pixel500', 'pixel600']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pct = perceptron(input_dim, hidden_dim, output_dim, lr=0.01)\n\nfor k in range(epoch):\n    print(\"{0}% training in process\".format(k*100/epoch))\n    for i, v in train_pixel.iterrows():\n        input_ = v\n        target_ = train_targets.iloc[i]\n        pct.training(input_, target_)\n\n        \ntest_pixel = test.applymap(input_norm)\n        \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction = list()\nfor i,v in test_pixel.iterrows():\n\n    prediction_list = pct.feedforward(v)\n    prediction.append(np.argmax(prediction_list))\n\nprediction[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = sample_submission.drop(columns=['Label'])\nsubmission['Label'] = prediction\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('sub1_0130.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}