一. 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