1. tf.placeholder、tf.Variable、tf.get_variable
tf.placeholder(dtype, shape=None, name=None)
仅仅作为一种占位符, 用于得到传递进来的真实的训练样本,赋值一般用sess.run(feed_dict = {x:xs, y_:ys}),其中x,y_是用placeholder创建出来的.
tf.Variable(initial_value=None, trainable=True, name=None, dtype=None, expected_shape=None)
主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias);声明时,必须提供初始值;在真实训练时,其值是会改变的;
tf.get_variable(name, shape=None, dtype=None, initializer=None, trainable=True)
获取已经存在的变量,如果不存在,就新建一个。使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错。
当我们需要共享变量的时候,需要使用tf.get_variable()。必须和reuse和tf.variable_scope()配合使用
import tensorflow as tf
with tf.variable_scope("scope1"):
w1 = tf.get_variable("w1", shape=[])
w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
w1_p = tf.get_variable("w1", shape=[])
w2_p = tf.Variable(1.0, name="w2")
print(w1 is w1_p, w2 is w2_p)
#输出
#True False
2.tf.name_scope()与tf.variable_scope()
tf.name_scope(name)
此函数作用是共享变量。在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,但只限于tf.Variable()的变量,tf.get_variable不受其约束。
tf.variable_scope(name_or_scope,reuse=None)
共享变量与命名空间管理,当reuse为False或者None时(这也是默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。简而言之,reuse=False时,tf.variable_scope创建变量;reuse=True时,tf.variable_scope获取变量。
3.随机数生成函数
函数名称 | 随机数分布 | 主要参数 | 样例 |
tf.random_normal | 正态分布 | shape、mean(0)、stddev(1.0)、dtype(float32)、seed(None)、name(None) | tf.random_normal(shape = [2, 3], name=‘a’) |
tf.truncated_normal | 截断的正态分布,如果随机出来的值偏离平均值超过2个标准差,那么这个数将被重新随机 | shape、mean(0)、stddev(1.0)、dtype(float32)、seed(None)、name(None) | tf.truncated_normal(shape = [2, 3], name=‘a’) |
tf.random_uniform | 均匀分布 | shape、minval(0)、maxval(None)、dtype(float32)、seed(None)、name(None) | tf.random_uniform(shape = [2, 3], name=‘a’) |
tf.random_gamma | Gamma分布 | shape、alpha、beta(None)、dtype(float32)、seed(None)、name(None) |
tf.random_gamma([10], [0.5, 1.5]) # a 的形状为[10, 2], 其中每个 slice [:, 0] 和 [:, 1] 表示从每个分布中抽取的样本 |
4.常数生成函数
函数名称 | 功能 | 主要参数 | 样例 |
tf.zeros | 产生全0的数组 | shape、dtype(float32)、name(None) | tf.zeros(shape = [2,3], dtype = 'int32') |
tf.zeros_like | 产生于给定tensor类型大小一致的全0数组 | tensor、dtype(None)、name(None) | tf.zeros_like(a) |
tf.ones | 产生全1的数组 | shape、dtype(float32)、name(None) | tf.ones(shape = [2,3], dtype = 'int32') |
tf.ones_like | 产生于给定tensor类型大小一致的全1数组 | tensor、dtype(None)、name(None) | tf.ones_like(a) |
tf.fill | 产生一个全部为给定数字的数组 | shape、value、name(None) | tf.fill(shape = [2,3],9) |
tf.constant | 产生一个给定值的常量 | value、(shape(None)、dtype(float32)、name(‘Const’) |
tf.constant([1,2,3]) tf.constant(0, shape = [2,2]) |
5.初始化方法
tf.constant_initializer(value, dtype)
tf.zeros_initializer(dtype)
tf.ones_initializer(dtype)
tf.random_normal_initializer(mean, stddev, seed, dtype)
tf.truncated_normal_initializer(mean, stddev, seed, dtype)
tf.random_uniform_initializer(minval, maxval, seed, dtype)
tf.uniform_unit_scaling_initializer(factor, seed, dtype) #不需要指定最大最小值
tf.variance_scaling_initializer(mode=,distribution,seed,dtype) #生成截断正太分布和均匀分布
import tensorflow as tf
init_uniform_unit = tf.uniform_unit_scaling_initializer(factor=1.0, seed=None, dtype=tf.float32)
with tf.Session() as sess:
x = tf.get_variable('x', shape=[10], initializer=init_uniform_unit)
x.initializer.run()
print(x.eval())
6.初始化模型参数
tf.global_variables_initializer()
初始化所有的变量。返回一个初始化所有全局变量的操作(Op)
tf.local_variables_initializer()
返回一个初始化所有局部变量的操作(Op)。
7.collection相关函数
tf.add_to_collection(name, value)
此函数将元素添加到列表中。如果不存在,创建一个新的列表
tf.get_collection(name)
此函数获取列表
tf.add_n(inputs)
此函数将元素相加并返回
import tensorflow as tf;
v1 = tf.get_variable(name='v1', shape=[1], initializer=tf.constant_initializer(0))
tf.add_to_collection('loss', v1)
v2 = tf.get_variable(name='v2', shape=[1], initializer=tf.constant_initializer(2))
tf.add_to_collection('loss', v2)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print sess.run(tf.add_n(tf.get_collection('loss')))