tf.trainable_variables返回的是可以用来训练的变量列表 tf.all_variables返回的是所有变量的列表…
tf.trainable_variables()  返回的是 所有需要训练的变量列表 tf.all_variables() 返回的是 所有变量的列表 v = tf.Variable(0, name='v') v1 = tf.Variable(tf.constant(5, shape=[1], dtype=tf.float32), name='v1') global_step = tf.Variable(6, name='global_step', trainable=False) # 声明不是训…
https://blog.csdn.net/shwan_ma/article/details/78879620 一般来说,打印tensorflow变量的函数有两个:tf.trainable_variables () 和 tf.all_variables()不同的是:tf.trainable_variables () 指的是需要训练的变量tf.all_variables() 指的是所有变量 一般而言,我们更关注需要训练的训练变量:值得注意的是,在输出变量名时,要对整个graph进行初始化 一.打印…
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护一个条理清晰的graph非常重要. ==因此,tensorflow中用tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()几个…
1. tf.add(x,  y, name) Args: x: A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `uint8`, `int8`, `int16`, `int32`, `int64`, `complex64`, `complex128`, `string`. y: A `Tensor`. Must have the same type as `x`.…
https://blog.csdn.net/lanchunhui/article/details/61712830 https://www.cnblogs.com/silence-tommy/p/7029561.html 二者的主要区别在于: tf.Variable:主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias): 声明时,必须提供初始值: 名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自然事先需要指定初…
1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 当然,变量也可以通过tf.Varivale来创建.当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价…
函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)   Defined in tensorflow/python/ops/state_ops.py.   将 value 赋值给 ref,并输出 ref,即 ref = value:   这使得需要使用复位值的连续操作变简单   Defined in tensorflow/python/framework/tensor_shape.py. Arg…
tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable(创建变量)与tf.get_variable(创建变量 或 复用变量) TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 变量可以通过tf.Varivale来创建.当tf.get_variable用于变量创建时,和tf.…
import tensorflow as tf import numpy as np W = tf.Variable([[2,1,8],[1,2,5]], dtype=tf.float32, name='weights') b = tf.Variable([[1,2,5]], dtype=tf.float32, name='biases') init= tf.global_variables_initializer() saver = tf.train.Saver() with tf.Sessi…
tf.expand_dims和tf.squeeze函数 一.tf.expand_dims() Function tf.expand_dims(input, axis=None, name=None, dim=None) Inserts a dimension of 1 into a tensor’s shape. 在第axis位置增加一个维度 Given a tensor input, this operation inserts a dimension of 1 at the dimensio…
首先我们分析一下下面的代码: import tensorflow as tf import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]]) b=np.float32(np.random.randn(3,2)) #c=tf.matmul(a,b) c=tf.multiply(a,b) init=tf.global_variables_initializer() with tf.Session() as sess: print(c.eva…
a = tf.Variable(0.0,dtype=tf.float32) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(a)) a = tf.assign(a,10) print(sess.run(a)) a = tf.assign(a,20) print(sess.run(a)) 0.0 10.0 20.0 a = tf.Variable(1,dtype=tf.flo…
网络层中变量存在两个问题: 随着层数的增多,导致变量名的增多: 在调用函数的时候,会重复生成变量,但他们存储的都是一样的变量.   tf.variable不能解决这个问题. 变量作用域使用tf.variable_scope和tf.get_variable完美解决了上边的这个问题. 网络层数很多,但一般结构就那么几种.我们使用tf.get_variable方法,变量会在前边加上作用域,类似于文件系统中的“/”. tf.get_variable在第二次使用某个变量时,可以用reuse=True来共享…
https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 当然,变量也可以通过…
官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods Tensor.eval() and Operation.run() will use that session to run ops. 翻译一下就是:tf.InteractiveSes…
tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3) b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3) ab1 = tf.concat([a,b], axis=0) # shape(4,3) ab2 = t…
____tz_zs tf.random_normal 从正态分布中输出随机值. . <span style="font-size:16px;">random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)</span> . shape:一个一维整数张量或Python数组.代表张量的形状. mean:数据类型为dtype的张量值或Python值.是正态分布的均值. std…
tf.add_to_collection(name, value)  用来把一个value放入名称是'name'的集合,组成一个列表; tf.get_collection(key, scope=None) 用来获取一个名称是'key'的集合中的所有元素,返回的是一个列表,列表的顺序是按照变量放入集合中的先后;   scope参数可选,表示的是名称空间(名称域),如果指定,就返回名称域中所有放入'key'的变量的列表,不指定则返回所有变量. tf.add_n(inputs, name=None),…
tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/api_docs/python/tf/unstack 解释:这是一个对矩阵进行分解的函数,以下为关键参数解释: value:代表需要分解的矩阵变量(其实就是一个多维数组,一般为二维): axis:指明对矩阵的哪个维度进行分解. 要理解tf.unstack函数,我们不妨先来看看tf.stack函数.T…
tf.Session()和tf.InteractiveSession()的区别 官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods Tensor.eval() and Operation.run() will use that sess…
1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow as tf import numpy as np x = [[1, 2], [3, 4]] Y = tf.split(axis=1, num_or_size_splits=2, value=x) sess = tf.Session() for y in Y: print(sess.run(y))…
tf.trainable_variable() 此函数返回的是需要训练的变量列表 tf.all_variable() 此函数返回的是所有变量列表 v = tf.Variable(tf.constant(0.0, shape=[1], dtype=tf.float32), name='v') v1 = tf.Variable(tf.constant(5, shape=[1], dtype=tf.float32), name='v1') global_step = tf.Variable(tf.co…
tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_scope() 和tf.name_scope()作用一样,不过包括tf.get_variable()的变量和tf.Variable()的变量 在同一个程序中多次调用,在第一次调用之后需要将reuse参数设置为True with tf.variable_scope("one"): a = tf…
tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value的变量. tf.get_variable(name,shape,dtype,initializer,trainable) 此函数用于定义图变量.获取已经存在的变量,如果不存在,就新建一个 参数: name:名称 shape:数据形状. dtype:数据类型.常用的tf.float32,tf.float64等数…
本文来自 guotong1988 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/guotong1988/article/details/77622790 import tensorflow as tf c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) result = tf.segment_sum(c, tf.constant([0, 0, 1]))#第二个参数长度必须为3 result_ = tf.s…
ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: 在使用tensorflow 中的tf.variable_scope和tf.get_variable搭建网络时,重复运行程序会报以上的ValueError错误,这是因为第二次运行时,内存中已经存在名字相同的层或者参数,发生了冲突,所以会提示…
1.tf.truncated_normal使用方法 tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) 从截断的正态分布中输出随机值. 生成的值服从具有指定平均值和标准偏差的正态分布,如果生成的值大于平均值2个标准偏差的值则丢弃重新选择. 在正态分布的曲线中,横轴区间(μ-σ,μ+σ)内的面积为68.268949%. 横轴区间(μ-2σ,μ+2σ)内的面积为95.4499…
tf.nn.softmax softmax是神经网络的最后一层将实数空间映射到概率空间的常用方法,公式如下: \[ softmax(x)_i=\frac{exp(x_i)}{\sum_jexp(x_j)} \] 本文意于分析tensorflow中的tf.nn.softmax(),关于softmax的具体推导和相关知识点,参照其它文章. tensorflow的tf.nn.softmax()函数实现位于这里,可以看到,实现起来相当简明: tf.exp(logits)/tf.reduce_sum(tf…
Update:2019/09/21 使用 tf.keras 时,请使用 tf.keras.optimizers 里面的优化器,不要使用 tf.train 里面的优化器,不然学习率衰减会出现问题. 使用 tf.keras 过程中,如果要使用 learning rate decay,不要使用 tf.train.AdamOptimizer() 等 tf.train 内的优化器,因为学习率的命名不同,导致 tf.keras 中学习率衰减的函数无法使用,一般都会报错 "AttributeError: 'T…