Python 在Tensorflow中为float32和float64实现新运算符时出错

Python 在Tensorflow中为float32和float64实现新运算符时出错,python,c++,tensorflow,Python,C++,Tensorflow,我在Tensorflow中实现了一个自定义操作符。我使用了T模板,这样我的操作符就可以处理float和double类型的输入。这是我的.cc文件的头: #include <stdio.h> #include <math.h> #include <cfloat> #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" #include "tensorflow/core/framework/op.

我在Tensorflow中实现了一个自定义操作符。我使用了T模板,这样我的操作符就可以处理float和double类型的输入。这是我的.cc文件的头:

#include <stdio.h>
#include <math.h>
#include <cfloat>

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "work_sharder.h"

using namespace tensorflow;
typedef Eigen::ThreadPoolDevice CPUDevice;

REGISTER_OP("NewOp")
    .Attr("T: {float, double}")
    .Attr("attr1: int")
    .Attr("attr2: float")
    .Input("input1: T")
    .Input("input2: T")
    .Output("output: T");



template <typename Device, typename T>
class NewOpOp : public OpKernel {
  public:
    explicit NewOpOp(OpKernelConstruction* context) : OpKernel(context) {
...
};
#包括
#包括
#包括
#包括“第三方/eigen3/不支持/Eigen/CXX11/Tensor”
#包括“tensorflow/core/framework/op.h”
#包括“tensorflow/core/framework/op_kernel.h”
#包括“tensorflow/core/framework/tensor_shape.h”
#包括“work_sharder.h”
使用名称空间tensorflow;
typedef Eigen::ThreadPoolDevice CPUDevice;
注册操作(“新操作”)
.Attr(“T:{float,double}”)
.Attr(“attr1:int”)
.Attr(“attr2:浮动”)
.输入(“输入1:T”)
.输入(“输入2:T”)
.产出(“产出:T”);
模板
类NewOpOp:公共操作内核{
公众:
显式NewOpOp(OpKernelConstruction*context):OpKernel(context){
...
};
它会正确编译,直到达到:

REGISTER_KERNEL_BUILDER(Name("NewOp").Device(DEVICE_CPU).TypeConstraint<T>("T"), NewOpOp<CPUDevice, T>);
REGISTER\u KERNEL\u BUILDER(Name(“NewOp”).Device(Device\u CPU).TypeConstraint(“T”),NewOpOp);
错误消息说括号内的Ts没有在此范围内声明,而T模板是在第一个块的末尾明确定义的! 如果我将此行更改为此行:

REGISTER_KERNEL_BUILDER(Name("NewOp").Device(DEVICE_CPU).TypeConstraint<float>("T"), NewOpOp<CPUDevice, float>);
REGISTER\u KERNEL\u BUILDER(Name(“NewOp”).Device(Device\u CPU).TypeConstraint(“T”),NewOpOp);

编译错误会消失,但它当然会强制输入为浮点。

我可以通过为每个操作注册两个不同的内核(浮点和双精度)来解决这个问题

REGISTER\u KERNEL\u BUILDER(Name(“NewOp”).Device(Device\u CPU).TypeConstraint(“T”),NewOpOp);
REGISTER_KERNEL_BUILDER(名称(“NewOp”).Device(Device_CPU).TypeConstraint(“T”),NewOpOp);
这有点难看,但它能工作

REGISTER_KERNEL_BUILDER(Name("NewOp").Device(DEVICE_CPU).TypeConstraint<float>("T"), NewOpOp<CPUDevice, float>);
REGISTER_KERNEL_BUILDER(Name("NewOp").Device(DEVICE_CPU).TypeConstraint<double>("T"), NewOpOp<CPUDevice, double>);