[PYTHON] Using TensorFlow in the cloud integrated development environment Cloud9 ~ Basics of usage ~

Introduction

I checked [Basic Usage] of TensorFlow (https://www.tensorflow.org/versions/r0.11/get_started/basic_usage.html#basic-usage) in Cloud9. I will summarize the basic idea and usage when using TensorFlow. It doesn't include GPU or Interactive Usage. First of all, I will study the others as needed, with the aim of creating basic code.

environment

Cloud9 Python 2.7.6 Sample Codes : GitHub For environment construction, refer to "Using TensorFlow in Cloud Integrated Development Environment Cloud9"

Concept of TensorFlow

There are two important points to consider.

Graphs is a class that sets values and calculations. Values can be constants, variables, tensors (matrix in 2D, defined in multidimensional arrays), and so on. It is also Graphs that sets the calculation of that value (for example, addition or multiplication).

Sessions is a class that performs Graphs values and calculations. TensorFlow seems to be able to use the GPU, but it seems to use it automatically via Sessions.

Therefore, set values and calculations in Graphs => Perform calculations in Sessions It will be the flow. Let's look at a concrete example below. Basic_usage.py on GitHub I will explain the execution results of.

Concrete example

constant and Sessions

import tensorflow as tf

# Graphs
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.], [2.]])
product = tf.matmul(matrix1, matrix2)

# Sessions
sess = tf.Session()
result = sess.run(product)
print(result)
# Output: [[12.]]
sess.close()
  1. tf.constant sets constants
  2. Set matrix1 to 1x2 matrix and matrix2 to 2x1 matrix
  3. Multiply the matrix with tf.matmul (this is where the Graphs values and calculations are set)
  4. Create a session with tf.Session ()
  5. Run the calculation with run
  6. Close the session with close ()
  7. Print the matrix calculation result with print

It is important to set up Graphs and then perform calculations in Sessions.

Variable

# Graphs
state = tf.Variable(0)
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init_op = tf.initialize_all_variables()

# Sessions
with tf.Session() as sess:
  sess.run(init_op)
  print(sess.run(state))
  for _ in range(3):
    sess.run(update)
    print(sess.run(state))
# Output: 0 1 2 3
  1. Set variables with tf.Variable
  2. Add with tf.add
  3. Put the result of addition back into state with tf.assign
  4. Initialize with tf.initialize_all_variables () (variables need to be initialized!)
  5. Create a session with Session ()
  6. Initialize variables with run (init_op)
  7. Run (update) in a loop and output the resulting state

The variables are being updated in sequence.

Run multiple at the same time

# Graphs
input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)

# Sessions
with tf.Session() as sess:
  result = sess.run([mul, intermed])
  print(result)
# Output: [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

The content of the code is the same as before, so you can understand it. The important thing is that run ([mul, intermed]) executes multiple calculations at the same time, and the output of the result is also output at the same time.

placeholder (assignment)

# Graphs
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)

# Sessions
with tf.Session() as sess:
  print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# Output: [array([ 14.], dtype=float32)]

The value can be assigned by tf.placeholder (tf.float32). It is calculated by substituting with run ([output], feed_dict = {input1: [7.], input2: [2.]}).

in conclusion

I think it is important to set up Graphs and then execute calculations in Sessions. There is no problem with simple code, but it is unknown what happens when it comes to complicated calculations. If you change the sample code and try various things, you will get a better understanding.

I'm still studying, so if you make a mistake, please let me know in the comments. I would like to continue studying how to use TensorFlow and make corrections and corrections.

Recommended Posts

Using TensorFlow in the cloud integrated development environment Cloud9 ~ Basics of usage ~
Django + MongoDB development environment maintenance (in the middle of writing)
Solve the problem of missing libcudart in Ubuntu 16.04 + CUDA 8.0 + Tensorflow environment
Django cannot be installed in the development environment of pipenv + pyenv
About the development environment you are using
The basics of running NoxPlayer in Python
The strongest Python integrated development environment PyCharm
Visualized the usage status of the sink in the company
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
Used from the introduction of Node.js in WSL environment
The story of building the fastest Linux environment in the world
Let's break down the basics of TensorFlow Python code
In the middle of development, we will introduce Alembic
Development environment in Python
Using TensorFlow in the cloud integrated development environment Cloud9 ~ Basics of usage ~
Easy 3 minutes TensorBoard in Google Colab (using TensorFlow 2.x)
Directory structure for test-driven development using pytest in python
Framework development in Python
Development environment in Python
Jupyter in Cloud9 IDE
Image normalization in TensorFlow
Slackbot development in Python
Use "% tensorflow_version 2.x" when using TPU with Tensorflow 2.1.0 in Colaboratory
Get Python scripts to run quickly in Cloud Run using responder
Test discovery fails when using tensorflow in vscode + pytest environment
The image is displayed in the local development environment, but the image is not displayed on the remote server of VPS
Switch the setting value of setting.py according to the development environment
Unify the environment of the Python development team starting with Poetry
Check the operation of Python for .NET in each environment
I tried refactoring the CNN model of TensorFlow using TF-Slim
Construction of Python local development environment Part 2 (pyenv-virtualenv, pip usage)
View using the python module of Nifty Cloud mobile backend
Commands often used in the development environment during Python implementation
[Tips] Problems and solutions in the development of python + kivy
What beginners learned from the basics of variables in python
Test discovery fails when using tensorflow in vscode + pytest environment
Construction of Cortex-M development environment for TOPPERS using Raspberry Pi
[Flask & Bootstrap] Visualize the content of lyrics in Word Cloud ~ Lyrics Word Cloud ~
The story of downgrading the version of tensorflow in the demo of Mask R-CNN.
How to handle multiple versions of CUDA in the same environment
[Understanding in the figure] Management of Python virtual environment by Pipenv
The simplest way to build a Spleeter usage environment using Windows
LINEbot development, I want to check the operation in the local environment
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
How to set up the development environment of ev3dev [Windows version]
Try using FireBase Cloud Firestore in Python for the time being
Installation procedure + usage of "virtualenv" and "pythonz" in Mountain Lion environment
The story of participating in AtCoder
Integrated Development Environment (IDE) vs. Virtual environment
Summary of various operations in Tensorflow
install tensorflow in anaconda + python3.5 environment
The story of the "hole" in the file
Review of the basics of Python (FizzBuzz)
Checking the NAOqi Python development environment
About the basics list of Python basics
Prepare the development environment with anyenv
Learn the basics of Python ① Beginners
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
Display the result of video analysis using Cloud Video Intelligence API from Colaboratory.
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
Development and deployment of REST API in Python using Falcon Web Framework
Graph the change in the number of keyword appearances per month using pandas
Until the Deep Learning environment (TensorFlow) using GPU is prepared for Ubuntu 14.04