TensorFlow study notes - preliminary understanding

Tensor (Tensor):

There are many kinds of tensors. A zero-order tensor is a scalar or a scalar (scalar), that is, a value. For example, [1]
A first-order tensor is a vector, such as a one-dimensional [1, 2, 3]
second-order Tensors are matrices, such as two-dimensional [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
and so on, as well as third-order three-dimensional...

Tensorflow process

Tensorflow first defines the structure of the neural network, and then puts the data into the structure for operation and training.
Because TensorFlow uses data flow graphs to calculate, first we have to create a data flow graph, and then Put our data (data in the form of tensors) in the data flow graph for calculation. Nodes (Nodes) represent mathematical operations in the graph, and the lines (edges) in the graph represent the interconnection between nodes Multi-dimensional data array, namely tensor (tensor). When training the model, tensor will continuously flow from one node in the data flow graph to another node, which is the origin of the name of TensorFlow.

write picture description here

Session control

SessionIt is a statement that Tensorflow uses to control and output the execution of the file. Run it to session.run()get the result of the operation you want to know, or the part you want to calculate.

import tensorflow as tf
# create two matrixes
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],
                       [2]])
product = tf.matmul(matrix1,matrix2)

Since productis not a direct calculation step, we will use Sessionto activate productand get the result of the calculation. There are two forms of using session control Session.

# method 1
sess = tf.Session()
result = sess.run(product)
# [[12]]

# method 2
with tf.Session() as sess:
    result = sess.run(product)
# [[12]]
Variable variable

Using to tf.Variabledefine variable, unlike python, it must be defined as a variable before it is a variable.
Definition syntax: state = tf.Variable()
The initial value is 0, and you can also give it a name counter:

state = tf.Variable(0, name='counter')

Load new_value onto state, and counter is updated:

update = tf.assign(state, new_value)

full variable:

import tensorflow as tf

state = tf.Variable(0, name='counter')

# 定义常量 one
one = tf.constant(1)

# 定义加法步骤 (注: 此步并没有直接计算)
new_value = tf.add(state, one)

# 将 State 更新成 new_value
update = tf.assign(state, new_value)

After the variable is defined, it must be initialized and defined init = tf.initialize_all_variables().
The variable is still not activated at this point, and it needs to be activated again in sess, , sess.run(init)and initthis step.

# 如果定义 Variable, 就一定要 initialize
# init = tf.initialize_all_variables() # tf 马上就要废弃这种写法
init = tf.global_variables_initializer()  # 替换成这样就好

# 使用 Session
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

Note: Direct print(state)does not work ! !
Be sure to point sessthe pointer to stateand then proceed printto get the desired result!

placeholder

placeholderIt is a placeholder in Tensorflow, which temporarily stores variables. Data is passed in from outside.
Transmission data formsess.run(***, feed_dict={input: **}).

import tensorflow as tf

#在 Tensorflow 中需要定义 placeholder 的 type ,一般为 float32 形式
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

# mul = multiply 是将input1和input2 做乘法运算,并输出为 output 
ouput = tf.multiply(input1, input2)

The work of passing the value is handed over sess.run(), and the value that needs to be passed in is placed in feed_dict={}and corresponds to each one one-to-one input. placeholderAnd feed_dict={}is bound to appear together.

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

Guess you like

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