执行代码时,我收到此错误AttributeError: 'ZeroOut' object has no attribute 'eval'
。我在tensorflow(https://www.tensorflow.org/extend/adding_an_op)
TF_CFLAGS=( $(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_compile_flags()))') )
TF_LFLAGS=( $(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_link_flags()))') )
g++ -std=c++11 -shared zero_out.cc -o zero_out.so -fPIC ${TF_CFLAGS[@]} ${TF_LFLAGS[@]} -O2
main.py
import tensorflow as tf
import numpy as np
forward_module = tf.load_op_library('./zero_out.so')
with tf.Session(''):
outgrad0 = np.arange(1,7).reshape(3,2).astype('float32')
print(forward_module.zero_out(outgrad0).eval())
zero_out.cc文件:
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/op_kernel.h"
using namespace tensorflow;
REGISTER_OP("ZeroOut")
.Input("to_zero: int32")
.Output("zeroed: int32")
.Output("indice: int32");
class ZeroOutOp : public OpKernel {
public:
explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {
}
void Compute(OpKernelContext* context) override {
const Tensor& input_tensor = context->input(0);
auto input = input_tensor.flat<int32>();
Tensor* output_tensor = NULL;
Tensor* output_tensor_indice = NULL;
TensorShape indice_shape;
int dims[] = {1};
TensorShapeUtils::MakeShape(dims, 1, &indice_shape);
OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(), &output_tensor));
OP_REQUIRES_OK(context, context->allocate_output(1, indice_shape, &output_tensor_indice));
auto output_flat = output_tensor->flat<int32>();
auto indice_flat = output_tensor_indice->flat<int32>();
indice_flat(0) = 3;
}
};
REGISTER_KERNEL_BUILDER(Name("ZeroOut").Device(DEVICE_CPU), ZeroOutOp);
正如你所看到的,我添加了另一个输出变量,因为我想要一个元组作为输出。但我总是得到这个愚蠢的错误,我不知道如何摆脱它。
当编译没有错误产生时,但是当我执行python代码时,如果没有eval()它就不能进行评估它至少有2个输出。
编辑:
另一种方法是在C ++中:
void addOne(int &y) // y is a reference variable
{
y = y + 1;
}
优点是我不需要任何return
。对于tensorflow,这是怎么做的?
答案 0 :(得分:0)
问题:
out0, out1 = zero_out_module.zero_out(volume0, img0_in, img1_in, np.array([0]), 1).eval() ---> leads to the error in the title
解决方案:
out0, out1 = zero_out_module.zero_out(volume0, img0_in, img1_in, np.array([0]), 1)
# use eval() seperately
print (out0.eval().shape)
print (out1.eval().shape)