Tensorflow:如何在范围之间交换变量并从另一个范围设置变量 [英] Tensorflow: how to swap variables between scopes and set variables in scope from another

查看:50
本文介绍了Tensorflow:如何在范围之间交换变量并从另一个范围设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的作用域,它们有名称相同但值不同的变量.我想在范围之间交换这些变量的值.示例:

I have different scopes and they have variables with same names but with different values. I want to swap values of these variables between scopes. Example:

with tf.variable_scope('sc1'):
   a1 = tf.Variable(0, name='test_var1')
   b1 = tf.Variable(1, name='test_var2')
with tf.variable_scope('sc2'):
   a2 = tf.Variable(2, name='test_var1')
   b2 = tf.Variable(3, name='test_var2')   

我想设置a2为0,b2为1,a1为2,b1为3.

I want to set a2 to 0, b2 to 1, a1 to 2 and b1 to 3.

我正在考虑使用 tf.get_collection_ref 获取所需的变量,但我看不到如何更改变量的范围,所以可能我需要更改变量的值.在这种情况下,我需要在临时变量中存储一个值,然后删除该临时变量.我不确定它会起作用,这似乎太复杂了.有没有简单的方法?

I was thinking about getting needed variables with tf.get_collection_ref but I can't see how I can change scope of the variable, so probably I need to change values of variables. In this case I need to store one value in temporary variable and then remove that temporary variable. I don't sure that it will work and this seems too complicated. Is there simple way to do it?

UPD1: 另外,我需要从另一个集合中设置一个集合中的所有变量.我认为这是类似的问题.例如,在上面的代码中,将 a2 设置为 0,将 b2 设置为 1.

UPD1: Also I need to set all variables in one collection from another collection. I think that it's similar issue. For example in code above set a2 equal to 0 and b2 to 1.

UPD2:此代码不起作用:

with tf.variable_scope('sc1'):
    a1 = tf.get_variable(name='test_var1', initializer=0.)
    b1 = tf.Variable(0, name='test_var2')

with tf.variable_scope('sc2'):
    a2 = tf.get_variable(name='test_var1', initializer=1.)
    b2 = tf.Variable(1, name='test_var2')


def swap_tf_scopes(col1, col2):
    col1_dict = {}
    col2_dict = {}
    for curr_var in col1:
        curr_var_name = curr_var.name.split('/')[-1]
        col1_dict[curr_var_name] = curr_var

    for curr_var in col2:
        curr_var_name = curr_var.name.split('/')[-1]
        curr_col1_var = col1_dict[curr_var_name]
        tmp_t = tf.identity(curr_col1_var)
        assign1 = curr_col1_var.assign(curr_var)
        assign2 = curr_var.assign(tmp_t)
    return [assign1, assign2]


col1 = tf.get_collection(tf.GraphKeys.VARIABLES, scope='sc1')
col2 = tf.get_collection(tf.GraphKeys.VARIABLES, scope='sc2')

tf_ops_t = swap_tf_collections(col1, col2)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
sess.run(tf_ops_t)
print sess.run(col1) #prints [0.0, 1] but I expect [1.0, 1]
print sess.run(col2) #prints [1.0, 1] but I expect [0.0, 0]

推荐答案

import tensorflow as tf
import numpy as np

with tf.variable_scope('sc1'):
    a1 = tf.get_variable(name='test_var1', initializer=0.)
    b1 = tf.Variable(0, name='test_var2')

with tf.variable_scope('sc2'):
    a2 = tf.get_variable(name='test_var1', initializer=1.)
    b2 = tf.Variable(1, name='test_var2')


def swap_tf_scopes(col1, col2):
    col1_dict = {}
    for curr_var in col1:
        curr_var_name = curr_var.name.split('/')[-1]
        col1_dict[curr_var_name] = curr_var
    for curr_var in col2:
        curr_var_name = curr_var.name.split('/')[-1]
        curr_col1_var = col1_dict[curr_var_name]
        tmp_t =tf.Variable(curr_col1_var.initialized_value()) 
        sess.run(tmp_t.initializer)
        sess.run(tf.assign(curr_col1_var,curr_var))
        sess.run(tf.assign(curr_var,tmp_t))

col1 = tf.get_collection(tf.GraphKeys.VARIABLES, scope='sc1')
col2 = tf.get_collection(tf.GraphKeys.VARIABLES, scope='sc2')
sess = tf.Session()
sess.run(tf.initialize_all_variables())
swap_tf_scopes(col1, col2)
print(sess.run(col1)) 
print(sess.run(col2))

你好!试试这个.我想它会起作用.

Hello!Try this one. I guess it will work.

这篇关于Tensorflow:如何在范围之间交换变量并从另一个范围设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆