【问题标题】:How to get two variables list from two different neural networks如何从两个不同的神经网络中获取两个变量列表
【发布时间】:2017-08-24 00:28:30
【问题描述】:

通常,如果我们在一个类中通过神经网络定义一个函数,那么在另一个类中,如果我们需要函数的参数或变量列表,在tensorflow中可以使用tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="function name"),方便又熟悉,虽然我猜还有很多其他更有效的方法可以做到这一点。

然而,在某些情况下,我们可能需要定义一个基于两个不同神经网络的函数,比如F(x) = F(NN_1(x), NN_2(x)),然后在另一个类中,获取两者的两个变量列表的正确方法是什么@987654323 @ 和 NN_2()?很明显,在这里使用tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="function name 会导致在NN_1NN_2 的两个变量列表中插入F(x) 的混合变量列表。

def function()
    with tf.name_scope(function):
        with tf.name_scope(subfunction_1):
           neural_network_1
        with tf.name_scope(subfunction_2):
           neural_network_2

【问题讨论】:

    标签: numpy machine-learning tensorflow


    【解决方案1】:

    在名称范围树中,您可以通过以下方式访问各个范围变量:

    vars_1 = tf.get_collection(
        tf.GraphKeys.TRAINABLE_VARIABLES, scope="function_name/subfunction_name_1")
    vars_2 = tf.get_collection(
        tf.GraphKeys.TRAINABLE_VARIABLES, scope="function_name/subfunction_name_2")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多