Basic usage of Tensorflow

1. Concept

    Tensorflow is a programming system that uses graphs to represent computing tasks. The nodes in the graphs are called ops (operations). An op obtains 0 or more Tensors, performs calculations, and generates 0 or more Tensor. Tensor is seen as an n-dimensional array or list. The graph must be started in a session.

2. Tensorflow structure


explain:

1. Tensor, that is, data of any dimension, one-dimensional, two-dimensional, three-dimensional, four-dimensional and other data are collectively referred to as tensors. The flow of tensors refers to keeping the computing nodes unchanged and allowing data to flow. Such designs are aimed at connected machine learning algorithms such as logistic regression, neural networks, etc. The connected machine learning algorithm can express the algorithm as a graph. The tensor travels from front to back in the graph to complete the forward operation; while the residual travels from the back to the front, the backward propagation is completed.

2. Variable variable

3. Operation In the implementation of TF, the machine learning algorithm is expressed as a graph, and the nodes in the graph are operators.

4. Session The client uses the session to interact with the TF system. The general mode is to establish a session, and an empty graph will be generated at this time; add nodes and edges to the session to form a graph, and then execute it.

2. Basic usage

import tensorflow as tf

x = tf.Variable([1,2])#define a variable
a = tf.constant([3,3])#define a constant #add
a subtraction op
sub = tf.subtract(x,a) #add
a Addition op
add = tf.add(x,a)
init = tf.global_variables_initializer()#Initialization operation
with tf.Session() as sess:
    sess.run(init)
    print(sess.run(sub))
    print(sess. run(add))

#Create a variable initialized to 0
state = tf.Variable(0,name='counter') #Create
an op, the function is to add 1 to the state
new_value = tf.add(state,1) #Assign
op
update = tf.assign (state, new_value) #Variable
initialization
init = tf.global_variables_initializer()
with tf.Session() as sess:

 

#Fetch
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)


add = tf.add(input2,input3)
mul = tf.multiply(input1,add)


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

    print(result)


  #Feed
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)


with tf.Session() as sees:
    #feed data is passed in the form of a dictionary Enter
    print(sess.run(output,feed_dict={input1:[7.],input2:[2.]}))

Guess you like

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