Tensorflow 中的 Deepcopy [英] Deepcopy in Tensorflow

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

问题描述

tensorflow 中是否有深拷贝?考虑以下操作:

Is there any deepcopy in tensorflow? Consider the following operation:

tt = tf.get_variable('t',shape=[2,2])
tt1= tf.identity(tt[0].assign([1,1]))
tt2 = tf.identity(tt[1].assign([2,2]))

我希望 tt1 只编辑 tt 的第一行,而 tt2 只编辑第二行.这就是我现在得到的:

I want tt1 to only edit the first row of tt, and tt2 only edits the second row. This is what I get now:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(tt1))
    print(sess.run(tt2))

输出:

 [[ 1.          1.        ]
  [-1.15554953 -0.78545022]]

 [[ 1.  1.]
  [ 2.  2.]].

相反,我想要类似的东西:

Instead, I want something like:

 [[ 1.          1.        ]
  [-1.15554953 -0.78545022]]

 [[ -0.31531231 1.6651651]
  [ 2.  2.]].

如您所见,第二个变量也受第一个赋值的影响.有没有办法拥有独立的副本,而无需复制对张量的引用?

As you see, the second variable is also affected by the first assign. Is there a way to have independent copies, without copying the references to tensors?

推荐答案

在 tensorflow 中制作你的 deepcopy 如下:

Make your deepcopy in tensorflow as follows:

tt = tf.get_variable('t',shape=[2,2])
deepcopy = tf.Variable(tt.initialized_value())    
tt1= tf.identity(tt[0].assign([1,1]))
tt2 = tf.identity(deepcopy[1].assign([2,2]))

这将为您提供所需的输出:

This will give you the desired output:

[[ 1.          1.        ]
 [-1.01704359 -1.16236985]]
[[-0.44483608  1.1660043 ]
 [ 2.          2.        ]]

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

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