2017-03-20 7 views
0

私は2つの入力、すなわち3Dテンソルと定数を持ち、4Dテンソルを出力する新しい演算をTensorflowに追加しようとしています。 4Dテンソルは、定数によって定義された回数だけ3Dテンソルを複製することによって得られる。私は(コードでOUT1によって定義される)四次元のサイズは第二の入力(すなわち、一定値)に設定されていることを希望テンソルフローに新しい演算を追加する - 形状関数

.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"); 

: 形状関数は、以下のように実装されています。どうやってするの?

答えて

0

おそらくMakeShapeFromShapeTensorはお探しですか?何かのように:Element-wise arithmetic operations in TensorFlow support broadcasting、少なくともこれらの場合にはこのカスタムOPを必要はありませんので:あなたはおそらくこれを知っているが、念のために、言わ

.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(); 
}) 

他の場合は、同じ効果を達成するためにtf.tile,tf.shapetf.concatおよびtf.reshapeを組み合わせることもできます。たとえば、次のコードはベクトルを繰り返すことによって行列を作成します。

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() 
関連する問題