DDR爱好者之家 Design By 杰米

我就废话不多说了,直接上代码吧!

import tensorflow as tf

def model_1():
  with tf.variable_scope("var_a"):
    a = tf.Variable(initial_value=[1, 2, 3], name="a")

  vars = [var for var in tf.trainable_variables() if var.name.startswith("var_a")]
  print(len(vars))
  return vars

def model_2():

  vars1 = model_1()

  with tf.variable_scope("var_b"):
    a = tf.Variable(initial_value=[1, 2, 3], name="a")

  vars2 = [var for var in tf.trainable_variables() if var.name.startswith("var")]
  print(len(vars2))
  return vars1


def pretrain_model1():
  print("-------- model 1 ------")
  vars = model_1()

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.save(sess, "./model.ckpt")

def train_model2():
  print("-------- model 2 ------")

  model1_vars = model_2()

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(var_list=model1_vars)
    saver.restore(sess, "./model.ckpt")
    vars = sess.run([model1_vars])
    for var in vars:
      print(var)

step = 2
if step == 1:
  pretrain_model1()
else:
  train_model2()

以上这篇tensorflow 只恢复部分模型参数的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米