张量流矩阵操作



我有矩阵下的

x = [x1, x2, x3
y1, y2, y3
z1, z2, z3
] 

和另一个矩阵

y = [False, False, True, True, True,  False, False, False
False, True,  True, True, False, False, False, False
False, False, False,False,True,  True,  True,  False
]

我想在下面创建一个新矩阵,其中x在z中填充,就像True在y中填充一样,0在其他情况下填充。

z = [0, 0,  x1, x2, x3, 0, 0, 0
0, x1, x2, x3, 0,  0, 0, 0
0, 0,  0,  0,  x1, x2,x3,0
] 

我能够通过实现这一点

index = tf.cast(tf.where(y), tf.int32)
z = tf.sparse_to_dense(index, tf.shape(y), tf.reshape(x, [-1]))

然而,使用sparse_to_ense是复杂的,并且最终会遇到其他问题。有人可以用其他方法帮忙吗?

谢谢!

这将使用tf.scatter_nd_update.

CCD_ 1仅复制CCD_ 2 3次,因此可以在3个位置填充3个副本。

import tensorflow as tf
import numpy as np
x = [100, 200, 300,
1, 2, 3,
4, 5, 6
]
y = [False, False, True, True, True,  False, False, False,
False, True,  True, True, False, False, False, False,
False, False, False,False,True,  True,  True,  False
]
x0 = tf.Variable( x )
y0 = tf.Variable( tf.cast( y, tf.int32 ))
with tf.Session() as sess :
sess.run(tf.initialize_all_variables())
indices = tf.constant( sess.run( tf.where( tf.equal(y0 , True )) ))
print(sess.run( tf.scatter_nd_update( y0,
indices,
tf.tile(x0[0:3], tf.constant([3])))))

输出为

[  0   0 100 200 300   0   0   0   0 100 200 300   0   0   0   0   0   0
0   0 100 200 300   0]

最新更新