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.
Cloud9 Python 2.7.6 Sample Codes : GitHub For environment construction, refer to "Using TensorFlow in Cloud Integrated Development Environment Cloud9"
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.
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()
It is important to set up Graphs and then perform calculations in Sessions.
# 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
The variables are being updated in sequence.
# 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.
# 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.]}).
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