{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Operations\n", "\n", "This function introduces various operations in TensorFlow\n", "\n", "Declaring Operations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import tensorflow as tf\n", "from tensorflow.python.framework import ops\n", "ops.reset_default_graph()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Open graph session" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sess = tf.Session()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic Operations\n", "TensorFlow has multiple types of arithmetic functions. Here we illustrate the differences between `div()`, `truediv()` and `floordiv()`.\n", "\n", "`div()` : integer of division (similar to base python `//`\n", "\n", "`truediv()` : will convert integer to floats.\n", "\n", "`floordiv()` : float of `div()`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "0.75\n", "0.0\n" ] } ], "source": [ "print(sess.run(tf.div(3,4)))\n", "print(sess.run(tf.truediv(3,4)))\n", "print(sess.run(tf.floordiv(3.0,4.0)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mod function:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0\n" ] } ], "source": [ "print(sess.run(tf.mod(22.0,5.0)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cross Product:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0. 1.]\n" ] } ], "source": [ "print(sess.run(tf.cross([1.,0.,0.],[0.,1.,0.])))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trig functions\n", "\n", "Sine, Cosine, and Tangent:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-7.23998e-06\n", "-1.0\n", "1.0\n" ] } ], "source": [ "print(sess.run(tf.sin(3.1416)))\n", "print(sess.run(tf.cos(3.1416)))\n", "print(sess.run(tf.div(tf.sin(3.1416/4.), tf.cos(3.1416/4.))))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom operations\n", "\n", "Here we will create a polynomial function:\n", "\n", "`f(x) = 3 * x^2 - x + 10`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "362\n" ] } ], "source": [ "test_nums = range(15)\n", "\n", "def custom_polynomial(x_val):\n", " # Return 3x^2 - x + 10\n", " return(tf.subtract(3 * tf.square(x_val), x_val) + 10)\n", "\n", "print(sess.run(custom_polynomial(11)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What should we get with list comprehension:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 12, 20, 34, 54, 80, 112, 150, 194, 244, 300, 362, 430, 504, 584]\n" ] } ], "source": [ "expected_output = [3*x*x-x+10 for x in test_nums]\n", "print(expected_output)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TensorFlow custom function output:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "12\n", "20\n", "34\n", "54\n", "80\n", "112\n", "150\n", "194\n", "244\n", "300\n", "362\n", "430\n", "504\n", "584\n" ] } ], "source": [ "for num in test_nums:\n", " print(sess.run(custom_polynomial(num)))" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }