Python 3.x 使用从另一个图导入的张量初始化变量

Python 3.x 使用从另一个图导入的张量初始化变量,python-3.x,tensorflow,deep-learning,Python 3.x,Tensorflow,Deep Learning,我在python3中使用tensorflow(版本:v1.1.0-13-g8ddd727 1.1.0)(python3.4.3(默认,2016年11月17日,01:08:31)[GCC 4.8.4]在linux上),它是从源代码和基于GPU安装的 我想知道是否可以使用从另一个会话导入的张量初始化变量,因为tensorflow文档没有提到它,我在stackoverflow上找到了它 train_dir = './gan/train_logs' ckpt = tf.train.latest_

我在python3中使用tensorflow(版本:v1.1.0-13-g8ddd727 1.1.0)(python3.4.3(默认,2016年11月17日,01:08:31)[GCC 4.8.4]在linux上),它是从源代码和基于GPU安装的

我想知道是否可以使用从另一个会话导入的张量初始化变量,因为tensorflow文档没有提到它,我在stackoverflow上找到了它

train_dir = './gan/train_logs'
    ckpt = tf.train.latest_checkpoint(train_dir)
    filename = ".".join([ckpt, 'meta'])
    print(filename)
    saver = tf.train.import_meta_graph(filename)
    saver.restore(sess, ckpt)
    test = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='generator')
这里,张量被成功导入,我想用它们初始化同一个生成器


谢谢你的帮助

您所要做的就是创建tf.assign ops

所以你会:

old_weights = .... # your loading

new_weights = tf.Variable( ... ) # any initialisation here!

initialise_new_weights = tf.assign(new_weights, old_weights)

with tf.train.MonitoredSession() as sess:
  # at this point new_weights are randomly initialised
  sess.run(initialise_new_weight) # now they are initialised to your values
或者您可以直接传递初始化器参数

old_weights = .... # your loading

new_weights = tf.Variable( ..., initializer = tf.constant_initialiser(old_weights) ) 

with tf.train.MonitoredSession() as sess:
  # they are initialised to your values

您所要做的就是创建tf.assign ops

所以你会:

old_weights = .... # your loading

new_weights = tf.Variable( ... ) # any initialisation here!

initialise_new_weights = tf.assign(new_weights, old_weights)

with tf.train.MonitoredSession() as sess:
  # at this point new_weights are randomly initialised
  sess.run(initialise_new_weight) # now they are initialised to your values
或者您可以直接传递初始化器参数

old_weights = .... # your loading

new_weights = tf.Variable( ..., initializer = tf.constant_initialiser(old_weights) ) 

with tf.train.MonitoredSession() as sess:
  # they are initialised to your values

非常感谢你的帮助!它真的起作用了!问题肯定是这个tf.assign,我没有意识到我必须作为op运行它。非常感谢您的帮助!它真的起作用了!问题肯定是这个tf.assign,我没有意识到我必须作为op运行它。