提交 1fc27ee5 编写于 作者: A Amirsina Torfi

add tensors

上级 5c8e02ea
......@@ -191,13 +191,19 @@ Basics
:alt: alternate text
:align: right
+----+---------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
| # | topic | Source Code | |
+====+=====================+========================================================================================+==============================================+
| 2 | *TensorFLow Basics* | `Basic Math Operations <basicmathsourcecode_>`_ / `IPython <ipythonbasicmath_>`_ | `Documentation <Documentationbasicmath_>`_ |
+----+---------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
| 3 | *TensorFLow Basics* | `TensorFlow Variables <variablssourcecode_>`_ / `IPython <ipythonvariabls_>`_ | `Documentation <Documentationvariabls_>`_ |
+----+---------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
.. _ipythontensors: codes/ipython/1-basics/tensors.ipynb
.. |Tensors| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/0-welcome/welcome.ipynb
+----+---------------------+--------------------------+-------------------------------------------+
| # | topic | Run | Source Code |
+====+=====================+==========================+===========================================+
| 1 | Tensors | |Tensors| | `Notebook <ipythontensors_>`_ |
+----+---------------------+--------------------------+-------------------------------------------+
==========================
......
===========================
Welcome to TensorFlow World
===========================
This document is dedicated to explain how to run the python script for this tutorial.
--------------------------------
How to run the code in Terminal?
--------------------------------
Please root to the ``code/`` directory and run the python script as the general form of below:
.. code:: shell
python [python_code_file.py] --log_dir='absolute/path/to/log_dir'
As an example the code can be executed as follows:
.. code:: shell
python 1-welcome.py --log_dir='~/log_dir'
The ``--log_dir`` flag is to provide the address which the event files (for visualizing in Tensorboard) will be saved. The flag of ``--log_dir`` is not required because its default value is available in the source code as follows:
.. code:: python
tf.app.flags.DEFINE_string(
'log_dir', os.path.dirname(os.path.abspath(__file__)) + '/logs',
'Directory where event logs are written to.')
----------------------------
How to run the code in IDEs?
----------------------------
Since the code is ready-to-go, as long as the TensorFlow can be called in the IDE editor(Pycharm, Spyder,..), the code can be executed successfully.
----------------------------
How to run the Tensorboard?
----------------------------
.. _Google’s words: https://www.tensorflow.org/get_started/summaries_and_tensorboard
TensorBoard is the graph visualization tools provided by TensorFlow. Using `Google’s words`_: “The computations you'll use TensorFlow for - like training a massive deep neural network - can be complex and confusing. To make it easier to understand,
debug, and optimize TensorFlow programs, we've included a suite of visualization tools called
TensorBoard.”
The Tensorboard can be run as follows in the terminal:
.. code:: shell
tensorboard --logdir="absolute/path/to/log_dir"
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#####################################################\n",
"########## Welcome to TensorFlow World ##############\n",
"#####################################################\n",
"\n",
"# The tutorials in this section is just a start for math operations.\n",
"# The TensorFlow flags are used for having a more user friendly environment.\n",
"\n",
"from __future__ import print_function\n",
"import tensorflow as tf\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Defining some constant values\n",
"a = tf.constant(5.0, name=\"a\")\n",
"b = tf.constant(10.0, name=\"b\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Some basic operations\n",
"x = tf.add(a, b, name=\"add\")\n",
"y = tf.div(a, b, name=\"divide\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 5.0\n",
"b = 10.0\n",
"a + b = 15.0\n",
"a/b = 0.5\n"
]
}
],
"source": [
"# Run the session\n",
"with tf.Session() as sess:\n",
" print(\"a =\", sess.run(a))\n",
" print(\"b =\", sess.run(b))\n",
" print(\"a + b =\", sess.run(x))\n",
" print(\"a/b =\", sess.run(y))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Closing the session.\n",
"sess.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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": 2
}
==============================
Basics
==============================
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "tensors.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "7i1UqqIkNxAt"
},
"source": [
"# Import necessary libraries\n",
"import tensorflow as tf\n",
"import numpy as np"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "I4YPHO9ba3Bc"
},
"source": [
"## Tensors\n",
"\n",
"Tensor are multi-dimensitonal arrays that are used in Tensorflow.\n",
"\n",
"We use the following definition:\n",
"\n",
"* **Rank:** The number of dimensions that a vector has.\n",
"\n",
"Below, we will define different kinds of tensors and show their rank using [tf.rank](https://www.tensorflow.org/api_docs/python/tf/rank) function."
]
},
{
"cell_type": "code",
"metadata": {
"id": "rcU8_F3fPUb5",
"outputId": "5c35e8ca-fe38-4dc9-81d7-32ce4fc80bb7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"source": [
"tensor = tf.constant(0)\n",
"print(\"Print constant tensor {} of rank {}\".format(tensor, tf.rank(tensor)))\n",
"print(\"Show full tensor:\", tensor)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"Print constant tensor 0 of rank 0\n",
"Show full tensor: tf.Tensor(0, shape=(), dtype=int32)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ahIBf6_4cRnm",
"outputId": "4fd2a8f2-8345-4138-bb81-d2c5176ccf27",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"source": [
"# NOTE: We use .numpy() to transform tf.tensor to numpy\n",
"tensor = tf.constant([1,2,3])\n",
"print(\"Tensor:\", tensor)\n",
"print(\"Rank:\", tf.rank(tensor).numpy())"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"Tensor: tf.Tensor([1 2 3], shape=(3,), dtype=int32)\n",
"Rank: 1\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ss3aDmDTd-LS"
},
"source": [
"### Tensor Operations"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TKX2U0Imcm7d"
},
"source": [
"x = tf.constant([[1, 1],\n",
" [1, 1]])\n",
"y = tf.constant([[2, 4],\n",
" [6, 8])\n",
"\n",
"# Add two tensors\n",
"print(tf.add(x, y), \"\\n\")\n",
"\n",
"# Add two tensors\n",
"print(tf.matmul(x, y), \"\\n\")\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "BlEgQ2t2edKl"
},
"source": [
"### Muti-dimentional Tensors\n",
"\n",
"This part is not much different compared to what we learned so far. However, it would be nice to try extracting as much information as we can from a multi-dimentional tensor.\n",
"\n",
"\n",
"Let's use [tf.ones](https://www.tensorflow.org/api_docs/python/tf/ones) for our purpose here. It creates an all-one tensor."
]
},
{
"cell_type": "code",
"metadata": {
"id": "Gdtt0e4-fDkl",
"outputId": "d79490ed-d8e4-41ef-b220-59f6ed3613c4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
}
},
"source": [
"# We set the shape of the tensor and the desired data type.\n",
"tensor = tf.ones(shape = [2, 3, 6], dtype = tf.float32)\n",
"print('Tensor:', tensor)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"Tensor: tf.Tensor(\n",
"[[[1. 1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1. 1.]]\n",
"\n",
" [[1. 1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1. 1.]\n",
" [1. 1. 1. 1. 1. 1.]]], shape=(2, 3, 6), dtype=float32)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "c5PChFhlfXmx",
"outputId": "93e20093-3d86-4291-9b5f-da10bbefbb81",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 146
}
},
"source": [
"print(\"Tensor Rank: \", tf.rank(tensor).numpy())\n",
"print(\"Shape: \", tensor.shape)\n",
"print(\"Elements' type\", tensor.dtype)\n",
"print(\"The size of the second axis:\", tensor.shape[1])\n",
"print(\"The size of the last axis:\", tensor.shape[-1])\n",
"print(\"Total number of elements: \", tf.size(tensor).numpy())\n",
"print(\"How many dimensions? \", tensor.ndim)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"Tensor Rank: 3\n",
"Shape: (2, 3, 6)\n",
"Elements' type <dtype: 'float32'>\n",
"The size of the second axis: 3\n",
"The size of the last axis: 6\n",
"Total number of elements: 36\n",
"How many dimensions? 3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cnYwTBqPhW1I"
},
"source": [
"### Indexing\n",
"\n",
"TensorFlow indexing is aligned with Python indexing. See the following examples."
]
},
{
"cell_type": "code",
"metadata": {
"id": "34-Tfcsnf6uG"
},
"source": [
"x = tf.constant([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])"
],
"execution_count": 13,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tNZhisXDhoLp",
"outputId": "fab91e2a-3517-466b-d460-7bb6326e6413",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"source": [
"# All elements\n",
"print(x[:].numpy())"
],
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "KUghwlZ7hr10",
"outputId": "da4d37d4-aecb-4549-a5a0-4e9c03d2afce",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"# All elements of the first row\n",
"print(x[0,:].numpy())"
],
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"text": [
"[1 2 3]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "NSCMESaPhwnV",
"outputId": "e4de3cb7-fe6c-46ed-986f-0d9726d32bbf",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"# First row and last column\n",
"print(x[0,-1].numpy())"
],
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"text": [
"3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "hH8Fhi2Sh2rt",
"outputId": "13062bf6-9589-4452-8fb6-23ae65873d72",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"# From second row to last and from third column to last\n",
"print(x[1:,2:].numpy)"
],
"execution_count": 21,
"outputs": [
{
"output_type": "stream",
"text": [
"<bound method _EagerTensorBase.numpy of <tf.Tensor: shape=(1, 1), dtype=int32, numpy=array([[6]], dtype=int32)>>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Y_zEE3-7iUmu"
},
"source": [
"### Data types\n",
"\n",
"You can change the data type of the tesnorflow tensors for your purpose. This will be done easily by [tf.cast](https://www.tensorflow.org/api_docs/python/tf/cast)."
]
},
{
"cell_type": "code",
"metadata": {
"id": "mFsqRDxAiK95",
"outputId": "7cc71baa-044c-4508-c773-321c1fee6b55",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 90
}
},
"source": [
"original_tensor = tf.constant([1, 2, 3, 4], dtype=tf.int32)\n",
"print('Original tensor: ', original_tensor)\n",
"print(\"Tensor type before casting: \", original_tensor.dtype)\n",
"\n",
"# Casting to change dtype\n",
"casted_tensor = tf.cast(original_tensor, dtype=tf.float32)\n",
"print('New tensor: ', casted_tensor)\n",
"print(\"Tensor type after casting: \", casted_tensor.dtype)"
],
"execution_count": 24,
"outputs": [
{
"output_type": "stream",
"text": [
"Original tensor: tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)\n",
"Tensor type before casting: <dtype: 'int32'>\n",
"New tensor: tf.Tensor([1. 2. 3. 4.], shape=(4,), dtype=float32)\n",
"Tensor type after casting: <dtype: 'float32'>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "81XDYbnxi-nx"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
\ No newline at end of file
==========
Variables
==========
This source code is dedicated to define and initialize variables.
--------------------------------
How to run the code in Terminal?
--------------------------------
Please root to the ``code/`` directory and run the python script as the general form of below:
.. code:: shell
python [python_code_file.py]
As an example the code can be executed as follows:
.. code:: shell
python variable.py
----------------------------
How to run the code in IDEs?
----------------------------
Since the code is ready-to-go, as long as the TensorFlow can be called in the IDE editor(Pycharm, Spyder,..), the code can be executed successfully.
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"## This code create some arbitrary variables and initialize them ###\n",
"# The goal is to show how to define and initialize variables from scratch.\n",
"\n",
"import tensorflow as tf\n",
"from tensorflow.python.framework import ops"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#######################################\n",
"######## Defining Variables ###########\n",
"#######################################\n",
"\n",
"# Create three variables with some default values.\n",
"weights = tf.Variable(tf.random_normal([2, 3], stddev=0.1),\n",
" name=\"weights\")\n",
"biases = tf.Variable(tf.zeros([3]), name=\"biases\")\n",
"custom_variable = tf.Variable(tf.zeros([3]), name=\"custom\")\n",
"\n",
"# Get all the variables' tensors and store them in a list.\n",
"all_variables_list = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"############################################\n",
"######## Customized initializer ############\n",
"############################################\n",
"\n",
"## Initialation of some custom variables.\n",
"## In this part we choose some variables and only initialize them rather than initializing all variables.\n",
"\n",
"# \"variable_list_custom\" is the list of variables that we want to initialize.\n",
"variable_list_custom = [weights, custom_variable]\n",
"\n",
"# The initializer\n",
"init_custom_op = tf.variables_initializer(var_list=variable_list_custom )"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"########################################\n",
"######## Global initializer ############\n",
"########################################\n",
"\n",
"# Method-1\n",
"# Add an op to initialize the variables.\n",
"init_all_op = tf.global_variables_initializer()\n",
"\n",
"# Method-2\n",
"init_all_op = tf.variables_initializer(var_list=all_variables_list)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"##########################################################\n",
"######## Initialization using other variables ############\n",
"##########################################################\n",
"\n",
"# Create another variable with the same value as 'weights'.\n",
"WeightsNew = tf.Variable(weights.initialized_value(), name=\"WeightsNew\")\n",
"\n",
"# Now, the variable must be initialized.\n",
"init_WeightsNew_op = tf.variables_initializer(var_list=[WeightsNew])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"######################################\n",
"####### Running the session ##########\n",
"######################################\n",
"with tf.Session() as sess:\n",
" # Run the initializer operation.\n",
" sess.run(init_all_op)\n",
" sess.run(init_custom_op)\n",
" sess.run(init_WeightsNew_op)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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": 2
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册