在 tensorflow 中复制网络 [英] Duplicate a network in tensorflow

查看:102
本文介绍了在 tensorflow 中复制网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处问过类似的问题.

然而,答案并没有满足我的需求,也没有人回复我的评论,所以我必须重新发布问题并说得更清楚.

我有 2 个网络,称为 Target &来源.为简单起见,网络定义如下:

# 源码定义s_input = tf.placeholder(tf.float32, [None, 1], name='input_layer')s_output = tf.contrib.layers.fully_connected(input=s_input, num_outputs=1)# target 的结构与 Source 相同,带有 t_input &t_输出.# 损失损失 = (alpha*t_output - s_input + beta*label)**2opt = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)

现在,经过一些训练步骤后,我想将 s_output 层的内容(参数)复制到 t_output 层,以便 t_output't change as s_output 由于梯度下降而改变 &t_output 仍然从 t_input 获取输入.

我尝试了 Yaroslav Bulatov 建议的解决方案,但没有奏效.

如果我只使用 tf.Variable 定义我的网络,我可以通过 tf.assign 轻松复制变量,但现在我想使用 tf.contrib.layers 更简单 &足够灵活,可以定义我自己的网络.

如果有人不明白我的问题,请通知我,以便我解决.

解决方案

您可以使用assign来创建复制操作,例如

s_output = tf.contrib.layers.fully_connected(input = s_input, num_outputs=1, weights_initializer=tf.contrib.layers.xavier_initializer())t_output = tf.contrib.layers.fully_connected(input = s_input, num_outputs=1)

现在您可以访问可训练变量

vars = tf.trainable_variables()

并复制它们(前半部分是来自 s_output 的变量,后半部分是来自 t_output 的变量):

copy_ops = [vars[ix+len(vars)//2].assign(var.value()) for ix, var in enumerate(vars[0:len(vars)//2])]

现在您可以使用以下方法复制数据:

init = tf.global_variables_initializer()sess = tf.Session()sess.run(初始化)地图(lambda x:sess.run(x),copy_ops)打印(sess.run(vars [2]))

希望这是您正在寻找的.

I've asked a similar question here.

However, the answer didn't satisfy my need, and no one has replied to my comment, so I have to repost the question and make it clearer.

I have 2 networks, called Target & Source. For simple, the networks are defined as follow:

# Definition for Source
s_input = tf.placeholder(tf.float32, [None, 1], name='input_layer')
s_output = tf.contrib.layers.fully_connected(input=s_input, num_outputs=1)
# Structure of target is the same as Source's with t_input & t_output.

# Loss
loss = (alpha*t_output - s_input + beta*label)**2
opt = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)

Now, after some training steps, I want to copy the content (parameters) of layer s_output to layer t_output so that t_output doesn't change as s_output changed due to the gradient descent & t_output still gets input from t_input.

I've tried the solution that Yaroslav Bulatov suggested, however it didn't work.

If I define my network using simply tf.Variable, I can easily copy the variable by tf.assign, but now I want to use tf.contrib.layers which is much more simpler & flexible enough to define my own network.

If anyone doesn't understand my question, please notify me so that I can fix it.

解决方案

Hi you can use assign to create a copy operation, e.g.

s_output = tf.contrib.layers.fully_connected(input = s_input, num_outputs=1, weights_initializer=tf.contrib.layers.xavier_initializer())
t_output = tf.contrib.layers.fully_connected(input = s_input, num_outputs=1)

Now you can access the trainable variables

vars = tf.trainable_variables()

and copy them (the first half are the variables from s_output and the second half from t_output):

copy_ops = [vars[ix+len(vars)//2].assign(var.value()) for ix, var in enumerate(vars[0:len(vars)//2])]

Now you can copy the data using:

init = tf.global_variables_initializer()
sess = tf.Session() 
sess.run(init)
map(lambda x: sess.run(x), copy_ops)
print(sess.run(vars[2]))

Hope this what you're looking for.

这篇关于在 tensorflow 中复制网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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