Tensorflow Keras将权重从一种模型复制到另一种模型 [英] Tensorflow Keras Copy Weights From One Model to Another

查看:184
本文介绍了Tensorflow Keras将权重从一种模型复制到另一种模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Tensorflow 1.4.1中的Keras,如何将一个权重从一个模型复制到另一个模型?

Using Keras from Tensorflow 1.4.1, how does one copy weights from one model to another?

作为某种背景,我正在尝试根据DeepMind发布的DQN为Atari游戏实现Deep-q网络(DQN).我的理解是该实现使用两个网络Q和Q'.使用梯度下降训练Q的权重,然后将权重定期复制到Q'.

As some background, I'm trying to implement a deep-q network (DQN) for Atari games following the DQN publication by DeepMind. My understanding is that the implementation uses two networks, Q and Q'. The weights of Q are trained using gradient descent, and then the weights are copied periodically to Q'.

这是我建立问与答的方式:

Here's how I build Q and Q':

ACT_SIZE   = 4
LEARN_RATE = 0.0025
OBS_SIZE   = 128

def buildModel():
  model = tf.keras.models.Sequential()

  model.add(tf.keras.layers.Lambda(lambda x: x / 255.0, input_shape=OBS_SIZE))
  model.add(tf.keras.layers.Dense(128, activation="relu"))
  model.add(tf.keras.layers.Dense(128, activation="relu"))
  model.add(tf.keras.layers.Dense(ACT_SIZE, activation="linear"))
  opt = tf.keras.optimizers.RMSprop(lr=LEARN_RATE)

  model.compile(loss="mean_squared_error", optimizer=opt)

  return model

我打电话两次以获得Q和Q'.

I call that twice to get Q and Q'.

我下面有一个updateTargetModel方法,这是我尝试复制砝码的方法.该代码运行良好,但是我的总体DQN实现失败.我真的只是在尝试验证这是否是将权重从一个网络复制到另一个网络的有效方法.

I have an updateTargetModel method below that is my attempt at copying weights. The code runs fine, but my overall DQN implementation is failing. I'm really just trying to verify if this is a valid way of copying weights from one network to another.

def updateTargetModel(model, targetModel):
  modelWeights       = model.trainable_weights
  targetModelWeights = targetModel.trainable_weights

  for i in range(len(targetModelWeights)):
    targetModelWeights[i].assign(modelWeights[i])

这里还有另一个问题,讨论如何在磁盘上保存和加载权重( Tensorflow复制权重问题),但没有可接受的答案.还有一个关于从各个图层加载权重的问题(从一个图层复制权重Conv2D图层到另一个),但我想复制整个模型的权重.

There's another question here that discusses saving and loading weights to and from disk (Tensorflow Copy Weights Issue), but there's no accepted answer. There is also a question about loading weights from individual layers (Copying weights from one Conv2D layer to another), but I'm wanting to copy the entire model's weights.

推荐答案

实际上,您要做的不仅仅是复制权重.您使这两个模型一直保持相同.每次更新一个模型时-第二个模型也会更新-因为两个模型具有相同的weights变量.

Actually what you've done is much more than simply copying weights. You made these two models identical all the time. Every time you update one model - the second one is also updated - as both models have the same weights variables.

如果您只想复制砝码-最简单的方法是通过以下命令:

If you want to just copy weights - the simplest way is by this command:

target_model.set_weights(model.get_weights()) 

这篇关于Tensorflow Keras将权重从一种模型复制到另一种模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆