简体   繁体   English

TensorFlow为Attr类型“张量”接受哪些Python类型?

[英]What Python types does TensorFlow accept for Attr's of type “tensor”?

I am defining a new Op in C++ which takes in a single attribute of type tensor , roughly following these instructions . 我在C ++中定义了一个新的Op,它接受了tensor类型的单个属性,大致遵循这些指令 A stripped version of the Op code is below: 以下是Op代码的剥离版本:

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"

using namespace tensorflow;

REGISTER_OP("DoStuff")
    .Attr("attr: tensor = { dtype: DT_FLOAT }")
    .Input("in: float")
    .Output("out: float");

class DoStuffOp : public OpKernel {
public:
    explicit DoStuffOp(OpKernelConstruction *context) : OpKernel(context) {
        OP_REQUIRES_OK(context, context->GetAttr("attr", &attr_));
        // ...
    }

    void Compute(OpKernelContext *context) override {
        // ...
    }

private:
    Tensor attr_;
};

REGISTER_KERNEL_BUILDER(Name("DoStuff").Device(DEVICE_CPU), DoStuffOp);

I can compile the Op into a .so file fine. 我可以将Op编译成.so文件。 However, I can't figure out how to successfully pass in a value for attr . 但是,我无法弄清楚如何成功传入attr的值。 When I run the following in Python: 当我在Python中运行以下内容时:

import tensorflow as tf
dostufflib = tf.load_op_library('build/do_stuff.so')
sess = tf.InteractiveSession()

A = [[1.0, 2.0, 3.0],
     [4.0, 5.0, 6.0]]
X = tf.Variable(tf.constant(1.0))

Y = dostufflib.do_stuff(X, A)

I get TypeError: Don't know how to convert [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] to a TensorProto for argument 'attr' . 我得到TypeError: Don't know how to convert [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] to a TensorProto for argument 'attr' Nothing I do seems to satisfy the type conversion: list , numpy array, tf.Tensor , tf.Variable , etc. How do you pass Python variables into an Op as tensor attributes? 我没做什么似乎满足类型转换: listnumpy数组, tf.Tensortf.Variable等。你如何将Python变量作为张量属性传递给Op?

After much more hunting, I found tf.contrib.util.make_tensor_proto , a function that converts a python scalar, python list, numpy ndarray, or numpy scalar into a tf.TensorProto object. 经过更多的搜索,我找到了tf.contrib.util.make_tensor_proto ,这是一个将python标量,python列表,numpy ndarray或numpy标量转换为tf.TensorProto对象的函数。 The following works: 以下作品:

A = tf.contrib.util.make_tensor_proto([[1.0, 2.0, 3.0],[4.0, 5.0, 6.0]])
X = tf.Variable(tf.constant(1.0))

Y = dostufflib.do_stuff(X, A)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:特征应该是`Tensor`的字典。 给定类型: <class 'tensorflow.python.framework.ops.Tensor'> - ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'> Python数据类型:&#39;| S ......&#39;是什么意思? - Python Data Types: What does '|S…' Mean? Python attr.s 多类型验证 - Python attr.s multiple type validation Python 中的 import attr 是什么意思? - What does import attr mean in Python? Tensorflow TypeError: 无法转换 object 类型<class 'tensorflow.python.framework.sparse_tensor.sparsetensor'>张量</class> - Tensorflow TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor 从 tensorflow 中的张量中检索值的最快方法是什么? - What's the fastest way to retrieve a value from a tensor in tensorflow? TensorFlow特定数据类型的目的是什么? - What's the purpose of TensorFlow specific data types? Python tensorflow lite 错误:无法设置张量:得到类型 1 的张量,但输入 88 的预期类型为 3 - Python tensorflow lite error:Cannot set tensor: Got tensor of type 1 but expected type 3 for input 88 ValueError:特征应该是一个“张量”的字典。 给定类型:<class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'> - ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.data.ops.dataset_ops.RepeatDataset'> Python的fileinput如何接受输入 - How does Python's fileinput accept input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM