1. 程式人生 > >《TensorFlow學習筆記》tf.concat函式用法

《TensorFlow學習筆記》tf.concat函式用法

tf版本:1.5.0

concat官方定義

Args:
values: A list of Tensor objects or a single Tensor. 單個張量或是一個關於張量的list
axis: 0-D int32 Tensor. Dimension along which to concatenate. Must be
in the range [-rank(values), rank(values)).維度,必須在values的維度之間的,否則無效,dtype = int32
name: A name for the operation (optional). 命名

Returns:
A Tensor resulting from concatenation of the input tensors.
返回一個 輸入張量的連線,連線方式按照axis所指維度

def concat(values, axis, name="concat"):
  """Concatenates tensors along one dimension.

  Concatenates the list of tensors `values` along dimension `axis`.  If
  `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 dimensions   輸入的張量維度個數需要match所有的維度
  except `axis` must be equal.

例項

  For example:
import tensorflow as tf
  ```python
  #和其他axis一樣, 如果只有二維的話, axis=0 其實就是行,axis=1為列
  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, 12]]
  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] ``` #如果要是用棧去 用一個new axis去連線 values的話 這裡推薦使用stack Note: If you are concatenating along a new axis consider using stack. E.g. ```python tf.concat([tf.expand_dims(t, axis) for t in tensors], axis) ``` can be rewritten as ```python tf.stack(tensors, axis=axis) ``` """