Java构造一个矩阵类_如何构造一个包含所有组合而不替换张量流中矩阵的成对行的矩阵...

[不幸的是,这比仅使用张量流运算符要复杂一些。我将使用while_loop为所有组合创建索引,然后使用tf.gather来收集值:

import tensorflow as tf

x = tf.constant([[1, 3],

[2, 4],

[3, 2],

[0, 1]], dtype=tf.int32)

m = tf.constant([], shape=(0,2), dtype=tf.int32)

_, idxs = tf.while_loop(

lambda i, m: i < tf.shape(x)[0] - 1,

lambda i, m: (i + 1, tf.concat([m, tf.stack([tf.tile([i], (tf.shape(x)[0] - 1 - i,)), tf.range(i + 1, tf.shape(x)[0])], axis=1)], axis=0)),

loop_vars=(0, m),

shape_invariants=(tf.TensorShape([]), tf.TensorShape([None, 2])))

z = tf.reshape(tf.transpose(tf.gather(x, idxs), (2,0,1)), (-1, 2))

#

# array([[1, 2],

# [1, 3],

# [1, 0],

# [2, 3],

# [2, 0],

# [3, 0],

# [3, 4],

# [3, 2],

# [3, 1],

# [4, 2],

# [4, 1],

# [2, 1]])>

这在TF1和TF2中都应该起作用。

如果事先知道x的长度,则不需要while_loop,只需在python中预先计算索引,然后将它们放在常量中即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值