深度學習-TF函數-layers.concatenate用法

環境: tensorfow 2.*

def concatenate(inputs, axis=-1, **kwargs):

axis=n表示從第n個維度進行拼接,對於一個三維矩陣,axis的取值可以爲[-3, -2, -1, 0, 1, 2]。

代碼

import numpy as np
import tensorflow as tf

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

d0 = tf.keras.layers.concatenate([t1, t2], axis=0)
d1 = tf.keras.layers.concatenate([t1, t2], axis=1)
d2 = tf.keras.layers.concatenate([t1, t2], axis=2)
d3 = tf.keras.layers.concatenate([t1, t2], axis=-1)

print(d0)
print(d1)
print(d2)
print(d3)

結果:

tf.Tensor(
[[[ 1  2]
  [ 2  3]]

 [[ 4  4]
  [ 5  3]]

 [[ 7  4]
  [ 8  4]]

 [[ 2 10]
  [15 11]]], shape=(4, 2, 2), dtype=int32)
tf.Tensor(
[[[ 1  2]
  [ 2  3]
  [ 7  4]
  [ 8  4]]

 [[ 4  4]
  [ 5  3]
  [ 2 10]
  [15 11]]], shape=(2, 4, 2), dtype=int32)
tf.Tensor(
[[[ 1  2  7  4]
  [ 2  3  8  4]]

 [[ 4  4  2 10]
  [ 5  3 15 11]]], shape=(2, 2, 4), dtype=int32)
tf.Tensor(
[[[ 1  2  7  4]
  [ 2  3  8  4]]

 [[ 4  4  2 10]
  [ 5  3 15 11]]], shape=(2, 2, 4), dtype=int32)

Process finished with exit code 0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章