Sorting 如何在TensorFlow中操作具有动态维数的二维张量中的某些元素
我有一个形状为(m,n)的二维张量a,但m和n是未知的。e、 g.A=[[1,2,3],[4,5,6]],这里m=2,n=3。在我的操作之后,我想得到B=[[1,20,3],[4,5,60]]。操作如下: 1) 按降序排列所有元素:6,5,4,3,2,1 2) 选择6,表示第2行和第3列已被占用 3) 跳过5、4和3,因为第2行或第3列已被占用 4) 选择2,因为第1行和第2列未被占用 5) 停止,因为所有行都已被占用(对于所有列也是如此)Sorting 如何在TensorFlow中操作具有动态维数的二维张量中的某些元素,sorting,dynamic,tensorflow,Sorting,Dynamic,Tensorflow,我有一个形状为(m,n)的二维张量a,但m和n是未知的。e、 g.A=[[1,2,3],[4,5,6]],这里m=2,n=3。在我的操作之后,我想得到B=[[1,20,3],[4,5,60]]。操作如下: 1) 按降序排列所有元素:6,5,4,3,2,1 2) 选择6,表示第2行和第3列已被占用 3) 跳过5、4和3,因为第2行或第3列已被占用 4) 选择2,因为第1行和第2列未被占用 5) 停止,因为所有行都已被占用(对于所有列也是如此) 6) 精选元素*10与朋友讨论后得到答案: A=A-t
6) 精选元素*10与朋友讨论后得到答案:
A=A-tf.reduce_min(A)
mask=A*0
def body(x,mask):
a=x-tf.reduce_max(x)
a=tf.sign(a)+1
mask=mask+a
where = tf.equal(x, tf.reduce_max(x))
indices = tf.where(where)
indices = tf.cast(indices, "int32")
col_slice=tf.slice(x, [0, indices[0][1]], [-1, 1])
col_slice=col_slice-tf.reduce_max(col_slice)
col_slice=-tf.sign(col_slice)
col_slice = tf.reshape(col_slice, [-1])
b=tf.matmul(tf.diag(col_slice),x)
row_slice=tf.slice(x, [indices[0][0],0], [1,-1])
row_slice=row_slice-tf.reduce_max(row_slice)
row_slice=-tf.sign(row_slice)
row_slice=tf.reshape(row_slice,[-1])
c=tf.matmul(b,tf.diag(row_slice))
return (c,mask)
def cond(x,mask):
return tf.greater(tf.reduce_sum(x),0)
_,final_mask=tf.while_loop(cond, body,(A,mask))
final_mask=final_mask*9+1
B=A*final_mask