Tensorflow framework introduced
This article describes a mind map about the content
· Tensorflow overall structure and data flow diagram
- Structural Analysis
Construction of a drawing stage: definition data (the Tensor tensor) and operation (node OP)
a stage executes: invoke various resources, the defined data and operations running
- The structure of FIG.
FIG flow that contains a set of data between the computation unit represented tf.operation tf.Tensor team objects and computing unit.
Briefly Tensorflow = Tensor + Flow = data + action
- The default map
By calling tf.get_default_graph()
the visit, the operation will be added to the default map can be.
op, sess have a graph property, the default in a graph, we can add the suffix in its .graph
direct access to
columns such as: sess.graph
You can view the properties map directly sess
- Custom map
Using tf.Graph()
returns a tensor object is
then to create a new context manager through FIG.
Attach Code:
# coding=utf-8
import tensorflow as tf
def graph():
a = tf.constant(1)
b = tf.constant(2)
c = tf.add(a, b)
print(c)
# 方法一,查看默认图
default_g = tf.get_default_graph()
print("默认图的属性:\n", default_g)
# 方法二,直接查看
print("a的图属性:\n", a.graph)
print("b的图属性:\n", b.graph)
# 开启会话
with tf.compat.v1.Session() as sess:
c_new = sess.run(c)
print("c_new的值:\n", c_new)
print("c_new的图属性:\n", sess.graph)
# 自定义图:
new_graph = tf.Graph()
with new_graph.as_default():
a_ng = tf.constant(1)
b_ng = tf.constant(2)
c_ng = tf.add(a, b)
print("c_ng:\n", c_ng)
return None
if __name__ == '__main__':
graph()
Output:
Tensor("Add:0", shape=(), dtype=int32)
默认图的属性:
<tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
a的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
b的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
c_new的值:
3
c_new的图属性:
<tensorflow.python.framework.ops.Graph object at 0x000001D9310E9188>
c_ng:
Tensor("Add_1:0", shape=(), dtype=int32)
· Session
A run TensorFlow operation class, there are two ways to open
- tf.Session: a complete program which
- tf.interactiveSession: for interactive context tensorflow
But generally we use the context manager to open the session.
- Session run () method to give
Provided using placeholder placeholder, run time using feed_dict specified parameters.
Attach Code:
import tensorflow as tf
def run():
# 定义占位符
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
c = tf.multiply(a, b)
# 开启上下文管理器
with tf.compat.v1.Session() as sess:
c_new = sess.run(c, feed_dict={a: 3.0, b: 7.0})
print ("c_new:\n", c_new)
return None
if __name__ == '__main__':
run()
· Tensor operations
- Properties and modify the properties
shape Shape:
modify a static shape: tensor.set_shape()
only in the case the shape is not completely fixed, static shape can be modified
# 没有完全固定下来的静态形状
a = tf.placeholder(dtype=tf.float32, shape=[None, None])
# 修改静态形状
a.set_shape([1, 2])
Modified dynamic shape: tf.reshape()
When creating dynamic tensor shape, must match the number of tensors
a = tf.placeholder(dtype=tf.float32, shape=[2, 3])
a_p = tf.reshape(a, [1, 2, 3])
# 可以跨阶,但是不能改变张量的总数量2 * 3 = 1 * 2 * 3
Code demonstrates:
dtype type:
tf.cast(tensor, dtype)
modifying tensor type
a = tf.placeholder(dtype=tf.float32, shape=[2, 3])
a_d = tf.cast(a, dtype=int32)
· Visualization of variables and model
When you define a certain model parameters, we use:
tf.Variable(initial_value = tf.random_normal(shape=[None, None]))
Because this is a variable, do not need to set their own values, we only need to fix its shape shape
- In a simple linear regression case, weights and bias is a pair of simple variable
weights = tf.Variable(initial_value=tf.random_nomal(shape=[1, 1]))
bias = tf.Variable(initial_value=tf.random_nomal(shape=[1, 1]))
y_predict = tf.matmul(x, weights) + bias
You must remember that after the variables used to initialize variables to be displayed:
init = tf.global_variables_initializer()
# 还要再会话中运行
sess.run(init)
Tensorflow Visualization
- Create an event file:
tf.summary.FileWriter("path", graph=sess.graph)
- Use tensorboard start event file
tensorboard --logdir=path