2017-03-20 8 views
0

Tensorflow에 새로운 연산을 추가하려고합니다. 여기에는 3 차원 텐서와 4D 텐서를 출력하는 상수라는 두 가지 입력이 있습니다. 4D 텐서는 상수에 의해 정의 된 횟수만큼 3D 텐서를 복제함으로써 얻어진다. 형상 함수는 다음과 같은 방식으로 실행된다 :tensorflow에 새 연산 추가 - 도형 함수

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

난 (코드에 out1 및에 의해 정의 됨) 사차원의 크기가 제 2 입력 (즉 일정한 값)로 설정되어 있는지 싶다. 그것을하는 방법?

답변

0

아마도 MakeShapeFromShapeTensor 무엇입니까? 아마 당신이 알고 있지만 단지 확실하게 말했다

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

: 같은 뭔가 Element-wise arithmetic operations in TensorFlow support broadcasting, 그래서 적어도 그 경우에 당신이 사용자 정의 연산을 필요가 없습니다.

다른 경우에는 동일한 효과를 얻기 위해 tf.tile, tf.shape, tf.concattf.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()