diff --git a/ComputationalGraphs.ipynb b/ComputationalGraphs.ipynb
index 4eaef5f..8540e86 100644
--- a/ComputationalGraphs.ipynb
+++ b/ComputationalGraphs.ipynb
@@ -363,6 +363,692 @@
"# dot=tf_to_dot(tf.get_default_graph())\n"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Training loss after 0 iterations: 0.693686\n",
+ "Training loss after 100 iterations: 0.342306\n",
+ "Training loss after 200 iterations: 0.210373\n",
+ "Training loss after 300 iterations: 0.174988\n",
+ "Training loss after 400 iterations: 0.152505\n",
+ "Training loss after 500 iterations: 0.136287\n",
+ "Training loss after 600 iterations: 0.123976\n",
+ "Training loss after 700 iterations: 0.114264\n",
+ "Training loss after 800 iterations: 0.106370\n",
+ "Training loss after 900 iterations: 0.099801\n",
+ "Training loss after 1000 iterations: 0.094227\n",
+ "Training loss after 1100 iterations: 0.089424\n",
+ "Training loss after 1200 iterations: 0.085229\n",
+ "Training loss after 1300 iterations: 0.081525\n",
+ "Training loss after 1400 iterations: 0.078224\n",
+ "Training loss after 1500 iterations: 0.075257\n",
+ "Training loss after 1600 iterations: 0.072572\n",
+ "Training loss after 1700 iterations: 0.070125\n",
+ "Training loss after 1800 iterations: 0.067885\n",
+ "Training loss after 1900 iterations: 0.065823\n",
+ "Accuracy of the model is 0.93\n"
+ ]
+ }
+ ],
+ "source": [
+ "%matplotlib inline\n",
+ "from graphviz import Digraph\n",
+ "from IPython.core.display import display, SVG\n",
+ "\n",
+ "def tf_to_dot(graph):\n",
+ " dot = Digraph()\n",
+ "\n",
+ " for n in graph.as_graph_def().node:\n",
+ " name = n.name.split('/')[0]\n",
+ " dot.node(name, label=name)\n",
+ "\n",
+ " for src in n.input:\n",
+ " src = src.split('/')[0]\n",
+ " if src != name:\n",
+ " dot.edge(src, name)\n",
+ " display(SVG(dot._repr_svg_()))\n",
+ " return dot\n",
+ "\n",
+ "import numpy as np\n",
+ "import scipy.io\n",
+ "import matplotlib.pyplot as plt\n",
+ "import tensorflow as tf\n",
+ "\n",
+ "mat = scipy.io.loadmat('./FaceNonFace.mat')\n",
+ "\n",
+ "faces = np.rollaxis(mat[\"face\"].astype(np.uint8),-1,0)\n",
+ "non_faces = np.rollaxis(mat[\"nonFace\"].astype(np.uint8),-1,0)\n",
+ "\n",
+ "rand_idx = np.arange(0,faces.shape[0])\n",
+ "np.random.shuffle(rand_idx)\n",
+ "\n",
+ "train_test_split = 0.8\n",
+ "\n",
+ "face_split = np.int(train_test_split*faces.shape[0])\n",
+ "\n",
+ "train_faces = faces[rand_idx[:face_split]]\n",
+ "test_faces = faces[rand_idx[face_split:]]\n",
+ "\n",
+ "non_face_split = np.int(train_test_split*non_faces.shape[0])\n",
+ "train_non_faces = non_faces[rand_idx[:non_face_split]]\n",
+ "test_non_faces = non_faces[rand_idx[non_face_split:]]\n",
+ "\n",
+ "train_data = np.vstack([train_faces,train_non_faces])\n",
+ "train_labels = np.array([0]*len(train_faces)+[1]*len(train_non_faces)).astype(np.float32).reshape(-1,1)\n",
+ "\n",
+ "test_data = np.vstack([test_faces,test_non_faces])\n",
+ "test_labels = np.array([0]*len(test_faces)+[1]*len(test_non_faces)).astype(np.float32).reshape(-1,1)\n",
+ "\n",
+ "train_data = train_data.reshape(train_data.shape[0],-1).astype(np.float32)\n",
+ "test_data = test_data.reshape(test_data.shape[0],-1).astype(np.float32)\n",
+ "\n",
+ "# plt.imshow((train_data[1000].reshape(60,60,3)*255).astype(np.float32))\n",
+ "# plt.show()\n",
+ "\n",
+ "# tf.reset_default_graph()\n",
+ "def build_graph():\n",
+ " g = tf.Graph()\n",
+ " with g.as_default():\n",
+ " train_x = tf.placeholder(shape=[None,train_data.shape[1]],name=\"train_data\",dtype=np.float32)\n",
+ " train_y = tf.placeholder(shape=[None,1],name=\"train_label\",dtype=np.float32)\n",
+ "\n",
+ "\n",
+ " train_x1 = tf.div(train_x,255.0)-0.5\n",
+ "\n",
+ " learning_rate = tf.constant(0.05)\n",
+ "\n",
+ " weights = tf.Variable(tf.random_normal([train_data.shape[1],1],stddev=1e-3),name=\"weights\")\n",
+ " bias = tf.Variable(0.1,dtype=np.float32)\n",
+ "\n",
+ " h = tf.matmul(train_x1,weights)+bias\n",
+ " z = tf.sigmoid(h)+1e-6\n",
+ "\n",
+ " loss = -tf.reduce_mean(train_y*tf.log(z) + (1-train_y)*tf.log(1-z))\n",
+ " dw,db = tf.gradients(loss,[weights,bias])\n",
+ "\n",
+ " weights_update = tf.assign_add(weights,-learning_rate*dw,name='weight_update')\n",
+ " bias_update = tf.assign_add(bias,-learning_rate*db,name='bias_update')\n",
+ "\n",
+ " with tf.control_dependencies([weights_update,bias_update]):\n",
+ " train_op = tf.no_op()\n",
+ " tf_to_dot(g)\n",
+ " return (g,loss,train_op,train_x,train_y,z)\n",
+ "\n",
+ "(g,loss,train_op,train_x,train_y,z) = build_graph()\n",
+ "\n",
+ "with g.as_default():\n",
+ " with tf.Session() as sess:\n",
+ " sess.run(tf.global_variables_initializer())\n",
+ " for i in xrange(2000):\n",
+ " l,_ = sess.run([loss,train_op],feed_dict={train_x:train_data,train_y:train_labels})\n",
+ " if i%100 == 0:\n",
+ " print 'Training loss after %d iterations: %f'%(i,l)\n",
+ "\n",
+ " y_ = sess.run(z,feed_dict = {train_x:test_data})\n",
+ " y_ = y_ > 0.5\n",
+ " accuracy = np.sum((y_ == (test_labels > 0)),0)[0]/(y_.shape[0]*1.0)\n",
+ " print 'Accuracy of the model is ',accuracy\n"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -376,7 +1062,7 @@
"metadata": {
"kernelspec": {
"display_name": "Python 2",
- "language": "python2",
+ "language": "python",
"name": "python2"
},
"language_info": {
diff --git a/LinearAlgebra.ipynb b/LinearAlgebra.ipynb
index 8049e9a..e623eb8 100644
--- a/LinearAlgebra.ipynb
+++ b/LinearAlgebra.ipynb
@@ -488,7 +488,7 @@
"import numpy as np\n",
"import pickle\n",
"\n",
- "faces = pickle.load(open('Linear Algebra KB v1.0/faces.pkl'))\n",
+ "faces = pickle.load(open('faces.pkl'))\n",
"\n",
"(num_of_images,height,width,clr_channels) = faces.shape #First dimension shows the number of face images we have.\n",
"\n",
@@ -903,7 +903,7 @@
"import pickle\n",
"import matplotlib.pyplot as plt\n",
"\n",
- "faces = pickle.load(open('Linear Algebra KB v1.0/faces.pkl'))\n",
+ "faces = pickle.load(open('faces.pkl'))\n",
"\n",
"(num_of_images,height,width,clr_channels) = faces.shape \n",
"\n",
@@ -1058,7 +1058,7 @@
"metadata": {
"kernelspec": {
"display_name": "Python 2",
- "language": "python2",
+ "language": "python",
"name": "python2"
},
"language_info": {