2015-12-07 14 views
7

テンソルの形状を変更し、ゼロオーバーフローを埋める方法はありますか?私はndarray.reshapeがこれを行うことを知っていますが、私が理解しているように、Tensorをndarrayに変換するには、GPUとCPUの間でフリップフロップが必要です。Tensorflowテンソルの形状変更とゼロのパッド

Tensorflowのreshape()ドキュメントによると、TensorShapesは同じ数の要素を持つ必要があると言われます。おそらく、最良の方法はpad()とreshape()でしょうか?

私が達成しようとしている:

a = tf.Tensor([[1,2],[3,4]]) 
tf.reshape(a, [2,3]) 
a => [[1, 2, 3], 
     [4, 0 ,0]] 

答えて

7

Tensorflowは今(アレイ用opencv2のパディング機能のような)いくつかの方法でテンソルにパディングを行い、パッドの機能を提供しています:

# 't' is [[1, 2, 3], [4, 5, 6]]. 
# 'paddings' is [[1, 1,], [2, 2]]. 
# rank of 't' is 2. 
pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0], 
            [0, 0, 1, 2, 3, 0, 0], 
            [0, 0, 4, 5, 6, 0, 0], 
            [0, 0, 0, 0, 0, 0, 0]] 

pad(t, paddings, "REFLECT") ==> [[6, 5, 4, 5, 6, 5, 4], 
           [3, 2, 1, 2, 3, 2, 1], 
           [6, 5, 4, 5, 6, 5, 4], 
           [3, 2, 1, 2, 3, 2, 1]] 

pad(t, paddings, "SYMMETRIC") ==> [[2, 1, 1, 2, 3, 3, 2], 
            [2, 1, 1, 2, 3, 3, 2], 
            [5, 4, 4, 5, 6, 6, 5], 
            [5, 4, 4, 5, 6, 6, 5]] 
:上記ドキュメントから

https://www.tensorflow.org/versions/r0.8/api_docs/python/array_ops.html#pad

tf.pad(tensor, paddings, mode='CONSTANT', name=None) 

10

は、私の知る限りでは、これを行い組み込みのオペレータは、(形状が一致しない場合tf.reshape()はあなたにエラーが発生します)ありません。しかし、あなたはいくつかの異なる事業者と同じ結果を得ることができます。

a = tf.constant([[1, 2], [3, 4]]) 

# Reshape `a` as a vector. -1 means "set this dimension automatically". 
a_as_vector = tf.reshape(a, [-1]) 

# Create another vector containing zeroes to pad `a` to (2 * 3) elements. 
zero_padding = tf.zeros([2 * 3] - tf.shape(a_as_vector), dtype=a.dtype) 

# Concatenate `a_as_vector` with the padding. 
a_padded = tf.concat(0, [a_as_vector, zero_padding]) 

# Reshape the padded vector to the desired shape. 
result = tf.reshape(a_padded, [2, 3]) 
関連する問題