Tensorflow 中的深度复制

Rez*_*ari 2 tensorflow

张量流中有深度复制吗?考虑以下操作:

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

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

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(tt1))
    print(sess.run(tt2))
Run Code Online (Sandbox Code Playgroud)

其输出:

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

 [[ 1.  1.]
  [ 2.  2.]].
Run Code Online (Sandbox Code Playgroud)

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

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

 [[ -0.31531231 1.6651651]
  [ 2.  2.]].
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,第二个变量也受到第一个分配的影响。有没有办法拥有独立的副本,而不复制对张量的引用?

JMA*_*JMA 5

在张量流中进行深度复制,如下所示:

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]))
Run Code Online (Sandbox Code Playgroud)

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

[[ 1.          1.        ]
 [-1.01704359 -1.16236985]]
[[-0.44483608  1.1660043 ]
 [ 2.          2.        ]]
Run Code Online (Sandbox Code Playgroud)