将Eigen :: matrixXd转换为Double(conversion of Eigen::matrixXd to Double)

我一直在尝试使用Eigen库重建输入到我用C ++编写的RBM程序的输入数据。 但是为了将重构矩阵的矩阵元素保持在某个特定范围内,我需要对这些矩阵元素应用sigmoid函数。 当我这样做时,我得到转换错误,我不知道它的方式。

这是我在头文件中计算的Sigmoid函数:

double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }

这是我如何计算重建:

MatrixXd V; double well[36]; Map<MatrixXd>( well, V.rows(), V.cols() ) = V; V = sigmoid(H * result3Eigen.transpose() + onesmat*result2Eigen.transpose());

最后这里编译代码时得到的错误信息:

error C2664:'utils::sigmoid':cannot convert parameter 1 from 'Eigen::MatrixXd' to 'double'

感谢您解决问题的任何提示。

I have been trying to reconstruct the input data fed to my RBM program written in C++ with the aid of Eigen library. But in order to keep the matrix elements of the reconstructed matrix into some specific range, i need to apply a sigmoid function to those. When i do so i get a conversion error and i don't know the way round it.

Here is my Sigmoid function computed in an header file:

double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); }

And here is how i compute the reconstruction:

MatrixXd V; double well[36]; Map<MatrixXd>( well, V.rows(), V.cols() ) = V; V = sigmoid(H * result3Eigen.transpose() + onesmat*result2Eigen.transpose());

At last here the error message i get when compiling the code:

error C2664:'utils::sigmoid':cannot convert parameter 1 from 'Eigen::MatrixXd' to 'double'

Thank you for any hints in solving the issue.

最满意答案

如果要将函数应用于特征矩阵的每个元素,可以使用unaryExpr函数:

V = my_matrix.unaryExpr(&sigmoid);

这将在特征矩阵my_matrix每个元素上运行sigmoid函数,然后返回另一个矩阵作为结果。

If you want to apply a function to each element of an Eigen matrix, you can use the unaryExpr function:

V = my_matrix.unaryExpr(&sigmoid);

This will run the sigmoid function on each element of the Eigen matrix my_matrix, and then return another matrix as the result.

更多推荐