Tensorflow:在嵌套范围内按名称获取变量或张量

时间:2017-08-25 15:14:37

标签: numpy machine-learning tensorflow

我有以下代码,用于定义嵌套的tf.variable_scope()

def function(inputs)
   with tf.variable_scope("1st") as scope:
     #define some variables here using tf.get_variable()
   with tf.variable_scope("2nd") as scope:
     #define some variables here using tf.get_variable()
     my_wanted_variable = tf.get_variable('my_wanted_variable',[dim0, 
                                             dim1], tf.float(32),
                                         tf.constant_initializer(0.0))

在另一堂课中,我想获得my_wanted_variable,我使用

with tf.variable_scope("function/2nd", reuse=True):
  got_my_wanted_variable = tf.get_variable("my_wanted_variable") 

我被告知

  

ValueError:变量function / 2nd / my_wanted_variable不存在,   或者不是用tf.get_variable()创建的。你的意思是设置   在VarScope中重用=无?

如果我在提取reuse=None时设置了my_wanted_variable,那么

  

ValueError:新变量的形状(function / 2nd / my_wanted_variable)   必须完全定义,而是。

那么,如何在嵌套范围内按名称获取变量(或张量)。

添加调试信息: 我使用print(xxx.name)确实看到了他们的名字和范围,我发现尽管他们的范围是正确的,例如xxx/function/2nd。范围1st2nd中定义的所有变量均未按其指定的名称命名,例如,my_wanted_variablexxx/function/2nd/sub:0

2 个答案:

答案 0 :(得分:0)

EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 1 libobjc.A.dylib objc_msgSend + 115024 2 UIKit -[_UIKeyboardTextSelectionController selectTextWithGranularity:atPoint:executionContext:] + 5209476 3 UIKit -[_UIKeyboardBasedNonEditableTextSelectionGestureController oneFingerForcePan:] + 5624368 4 UIKit -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 6166732 5 UIKit _UIGestureRecognizerSendTargetActions + 6181120 6 UIKit _UIGestureRecognizerSendActions + 1700016 7 UIKit -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 266176 8 UIKit _UIGestureEnvironmentUpdate + 6116260 9 UIKit -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 6115188 10 UIKit -[UIGestureEnvironment _updateGesturesForEvent:window:] + 6111776 11 UIKit -[UIWindow sendEvent:] + 258892 12 UIKit -[UIApplication sendEvent:] + 65404 13 UIKit __dispatchPreprocessedEventFromEventQueue + 8428060 14 UIKit __handleEventQueue + 8405368 15 UIKit __handleHIDEventFetcherDrain + 8406436 16 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 898088 17 CoreFoundation __CFRunLoopDoSources0 + 896408 18 CoreFoundation __CFRunLoopRun + 887204 19 CoreFoundation CFRunLoopRunSpecific + 36256 20 GraphicsServices GSEventRunModal + 49264 21 UIKit UIApplicationMain + 478360 22 XamarinApp.iOS (wrapper_managed-to-native)_UIKit.UIApplication:UIApplicationMain (<unknown>:1) 23 XamarinApp.iOS Main (UIApplication.cs:79) 24 XamarinApp.iOS XamarinApp_iOS_XamarinApp_iOS_Application_Main_string__ + 39656 25 XamarinApp.iOS wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 634656 26 Mono 4316120312 + 378104 27 Mono 4316622816 + 880608 28 Mono 4316635652 + 893444 29 Mono 4316004656 + 262448 30 XamarinApp.iOS xamarin_main (monotouch-main.m:480) 31 XamarinApp.iOS main (main.m:131) 32 libdyld.dylib start + 17816 对于每个变量都是正常的(它表示端点)。 名称sub并不奇怪,它只是表明你没有明确地命名变量,所以它给你使用的操作的名称(可能是:0)给张量。

明确地使用名为name =&#34; my_wanted_variable&#34;的参数。首先尝试没有范围,以确保它被恰当地命名。然后使用print tf.sub()或检查nn.name对象的节点进行检查。

答案 1 :(得分:0)

或者我们可以使用

检查调试模式中的所有张量
with tf.Session() as sess:
    model = tf.train.import_meta_graph('./model.ckpt-30000.meta')
    model.restore(sess, tf.train.latest_checkpoint('./'))
graph = tf.get_default_graph()

然后,在调试模式下,

graph._collections

这将招募所有context_tensor,training_op,trainable_variables,变量。

或更好的是:

[tensor.name for tensor in tf.get_default_graph().as_graph_def().node]
相关问题