{"metadata": {"kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}, "language_info": {"version": "3.6.1", "nbconvert_exporter": "python", "name": "python", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "pygments_lexer": "ipython3", "file_extension": ".py"}}, "cells": [{"metadata": {"trusted": false, "_execution_state": "idle", "_uuid": "e8f2c2314a76ce646a5516c4affbd6817a6d6fe1", "_cell_guid": "27a4a8f4-5930-400e-a02e-576802244d92", "collapsed": true}, "outputs": [], "execution_count": null, "source": "#typed out from the python TensorFlow workbook\nimport numpy as np\nimport pandas as pd\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\n\nimport tensorflow as tf\n\n# settings\nLEARNING_RATE = 1e-4\n# set to 20000 on local environment to get 0.99 accuracy\nTRAINING_ITERATIONS = 2500        \n    \nDROPOUT = 0.5\nBATCH_SIZE = 50\n\n# set to 0 to train on all available data\nVALIDATION_SIZE = 2000\n\n# image number to output\nIMAGE_TO_DISPLAY = 10\n# read training data from CSV file \ndata = pd.read_csv(r'../input/train.csv')\n\nprint('data({0[0]},{0[1]})'.format(data.shape))\nprint (data.head())\nimages = data.iloc[:,1:].values\nimages = images.astype(np.float)\n\n# convert from [0:255] => [0.0:1.0]\nimages = np.multiply(images, 1.0 / 255.0)\n\n#print('images({0[0]},{0[1]})'.format(images.shape))\nimage_size = images.shape[1]\n#print ('image_size => {0}'.format(image_size))\n\n# in this case all images are square\nimage_width = image_height = np.ceil(np.sqrt(image_size)).astype(np.uint8)\n\n#print ('image_width => {0}\\nimage_height => {1}'.format(image_width,image_height))\ndef display(img):\n    \n    # (784) => (28,28)\n    one_image = img.reshape(image_width,image_height)\n    \n    plt.axis('off')\n    plt.imshow(one_image, cmap=cm.binary)\n\n# output image     \nlabels_flat = data[['label']].values.ravel()\n\nprint('labels_flat({0})'.format(len(labels_flat)))\nprint ('labels_flat[{0}] => {1}'.format(IMAGE_TO_DISPLAY,labels_flat[IMAGE_TO_DISPLAY]))\ndef dense_to_one_hot(labels_dense,num_classes):\n    num_labels = labels_dense.shape[0]\n    index_offset = np.arange(num_labels)*num_classes\n    labels_one_hot = np.zeros((num_labels,num_classes))\n    labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1\n    return labels_one_hot\n\nlabels_count = np.unique(labels_flat).shape[0]\n\nprint('labels_count => {0}'.format(labels_count))\n\nlabels = dense_to_one_hot(labels_flat,labels_count)\nlabels = labels.astype(np.uint8)\nprint('labels({0[0]},{0[1]})'.format(labels.shape))\nprint ('labels[{0}] => {1}'.format(IMAGE_TO_DISPLAY,labels[IMAGE_TO_DISPLAY]))\n\nvalidation_images = images[:VALIDATION_SIZE]\nvalidation_labels = labels[:VALIDATION_SIZE]\n\ntrain_images = images[VALIDATION_SIZE:]\ntrain_labels = labels[VALIDATION_SIZE:]\nprint('train_images({0[0]},{0[1]})'.format(train_images.shape))\nprint('test_images({0[0]},{0[1]})'.format(validation_images.shape))\n\ndef weight_variable(shape):\n    initial = tf.truncated_normal(shape,stddev=0.1)\n    return tf.Variable(initial)\ndef bias_variable(shape):\n    initial = tf.constant(0.1,shape = shape)\n    return tf.Variable(initial)\ndef conv2d(x,W):\n    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding = 'SAME')\ndef max_pool_2x2(x):\n    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')\nx = tf.placeholder('float',shape=[None,image_size])\ny_ = tf.placeholder('float',shape=[None,labels_count])\n\nW_conv1 = weight_variable([5, 5, 1, 32])\nb_conv1 = bias_variable([32])\nimage = tf.reshape(x,[-1,image_width,image_height,1])\nh_conv1 = tf.nn.relu(conv2d(image,W_conv1) + b_conv1)\nh_pool1 = max_pool_2x2(h_conv1)\nlayer1 = tf.reshape(h_conv1,(-1,image_height,image_width,4,8))\nlayer1 = tf.reshape(layer1,(0,3,1,4,2))\nlayer1 = tf.reshape(layer1,(-1,image_height*4,image_width*4))\nW_conv2 = weight_variable([5,5,32,64])\nb_conv2 = bias_variable([64])\nh_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)\nh_pool2 = max_pool_2x2(h_conv2)\nlayer2 = tf.reshape(h_conv2,(-1,14,14,4,16))\nlayer2 = tf.transpose(layer2,(0,3,1,4,2))\nlayer2 = tf.reshape(layer2,(-1,14*4,14*16))\nW_fc1 = weight_variable([7*7*64,1024])\nb_fc1 = bias_variable([1024])\nh_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])\nh_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)\nkeep_prob = tf.placeholder('float')\nh_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)\nW_fc2 = weight_variable([1024,labels_count])\nb_fc2 = bias_variable([labels_count])\n\ny = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)\ncross_entropy = -tf.reduce_sum(y*tf.log(y))\ntrain_step = tf.train.AdamOptimizer(LEARNING_RATE).minimize(cross_entropy)\ncorrect_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\naccuracy = tf.reduce_mean(tf.cast(correct_prediction,'float'))\nprint(accuracy)\npredict = tf.argmax(y,1)\nepochs_completed = 0\nindex_in_epoch = 0\nnum_examples = train_images.shape[0]\ndef next_batch(batch_size):\n    global train_images\n    global train_labels\n    global index_in_epoch\n    global epochs_completed\n    start = index_in_epoch\n    index_in_epoch += batch_size\n    if index_in_epoch > num_examples:\n        # finished epoch\n        epochs_completed += 1\n        # shuffle the data\n        perm = np.arange(num_examples)\n        np.random.shuffle(perm)\n        train_images = train_images[perm]\n        train_labels = train_labels[perm]\n        # start next epoch\n        start = 0\n        index_in_epoch = batch_size\n        assert batch_size <= num_examples\n    end = index_in_epoch\n    return train_images[start:end], train_labels[start:end]\n\ninit = tf.initialize_all_variables()\nsess = tf.InteractiveSession()\nsess.run(init)\n\ntrain_accuracies = []\nvalidation_accuracies = []\nx_range = []\n\ndisplay_step = 1\nfor i in range(TRAINING_ITERATIONS):\n    batch_xs,batch_ys = next_batch(BATCH_SIZE)\n    if i % display_step == 0 or (i+1) == TRAINING_ITERATIONS:\n        train_accuracy = accuracy.eval(feed_dict={x:batch_xs,y_:batch_ys,keep_prob:1})\n        if(VALIDATION_SIZE):\n            validation_accuracy = accuracy.eval(feed_dict={x:validation_images[0:BATCH_SIZE],y_:validation_labels[0:BATCH_SIZE],keep_prob:1})\n            print('training_accuracy / validation accuracy => %.2f / %.2f for step %d'%(train_accuracy,validation_accuracy,i))\n            validation_accuracies.append(validation_accuracy)\n        else:\n            print('training_accuracy => %.4f for step %d'%(train_accuracy,i))\n        train_accuracies.append(train_accuracy)\n        x_range.append(i)\n        \n        if i%(display_step*10) == 0 and i:\n            display_step *= 10\n        sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys,keep_prob:DROPOUT})", "cell_type": "code"}, {"metadata": {"trusted": false, "_uuid": "43badb684f3fe4b94d5979cc5e802a3328e952de", "_cell_guid": "39d92291-6c43-4fae-8a1e-8baab5e6d680", "collapsed": true}, "outputs": [], "execution_count": null, "source": "", "cell_type": "code"}], "nbformat": 4, "nbformat_minor": 1}