javascript - 按列对矩阵进行排序

标签 javascript tensorflow matrix tensorflow.js

我有一个看起来像这样的矩阵:

[[5,2],
[4,3],
[3,4]]
使用命令
tf.tensor([...])
我想按索引为 0 的列排列矩阵,这样它看起来像这样:
[[3,4],
[4,3],
[5,2]]
我将如何使用 Tensorflow.js 做到这一点?

最佳答案

看这个例子,张量在第一个轴上排序。
目前还没有tf.sort在 tensorflow.js 中。但是为了达到同样的效果,我们可以在第一个轴上对张量进行切片,然后对其进行排序并获取索引。使用后者,我们可以使用 tf.gather 对初始张量进行排序

a = tf.tensor2d([[3,4], [4,3],[5,2]])
firstAxis = a.gather([0], 1).reshape([-1]);
ind = tf.topk(firstAxis, a.shape[0]).indices
a.gather(ind.reverse(), 0).print()

关于javascript - 按列对矩阵进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66718587/

相关文章:

c++ - 如何从文件中一个一个地读取矩阵元素

data-visualization - 当你有很多变量时,如何从 Rstudio 中提取可见的散点图矩阵?

javascript - 多个ajax请求未定义

python - 值错误 : Shape must be rank 3 but is rank 4 for 'adjust_hue/Slice' (op: 'Slice' ) with input shapes: [384, 12,12,3], [3], [3]

python - 如何将 'instance keys' 添加到 keras 模型输入以在 gcloud ai-platform 中进行批量预测?

multithreading - 在 Cifar10 上更改 TensorFlow 中的线程数

r - 将 dist 对象列表保存到 R 中的表

javascript - jQuery 不显示 Bootstrap 警报

javascript - 使用jquery添加div内容

javascript - 使用 Javascript 更改 a 元素的类