我正在使用中的flowers数据集示例,并尝试重用模型的共享权重
def my_cnn(images, num_classes, is_training): # is_training is not used...
with slim.arg_scope([slim.max_pool2d], kernel_size=[3, 3], stride=2):
net = slim.conv2d(images, 64, [5, 5])
net = slim.max_pool2d(net)
net = slim.conv2d(net, 64, [
我正在开发一个使用tensorflow实现的模型。一切都进行得很顺利,直到我不得不实现一个嵌套函数(比如)
使用嵌套的FOR循环。例如,如下所示:
for k in range(n):
Gk = tf.convert_to_tensor(G[k], dtype=self.dtype)
Gk = tf.tile(tf.expand_dims(Gk,0),[tf.shape(trueX)[0],1])
for j in range(m):
pre_Y = self.build_nn( Gk, 200, '{}phi2'.format(j
我的代码中有一个问题,我希望在我的lstm_decoder中共享权重(所以基本上只使用一个LSTM)。我知道网上有一些关于这方面的资源,但我仍然无法理解为什么以下内容不共享权重:
initial_input = tf.unstack(tf.zeros(shape=(1,1,hidden_size2)))
for index in range(window_size):
with tf.variable_scope('lstm_cell_decoder', reuse = index > 0):
rnn_decoder_cell = tf.nn.r
各位,我要试试这个
loss = tf.reduce_mean(tf.square(outputs - y)) # MSE
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train = optimizer.minimize(loss)
但是我得到了一个错误
Variable rnn/output_projection_wrapper/basic_lstm_cell/kernel/Adam/ already exists, disallowed. Did you mean to set reuse=True or
有什么想法可以解决下面显示的问题吗?根据我在web上找到的信息,它与重用tensorflow作用域的问题有关,但是什么都不起作用。
ValueError: Variable rnn/basic_rnn_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "/code/backend/management/commands/RNN.py", line 370,
我正在测试tf.variable_scope对象,以便在同一个session.run调用中多次重用网络: data1 = tf.constant([[[3.,5.,6.,1.]]],dtype=tf.float64)
data2 = tf.constant(np.zeros((1,5)))
def networkS(input_1, input_2, reuse):
#this is a multi-input network using tf.keras api
with tf.variable_scope("test", reuse=reuse):
我正在Variables上试用Variables示例,但是当我尝试使用tf.Session打印时,我得到了一个错误
ValueError: Variable my_int_variable already exists, disallowed. Did
you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
我确信我没有在其他任何地方定义,并且删除了Spyder上的所有变量。
import tensorflow as tf
my_int_variable = tf.get_v
我正在尝试创建一个过滤器,它依赖于培训的当前global_step,但我没有正确地这样做。
首先,我不能在下面的代码中使用tf.train.get_or_create_global_step(),因为它会引发
ValueError: Variable global_step already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
这就是我尝试用tf.get_default_graph().get_name_scope
在训练过程中,我试图在每一个时代之后进行验证。
我创建的图表如下:
import tensorflow as tf
from networks import densenet
from networks.densenet_utils import dense_arg_scope
with tf.variable_scope('scope') as scope:
with slim.arg_scope(dense_arg_scope()):
logits_train, _ = densenet(images, blocks=networks[
我是第一次使用google colab,我正在测试TPU。但是当我运行我的keras模型时,我得到了将keras模型转换为tpu模型的错误ValueError: Variable tpu_140268413570632//kernel/0 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?,我使用了下面的代码model = tf.contrib.tpu.keras_to_tpu_model(keras_model, strategy=tf.contrib.
我正在尝试在tensorflow 2.4.1中实现PReLU激活,如下所示:
跟踪误差
ValueError: Variable alpha already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?
这是我的密码
def prelu(_x):
alphas = tf.compat.v1.get_variable('alpha', _x.get_shape()[-1],
initializer=tf.constan
def build_metric(self):
with tf.variable_scope('fc', reuse=tf.AUTO_REUSE):
response_m = self.response
shape = response_m.get_shape().as_list()[1:3]
output_list = []
for i in range(shape[0]):
for j in range(shape[1]):
t1 = self.instance_embeds[:
我在运行tf.graph时遇到了这个错误
只有当形状未完全定义时,才能传递不需要参数的初始化函数。
我能够复制错误:
m = tf.placeholder(shape=[None, None], dtype=tf.int32)
with tf.variable_scope("foo", reuse=tf.AUTO_REUSE):
v = tf.get_variable("v", shape=m.shape)
with tf.Session() as sess:
sess.run(tf.global_variables_initializ
我想将几个不同的输入传递到一个可重用的tensorflow架构(解码器)中。为此,我使用了一个for循环,在该循环中,我将输入输入到模型中。但是,我没有重用layer变量,而是为每个循环迭代创建变量。假设代码如下: import tensorflow as tf
for i in range(5):
decoder(input=input, is_training=is_training) 而解码器是: def decoder(self, input, is_training):
with tf.variable_scope("physics", reus
怎么做这样的事?
nn = get_networks()
A = nn(X_input)
B = nn(X_other_input)
C = A + B
model = ...
所以nn中的所有张量都是相同的,只有输入训练分支是不同的?
在纯tensorflow中,您可以用
tf.variable_scope('something', reuse=tf.AUTO_REUSE):
define stuff here
小心地命名你们的图层。
但是基本上,首先您可以构造nn,因为您不能将一个非调用层传递给一个层调用!
例如:
In [21]: tf.keras.lay
如果变量的类型被定义为浮点型,tf.scatter_update似乎不会更新该变量。这是一个你可以尝试的代码:
import tensorflow as tf
def cond(size, i):
return tf.less(i,size)
def body(size, i):
b=2*7.5+c
with tf.variable_scope("a", reuse=tf.AUTO_REUSE):
a = tf.get_variable("a",[6],dtype=tf.float32)
a = t
我正在尝试构建一个自定义的变分自动编码器网络,在其中我使用编码层的权重转置来初始化解码器权重,我找不到tf.contrib.layers.fully_connected原生的东西,所以我使用tf.assign代替,这是我的层代码:
def inference_network(inputs, hidden_units, n_outputs):
"""Layer definition for the encoder layer."""
net = inputs
with tf.variable_scope('infer
我目前正在研究分布式tensorflow,考虑两个工作进程,并面临在这两个工作进程之间共享变量的问题。我找到了tf.get_collection/tf.add_collection,但仍然无法获得两个进程之间共享的变量值。
添加一些关于如何在分布式Tensorflow中的工作进程之间共享数据的详细信息:
def create_variable(layer_shape):
with tf.variable_scope("share_lay"):
layers = tf.get_variable("layers", s
我尝试在两个循环中对tf节点执行一些操作。但是,TensorFlow倾向于在循环的每次迭代中为图形创建更多节点。有没有办法用Python的方式在tf节点上做多个操作,比如说不用创建所有的节点,而只是简单地迭代循环并进行操作?
以下面的代码为例,似乎TensorFlow在执行循环时创建了所有节点(此处为xi、xj和loss ):
def get_marginal_loss(features, labels, threshold, margin):
with tf.variable_scope("marginal", reuse=tf.AUTO_REUSE):
xi = t
我正在使用双向RNN为编码器和解码器实现一个编码器解码器模型。由于我在编码器端初始化双向RNN,并且与双向RNN关联的权重和向量已经初始化,所以当我尝试在解码器端初始化另一个实例时,我得到以下错误: ValueError: Variable bidirectional_rnn/fw/gru_cell/w_ru already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? 我试着在它自己的name_scope中定义每一个,如下所示,但是没有用: def enc(mes