tensorflow中使用tf.variable_scope和tf.get_variable的ValueError

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错误,这是因为第二次运行时,内存中已经存在名字相同的层或者参数,发生了冲突,所以会提示是否要reuse。

解决方案:

(1)如果你用的是spyder编译的程序,重新启动一下python console就好了

(2)终端运行程序则不会出现上述问题

原文地址:https://www.cnblogs.com/wktwj/p/8340711.html

时间: 2025-01-08 02:40:50

tensorflow中使用tf.variable_scope和tf.get_variable的ValueError的相关文章

tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope关系

1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 当然,变量也可以通过tf.Varivale来创建.当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价

[tf] tensorflow中dropout小坑记录

tensorflow中dropout小坑记录 几天看别人写的代码,有几行总觉得没什么用,自己写了小程序测试了下,果然. 虽然平时这么写的人不多,但是还是记录下吧. 对tensorflow使用时要转变下思维,和平时写的C++不太一样,只是建立了一个静态图. 在list中进行for循环,内部操作是局部变量操作,与原list无关. tf.nn.dropout操作,在随机舍掉部分节点的同时为了保证输出值的平稳会将保留下的节点数据除以keep_prob进行扩大. 赋值操作即使赋值给原数据,也是两个op节点

TensorFlow 中的 tf.train.exponential_decay() 指数衰减法

exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None) 使用方式为 tf.train.exponential_decay( ) 在 Tensorflow 中,exponential_decay()是应用于学习率的指数衰减函数. 在训练模型时,通常建议随着训练的进行逐步降低学习率.该函数需要`global_step`值来计算衰减的学习速率. 该函数返回衰减后

tf.train.Saver()-tensorflow中模型的保存及读取

作用:训练网络之后保存训练好的模型,以及在程序中读取已保存好的模型 使用步骤: 实例化一个Saver对象 saver = tf.train.Saver() 在训练过程中,定期调用saver.save方法,像文件夹中写入包含当前模型中所有可训练变量的checkpoint文件 saver.save(sess,FLAGG.train_dir,global_step=step) 之后可以使用saver.restore()方法,重载模型的参数,继续训练或者用于测试数据 saver.restore(sess

【tf.keras】tf.keras使用tensorflow中定义的optimizer

我的 tensorflow+keras 版本: print(tf.VERSION) # '1.10.0' print(tf.keras.__version__) # '2.1.6-tf' tf.keras 没有实现 AdamW,即 Adam with Weight decay.论文<DECOUPLED WEIGHT DECAY REGULARIZATION>提出,在使用 Adam 时,weight decay 不等于 L2 regularization.具体可以参见 当前训练神经网络最快的方式

TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同

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 =

tensorflow中四种不同交叉熵函数tf.nn.softmax_cross_entropy_with_logits()

Tensorflow中的交叉熵函数tensorflow中自带四种交叉熵函数,可以轻松的实现交叉熵的计算. tf.nn.softmax_cross_entropy_with_logits() tf.nn.sparse_softmax_cross_entropy_with_logits() tf.nn.sigmoid_cross_entropy_with_logits() tf.nn.weighted_cross_entropy_with_logits()注意:tensorflow交叉熵计算函数输入

TensorFlow 学习(二)—— tf Graph tf Session 与 tf Session run

session: with tf.Session() as sess:/ tf.InteractiveSession() 初始化: tf.global_variables_initializer() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) 1 2 0. tf.Graph 命名空间与 operation name(oper.name 获取操作名): c_0 = tf.constant(0, nam

TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

二者的主要区别在于: tf.Variable:主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias): 声明时,必须提供初始值: 名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自然事先需要指定初始值:  weights = tf.Variable( tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1./math.sqrt(float(IMAGE_