Один из способов добиться этого - определить собственный слой bias
, и вот как вы можете это сделать. PS: Изменить входные формы / инициализатор в соответствии с вашими потребностями.
import tensorflow as tf
print('TensorFlow:', tf.__version__)
class BiasLayer(tf.keras.layers.Layer):
def __init__(self, units, *args, **kwargs):
super(BiasLayer, self).__init__(*args, **kwargs)
self.bias = self.add_weight('bias',
shape=[units],
initializer='zeros',
trainable=True)
def call(self, x):
return x + self.bias
z1 = tf.keras.Input(shape=[1])
z2 = tf.keras.Input(shape=[1])
z3 = tf.keras.Input(shape=[1])
z4 = tf.keras.Input(shape=[1])
z5 = tf.keras.Input(shape=[1])
dense_layer = tf.keras.layers.Dense(units=10, use_bias=False)
op1 = BiasLayer(units=10)(dense_layer(z1))
op2 = BiasLayer(units=10)(dense_layer(z2))
op3 = BiasLayer(units=10)(dense_layer(z3))
op4 = BiasLayer(units=10)(dense_layer(z4))
op5 = BiasLayer(units=10)(dense_layer(z5))
model = tf.keras.Model(inputs=[z1, z2, z3, z4, z5], outputs=[op1, op2, op3, op4, op5])
model.summary()
Вывод:
TensorFlow: 2.1.0-dev20200107
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_3 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_4 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_5 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
dense (Dense) (None, 10) 10 input_1[0][0]
input_2[0][0]
input_3[0][0]
input_4[0][0]
input_5[0][0]
__________________________________________________________________________________________________
bias_layer (BiasLayer) (None, 10) 10 dense[0][0]
__________________________________________________________________________________________________
bias_layer_1 (BiasLayer) (None, 10) 10 dense[1][0]
__________________________________________________________________________________________________
bias_layer_2 (BiasLayer) (None, 10) 10 dense[2][0]
__________________________________________________________________________________________________
bias_layer_3 (BiasLayer) (None, 10) 10 dense[3][0]
__________________________________________________________________________________________________
bias_layer_4 (BiasLayer) (None, 10) 10 dense[4][0]
==================================================================================================
Total params: 60
Trainable params: 60
Non-trainable params: 0
__________________________________________________________________________________________________