{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.3.0\n" ] } ], "source": [ "print(tf.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Running first tensor graph" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "x = tf.Variable(3, name='x')\n", "y = tf.Variable(4, name='4')\n", "\n", "z = x*x*y+ y + 2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "sess = tf.Session()\n", "\n", "sess.run(x.initializer)\n", "sess.run(y.initializer)\n", "res = sess.run(z)\n", "print(res)\n", "\n", "sess.close()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "# another way to write the same run code\n", "\n", "with tf.Session() as sess:\n", " x.initializer.run()\n", " y.initializer.run()\n", " \n", " res = z.eval()\n", " print(res) #session will close automatically" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "# instead of running initializer everytime, we can add a initialize node in our computation graph\n", "\n", "init = tf.global_variables_initializer()\n", "\n", "with tf.Session() as sess:\n", " init.run()\n", " print(z.eval())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }