简体   繁体   English

在tensorflow中添加新的op-形状函数

[英]Adding new op in tensorflow - Shape functions

I'm trying to add a new operation in Tensorflow where I have two inputs, namely a 3D tensor and a constant, which outputs a 4D tensor. 我正在尝试在Tensorflow中添加一个新操作,其中有两个输入,即3D张量和一个常数,输出4D张量。 The 4D tensor is obtained by replicating the 3D tensor a number of times defined by the constant. 通过将3D张量复制常数定义的次数,可以得到4D张量。 The shape function is implemented in the following way: shape函数通过以下方式实现:

.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c)
{
    ::tensorflow::shape_inference::ShapeHandle output;
    ::tensorflow::shape_inference::ShapeHandle out1 = c->Vector(::tensorflow::shape_inference::DimensionOrConstant(5));
    TF_RETURN_IF_ERROR(c->Concatenate(c->input(0),out1,&output));
    c->set_output(0,output);
    return Status::OK();
})
.Doc(R"doc(
     Replicating the 3D input tensor in a 4D tensor.
)doc");

I would like that the size of the fourth dimension (defined by out1 in the code) is set to the second input (namely the constant value). 我想将第四维的大小(由代码中的out1定义)设置为第二个输入(即常量值)。 How to do it? 怎么做?

Perhaps MakeShapeFromShapeTensor is what you're looking for? 也许MakeShapeFromShapeTensor是您要找的东西? Something like: 就像是:

.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c)
{
    ::tensorflow::shape_inference::ShapeHandle n;
    TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(1, &n));
    ::tensorflow::shape_inference::ShapeHandle out;
    TF_RETURN_IF_ERROR(c->Concatenate(n, c->input(0), &out));
    c->set_output(0, out);
    return Status::OK();
})

That said, you probably know this, but just to be sure: Element-wise arithmetic operations in TensorFlow support broadcasting , so at least in those case you shouldn't need this custom op. 也就是说,您可能知道这一点,但是请确保: TensorFlow中的逐元素算术运算支持broadcast ,因此至少在这种情况下,您不需要此自定义操作。

For other cases, you could also combine tf.tile , tf.shape , tf.concat and tf.reshape to achieve the same effect. 对于其他情况,您还可以结合使用tf.tiletf.shapetf.concattf.reshape来达到相同的效果。 For example, the following creates a matrix by repeating a vector: 例如,以下通过重复向量创建矩阵:

import tensorflow as tf
oneD = tf.constant([1,2])
n = tf.constant([5])
twoD = tf.reshape(tf.tile(oneD, n), tf.concat([n, tf.shape(oneD)], 0))

with tf.Session() as sess:
  print oneD.eval()
  print twoD.eval()

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

相关问题 在Windows中的Tensorflow中添加操作 - Adding an op in tensorflow in windows 在Tensorflow中添加GPU Op - Adding a GPU Op in Tensorflow 在Tensorflow中实现新操作时使用简单的数学函数 - Using simple math functions when implementing new op in Tensorflow 在 tensorflow 中添加自定义操作时出现“tensorflow/core/framework/common_shape_fns.h: No such file or directory” - “tensorflow/core/framework/common_shape_fns.h: No such file or directory” when adding custom op in tensorflow 在 Tensorflow 中,我是否需要为“sinc”或“gaussian”激活函数添加新操作? - In Tensorflow, do I need to add new op for "sinc" or "gaussian" activation functions? 编译new_op教程时出错(Tensorflow) - Error compiling the new_op tutorial (Tensorflow) 在张量流中导入图形时使用新的op - Using new op while importing graph in tensorflow 如何根据现有Tensorflow操作组成来创建新的Tensorflow操作 - How to create new Tensorflow op from composition of existing Tensorflow ops Tensorflow:未为标准操作注册任何形状函数:ExtractGlimpse。 我在哪里添加形状函数的代码? - Tensorflow: No shape function registered for standard op: ExtractGlimpse. Where do I add my code for the shape function? Tensorflow添加了一个新的op,无法从python导入 - Tensorflow add a new op, could not import from python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM