我一直在阅读TensorFlow上的教程,他们在那里写道
with tf.name_scope('read_inputs') as scope:
# something
示例
a = tf.constant(5)
和
with tf.name_scope('s1') as scope:
a = tf.constant(5)
似乎也有同样的效果。那么,我们为什么要使用name_scope呢
我想用一些张量的值来改变variable_scope。对于一个简单的例子,我定义了如下非常简单的代码:
import tensorflow as tf
def calculate_variable(scope):
with tf.variable_scope(scope or type(self).__name__, reuse=tf.AUTO_REUSE):
w = tf.get_variable('ww', shape=[5], initializer=tf.truncated_normal_initializer(mean=0.0, stddev
我想将几个不同的输入传递到一个可重用的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
各位,我要试试这个
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
我的代码中有一个问题,我希望在我的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
我正在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
在这两种场景中,变量Test是共享的吗?
with tf.name_scope("ns1"):
with tf.variable_scope("vs1"):
var = tf.get_variable("Test", [1,2,3])
with tf.name_scope("ns2"):
with tf.variable_scope("vs1", reuse=True):
var = tf.get_variable("Test", [1,2,3])
和
with tf.na
我尝试对不同的输入使用相同的LSTM架构,因此在展开双向LSTM的同时展开不同的输入时传递相同的单元。我不确定它是否创建了两个完全不同的LSTM网络。看起来我的Graph中有两个不同的节点。我的代码和图形如下所示: def get_multirnn_cell(self):
cells = []
for _ in range(config.n_layers):
cell = tf.nn.rnn_cell.LSTMCell(config.n_hidden, initializer=tf.glorot_uniform_initializer())
d
我想在matlab中调用tensorflow的python (参见)。matlab不支持"with“语句。如果没有"with“语句,我就无法创建tf.variable_scope。我试过以下两种密码,但两者都不起作用。有什么解决办法吗?
Python:
import tensorflow as tf
with tf.variable_scope('123') as vs:
print(vs.name) # OK
vs2 = tf.variable_scope('456')
print(vs2.name) # AttributeErro
我试图在多个GPU上运行一个简单的前馈网络(异步更新共享权重)。
然而,我无法让重量被分享。
从我所做的研究来看,我只需要在variable_scope上设置variable_scope,但这似乎不起作用:
for i_, gpu_id in enumerate(gpus):
with tf.device(gpu_id):
# [Build graph in here.]
with variable_scope.variable_scope(variable_scope.get_variable_scope(), reuse=i_>0):
以下代码的工作方式与预期一致:
import tensorflow as tf
with tf.variable_scope('layer123'):
v = tf.get_variable('v', [], initializer=tf.constant_initializer(3., tf.float32))
w = v * 2
print(v.name) # Prints layer123/v:0
print(w.name) # Prints layer123/mul:0
但是,当我尝试执行以下操作时:
with tf.var
目前,我正在Tf库中使用经过预先训练的VGG模型。我的动机是为这个netowrk生成一个给定图像的对抗性示例。任务摘要如下:
x= tf.placeholder(shape=(None, 32, 32,3), dtype=tf.float32)
for i in range(2):
logits= vgg.vgg_16(x, is_training=False, spatial_squeeze=False, fc_conv_padding='SAME')
x = x + learning_rate*cal_gradient_of_logits_wrt_x(lo
我正在使用Colab运行文本分析代码。我想从那里得到tensorflow_hub。但是,在任何时候运行包含以下代码的块:
module = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
我知道这个错误:
RuntimeError: variable_scope module_8/ was unused but the
corresponding name_scope was already taken.
如果你知道如何修复这个错误,我很感激?
我尝试在两个循环中对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
我正在尝试创建一个过滤器,它依赖于培训的当前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
我想用CW算法训练一些对抗性的例子,我用了一个来自的例子和一个来自的CW实现。但是我遇到了一个关于tf.zeros_initializer的错误
ValueError: The initializer passed is not valid. It should be a callable with no arguments and the shape should not be provided or an instance of
'tf.keras.initializers.*' and `shape` should be fully defined.
编辑:,似乎非完全
我正在测试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):