在tensorflow中,如何初始化部分变量

时间:2018-07-04 03:53:15

标签: variables tensorflow initialization

我想初始化一个变量列表,并将它们定义为名为“ block_var”的列表。我想使用截断的普通方法来初始化它们。

block_var = [v for v in tf.global_variables() if 'block' in v.name]
init_block = tf.variables_initializer(var_list = block_var)

那我该怎么办? 我尝试过

for v in block_var:
    v.initializer = tf.truncated_normal_initializer()

我也尝试过

init_block = tf.truncated_normal_initializer()

两个字段。

2 个答案:

答案 0 :(得分:1)

解决方案1 ​​

您需要像{p>这样传递get_variable的初始值设定项参数

import tensorflow as tf
import numpy as np

ref0 = tf.get_variable('block0', [2], initializer=tf.truncated_normal_initializer(mean=40))
ref1 = tf.get_variable('block1', [2], initializer=tf.truncated_normal_initializer(mean=40))
ref2 = tf.get_variable('block2', [2], initializer=tf.truncated_normal_initializer(mean=40))

ref4 = tf.get_variable('foo0', [2], initializer=tf.truncated_normal_initializer(mean=10))
ref5 = tf.get_variable('foo1', [2], initializer=tf.truncated_normal_initializer(mean=10))

block_vars = [v for v in tf.global_variables() if 'block' in v.name]
block_vars_complement = [v for v in tf.global_variables() if 'block' not in v.name]

with tf.Session() as sess:
    sess.run(tf.variables_initializer(var_list=block_vars))
    print(np.mean(sess.run([ref0, ref1, ref2])), 'should be ~ 40')

    try:
        print(np.mean(sess.run([ref4])))
    except Exception as e:
        print('[INFO] failed as expected with message %s' % e)

    sess.run(tf.variables_initializer(var_list=block_vars_complement))
    print(np.mean(sess.run([ref4, ref5])), 'should be ~ 10')

解决方案2

如果您不想将initializer传递给每个get_variable,则可以使用类似的自定义吸气剂

import tensorflow as tf
import numpy as np


def my_getter(getter, name, shape, *args, **kwargs):
    if 'block' not in name:
        return getter(name=name, shape=shape, *args, **kwargs)
    else:
        kwargs['initializer'] = tf.truncated_normal_initializer(mean=40)
        return getter(name=name, shape=shape, *args, **kwargs)

with tf.variable_scope("some_scopename", custom_getter=my_getter):
    ref0 = tf.get_variable('block0', [2], initializer=tf.truncated_normal_initializer(mean=10))
    ref1 = tf.get_variable('block1', [2], initializer=tf.truncated_normal_initializer(mean=10))
    ref2 = tf.get_variable('block2', [2], initializer=tf.truncated_normal_initializer(mean=10))

    ref4 = tf.get_variable('foo0', [2], initializer=tf.truncated_normal_initializer(mean=10))
    ref5 = tf.get_variable('foo1', [2], initializer=tf.truncated_normal_initializer(mean=10))

block_vars = [v for v in tf.global_variables() if 'block' in v.name]
block_vars_complement = [v for v in tf.global_variables() if 'block' not in v.name]

with tf.Session() as sess:
    sess.run(tf.variables_initializer(var_list=block_vars))
    print(np.mean(sess.run([ref0, ref1, ref2])), 'should be ~ 40')

    try:
        print(np.mean(sess.run([ref4])))
    except Exception as e:
        print('[INFO] failed as expected with message %s' % e)

    sess.run(tf.variables_initializer(var_list=block_vars_complement))
    print(np.mean(sess.run([ref4, ref5])), 'should be ~ 10')

解决方案3

tf.truncated_normal_initializer或其他初始化程序本身就是操作。因此,可以将它们循环应用于集合中的所有变量,并且最终可以应用这种分组的更新(请参见initialize_collection):

import tensorflow as tf
import numpy as np

ref0 = tf.get_variable('block0', [2], initializer=tf.truncated_normal_initializer(mean=40))
ref1 = tf.get_variable('block1', [2], initializer=tf.truncated_normal_initializer(mean=40))
ref2 = tf.get_variable('block2', [2], initializer=tf.truncated_normal_initializer(mean=40))

ref4 = tf.get_variable('foo0', [2], initializer=tf.truncated_normal_initializer(mean=10))
ref5 = tf.get_variable('foo1', [2], initializer=tf.truncated_normal_initializer(mean=10))

block_vars = [v for v in tf.global_variables() if 'block' in v.name]
block_vars_complement = [v for v in tf.global_variables() if 'block' not in v.name]


def initialize_collection(collection, initializer):
    ops = []
    for v in collection:
        ops.append(v.assign(initializer(shape=v.shape)))
    return tf.group(ops)


with tf.Session() as sess:
    sess.run(tf.variables_initializer(var_list=block_vars))
    print(np.mean(sess.run([ref0, ref1, ref2])), 'should be ~ 40')

    sess.run(initialize_collection(block_vars, tf.truncated_normal_initializer(mean=-40, stddev=0.01)))
    print(np.mean(sess.run([ref0, ref1, ref2])), 'should be ~ -40')

答案 1 :(得分:-1)

更新:如评论中所述,我的早期代码存在错误。它创建了新变量。因此,我进行了改进,现在使用assert进行检查。

可能是这样。

import tensorflow as tf
import numpy as np

with tf.variable_scope("reuse"):
    x = tf.get_variable('x', [5, 5])
    y = tf.get_variable('y', [5, 5])

block_var = [v.name for v in tf.trainable_variables()]

def initialize( name, shape ):
    with tf.variable_scope("reuse",reuse=True):
        x = tf.get_variable(name.split(':')[0][-1],  shape = shape, initializer=tf.random_normal_initializer())
        x.initializer.run()
        print (x.eval())


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    vars = sess.run(block_var)
    for name,shape in zip(block_var,vars):
        initialize( name, shape.shape )
    assert (len(tf.global_variables()) == 2), "Variables are not reused"