初始化变量而不覆盖权重

时间:2019-11-13 01:31:19

标签: python tensorflow keras

我有以下问题

# this is in file A
init_inter = tf.constant_initializer(...)
inter = tf.get_variable(name=..., shape=..., initialiter=init_inter)
yolov3 = YOLO()
output = yolov3.output
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(output, feed_dict{model.input: inputdata}

# this is file B
  YOLO(object):
    def__init__(self,):
      self.output = getoutput()

    def getoutput():
      model=keras.models.load_model(modelpath)
      print(model.get_weights()) # this prints the already trained weights of the model
      ...

此全局初始化程序会初始化我的变量inter,但也会完全擦除已加载模型的权重,并将其置于标准初始化中,因此输出是完全错误的。

现在我尝试仅初始化内部,文件A现在看起来像这样

my_variables_collection = tf.get_collection('my_variables')
init_inter = ...
inter = ...
tf.add_to_collection('my_variables', self.inter)
self.sess.run(tf.variables_initializer(self.my_variables_collection))

但是现在我得到一个错误,即一些卷积层没有初始化,所以我的模型的权重没有初始化。我以为 load_model 会初始化它们。但是我已经可以打印权重了,为什么模型中的变量没有初始化,如何用正确的值初始化它们?

0 个答案:

没有答案