简体   繁体   English

如何在Tensorflow 2.0中复制网络

[英]How to copy a network in Tensorflow 2.0

I am not sure on how to copy over a network in Tensorflow 2.0. 我不确定如何在Tensorflow 2.0中通过网络复制。 There are plenty of answers on how to do it in Tensorflow 1.x, but none about 2.0. 在Tensorflow 1.x中有很多关于如何执行此操作的答案,但关于2.0则没有答案。 Both of the networks are made through subclassing the tf.keras.Model , so I can't use the tf.keras.models.clone_model function. 这两个网络都是通过将tf.keras.Model子类化而tf.keras.Model ,因此我不能使用tf.keras.models.clone_model函数。

I have tried different approaches outlined below but none of them seem to work. 我尝试了下面概述的不同方法,但是似乎都没有用。

network1 = network2
network1.weights = network2.weights

from copy import copy
network1 = copy(network2)

Some of these methods will make a reference to the current network but not actually copy it. 其中一些方法将引用当前网络,但实际上不会对其进行复制。 Would appreciate all the help I can get! 希望能得到我所有的帮助!

Suppose that model_a and model_b are instantiations of the same Keras Model. 假设model_amodel_b是同一Keras模型的实例。 Then do: 然后做:

for a, b in zip(model_a.variables, model_b.variables):
  a.assign(b)  # copies the variables of model_b into model_a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM