Tensorflow基本概念——常量、变量、占位符、计算图、会话

Tensorflow基本概念

基于Tensorflow的神经网络通过常量、变量、占位符等完成神经网络的结构搭建、数据输入等一些列与数据有关操作,这些构成了一个计算图;然后运行会话,使前面定义好的计算图运行,训练神经网络,最终得到一个准确率较高的模型(可理解为一组权重参数)。

1 Tensorflow工作流程

  • 创建Tensor
  • 定义Operations(Operations输入Tensor,然后输出另一个Tensor)
  • 执行计算(也就是运行一个可计算的图)

其中Tensor包括三种主要类型:

  • 常量:tf.constant()
  • 变量:tf.Variable()
  • 占位符:tf.palceholder()

2 常量

2.1 调用语法

tf.constant(value, dtype=None, shape=None, name="Const", verify_shape=False)
  • value: 符合tf中定义的数据类型的常数值或者常数列表;
  • dtype:数据类型,可选;
  • shape:常量的形状,可选;
  • name:常量的名字,可选;
  • verify_shape:常量的形状是否可以被更改,默认不可更改;

constant()是一个函数,提供在tensorflow中定义常量(不可更改的张量)的方法。

常量是一个不可更新的张量,不需要初始化,在计算图中计算完后,运行会话即可赋值。

2.2 实例

import tensorflow as tf

# 常量
a0 = tf.constant(1,name='a0')
print(a0)

with tf.Session() as sess:
    a1 = sess.run(a0)
    print(a1)

2.3 运行结果

Tensor("a0:0", shape=(), dtype=int32)
1

2.4 小结

常量在定义时,以张量形式保存在计算图中,通过name='a0'标记来在计算图里区分张量,而当运行会话,计算后,便可获得所赋的值。


3 变量

3.1 调用语法

tf.Variable(self,
               initial_value=None,
               trainable=True,
               collections=None,
               validate_shape=True,
               caching_device=None,
               name=None,
               variable_def=None,
               dtype=None,
               expected_shape=None,
               import_scope=None)

tensorflow中的变量是通过Variable类来实现的,类初始化函数为tf.Variable(),而类的名字首字母要大写,这一点需要注意。

变量是一个可以更新的张量,常用于神经网络中的权重,不断迭代更新。

3.2 实例

import tensorflow as tf

# 变量
b0 = tf.Variable(2, tf.int16, name='b0')

with tf.Session() as sess:
    tf.global_variables_initializer().run() # 变量需要初始化
    b1 = sess.run(b0)
    print(b0)
    print(b1)

3.3 运行结果

<tf.Variable 'b0:0' shape=() dtype=int32_ref>
2

3.4 小结

变量是可以在会话中更新,需要初始化,这里的初始化可以全局初始化,也可以手动初始化每个变量,运行后,方可获得赋值。

下面是几种变量初始化方法:

# 手动初始化
ses.run(var1.initializer)
ses.run(var2.initializer)
ses.run(var3.initializer)

# 分步骤全局初始化
init_op = tf.global_variables_initializer()
sess.run(init_op)

# 简洁的初始化
var1.initializer.run()
tf.global_variables_initializer().run()

4 占位符

4.1 调用语法

tf.placeholder(dtype, shape=None, name=None)
  • dtype:表示tensorflow中的数据类型,如常用的tf.float32,tf.float64等数值类型;
  • shape:表示数据类型,默认的None是一个一维的数值,shape=[None,5],表示行不定,列是5;
  • name:张量名称;

占位符,用于声明一个张量的数据格式,告诉系统这里会有一个这种格式的张量,但是还没有给定具体数值,具体的数值要在正式运行的时候给到。占位变量是一种TensorFlow用来解决读取大量训练数据问题的机制,它允许你现在不用给它赋值,随着训练的开始,再把训练数据传送给训练网络学习。

4.2 实例

import tensorflow as tf

# 占位符
c0 = tf.placeholder(tf.float32,name = 'c0')
c1 = tf.placeholder(tf.float32,name = 'c1')
c2 = tf.add(c0,c1,name='c2')

with tf.Session() as sess:
    c3 = sess.run(c2,feed_dict = {c0:1.0,c1:2.0})  # 占位符在运算时喂入数据
    print(c2)
    print(c3)

4.3 运行结果

Tensor("c2:0", dtype=float32)
3.0

4.4 小结

占位符一般与feed_dict搭配使用,完成数据的输入。


5 计算图

一般在未定义计算图时,系统会自动维护一个默认的计算图,通过tf.get_default_graph()函数可以获取这个默认的计算图。当定义一个新的计算图时,计算图内的变量可以视作仅属于该计算图的局部变量,不会与其他计算图共享。

5.1 调用语法

import tensorflow as tf

graph1 = tf.Graph()
graph2 = tf.Graph()  # 直接无视默认缺省的Graph
# graph2 = tf.get_default_graph() 为其分配一个句柄

with graph1.as_default():
    pass

with graph2.as_default():
    pass

5.2 实例

import tensorflow as tf

# 计算图
d0 = tf.constant(2, tf.int16,name='d0')
d1 = tf.constant(4, tf.float32,name='d1')

graph = tf.Graph()
with graph.as_default():
    d0 = tf.Variable(8, tf.float32,name='d0')
    d1 = tf.Variable(tf.zeros([2,2], tf.float32),name='d1')
    
with tf.Session(graph=graph) as sess:
    tf.global_variables_initializer().run()
    print(sess.run(d0))
    print(sess.run(d1))

5.3 运行结果

8
[[ 0.  0.]
 [ 0.  0.]]

5.4 小结

可以看到,定义在默认计算图的张量并没有获得值,而在指定计算图内运行会话,将会得到该计算图内的张量值。

猜你喜欢

转载自www.cnblogs.com/ctgu/p/12731005.html