From f41959ccb2d9d4c722fe8fc3351401d53bcf4900 Mon Sep 17 00:00:00 2001 From: Manjunath Kudlur Date: Fri, 6 Nov 2015 16:27:58 -0800 Subject: TensorFlow: Initial commit of TensorFlow library. TensorFlow is an open source software library for numerical computation using data flow graphs. Base CL: 107276108 --- tensorflow/core/kernels/matrix_inverse_op.cc | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tensorflow/core/kernels/matrix_inverse_op.cc (limited to 'tensorflow/core/kernels/matrix_inverse_op.cc') diff --git a/tensorflow/core/kernels/matrix_inverse_op.cc b/tensorflow/core/kernels/matrix_inverse_op.cc new file mode 100644 index 0000000000..ad0948d6ef --- /dev/null +++ b/tensorflow/core/kernels/matrix_inverse_op.cc @@ -0,0 +1,64 @@ +// See docs in ../ops/linalg_ops.cc. +#include + +#include "tensorflow/core/framework/kernel_def_builder.h" +#include "tensorflow/core/framework/op_kernel.h" +#include "tensorflow/core/kernels/linalg_ops_common.h" +#include "tensorflow/core/lib/core/errors.h" +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/port.h" +#include "tensorflow/core/public/tensor_shape.h" +#include "third_party/eigen3/Eigen/LU" + +namespace tensorflow { + +template +class MatrixInverseOp + : public LinearAlgebraOp { + public: + explicit MatrixInverseOp(OpKernelConstruction* context) + : LinearAlgebraOp(context) {} + ~MatrixInverseOp() override {} + + TensorShape GetOutputMatrixShape( + const TensorShape& input_matrix_shape) override { + return input_matrix_shape; + } + + int64 GetCostPerUnit(const TensorShape& input_matrix_shape) override { + const int64 rows = input_matrix_shape.dim_size(0); + if (rows > (1LL << 20)) { + // A big number to cap the cost in case overflow. + return kint32max; + } else { + return rows * rows * rows; + } + } + + using typename LinearAlgebraOp::MatrixMap; + using + typename LinearAlgebraOp::ConstMatrixMap; + + void ComputeMatrix(OpKernelContext* context, const ConstMatrixMap& input, + MatrixMap* output) override { + OP_REQUIRES(context, input.rows() == input.cols(), + errors::InvalidArgument("Input matrix must be square.")); + if (input.rows() == 0) { + // By definition, an empty matrix's inverse is an emptry matrix. + return; + } + Eigen::FullPivLU> lu_decomposition(input); + OP_REQUIRES(context, lu_decomposition.isInvertible(), + errors::InvalidArgument("Input is not invertible.")); + *output = lu_decomposition.inverse(); + } +}; + +REGISTER_LINALG_OP("MatrixInverse", (MatrixInverseOp), float); +REGISTER_LINALG_OP("MatrixInverse", (MatrixInverseOp), double); +REGISTER_LINALG_OP("BatchMatrixInverse", (MatrixInverseOp), float); +REGISTER_LINALG_OP("BatchMatrixInverse", (MatrixInverseOp), + double); + +} // namespace tensorflow -- cgit v1.2.3