gpt4 book ai didi

python - 将相同的权重加载到新图中的多个变量

转载 作者:行者123 更新时间:2023-12-01 08:05:11 25 4
gpt4 key购买 nike

我想将预训练模型中的相同变量加载到新模型中的多个变量

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
inc_v1 = v1.assign(v1+1)
init_op = tf.global_variables_initializer()
saver = tf.train.Saver(v1)

with tf.Session() as sess:
sess.run(init_op)
sess.run(v1+1)
save_path = saver.save(sess, "/tmp/model.ckpt")

及后记

# Create some variables.
v1 = tf.get_variable("v1", shape=[3])
v2 = tf.get_variable("v2", shape=[3])

# Add ops to save and restore all the variables.
saver = tf.train.Saver({"v1" : v1,"v1":v2})

with tf.Session() as sess:
saver.restore(sess, "/tmp/model.ckpt")

即我希望这两个变量都从先前模型的 v1 变量进行初始化。
下面的示例崩溃,因为它表示图表不同。

最佳答案

评估原始图中变量的分配值,然后使用该值初始化新图中的新变量:

import tensorflow as tf

with tf.Graph().as_default():
# the variable from the original graph
v0 = tf.Variable(tf.random_normal([2, 2]))

with tf.Session(graph=v0.graph) as sess:
sess.run(v0.initializer)
init_val = v0.eval() # <-- evaluate the assigned value
print('original graph:')
print(init_val)
# original graph:
# [[-1.7466899 1.1560178 ]
# [-0.46535382 1.7059366 ]]

# variables from new graph
with tf.Graph().as_default():
v1 = tf.Variable(init_val) # <-- variable from new graph
v2 = tf.Variable(init_val) # <-- variable from new graph

with tf.Session(graph=v1.graph) as sess:
sess.run([v.initializer for v in [v1, v2]])
print('new graph:')
print(v1.eval())
print(v2.eval())
# new graph:
# [[-1.7466899 1.1560178 ]
# [-0.46535382 1.7059366 ]]
# [[-1.7466899 1.1560178 ]
# [-0.46535382 1.7059366 ]]

关于python - 将相同的权重加载到新图中的多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com