tensorflow basic example (1)

      In this article, I mainly explain the basics of TensorFlow, commonly used sessions, constant definitions, model generation, and model storage, and how to view the actual saved content in the model.

1. TensorFlow model

    The naming of TensorFlow comes from its operating principle. tensor (tensor) represents an N-dimensional array, Flow (flow) means calculation based on the data flow graph.

    Tensorflow is the calculation (programming model) where a tensor flows from one end of the image to the other .

2. The operating mechanism of the model

    The operating mechanism of TensorFlow is separated from "definition" and "operation". From the operation point of view, it is divided into two layers: model construction and model operation

    The concepts required for model construction are as follows:

    

      The above definition is a "picture":

  •  A "graph" is a computational task
  • When the model is running, the "graph" will be started in the session,
  • The session distributes the op of the graph to the CPU or GPU device, and then provides a method to execute the op. After execution, it will return a Tensor value. In python, the returned Tensor is a numpy ndarry object; in C or C++, it returns a Tensor instance.

   The working relationship between session and graph is as follows:

  

    In the actual environment, the operation of the above figure is divided into three types, training scenarios, test scenarios, and usage scenarios. The training scene is different from the other two scenes.

  (1) Training scenario: The process of realizing the model from scratch, through the learning and training of samples, adjusting the learning parameters, and forming the final model.

           Process: Taking the given sample and label as the input node, through a large number of loop iterations, the value obtained by the forward operation (from the input sample through the op operation to get the output direction) in the graph, and the reverse operation (from the output To the input direction) to update the learning parameters in the model, and finally get the close sample label that maximizes the positive result of the model.

   (2) Test scenario and application scenario: The test scenario uses the forward calculation of the graph to compare the result with the real value to obtain the error value; the use scenario also uses the forward calculation of the graph to obtain the result and use it directly. In a function, it can be divided into actual parameters, formal parameters, function body and return value. In the model, the actual parameters are the input samples, the formal parameters are the placeholders, the operation process is equivalent to the function body, and the result obtained is the return value.

Other data flows defined under the interaction between session and graph:

  • Injection mechanism (feed): pass data into the pattern through placeholders
  • Fetch mechanism (fetch): get results from the pattern

Three, examples

   1. The role of session, and usage examples

# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time    : 2019/5/15 11:32
# @Author  : xhh
# @Desc    :  session的演示
# @File    : tensor_tf4.py
# @Software: PyCharm

import tensorflow as tf

hello = tf.constant('hello, tensorflow')  # 定义一个常量
sess = tf.Session()  # 建立一个session
print(sess.run(hello))  # 通过sess中的run函数来运行结果
sess.close()  # 关闭session

operation result:

Note: If the constant you define contains Chinese,

hello = tf.constant('你好, tensorflow')  # 定义一个常量

Yes, there will be coding problems

In the above code, tf.constant defines a constant, and the content of hello can only be returned in the run of the session.

2. Use of with session

With session is what we will use frequently in the future. It follows the with usage in python. When the program ends, the session will be closed automatically. No need to close manually.

Example: Use the with session method to establish a session, and calculate the addition and multiplication values ​​of two variables (3 and 4) in the session

import tensorflow as tf

a = tf.constant(3)  # 定义常量3
b = tf.constant(4)  # 定义常量4
with tf.Session() as sess:   # 建立session
    print("相加: %i"%sess.run(a+b))
    print("相乘: %i"%sess.run(a*b))

     operation result:

3. Injection mechanism

Inject specific actual parameters into the corresponding placeholder. The feed is only valid in the method that calls it, and the feed disappears after the method ends.

Define placeholders, use the feed mechanism to pass in specific values ​​through the placeholders, and add and multiply them.

import tensorflow as tf
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
mul = tf.multiply(a, b)

with tf.Session() as sess:
    # 计算具体数值
    print("feed相加:%i"%sess.run(add, feed_dict={a:3, b:4}))
    print("feed相乘:%i"%sess.run(mul, feed_dict={a:3, b:4}))

operation result:

Marking method: use tf.placeholder to create placeholders for these operations, and then use feed_dict to put specific values ​​in the placeholders.

 4. Save/load linear regression model

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

plotdata = {"batchsize":[], "loss":[]}

def moving_average(a, w=10):
    if len(a)<w:
        return a[:]
    return [val if idx < w else sum(a[(idx-w):idx])/w for idx, val in enumerate(a)]

#  模拟数据
train_X = np.linspace(-1, 1, 100)
train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.3  # 加入了噪声

# 图形展示
plt.plot(train_X,train_Y,'ro',label="original data") # label数据标签
plt.legend()
plt.show()

tf.reset_default_graph()  # 重置会话

# 创建模型
# 占位符
X = tf.placeholder("float")
Y = tf.placeholder("float")
# 模型参数
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")

# 前向结构
z = tf.multiply(X, W) +b

# 反向优化
cost = tf.reduce_mean(tf.square(Y-z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# 初始化变量
init = tf.global_variables_initializer()
# 参数设置
training_epochs = 20
display_step = 2
saver = tf.train.Saver() # 模型生成,并保存
savedir = "log/"

# 启动session
with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X,train_Y):
            sess.run(optimizer, feed_dict={X:x, Y:y})

        # 显示训练中的详细信息
        if epoch % display_step == 0:
            loss = sess.run(cost, feed_dict={X:train_X, Y:train_Y})
            print("Epoch:",epoch+1,"cost=", loss,"W=",sess.run(W),"b=",sess.run(b))
            if not (loss=="NA"):
                plotdata["batchsize"].append(epoch)
                plotdata["loss"].append(loss)

        print("finished!")
        saver.save(sess, savedir+"linermodel.cpkt")
        print("cost=",sess.run(cost, feed_dict={X:train_X, Y:train_Y}),"W=", sess.run(W),"b=",sess.run(b))

        # 图形显示
        plt.plot(train_X, train_Y, 'ro', label='Original data')
        plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
        plt.legend()
        plt.show()

        plotdata["avgloss"] = moving_average(plotdata["loss"])
        plt.figure(1)
        plt.subplot(211)
        plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')
        plt.xlabel('Minibatch number')
        plt.ylabel('Loss')
        plt.title('Minibatch run vs. Training loss')
        plt.show()

The model is generated under the log folder in the same directory:

After restarting a session and name it sess2, load the model through saver's restore function

    # 重启一个Session
    with tf.Session() as sess2:
        sess2.run(tf.global_variables_initializer())
        saver.restore(sess2, savedir+"linermodel.cpkt")
        print("x=0.2, z=",sess2.run(z, feed_dict={X: 0.2}))

Test Results:

5. View the contents of the model

View the internal content of the saved model

from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file

savedir = "log/"
print_tensors_in_checkpoint_file(savedir+"linermodel.cpkt",None,True)

operation result:

Through the results of the operation, you can see that the name of the created variable and its value are saved in the model.

 

You can pay attention to the official account of me and my friends~~~ Here are some python technical information that my friends and I update from time to time! ! You can also leave a message to discuss technical issues, I hope you can support, pay attention to it, click on the small advertisement, thank you~~

 

Guess you like

Origin blog.csdn.net/weixin_39121325/article/details/90231254