입력 x를 x + b 형식의 출력에 매핑하는 Keras에서 레이어를 구축하려면 어떻게해야합니까? 여기서 b는 동일한 차원의 학습 가능한 가중치입니다. (또한 여기에서 활성화 기능은 ID가 될 것입니다).
tf.keras.layers.Layer
클래스 를 확장하여 언제든지 사용자 정의 레이어를 만들 수 있습니다.
import tensorflow as tf
print('TensorFlow:', tf.__version__)
class BiasLayer(tf.keras.layers.Layer):
def __init__(self, *args, **kwargs):
super(BiasLayer, self).__init__(*args, **kwargs)
def build(self, input_shape):
self.bias = self.add_weight('bias',
shape=input_shape[1:],
initializer='zeros',
trainable=True)
def call(self, x):
return x + self.bias
input_layer = tf.keras.Input(shape=[5])
x = BiasLayer()(input_layer)
model = tf.keras.Model(inputs=[input_layer], outputs=[x])
model.summary()
TensorFlow: 2.1.0
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_7 (InputLayer) [(None, 5)] 0
_________________________________________________________________
bias_layer_3 (BiasLayer) (None, 5) 5
=================================================================
Total params: 5
Trainable params: 5
Non-trainable params: 0
_________________________________________________________________
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다