tensorflow 行转为列_在矩阵行和列中交换元素 - TensorFlow scatter_nd

假设您想交换第二维中的元素,保留第一维的顺序与否。

import tensorflow as tf

sess = tf.InteractiveSession()

def prepare_fd(fd_indices, sd_dims):

fd_indices = tf.expand_dims(fd_indices, 1)

fd_indices = tf.tile(fd_indices, [1, sd_dims])

return fd_indices

# define the updates

updates = tf.constant([[11, 12, 13, 14],

[21, 22, 23, 24],

[31, 32, 33, 34]])

sd_dims = tf.shape(updates)[1]

sd_indices = tf.constant([[1, 0, 2, 3], [0, 2, 1, 3], [0, 1, 3, 2]])

fd_indices_range = tf.range(0, limit=tf.shape(updates)[0])

fd_indices_custom = tf.constant([2, 0, 1])

# define the indices

indices1 = tf.stack((prepare_fd(fd_indices_range, sd_dims), sd_indices), axis=2)

indices2 = tf.stack((prepare_fd(fd_indices_custom, sd_dims), sd_indices), axis=2)

# define the shape

shape = tf.shape(updates)

scatter1 = tf.scatter_nd(indices1, updates, shape)

scatter2 = tf.scatter_nd(indices2, updates, shape)

print(scatter1.eval())

# array([[12, 11, 13, 14],

# [21, 23, 22, 24],

# [31, 32, 34, 33]], dtype=int32)

print(scatter2.eval())

# array([[21, 23, 22, 24],

# [31, 32, 34, 33],

# [12, 11, 13, 14]], dtype=int32)

可能这个例子有帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值