tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享. tf通过 tf.get_variable() 可以建立或者获取一个共享的变量. tf.get_variable函数的作用从tf的注释里就可以看出来-- 'Gets an existing variable with this name or create a new one'. 与 tf.get_variable 函数相对的还有一个 tf.Variable 函数,两者的区别是: tf.Va…
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regularizerd()函数在tf 2.x版本中被弃用了. 两者都能用来L2正则化处理,但运算有一点不同. import tensorflow as tf sess = InteractiveSession() a = tf.constant([1, 2, 3], dtype=tf.float32) b =…
What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, tf.name_scope. Why: 在自己的编写代码过程中, 用如下代码进行变量生成并进行卷积操作: import tensorflow as tf import numpy as np def my_conv2d(data, name, kh, kw, sh, sw, n_out): n_in…
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…
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行一些描述性语言,跟html很像,很适合用标记性语言来描述. Tensorflow是有向图,是一个有向无环图.张量为边,操作为点,数据在图中流动. Tensorflow为每个结点都起了唯一的一个名字. import tensorflow as tf a = tf.constant(3) # name=…
#-*-coding:utf8-*- __author = "buyizhiyou" __date = "2017-11-21" import random, time, os, decoder from PIL import Image import numpy as np import tensorflow as tf import pdb import decoder import random ''' 在汉字ocr项目中,利用基于attention的enco…
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错误,这是因为第二次运行时,内存中已经存在名字相同的层或者参数,发生了冲突,所以会提示…
import tensorflow as tf with tf.variable_scope('v_scope',reuse=True) as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) bias1 = tf.get_variable('bias', shape=[3]) # 下面来共享上面已经定义好的变量 # note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错 with…
# tensorflow中的两种定义scope(命名变量)的方式tf.get_variable和tf.Variable.Tensorflow当中有两种途径生成变量 variable import tensorflow as tf #T1法 tf.name_scope() with tf.name_scope("a_name_scope"): initializer = tf.constant_initializer(value=1) #定义常量 var1 = tf.get_variab…
tf.add_to_collection(name, value)  用来把一个value放入名称是'name'的集合,组成一个列表; tf.get_collection(key, scope=None) 用来获取一个名称是'key'的集合中的所有元素,返回的是一个列表,列表的顺序是按照变量放入集合中的先后;   scope参数可选,表示的是名称空间(名称域),如果指定,就返回名称域中所有放入'key'的变量的列表,不指定则返回所有变量. tf.add_n(inputs, name=None),…