2016-12-13 7 views
1

(tensorflowバージョン: '0.12.headは')TensorArray.close()を呼び出す効果は何ですか?

TensorArray.closeのドキュメントは、それが現在のTensorArrayを閉じと言います。 TensorArrayのステータスはどういう意味ですか?次のコードを試してみます

import tensorflow as tf 
sess = tf.InteractiveSession() 
a1 = tf.TensorArray(tf.int32, 2) 
a1.close().run() 
a2 = a1.write(0, 0) 
a2.close().run() 
print(a2.read(0).eval()) 

エラーはありません。 closeの用途は何ですか?

Learning-to-learn includes TensorArray.close in the reset operations of the network。コメントEmpty array as part of the reset processの意味を理解できません。


更新例について

import tensorflow as tf 

sess = tf.InteractiveSession() 

N = 3 

def cond(i, arr): 
    return i < N 

def body(i, arr): 
    arr = arr.write(i, i) 
    i += 1 
    return i, arr 

arr = tf.TensorArray(tf.int32, N) 
_, result_arr = tf.while_loop(cond, body, [0, arr]) 
reset = arr.close() # corresponds to https://github.com/deepmind/learning-to-learn/blob/6ee52539e83d0452051fe08699b5d8436442f803/meta.py#L370 

NUM_EPOCHS = 3 
for _ in range(NUM_EPOCHS): 
    reset.run() # corresponds to https://github.com/deepmind/learning-to-learn/blob/6ee52539e83d0452051fe08699b5d8436442f803/util.py#L32 
    print(result_arr.stack().eval()) 

はなぜarr.close() whileループは失敗がありませんか?各エポックの初めにarr.close()を呼び出す利点は何ですか?

答えて

0

学習の学習の例で閉じているTensorArrayは、元のTensorArrayがwhileループに渡されていません。ここからfx_array.close()

# original array (fx_array) declared here 
fx_array = tf.TensorArray(tf.float32, size=len_unroll + 1, 
          clear_after_read=False) 
# new array (fx_array) returned here 
_, fx_array, x_final, s_final = tf.while_loop(
    cond=lambda t, *_: t < len_unroll, 
    body=time_step, 
    loop_vars=(0, fx_array, x, state), 
    parallel_iterations=1, 
    swap_memory=True, 
    name="unroll") 

任意の後続の呼び出しは、新しいwhileループによって返される配列はなく、最初の反復のループに渡された元の配列を閉じます。

あなたが期待通りにどのように振舞うclose確認したい場合は、実行します。loss opが閉じられたアレイ上pack()を実行しようとするので

session.run([reset, loss]) 

これはTensorArray has already been closed.で失敗します。

0

これはPythonのopで、ネイティブopをラップしていて、どちらもヘルプ文字列を持っていますが、ネイティブopヘルプ文字列はより有益です。 inspect.getsourcefile(fx_array.close)を見ると、tensorflow/python/ops/tensor_array_ops.pyが表示されます。実装の内部では、_tensor_array_close_v2に代わっていることがわかります。だから、あなたはTensorArrayCloseOpがでTensorArrayCloseV2ために登録実装で、より多くの情報

// Delete the TensorArray from its resource container. This enables 
// the user to close and release the resource in the middle of a step/run. 
// TODO(ebrevdo): decide whether closing the grad op should happen 
// here or on the python side. 
class TensorArrayCloseOp : public OpKernel { 
public: 
    explicit TensorArrayCloseOp(OpKernelConstruction* context) 
     : OpKernel(context) {} 

    void Compute(OpKernelContext* ctx) override { 
    TensorArray* tensor_array; 
    OP_REQUIRES_OK(ctx, GetTensorArray(ctx, &tensor_array)); 
    core::ScopedUnref unref(tensor_array); 
    // Instead of deleting this TA from the ResourceManager, we just 
    // clear it away and mark it as closed. The remaining memory 
    // consumed store its mutex and handle Tensor. This will be 
    // cleared out at the end of the step anyway, so it's fine to keep 
    // it around until the end of the step. Further calls to the 
    // TensorArray will fail because TensorArray checks internally to 
    // see if it is closed or not. 
を持っていることがわかり、同じドキュメントの文字列が tensorflow/core/kernels/tensor_array_ops.ccを見てみると

TensorArrayCloseV2tensorflow/core/ops/ops.pbtxtでもあるこの

> from tensorflow.python.ops import gen_data_flow_ops 
> help(gen_data_flow_ops._tensor_array_close_v2) 
Delete the TensorArray from its resource container. This enables 
the user to close and release the resource in the middle of a step/run. 

を行うことができます

説明が表示されている動作と矛盾するようで、バグかもしれません。

+0

ありがとうございました@Yaroslav Bulatov。 docstringを読むと私はもっと混乱します。これにより、 ユーザーはステップ/実行の途中でリソースを閉じて解放することができます._さらに、TensorArrayが内部で閉じているかどうかをチェックするため、TensorArrayへの呼び出しが失敗します。_ 質問。学習の学習では、各エポックの初めにTensorArray.closeを呼び出します。'close'によってTensorArrayへのさらなる呼び出しが失敗すると、何を意味するのか分かりません。 – Jenny

+0

私は混乱していますが、この使用法はドキュメントと矛盾しているようです –

+0

@sirfzの答えによると、 'TensorArray'の状態は各実行で独立しているようです。 – Jenny

関連する問題