tf 矩阵行和列交换_交换矩阵中的元素行和列TensorFlow散点图

该篇博客介绍了如何在TensorFlow中通过scatter_nd函数交换矩阵的行和列。提供了具体的代码示例,展示了如何使用tf.expand_dims, tf.tile, tf.range等函数准备索引,并通过tf.scatter_nd进行更新,达到交换元素的目的。" 131783554,11528688,HCIA网络配置实践:静态路由与VLAN结合,"['网络', '服务器', '运维']
摘要由CSDN通过智能技术生成

假设您想交换第二个维度中的元素,或者保持第一个维度的顺序。在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)

希望这个例子能有所帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值