Python tensorflow:完成图形后分配权重

Python tensorflow:完成图形后分配权重,python,tensorflow,keras,keras-2,Python,Tensorflow,Keras,Keras 2,下面的解决方案 如果你只是对解决这个问题感兴趣,你可以跳到下面我的答案 原始问题 我正在使用tensorflow进行强化学习。一群代理并行使用该模型,一个中心实体根据收集的数据对其进行训练 我在这里发现:tensorflow会话是线程安全的。所以我只是让预测和更新并行运行 但是现在我想更改设置。我现在需要保留两个模型,而不是在一个模型上进行更新和培训。一个用于预测,第二个用于训练。经过一些训练步骤后,将第二步的重量复制到第一步。下面是keras中的一个最小示例。对于多处理,建议最终确定图表,但我

下面的解决方案

如果你只是对解决这个问题感兴趣,你可以跳到下面我的答案

原始问题

我正在使用tensorflow进行强化学习。一群代理并行使用该模型,一个中心实体根据收集的数据对其进行训练

我在这里发现:tensorflow会话是线程安全的。所以我只是让预测和更新并行运行

但是现在我想更改设置。我现在需要保留两个模型,而不是在一个模型上进行更新和培训。一个用于预测,第二个用于训练。经过一些训练步骤后,将第二步的重量复制到第一步。下面是keras中的一个最小示例。对于多处理,建议最终确定图表,但我无法复制权重:

# the usual imports
import numpy as np
import tensorflow as tf

from keras.models import *
from keras.layers import *

# set up the first model
i = Input(shape=(10,))
b = Dense(1)(i)
prediction_model = Model(inputs=i, outputs=b)

# set up the second model
i2 = Input(shape=(10,))
b2 = Dense(1)(i2)
training_model = Model(inputs=i2, outputs=b2)

# look at this code, to check if the weights are the same
# here the output is different
prediction_model.predict(np.ones((1, 10)))
training_model.predict(np.ones((1, 10)))

# now to use them in multiprocessing, the following is necessary
prediction_model._make_predict_function()
training_model._make_predict_function()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
default_graph = tf.get_default_graph()

# the following line is the critical part
# if this is uncommented, the two options below both fail
# default_graph.finalize()

# option 1, use keras methods to update the weights
prediction_model.set_weights(training_model.get_weights())

# option 2, use tensorflow to update the weights
update_ops = [tf.assign(to_var, from_var) for to_var, from_var in
              zip(prediction_model.trainable_weights, training_model.trainable_weights)]
sess.run(update_ops)

# now the predictions are the same
prediction_model.predict(np.ones((1, 10)))
training_model.predict(np.ones((1, 10)))
根据上述问题,建议最终确定图表。如果没有最终确定,可能会有内存泄漏(!?),所以这似乎是一个强烈的建议

但是如果我完成了它,我就不能再更新权重了。
让我困惑的是:训练网络是可能的,所以改变权重是允许的。在我看来,赋值似乎只是覆盖了权重,为什么这与应用优化器步骤不同?

简而言之,我的问题是为最终确定的图形的权重赋值。如果此任务在完成后完成,tensorflow会抱怨无法再更改图形

我不明白为什么这是禁止的。毕竟,通过反向传播改变权重是允许的

但问题与改变权重无关。Keras
set_weights()
令人困惑,因为它看起来好像只是简单地覆盖了权重(就像在backprop中一样)。实际上,在幕后添加并执行赋值操作。这些新操作表示图形中的更改,该更改是禁止的

因此,解决方案是在完成图形之前设置赋值操作。您必须对代码重新排序:

# the usual imports
import numpy as np
import tensorflow as tf

from keras.models import *
from keras.layers import *

# set up the first model
i = Input(shape=(10,))
b = Dense(1)(i)
prediction_model = Model(inputs=i, outputs=b)

# set up the second model
i2 = Input(shape=(10,))
b2 = Dense(1)(i2)
training_model = Model(inputs=i2, outputs=b2)

# set up operations to move weights from training to prediction
update_ops = [tf.assign(to_var, from_var) for to_var, from_var in
              zip(prediction_model.trainable_weights, training_model.trainable_weights)]

# now to use them in multiprocessing, the following is necessary
prediction_model._make_predict_function()
training_model._make_predict_function()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
default_graph = tf.get_default_graph()

default_graph.finalize()

# this can be executed now
sess.run(update_ops)

# now the predictions are the same
prediction_model.predict(np.ones((1, 10)))
training_model.predict(np.ones((1, 10)))