[英]Tensorflow share layers between two models
讓我們的模型成為幾個全連接層:
我想共享中間層並使用兩個具有相同權重的模型,如下所示:
我可以用 Tensorflow 做這個嗎?
是的,您可以共享圖層! 官方 TensorFlow 教程在這里。
在您的情況下,可以使用可變范圍實現層共享
#Create network starts, which are not shared
start_of_net1 = create_start_of_net1(my_inputs_for_net1)
start_of_net2 = create_start_of_net2(my_inputs_for_net2)
#Create shared middle layers
#Start by creating a middle layer for one of the networks in a variable scope
with tf.variable_scope("shared_middle", reuse=False) as scope:
middle_of_net1 = create_middle(start_of_net1)
#Share the variables by using the same scope name and reuse=True
#when creating those layers for your other network
with tf.variable_scope("shared_middle", reuse=True) as scope:
middle_of_net2 = create_middle(start_of_net2)
#Create end layers, which are not shared
end_of_net1 = create_end_of_net1(middle_of_net1)
end_of_net2 = create_end_of_net2(middle_of_net2)
一旦在變量范圍內創建了一個層,您可以根據需要多次重用該層中的變量。 在這里,我們只重用它們一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.