{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tensor Manipulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# https://www.tensorflow.org/api_guides/python/array_ops\n", "import tensorflow as tf\n", "import numpy as np\n", "import pprint\n", "tf.set_random_seed(777) # for reproducibility\n", "\n", "pp = pprint.PrettyPrinter(indent=4)\n", "sess = tf.InteractiveSession()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple Array" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([ 0., 1., 2., 3., 4., 5., 6.])\n", "(7,) 1\n", "0.0 1.0 6.0\n", "[ 2. 3. 4.] [ 4. 5.]\n", "[ 0. 1.] [ 3. 4. 5. 6.]\n" ] } ], "source": [ "t = np.array([0., 1., 2., 3., 4., 5., 6.])\n", "pp.pprint(t)\n", "print(t.ndim) # rank\n", "print(t.shape) # shape\n", "print(t[0], t[1], t[-1])\n", "print(t[2:5], t[4:-1])\n", "print(t[:2], t[3:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2D Array" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array([[ 1., 2., 3.],\n", " [ 4., 5., 6.],\n", " [ 7., 8., 9.],\n", " [ 10., 11., 12.]])\n", "2\n", "(4, 3)\n" ] } ], "source": [ "t = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.]])\n", "pp.pprint(t)\n", "print(t.ndim) # rank\n", "print(t.shape) # shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shape, Rank, Axis" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([4], dtype=int32)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = tf.constant([1,2,3,4])\n", "tf.shape(t).eval()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 2], dtype=int32)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = tf.constant([[1,2],\n", " [3,4]])\n", "tf.shape(t).eval()" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4], dtype=int32)" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = tf.constant([[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]])\n", "tf.shape(t).eval()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],\n", " [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\n", " [\n", " [\n", " [1,2,3,4], \n", " [5,6,7,8],\n", " [9,10,11,12]\n", " ],\n", " [\n", " [13,14,15,16],\n", " [17,18,19,20], \n", " [21,22,23,24]\n", " ]\n", " ]\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matmul VS multiply" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 12.]], dtype=float32)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix1 = tf.constant([[3., 3.]])\n", "matrix2 = tf.constant([[2.],[2.]])\n", "tf.matmul(matrix1, matrix2).eval()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 6., 6.],\n", " [ 6., 6.]], dtype=float32)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(matrix1*matrix2).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Watch out broadcasting" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 5., 5.],\n", " [ 5., 5.]], dtype=float32)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix1 = tf.constant([[3., 3.]])\n", "matrix2 = tf.constant([[2.],[2.]])\n", "(matrix1+matrix2).eval()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 5., 5.]], dtype=float32)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix1 = tf.constant([[3., 3.]])\n", "matrix2 = tf.constant([[2., 2.]])\n", "(matrix1+matrix2).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random values for variable initializations " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([ 2.20866942, -0.73225045, 0.33533147], dtype=float32)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.random_normal([3]).eval()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.08186948, 0.42999184], dtype=float32)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.random_uniform([2]).eval()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.43535876, 0.76933432, 0.65130949],\n", " [ 0.90863407, 0.06278825, 0.85073185]], dtype=float32)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.random_uniform([2, 3]).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reduce Mean/Sum" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_mean([1, 2], axis=0).eval()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [[1., 2.],\n", " [3., 4.]]\n", "\n", "\n", "tf.reduce_mean(x).eval()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 2., 3.], dtype=float32)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_mean(x, axis=0).eval()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, 3.5], dtype=float32)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_mean(x, axis=1).eval()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.5, 3.5], dtype=float32)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_mean(x, axis=-1).eval()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "10.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_sum(x).eval()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 4., 6.], dtype=float32)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_sum(x, axis=0).eval()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 3., 7.], dtype=float32)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_sum(x, axis=-1).eval()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reduce_mean(tf.reduce_sum(x, axis=-1)).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Argmax with axis" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 0])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [[0, 1, 2],\n", " [2, 1, 0]]\n", "tf.argmax(x, axis=0).eval()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 0])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.argmax(x, axis=1).eval()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([2, 0])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.argmax(x, axis=-1).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reshape, squeeze, expand_dims" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(2, 2, 3)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = np.array([[[0, 1, 2], \n", " [3, 4, 5]],\n", " \n", " [[6, 7, 8], \n", " [9, 10, 11]]])\n", "t.shape" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2],\n", " [ 3, 4, 5],\n", " [ 6, 7, 8],\n", " [ 9, 10, 11]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reshape(t, shape=[-1, 3]).eval()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2]],\n", "\n", " [[ 3, 4, 5]],\n", "\n", " [[ 6, 7, 8]],\n", "\n", " [[ 9, 10, 11]]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.reshape(t, shape=[-1, 1, 3]).eval()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2], dtype=int32)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.squeeze([[0], [1], [2]]).eval()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0],\n", " [1],\n", " [2]], dtype=int32)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.expand_dims([0, 1, 2], 1).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One hot" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 1., 0., 0.]],\n", "\n", " [[ 0., 1., 0.]],\n", "\n", " [[ 0., 0., 1.]],\n", "\n", " [[ 1., 0., 0.]]], dtype=float32)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.one_hot([[0], [1], [2], [0]], depth=3).eval()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0.],\n", " [ 0., 1., 0.],\n", " [ 0., 0., 1.],\n", " [ 1., 0., 0.]], dtype=float32)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = tf.one_hot([[0], [1], [2], [0]], depth=3)\n", "tf.reshape(t, shape=[-1, 3]).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## casting" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4], dtype=int32)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.cast([1.8, 2.2, 3.3, 4.9], tf.int32).eval()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 1, 0], dtype=int32)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.cast([True, False, 1 == 1, 0 == 1], tf.int32).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stack" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]], dtype=int32)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1, 4]\n", "y = [2, 5]\n", "z = [3, 6]\n", "\n", "# Pack along first dim.\n", "tf.stack([x, y, z]).eval()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]], dtype=int32)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.stack([x, y, z], axis=1).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ones like and Zeros like" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [1, 1, 1]], dtype=int32)" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [[0, 1, 2],\n", " [2, 1, 0]]\n", "\n", "tf.ones_like(x).eval()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0],\n", " [0, 0, 0]], dtype=int32)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tf.zeros_like(x).eval()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Zip\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 4\n", "2 5\n", "3 6\n" ] } ], "source": [ "for x, y in zip([1, 2, 3], [4, 5, 6]):\n", " print(x, y)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 4 7\n", "2 5 8\n", "3 6 9\n" ] } ], "source": [ "for x, y, z in zip([1, 2, 3], [4, 5, 6], [7, 8, 9]):\n", " print(x, y, z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Transpose" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2, 3)\n", "array([[[ 0, 1, 2],\n", " [ 3, 4, 5]],\n", "\n", " [[ 6, 7, 8],\n", " [ 9, 10, 11]]])\n" ] } ], "source": [ "t = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]])\n", "pp.pprint(t.shape)\n", "pp.pprint(t)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2, 3)\n", "array([[[ 0, 1, 2],\n", " [ 6, 7, 8]],\n", "\n", " [[ 3, 4, 5],\n", " [ 9, 10, 11]]])\n" ] } ], "source": [ "t1 = tf.transpose(t, [1, 0, 2])\n", "pp.pprint(sess.run(t1).shape)\n", "pp.pprint(sess.run(t1))" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2, 3)\n", "array([[[ 0, 1, 2],\n", " [ 3, 4, 5]],\n", "\n", " [[ 6, 7, 8],\n", " [ 9, 10, 11]]])\n" ] } ], "source": [ "t = tf.transpose(t1, [1, 0, 2])\n", "pp.pprint(sess.run(t).shape)\n", "pp.pprint(sess.run(t))" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 3, 2)\n", "array([[[ 0, 6],\n", " [ 1, 7],\n", " [ 2, 8]],\n", "\n", " [[ 3, 9],\n", " [ 4, 10],\n", " [ 5, 11]]])\n" ] } ], "source": [ "t2 = tf.transpose(t, [1, 2, 0])\n", "pp.pprint(sess.run(t2).shape)\n", "pp.pprint(sess.run(t2))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 2, 3)\n", "array([[[ 0, 1, 2],\n", " [ 3, 4, 5]],\n", "\n", " [[ 6, 7, 8],\n", " [ 9, 10, 11]]])\n" ] } ], "source": [ "t = tf.transpose(t2, [2, 0, 1])\n", "pp.pprint(sess.run(t).shape)\n", "pp.pprint(sess.run(t))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 0 }