2017-08-02 8 views
0

としてテンソルを使用します。Tensorflow - 私は後方の累積合計機能を使用したいインデックス

def _backwards_cumsum(x, length, batch_size): 

upper_triangular_ones = np.float32(np.triu(np.ones((length, length)))) 
repeated_tri = np.float32(np.kron(np.eye(batch_size), upper_triangular_ones)) 
return tf.matmul(repeated_tri, 
        tf.reshape(x, [length, 1])) 

をただし長さはプレースホルダです:

length = tf.placeholder("int32" ,name = 'xx') 

それは、新しい値を取得しますので、毎回_backwards_cumsumの計算が開始されます。

TypeError: 'Tensor' object cannot be interpreted as an index 

フルトレースバック:

は一度機能を実行しようと、私はエラーを得た

_maxxは、上記の長さのプレースホルダと同じである
{ 
TypeError         Traceback (most recent call last) 
<ipython-input-561-970ae9e96aa1> in <module>() 
----> 1 rewards = _backwards_cumsum(tf.reshape(tf.reshape(decays,[-1,1]) * tf.sigmoid(disc_pred_gen_ph), [-1]), _maxx, batch_size) 

<ipython-input-546-5c6928fac357> in _backwards_cumsum(x, length, batch_size) 
     1 def _backwards_cumsum(x, length, batch_size): 
     2 
----> 3  upper_triangular_ones = np.float32(np.triu(np.ones((length, length)))) 
     4  repeated_tri = np.float32(np.kron(np.eye(batch_size), upper_triangular_ones)) 
     5  return tf.matmul(repeated_tri, 

/Users/onivron/anaconda/envs/tensorflow/lib/python2.7/site-packages/numpy/core/numeric.pyc in ones(shape, dtype, order) 
    190 
    191  """ 
--> 192  a = empty(shape, dtype, order) 
    193  multiarray.copyto(a, 1, casting='unsafe') 
    194  return a 

回避策はありますか?

+0

完全なバックトレースなしではわかりにくいです。 Pythonインタプリタのエラーですか? TensorFlowランタイムエラー?等 –

答えて

1

エラーは、numpy配列で知らないうちに使用しているテンソルオブジェクト:lengthに関連しています。テンソルフロー内でnumpyの機能を使用する最も良い方法は、tf.py_funcを使用することです。

# Define a new function that only depends on numpy/any non tensorflow graph object 

def get_repeated_tri(length, batch_size): 
    upper_triangular_ones = np.float32(np.triu(np.ones((length, length)))) 
    repeated_tri = np.float32(np.kron(np.eye(batch_size), upper_triangular_ones)) 
    return repeated_tri 
# Here length and batch size must be non tensor object 
repeated_tri = tf.py_func(get_repeated_tri, [length, batch_size], tf.int32) 
# there're some size mismacthes also in your code `tf.matmul` 
def _backwards_cumsum(repeated_tri, x, length_, batch_size): 
    return tf.matmul(repeated_tri, tf.reshape(x, [length_*batch_size, -1])) 
length_ = tf.placeholder(tf.int32, name='length') 
# also define length, batch_size as nump constants 
# x as tensorflow tensor 
some_tensor_out= _backwards_cumsum(repeated_tri, x, length_, batch_size) 

some_tensor_out_ = sess.run(some_tensor_out, {length_:length}) 
+0

主な問題は、repett_triに影響を与える様々な長さを持っていると思います。 – moreo

+0

問題は何ですか、このコードはさまざまな長さをサポートします。 –