Basic usage of Tensorflow 3

basic use

Overview

Computational graph

Construction diagram: (the official website explains in great detail)

import tensorflow as tf
# Create a constant op that produces a 1x2 matrix. The op is added as a node
# to the default graph.
#
# The return value of the constructor represents the return value of the constant op.
matrix1 = tf.constant([ [3., 3.]])
# Create another constant op, yielding a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

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

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

# call sess'run() ' method to perform the matrix multiplication op, passing in 'product' as an argument to the method. 
# As mentioned above, 'product' represents the output of the matrix multiplication op, passing it in indicates to the method that we want to get back
# matrix multiplication The output of the op.
#
# The entire execution process is automated, the session is responsible for passing all the input required by the op. The op is usually executed concurrently.

# The function calls 'run(product)' Triggers the execution of three ops in the graph (two constant ops and one matrix multiplication op).
#
# The return value 'result' is a numpy `ndarray` object.
result = sess.run(product)
#print result
print(result) // Modify the class, suitable for python3
# ==> [[ 12.]]

# Task Done, close the session.
sess.close()

Start a graph in one session

import tensorflow as tf
# Create a constant op that produces a 1x2 matrix. The op is added as a node
# to the default graph.
#
# The return value of the constructor represents the return value of the constant op.
matrix1 = tf.constant([ [3., 3.]])
# Create another constant op, yielding a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])
# Create a matrix multiplication matmul op, putting 'matrix1 ' and 'matrix2' as input.
# The return value 'product' represents the result of matrix multiplication.
product = tf.matmul(matrix1, matrix2)

with tf.Session() as sess:

  result = sess.run(product)

  result1 = sess.run([product]) #The output result is different, strange

  print(result)

  print(result1)

You can use with tf.device() to specify a specific CPU and GPU, and take a screenshot, as follows:


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])

# use the initializer op's The run() method initializes 'x' 
x.initializer.run()

# Add a subtraction sub op, subtract 'a' from 'x'. Run the subtraction op, output the result 
sub = tf.subtract(x, a)
#Function Rename #print 
(sub.eval())
print(sub.eval())

Tensor

variable

import tensorflow as tf
state = tf.Variable(0, name="counter")

# Create an op whose role is to increase state by 1

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# After starting the graph, the variable must first be initialized by the `init` (init) op,
# must first increase An `init` op into the graph.

init_op = tf.initialize_all_variables()

# start graph, run op
with tf.Session() as sess:
    # run 'init' op
    sess.run(init_op)
    # print initial 'state' value
    print(sess.run(state))
    # run op, update 'state', and print 'state'
    for _ in range(3):
        sess.run(update)

        print (sess.run (state))

Fetch

In order to retrieve the output of the operation, you can pass in some tensors when executing the graph using  Session the object's  run() call. These tensors will help you retrieve the results. In the previous example, we only retrieved a single node  state, but you can also Retrieve multiple tensors:

There is an error in the official website program:

import tensorflow as tf
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.multiply(input1, intermed)

with tf.Session() as sess:
  result = sess.run([mul, intermed])

  print(result)

Feed

Very simple, a bit similar to pre-allocating memory, reducing overhead!

import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1, input2)

with tf.Session() as sess:
  print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529425&siteId=291194637