TF-day2 神经网络基础知识

来源:互联网 发布:淘宝网拍模特兼职 编辑:程序博客网 时间:2024/05/23 07:23

学习内容:

  1. 计算图 graph
  2. 张量 tensor
  3. 会话 session
  4. 变量 variable:常用的变量生成函数
  5. 占位符 placeholder

1.计算图 graph
tensorflow中的每一个计算都是计算图中的一个节点,而节点之间的边描述了计算之间的依赖关系。

g1 = tf.Graph()with g1.as_default():    v = tf.get_variable("v",shape=[1],dtype=tf.int32,initializer=tf.zeros_initializer())with tf.Session(graph=g1) as sess:    tf.global_variables_initializer().run()    with tf.variable_scope("",reuse=True):    print(sess.run(tf.get_variable("v",dtype=tf.int32)))

除了使用默认的图,tensorflow支持使用tf.Graph函数来生成新的计算图,不同的计算图上的张量、运算都不会共享。


2.张量
tensorflow在张量中并没有真正保存数字,它保存的是如何得到这些数字的计算过程。

import tensorflow as tfa = Falseb = tf.cast(a, tf.float32)print(b)c = tf.constant(21,dtype=tf.float32,shape = [1],name = "c")d = tf.constant([1,2],dtype=tf.float32,shape = [1,2],name = "d")result1 = tf.add(c,d,name = 'add')result2 = tf.add(b,d,name = 'add1')print(result1,result2)###Tensor("Cast:0", shape=(), dtype=float32)Tensor("add:0", shape=(1, 2), dtype=float32) Tensor("add1:0", shape=(1, 2), dtype=float32)

结果并不是返回的具体数字,而是一个张量的结构。三个属性:名字name,维度shape,类型dtype.
其中名字属性不仅是张量的唯一标识符,还表示它是怎么计算出来的。
“add:0”表示result1这个tensor是计算节点”add”输出的第一个结果。

3.会话session
graph和tensor是用来组织数据和运算过程的。执行定义好的运算需要用到会话session.

with tf.Session() as sess:    tf.global_variables_initializer().run()    print(sess.run(result2))    print(result1.eval())##[[ 1.  2.]][[ 22.  23.]]

4.变量 variable
在tensorflow中变量作用就是用来保存神经网络中的参数.Tensorflow通过tf.Variable和tf.get_variable()两种机制来创建或者获取变量.

4.1 tf.Variable

weights = tf.Variable(tf.random_normal([2,2],stddev=2.))with tf.Session() as sess:    tf.global_variables_initializer().run()   ##对所有变量初始化    print(sess.run(weights))##[[ 0.05168185  2.36352181] [ 2.82884121 -2.07777405]]

tensorflow随机生成函数:

##正太分布tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=dtypes.float32,seed=None,name=None) ##正态分布tf.truncated_normal(shape,mean=0.0,stddev=1.0,dtype=dtypes.float32,seed=None,name=None) ##平均分布tf.random_uniform(shape,minval=0,maxval=None,dtype=dtypes.float32,seed=None,name=None)##gamma分布tf.random_gamma()

tensorflow常数生成函数:

b1 = tf.Variable(tf.zeros(shape=[3]))b2 = tf.Variable(tf.ones([2,2]))b3 = tf.Variable(tf.fill([2,3],3))b4 = tf.Variable(tf.constant(2,dtype=tf.float32,shape=[1,2]))

4.2 tf.get_variable()
与上面部分功能基本一致,对应也有随机数生成函数和常数生成函数

tf.constant_initializer()tf.random_normai_initializer()tf.truncated_naomal_initializer()tf.random_uniformal_initializer()....

两者最大区别在于:tf.get_variable函数,其参数中变量名字是必须填写的.当出现与上文中重复的变量名时,就会报错.如果需要通过tf.get_variable获取一个已经创建的变量,需要通过tf.variable_scope函数来生成一个上下文管理器,并明确指定,在这个上下文管理器中,tf.get_variable将直接获得已经生成的变量.

  • 对于 tf.get_variable_scope()
  • 参数 reuse = True时,tf.get_variable将直接获取已经创建的变量.若变量不存在则报错
  • 参数 reuse = False/None时,tf.get_variable将创建新的变量.若变量已存在,则报错.

所有的变量都会自动的加到Graphkeys.TRAINABLE_VARIABLES这个集合(collection)中,通过 tf.all_valiables函数可以拿到当前计算图上所有的变量。如果声明变量时,参数trainable =True,则该变量就加入到Graphkeys.TRAINABLE_VARIABLES,若trainable =False,则未加入进去。通过 tf.trainable_variables 函数可以得到所有需要优化的参数。


5.占位符 placeholder
每生成一个常量,tensorflow都会在计算图中增加一个节点,这样计算图会非常大.为避免这样的问题,tensorflow提供了placeholder机制用于提供输入数据. placeholder相当于定义了一个位置,这个位置中的数据在程序运行时再指定.

import tensorflow as tfw1 = tf.get_variable("w1",shape=[2,3],initializer = tf.random_normal_initializer())w2 = tf.Variable(tf.random_normal([3,3],stddev = 1))x = tf.placeholder(tf.float32,name = 'input')a = tf.matmul(x,w1)y = tf.matmul(a,w2)sess = tf.Session()init_op = tf.global_variables_initializer()sess.run(init_op)print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))

在程序计算时,需要提供一个feed_dict来指定x的取值.

原创粉丝点击