{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import numpy.random as rng\n", "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 1000\n", "din = np.random.randint(0,2, (n, 2)).astype(bool)\n", "out = np.logical_xor(din[:,0], din[:,1])\n", "\n", "out = np.zeros((n, 2), dtype=\"bool\")\n", "\n", "out[:,1] = np.logical_xor(din[:,0], din[:,1])\n", "out[:,0] = True ^ out[:,1]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = tf.placeholder(\"bool\", shape=[None,2])\n", "x1 = tf.cast(x, \"float\")\n", "\n", "with tf.name_scope('Layer1'):\n", " W1 = tf.Variable(tf.random_normal([2,10]))\n", " b1 = tf.Variable(tf.random_normal([10]))\n", " hidden = tf.nn.relu(tf.matmul(x1, W1) + b1)\n", " \n", " \n", "with tf.name_scope('Layer2'):\n", " W2 = tf.Variable(tf.random_normal([10,2]))\n", " b2 = tf.Variable(tf.random_normal([2]))\n", " hidden1 = tf.matmul(hidden, W2) + b2\n", "\n", "with tf.name_scope('output'):\n", " y = tf.nn.softmax(hidden1)\n", " y_val = tf.arg_max(y,1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y_ = tf.placeholder(\"float\",[None,2])\n", "\n", "loss = tf.reduce_sum(-y_*tf.log(y))\n", "init = tf.initialize_all_variables()\n", "optimizer = tf.train.GradientDescentOptimizer(.001).minimize(loss)\n", "\n", "\n", "tf.scalar_summary(\"cost\", loss)\n", "\n", "merged = tf.merge_all_summaries()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3329.73\n", "Iteration 0 Loss 3329.73\n", "Iteration 10 Loss 277.95\n", "Iteration 20 Loss 126.06\n", "Iteration 30 Loss 72.0056\n" ] } ], "source": [ "fd = {x: din, y_:out}\n", "with tf.Session() as sess:\n", " sess.run(init)\n", " summary_writer = tf.train.SummaryWriter(\"./logs/run1\")\n", " summary_writer.add_graph(y.graph)\n", " \n", " print(sess.run(loss, feed_dict=fd))\n", " \n", " \n", " \n", " for step in range(40):\n", " summ, cost_curr, _ = sess.run([merged, loss, optimizer], feed_dict={x: din, y_: out})\n", " if step % 10 == 0 : print(\"Iteration\", step, \"Loss\", cost_curr)\n", " \n", " summary_writer.add_summary(summ, step)\n", " \n", " \n", "# print(sess.run(y_val, feed_dict={x:[[0,0], [1,1], [1,0], [0,1]]}))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }