當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.concat()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

concat()用於沿一維連接張量。

用法:tensorflow.concat( values, axis, name )

參數:

  • values:它是張量或張量列表。
  • axis:它是0-D張量,表示要連接的尺寸。
  • name(optional):它定義了操作的名稱。

返回值:它返回串聯的張量。



範例1:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
t1 = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] 
t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]] 
  
  
# Printing the input tensor 
print('t1:', t1) 
print('t2:', t2) 
  
# Calculating result 
res = tf.concat([t1, t2], 2) 
  
# Printing the result 
print('Result:', res)

輸出:

t1: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
t2: [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]
Result: tf.Tensor(
[[[ 1  2  7  4]
  [ 3  4  8  4]]

 [[ 5  6  2 10]
  [ 7  8 15 11]]], shape=(2, 2, 4), dtype=int32)
  
  

範例2:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
t1 = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] 
t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]] 
  
  
# Printing the input tensor 
print('t1:', t1) 
print('t2:', t2) 
  
# Calculating result 
res = tf.concat([t1, t2], 1) 
  
# Printing the result 
print('Result:', res)

輸出:

t1: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
t2: [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]
Result: tf.Tensor(
[[[ 1  2]
  [ 3  4]
  [ 7  4]
  [ 8  4]]

 [[ 5  6]
  [ 7  8]
  [ 2 10]
  [15 11]]], shape=(2, 4, 2), dtype=int32)





相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.concat()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。