2017-10-13 209 views
0

我对tensorflow很陌生,想知道是否可以调整张量内的单个维度。tensorflow - 调整尺寸

让我有一个给定的张量T:

t = [[1, 10], [2, 20]] 
shape(t) = [2, 2] 

现在我要修改此张量的形状,使:

shape(t) = [2, 3] 

到目前为止,我刚发现的功能:

  • 重塑 - >此功能能够以这种方式重塑张量,维度的总数保持不变相同的(据我理解)

    shape(t) = [1, 3] | [3, 1] | [4] 
    
  • expand_dims - >该功能能够添加新的一维尺寸

    shape(t) = [1, 2, 2] | [2, 1, 2] | [2, 2, 1] 
    

是对我的描述目的的功能到位?如果不是:为什么? (也许它没有意义有这样的功能?)

亲切的问候

+0

从(2,2)(2,3),你在张量两个元素。他们应该从哪里来,或者应该如何计算? – Psidom

+1

你能描述一下你试图通过这么做来达到什么样的目的,以及哪些数据应该填补额外空间?如果你想用零填充额外的空间,你可以像这样使用tf.pad:'''tf.pad(t,[[0,0],[0,1]])''' –

+0

谢谢, tf.pad是我搜索的那个。 –

回答

0

使用tf.concat可以做到这一点。这是一个例子。

import tensorflow as tf 

t = tf.constant([[1, 10], [2, 20]], dtype=tf.int32) 
# the new tensor w/ the shape of [2] 
TBA_a = tf.constant([3,30], dtype=tf.int32) 
# reshape TBA_a to [2,1], then concat it to t on axis 1 (column) 
new_t = tf.concat([t, tf.reshape(TBA_a, [2,1])], axis=1) 

sess = tf.InteractiveSession() 
print(new_t.eval()) 

它会给我们

[[ 1 10 3] 
[ 2 20 30]]