{"cells":[{"metadata":{"trusted":true,"_uuid":"ac9c669de2609be1ced9de78783b4e7c91f40cef","_kg_hide-output":true,"_kg_hide-input":true},"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport numpy as np\nfrom sklearn.preprocessing import OneHotEncoder\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34e7d340edbabc1a3488bec0b4d3dd091d3b7e10"},"cell_type":"code","source":"data_dir = r'../input/train.csv'\n# mnist = input_data.read_data_sets(data_dir, one_hot=True)\ntrain_set = pd.read_csv(data_dir)\ntest_set = pd.read_csv('../input/test.csv')\nprint(train_set.head())\nprint(test_set.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54c5befd7f8a9a9045f5a4fc0cffcbcc7aeb9ef5"},"cell_type":"code","source":"y_train = pd.get_dummies((train_set['label']))\nx_train = pd.get_dummies(train_set.drop(labels=['label'], axis=1))\n# y_test = pd.get_dummies((test_set['label']))\ntest = pd.get_dummies(test_set)\n\nprint(x_train.head())\nprint(y_train.head()) \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11c6727fc181d493efef7a751b7ba2fafc991646"},"cell_type":"code","source":"in_x = 784\nw1_num = 512\nw2_num = 216\nout = 10\nlearn_rate = 0.001\nalpha = 0.001","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c1d642d1b8efa5585db00b150dfb0a98fc95620"},"cell_type":"code","source":"pred = tf.placeholder(tf.float32, [None, 10])\nx = tf.placeholder(tf.float32, [None, in_x])\n# help(tf.initializers.random_normal)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b2cf3da90d5fdba112ec43e9cbc18546d220c62"},"cell_type":"code","source":"weight = {\n    'w1': tf.Variable(tf.random_normal([in_x, w1_num], stddev=tf.sqrt(1/w1_num))),\n    'w2': tf.Variable(tf.random_normal([w1_num, w2_num], stddev=tf.sqrt(1/w2_num))),\n    'out': tf.Variable(tf.random_normal([w2_num, out], stddev=tf.sqrt(1/out)))\n}\nbias = {\n    'b1': tf.Variable(tf.random_normal([w1_num], stddev=tf.sqrt(1/w1_num))),\n    'b2': tf.Variable(tf.random_normal([w2_num], stddev=tf.sqrt(1/w2_num))),\n    'out': tf.Variable(tf.random_normal([out], stddev=tf.sqrt(1/out)))\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ca4e29b49fa529e634b3497fc91b2da695fe2ef"},"cell_type":"code","source":"o_1 = tf.nn.relu(tf.matmul(x, weight['w1']) + bias['b1'])\no_2 = tf.nn.relu(tf.matmul(o_1, weight['w2']) + bias['b2'])\ny = tf.matmul(o_2, weight['out']) + bias['out']\n# tf.summary.histogram('weight_out', weight['out'])\n# tf.summary.histogram('bias_out', bias['out'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a859eac4c28d2cd34664f9396bf186e07e702ac"},"cell_type":"code","source":"l2 = alpha * tf.square(tf.reduce_sum(weight['w1'])+tf.reduce_sum(weight['w2'])+tf.reduce_sum(weight['out']))\nloss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=pred, logits=y) + l2)\n# tf.summary.scalar('loss', loss)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3590977e827f2c6f649df2640c0b42dee7c4decc"},"cell_type":"code","source":"train_step = tf.train.AdamOptimizer(learn_rate).minimize(loss)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"640e08d5cfa42d51c9329b3593a8fb4ae4d8fa79"},"cell_type":"code","source":"init = tf.global_variables_initializer()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75a06299d8fb3613280dc95eeef96406be4344d9"},"cell_type":"code","source":"epoch = 100\nbatch_size = 128\nenx = OneHotEncoder()\neny = OneHotEncoder()\neny.fit_transform(train_set[['label']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f56a9ff7cbb4a6edc491b02b26699b7aa3506fc"},"cell_type":"code","source":"with tf.Session() as sess:\n    sess.run(init)\n#     merged = tf.summary.merge_all()\n#     writer = tf.summary.FileWriter(r'./logs/lastdemo', sess.graph)\n    for i in range(epoch):\n        batch_num = int(x_train.shape[0] / batch_size)\n        for j in range(batch_num):\n            batch = train_set.sample(n=batch_size)\n            batch_ys = eny.transform(batch[['label']]).toarray()\n            batch_xs = np.array(batch.drop(labels='label', axis=1))\n            _, a = sess.run([train_step, loss], feed_dict={x: batch_xs, pred: batch_ys})\n            if j % 50 == 0:\n                print('训练第{0}次, loss:={1}'.format(j,a))\n#                 writer.add_summary(merged_summary, i*batch_num+j)\n        if (i) % 1 == 0:\n            train_accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(pred, 1)), tf.float32))\n#             test_accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), tf.argmax(pred, 1)), tf.float32))\n            print('{0}次\\nloss:{1}\\ntrain_acc:{2}\\n:'.format(i+1, a, sess.run(train_accuracy, feed_dict={x: x_train, pred: y_train}))), \n#     writer.close()\n                \n    # predict:\n    mytest_batch = train_set.sample(n=500)\n    mytest_x, mytest_y= sess.run([x,y], feed_dict={x: np.array(mytest_batch.drop(labels='label', axis=1))})\n    mytest_y = np.argmax(mytest_y, 1)\n#     print(mytest_y)\n    mytest_pred = np.array(mytest_batch['label'])\n#     print(mytest_pred)\n\n    print(mytest_x.shape)\n    wrong_index = np.where(mytest_y != mytest_pred)[0]\n    print(wrong_index)\n    fig = plt.figure(figsize=(84/4, len(wrong_index)/4))\n    for i in enumerate(wrong_index):\n        ax = fig.add_subplot(3, 15, i[0]+1)\n        ax.imshow(mytest_x[i[1]].reshape((28, 28)))\n        ax.set_title('pred %d\\nlabel%d' % (mytest_y[i[1]], mytest_pred[i[1]]))\n    plt.show()\n    \n    \n    \n    pred_test = sess.run(y, feed_dict={x: test_set})\n    pred_test = tf.argmax(pred_test, 1).eval()\n    res = pd.Series(pred_test, name=\"Label\")\n    imgid = pd.Series(range(1, 28001), name=\"ImageId\")\n    submission = pd.concat([imgid, res], axis=1)\n    print(submission[:10])\n    submission.to_csv(\"mnist_sub.csv\",index=False)\n    \n            ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e03a932923edaa483850c03ad73c1a5f7845f63f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b54081cfb89e0e6095057439d7a1c5885b0cb721"},"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}