【TensorFlow】tf.concat的用法

def concat(values, axis, name="concat"):                             
  """Concatenates tensors along one dimension.                       
                                                                     
  Concatenates the list of tensors `values` along dimension `axis`.  
  `values[i].shape = [D0, D1, ... Daxis(i), ...Dn]`, the concatenated
  result has shape                                                   
                                                                     
      [D0, D1, ... Raxis, ...Dn]                                     
                                                                     
  where                                                              
                                                                     
      Raxis = sum(Daxis(i))                                          
                                                                     
  That is, the data from the input tensors is joined along the `axis`
  dimension.                                                         
                                                                     
  The number of dimensions of the input tensors must match, and all d
  except `axis` must be equal.                                       
                                                                     
  For example:                                                       
                                                                     
  ```python                                                          
  t1 = [[1, 2, 3], [4, 5, 6]]                                        
  t2 = [[7, 8, 9], [10, 11, 12]]                                     
  tf.concat([t1, t2], 0)  # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11
  tf.concat([t1, t2], 1)  # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12
                                                                     
  # tensor t3 with shape [2, 3]                                      
  # tensor t4 with shape [2, 3]                                      
  tf.shape(tf.concat([t3, t4], 0))  # [4, 3]                         
  tf.shape(tf.concat([t3, t4], 1))  # [2, 6]                         

As in Python, the axis could also be negative numbers. Negative are interpreted as counting from the end of the rank, i.e.,axis + rank(values)`-th dimension.

For example:

t1 = [[[1, 2], [2, 3]], [[4, 4], [5, 3]]]                          
t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]                       
tf.concat([t1, t2], -1)                                            

would produce:

[[[ 1,  2,  7,  4],                                                
  [ 2,  3,  8,  4]],                                               
                                                                   
 [[ 4,  4,  2, 10],                                                
  [ 5,  3, 15, 11]]]                                               

Note: If you are concatenating along a new axis consider using stack
E.g.

tf.concat([tf.expand_dims(t, axis) for t in tensors], axis)        

can be rewritten as

tf.stack(tensors, axis=axis)           
                            

Args:
values: A list of Tensor objects or a single Tensor.
axis: 0-D int32 Tensor. Dimension along which to concatenate
in the range [-rank(values), rank(values)). As in Python, ind
for axis is 0-based. Positive axis in the rage of
[0, rank(values)) refers to axis-th dimension. And negative
refers to axis + rank(values)-th dimension.
name: A name for the operation (optional).

Returns:
A Tensor resulting from concatenation of the input tensors.

 import tensorflow as tf

In [3]: t1=tf.constant([1,2,3])
   ...: t2=tf.constant([4,5,6])
In [4]: tf.concat([t1,t2],1)
ValueError: Shape must be at least rank 2 but is rank 1 for 'concat_3' (op: 'ConcatV2') with input shapes: [3], [3], [] and with computed input tensors: input[2] = <1>.



In [5]: tf.expand_dims(tf.constant([1,2,3]),1)
Out[5]: <tf.Tensor 'ExpandDims:0' shape=(3, 1) dtype=int32>

In [6]: t1= tf.expand_dims(tf.constant([1,2,3]),1)

In [7]: t2= tf.expand_dims(tf.constant([4,5,6]),1)

In [8]: sess=tf.Session()
 sess.run(t1)
Out[9]:
array([[1],
       [2],
       [3]])

In [10]: sess.run(t2)
Out[10]:
array([[4],
       [5],
       [6]])

In [11]: sess.run(tf.concat([t1,t2],1))
Out[11]:
array([[1, 4],
       [2, 5],
       [3, 6]])

In [12]:
    ...:
    ...: t1=tf.constant([1,2,3])
    ...: t2=tf.constant([4,5,6])
    ...:


: t=tf.concat([tf.expand_dims(t,1) for t in (t1,t2)],1)

In [16]: sess.run(t)
Out[16]:
array([[1, 4],
       [2, 5],
       [3, 6]])

In [17]: t2=tf.stack([t1,t2],axis=1)

In [18]: sess.run(t2)
Out[18]:
array([[1, 4],
       [2, 5],
       [3, 6]])

In [19]:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值