C++ Eigen 库中的 argmax() 方法
argmax() method in C++ Eigen Library
我正在使用Eigen
库进行矩阵/张量计算,我想返回沿深度轴的最大值的索引。类似于 numpy.argmax(( 在 Python 中所做的。
张量维数如下: (行 = 200,列 = 200,深度 = 4(
#include <Eigen/Dense>
int main(){
Eigen::Tensor<double, 3> table(4,200,200);
table.setRandom();
// How can I do this task for axis = 2, i.e depth of a tensor?
// int max_axis = table.argmax(ax=2);
return 0;
}
Eigen的张量库有一个argmin/argmax
成员函数,不幸的是,该函数目前没有记录在 https://eigen.tuxfamily.org/dox/unsupported/eigen_tensors.html 上。
Eigen的矩阵库可以通过minCoeff/maxCoeff
的访问者重载来模仿相同的行为。请参阅:https://eigen.tuxfamily.org/dox/group__TutorialReductionsVisitorsBroadcasting.html
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
#include <iostream>
#define STR_(x) #x
#define STR(x) STR_(x)
#define PRINT(x) std::cout << STR(x) << ":n" << (x) << std::endl
int main()
{
using namespace Eigen;
using T = int;
using S = Sizes<2, 3>;
S const sizes{};
T constexpr data[S::total_size]{
8, 4,
1, 6,
9, 2,
};
Map<MatrixX<T> const> const matrix(data, sizes[0], sizes[1]);
PRINT(matrix);
RowVector2<Index> argmax{};
matrix.maxCoeff(&argmax.x(), &argmax.y());
PRINT(argmax);
VectorX<Index> argmax0{matrix.cols()};
for (Index col = 0; col < matrix.cols(); ++col)
matrix.col(col).maxCoeff(&argmax0[col]);
PRINT(argmax0);
VectorX<Index> argmax1{matrix.rows()};
for (Index row = 0; row < matrix.rows(); ++row)
matrix.row(row).maxCoeff(&argmax1[row]);
PRINT(argmax1);
TensorMap<Tensor<T const, S::count>> const tensor(data, sizes);
PRINT(tensor);
PRINT(tensor.argmax());
PRINT(tensor.argmax(0));
PRINT(tensor.argmax(1));
// Note that tensor.argmax() is the index for a 1D view of the data:
Index const matrix_index = sizes.IndexOfColMajor(std::array{argmax.x(), argmax.y()});
Index const tensor_index = Tensor<Index, 0>{tensor.argmax()}();
PRINT(matrix_index == tensor_index);
}
输出:
matrix:
8 1 9
4 6 2
argmax:
0 2
argmax0:
0
1
0
argmax1:
2
1
tensor:
8 1 9
4 6 2
tensor.argmax():
4
tensor.argmax(0):
0
1
0
tensor.argmax(1):
2
1
matrix_index == tensor_index:
1
老实说,这是一个迂回的交叉路口,而不是问题本身的令人满意的答案。尽管如此,以下方法确实对我有用。
我在特征库中找不到我想要的东西。相反,我切换到犰狳库,它与具有用户友好 API 的 numpy 非常相似。此外,相对而言,犰狳对于来自Python或Matlab背景的人来说更容易理解。
在犰狳中,找到argmax非常简单,如下所示: (在所有深度轴上查找第二行和第二列的 argmax(
arma::Cube<double> A(200, 200, 4, arma::fill::randu);
uword i = A(arma::span(1), arma::span(1), arma::span::all).index_max();
特征代码库中的这个测试套件有几个跨轴子集执行缩减的示例。
这是一种似乎有效的方法:
std::array<int, 1> reduce_dims{2};
Eigen::Tensor<Eigen::Tuple<Eigen::Index, double>, 2> reduced =
table.index_tuples().reduce(reduce_dims,
Eigen::internal::ArgMaxTupleReducer<
Eigen::Tuple<Eigen::Index, double> >());
reduce_dims
指定应减少的轴列表(在本例中为仅通道(。 返回的张量包含"线性"索引和最大值本身,因此要提取实际的通道索引,您可能需要迭代张量:
for (int c = 0; c < reduced.dimension(1); ++c) {
for (int r = 0; r < reduced.dimension(0); ++r) {
Eigen::Index argmax_channel_index =
reduced(r, c).first / (table.dimension(0) * table.dimension(1));
std::cout << "argmax channel: " << argmax_channel_index << " "
<< "max: " << reduced(r, c).second << std::endl;
}
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- C++ Eigen 库中的 argmax() 方法