2016-12-20 5 views
4
import tensorflow as tf 

array = tf.Variable(tf.random_normal([10])) 
i = tf.constant(0) 
l = [] 

def cond(i,l): 
    return i < 10 

def body(i,l): 
    temp = tf.gather(array,i) 
    l.append(temp) 
    return i+1,l 

index,list_vals = tf.while_loop(cond, body, [i,l]) 

위의 코드에서 설명한 것과 비슷한 방식으로 텐서 배열을 처리하고 싶습니다. while 루프의 본문에서는 요소를 기준으로 배열을 처리하여 일부 기능을 적용하려고합니다. 데모를 위해 작은 코드 스 니펫을 제공했습니다. 그러나 다음과 같은 오류 메시지가 나타납니다.루프 실행 중 Tensorflow

ValueError: Number of inputs and outputs of body must match loop_vars: 1, 2 

이 문제를 해결할 수있는 도움이 필요합니다.

덕분에 문서를 인용

답변

6

:

loop_vars은 (아마도 중첩) 튜플, namedtuple 또는 목록을 모두 condbody

에 전달 텐서의 정규 파이썬 배열을 텐서 (tensor)로 전달할 수 없습니다. 당신이 할 수있는 일입니다 : 일반적으로 tf.while_loop 루프가 변경되지 않습니다 동안 내부 텐서의 모양을 기대하기 때문에

i = tf.constant(0) 
l = tf.Variable([]) 

def body(i, l):            
    temp = tf.gather(array,i) 
    l = tf.concat([l, [temp]], 0) 
    return i+1, l 

index, list_vals = tf.while_loop(cond, body, [i, l], 
           shape_invariants=[i.get_shape(), 
                tf.TensorShape([None])]) 

모양 불변이있다.

sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 
sess.run(list_vals) 
Out: array([-0.38367489, -1.76104736, 0.26266089, -2.74720812, 1.48196387, 
      -0.23357525, -1.07429159, -1.79547787, -0.74316853, 0.15982138], 
      dtype=float32)