Personal notes on basic usage of tensorflow

Keywords: Python Session

Overview

TensorFlow program is divided into construction phase and execution phase. Build a graph and execute it to get the result.

Building map

                      .

import tensorflow as tf

# Create a constant op to generate a 1x2 matrix. The op is used as a node
matrix1 = tf.constant([[3., 3.]])

# Create another constant op to produce a 2x1 matrix
matrix2 = tf.constant([[2.],[2.]])

# Create a matrix multiplication matrix op with 'matrix1' and 'matrix2' as input
# The return value 'product' represents the result of matrix multiplication
product = tf.matmul(matrix1, matrix2)

Start graph in a session

After you construct the diagram, you need to create a Session object to start the diagram.

# Start the default graph
sess = tf.Session()

#Execute the diagram just now
result = sess.run(product)
print result
# ==> [[ 12.]]

# Task complete, close session
sess.close()

   use the with code block to automatically complete the close action.

with tf.Session() as sess:
    result = seff.run([product])
    print result

Generally, Tensorflow automatically detects the GPU to perform the operation, and uses the first GPU by default, so in some cases, you may need to specify the GPU manually. with...Device

with tf.Session() as sess:
  with tf.device("/gpu:1"):
    matrix1 = tf.constant([[3., 3.]])
    matrix2 = tf.constant([[2.],[2.]])
    product = tf.matmul(matrix1, matrix2)
    ...
  • "/ cpu:0": CPU of the machine
  • "/ gpu:0": the first GPU of the machine
  • "/ gpu:1": the second GPU of the machine

Interactive use

# Enter an interactive TensorFlow session
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of initializer op 
x.initializer.run()

# Add a subtraction sub op to subtract 'a' from 'x'. Run subtraction op to output the result 
sub = tf.sub(x, a)
print sub.eval()
# ==> [-2. -1.]

Tensor (tensor)

The tensor flow program uses the sensor data structure to represent all data.

variable

# Create a variable, initialize to scalar 0
state = tf.Variable(0, name="counter")

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value) #Assign new value to variable state

# After starting the graph, the variables must be initialized by init Op,
init_op = tf.initialize_all_variables()

# Startup diagram, running op
with tf.Session() as sess:
  # Run 'init' op to initialize variables
  sess.run(init_op)
  # Print the initial value of 'state'
  print sess.run(state)
  # Run op, update 'state', and print 'state'
  for _ in range(3):
    sess.run(update)
    print sess.run(state)

# Output:
# 0
# 1
# 2
# 3

Feed

input1 = tf.placeholder(tf.types.float32)
input2 = tf.placeholder(tf.types.float32)
#Placeholder is a temporary placeholder
output = tf.mul(input1, input2)

with tf.Session() as sess:
  print sess.run([output], feed_dict={input1:[7.], input2:[2.]})
  #Here, you can fill the data into the placeholder just now through feed_dict.
# Output:
# [array([ 14.], dtype=float32)]

Reference website: TensorFlow Chinese community

Posted by theblacksheep on Sun, 15 Dec 2019 11:00:49 -0800