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

本文详细介绍了 TensorFlow 中的 `tf.keras.layers.add()` 和 `tf.keras.layers.concatenate()` 两种层操作。`add()` 层实现了输入张量对应元素的相加,保持了原有的高度(H)、宽度(W)和通道数(C)不变。而 `concatenate()` 层则将多个输入张量沿着指定轴拼接,通道数增加,H和W保持不变。通过实例展示了不同轴上的拼接效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 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
`tf.keras.layers.concatenate`和`tf.concat`都是用于在TensorFlow中进行张量连接的函数,但是它们在使用方式和功能上有一些区别。 `tf.keras.layers.concatenate`是一个高级API,它是Keras中的一种层操作。它接受一个张量列表作为输入,并返回一个连接后的张量。例如,可以将两个具有相同维度的张量连接在一起。 示例代码: ```python import tensorflow as tf # 创建输入张量 input1 = tf.keras.Input(shape=(10,)) input2 = tf.keras.Input(shape=(20,)) # 使用tf.keras.layers.concatenate连接张量 concatenated = tf.keras.layers.concatenate([input1, input2], axis=-1) # 创建模型 model = tf.keras.Model(inputs=[input1, input2], outputs=concatenated) ``` `tf.concat`是TensorFlow的低级API函数,用于在给定轴上连接多个张量。它接受一个张量列表作为输入,并返回一个连接后的张量。`tf.keras.layers.concatenate`不同的是,`tf.concat`可以在任意轴上进行连接。 示例代码: ```python import tensorflow as tf # 创建输入张量 input1 = tf.constant([[1, 2], [3, 4]]) input2 = tf.constant([[5, 6], [7, 8]]) # 使用tf.concat连接张量 concatenated = tf.concat([input1, input2], axis=1) # 打印结果 print(concatenated) ``` 输出结果: ``` tf.Tensor( [[1 2 5 6] [3 4 7 8]], shape=(2, 4), dtype=int32) ``` 总结来说,`tf.keras.layers.concatenate`是一个更高级的操作,特别适用于在Keras模型中进行张量连接,而`tf.concat`是TensorFlow的低级API函数,更加灵活,可以在任意轴上进行连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值