Tensorflow puzzles [2]: feed_dict is not necessarily tied to placeholder


The design concept of Tensorflow is called the calculation flow graph. When writing a program, the graph of the entire system is first constructed, and the code will not take effect directly. This is different from other numerical computing libraries in python (such as Numpy, etc.), and the graph is static. , similar to images in docker. Then, at the actual runtime, start a session, and the program will actually run. The advantage of this is that it avoids repeatedly switching the actual running context of the underlying program, and tensorflow helps you optimize the code of the entire system. We know that the bottom layer of many python programs is C language or other languages. When executing a line of script, it has to be switched once, which is costly. tensorflow can help you optimize the code that needs to be executed in the entire session by calculating the flow graph. advantage.
There are a lot of false things above, and then introduce the protagonist of this article: feed_dict. When I first learned tensorflow, I thought feed_dict was paired with placeholder. For example, the following code illustrates the basic usage of feed_dict:

import tensorflow as tf

a = tf.placeholder(dtype=tf.float32)
b = tf.placeholder(dtype=tf.float32)
c = tf.add(a, b)

with tf.Session() as sess:
    print sess.run(c, feed_dict = {a: 1.0, b: 2.0})

OK, in fact, feed_dict can feed things to other tensors, not just placeholders. For example, the following code:

import tensorflow as tf

a = tf.placeholder(dtype=tf.float32)
b = tf.constant(2.0)
c = tf.add(a, b)

with tf.Session() as sess:
    print sess.run(c, feed_dict = {a: 1.0, b: 3.0})

The result of the operation is 4. Here, 3.0 is sent to tensor b using feed_dict.
To sum up, knowing this principle is very helpful for the understanding of model recovery. The machine learning system is accompanied by the flow of tensor (the meaning of tensorflow is this, neural network, etc., in fact, it is the linear transformation and nonlinear activation of tensor), maybe, we only get the middle tensor. For example, if you are working on image classification, during the training process, the placeholder of the graph is a pixel matrix of any size, but when you restore the model, there is already a preprocessed image pixel tensor, then you can directly convert the It can be imported into the corresponding tensor, provided that the name or symbol of the corresponding tensor is known. At this time, the function tf.get_tensor_by_name may be used. The flexible use of feed_dict can also reflect the understanding of the idea of ​​graph.

Guess you like

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