{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Math\n", "- https://www.tensorflow.org/api_docs/python/math_ops/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 0. Loading TF and Fixtures" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tensorflow as tf\n", "sess = tf.InteractiveSession()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def namestr(obj, namespace):\n", " return [name for name in namespace if namespace[name] is obj][0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "======Fixture=======\n", "Tensor Matrix - m1\n", "Shape: (1, 2)\n", "[[ 1. 2.]]\n", "\n", "Tensor Matrix - m2\n", "Shape: (2, 1)\n", "[[ 3.]\n", " [ 4.]]\n", "\n", "Tensor Matrix - m3\n", "Shape: (1, 2, 3)\n", "[[[ 1. 2. 3.]\n", " [ 4. 5. 6.]]]\n", "\n", "Tensor Matrix - m4\n", "Shape: (2, 2, 3)\n", "[[[ 1. 2. 3.]\n", " [ 4. 5. 6.]]\n", "\n", " [[ 7. 8. 9.]\n", " [ 10. 11. 12.]]]\n", "\n", "Tensor Matrix - m5\n", "Shape: (1, 2, 2)\n", "[[[ 1. 2.]\n", " [ 3. 4.]]]\n", "\n", "Tensor Matrix - m6\n", "Shape: (1, 3)\n", "[[ 3. 4. 5.]]\n", "\n", "====================\n" ] } ], "source": [ "m1 = tf.constant(value = [[1., 2.]])\n", "m2 = tf.constant(value = [[3.],[4.]])\n", "m3 = tf.constant(value = [[[1., 2., 3.], [4., 5., 6.]]])\n", "m4 = tf.constant(value = [[[1., 2., 3.], [4., 5., 6.]], [[7., 8., 9.] ,[10., 11., 12.]]])\n", "m5 = tf.constant(value = [[[1., 2.], [3., 4.]]])\n", "m6 = tf.constant(value = [[3., 4., 5.]])\n", "\n", "def printFixture(isShapeOut, tensorMatrixList):\n", " print \"======Fixture=======\"\n", " \n", " for m in tensorMatrixList:\n", " print \"Tensor Matrix - \" + namestr(m, globals())\n", " if (isShapeOut):\n", " print \"Shape:\", m.get_shape()\n", " print m.eval()\n", " print\n", " \n", " print \"====================\"\n", "\n", "printFixture(True, (m1, m2, m3, m4, m5, m6)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Arithmetic Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### - tf.add(x, y, name=None)\n", "- https://www.tensorflow.org/versions/master/api_docs/python/math_ops/arithmetic_operators#add" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "======Fixture=======\n", "Tensor Matrix - m1\n", "Shape: (1, 2)\n", "[[ 1. 2.]]\n", "\n", "Tensor Matrix - m2\n", "Shape: (2, 1)\n", "[[ 3.]\n", " [ 4.]]\n", "\n", "====================\n", "[[ 4. 5.]\n", " [ 5. 6.]]\n", "[[ 4. 5.]\n", " [ 5. 6.]]\n" ] } ], "source": [ "printFixture(True, (m1, m2))\n", "r1 = tf.add(m1, m2)\n", "print r1.eval()\n", "r2 = m1 + m2\n", "print r2.eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### - tf.multiply(x, y, name=None)\n", "- https://www.tensorflow.org/api_docs/python/tf/multiply" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "======Fixture=======\n", "Tensor Matrix - m1\n", "Shape: (1, 2)\n", "[[ 1. 2.]]\n", "\n", "Tensor Matrix - m2\n", "Shape: (2, 1)\n", "[[ 3.]\n", " [ 4.]]\n", "\n", "====================\n", "[[ 3. 6.]\n", " [ 4. 8.]]\n", "[[ 3. 6.]\n", " [ 4. 8.]]\n", "[[ 3. 6.]\n", " [ 4. 8.]]\n" ] } ], "source": [ "printFixture(True, (m1, m2))\n", "r1 = tf.multiply(m1, m2)\n", "print r1.eval()\n", "r2 = tf.multiply(m1, m2)\n", "print r2.eval()\n", "r3 = m1 * m2\n", "print r3.eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Matrix Math Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### - tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None)\n", "- https://www.tensorflow.org/api_docs/python/tf/matmul" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "======Fixture=======\n", "Tensor Matrix - m1\n", "Shape: (1, 2)\n", "[[ 1. 2.]]\n", "\n", "Tensor Matrix - m2\n", "Shape: (2, 1)\n", "[[ 3.]\n", " [ 4.]]\n", "\n", "====================\n", "[[ 11.]]\n", "[[ 3. 6.]\n", " [ 4. 8.]]\n" ] } ], "source": [ "printFixture(True, (m1, m2))\n", "r1 = tf.matmul(a = m1, b = m2) #(1, 2) x (2, 1)\n", "print r1.eval()\n", "r2 = tf.matmul(a = m2, b = m1) #(2, 1) x (1, 2)\n", "print r2.eval()" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:ipykernel_py2]", "language": "python", "name": "conda-env-ipykernel_py2-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 0 }