Keras中layers.add()与layers.concatenate()的区别

一. tf.keras.layers.add()

只进行相应元素的相加,H,W,C都不改变

例子:

from keras.models import Model
from keras.layers import Dense,add,Input
from keras.layers.merge import concatenate
from keras.utils.vis_utils import plot_model

input1 = Input(shape=(16,))
x1 = Dense(8, activation='relu')(input1)
input2 = Input(shape=(32,))
x2 = Dense(8, activation='relu')(input2)
added = add([x1, x2])
 
out = Dense(4)(added)
model = Model(inputs=[input1, input2], outputs=out)

 
# write model image
plot_model(model, show_shapes=True, show_layer_names=False)

在这里插入图片描述
我们可以看到Add层的output,与input的维度相同,因此只进行了数值的相加。

二. tf.keras.layers.concatenate()

拼接,H 、 W 不改变 , 但是通道数增加

在TensorFlow函数中,axis输入参数的取值范围是[-rank(input_tensor), rank(input_tensor))

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)

输出:

由该例子可以看出,axis=

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值