-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeed_forward.py
54 lines (45 loc) · 1.78 KB
/
feed_forward.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import tensorflow as tf
class FeedForward(tf.keras.layers.Layer):
"""
Feed-forward layer implementation.
Args:
config: Configuration object containing hyperparameters.
Attributes:
supports_masking: Boolean indicating if the layer supports masking.
fc1: First dense layer.
fc2: Second dense layer.
dropout: Dropout layer.
"""
def __init__(self, config, name=None, **kwargs):
super(FeedForward, self).__init__(name=name)
super(FeedForward, self).__init__(**kwargs)
self.supports_masking = True
self.fc1 = tf.keras.layers.Dense(config.intermediate_fc_size, activation=tf.keras.activations.gelu)
self.fc2 = tf.keras.layers.Dense(config.hidden_size)
self.dropout = tf.keras.layers.Dropout(config.hidden_dropout_prob)
def call(self, hidden_state, training=False):
"""
Applies feed-forward transformation to the input hidden state.
Args:
hidden_state: Hidden state tensor (batch_size, sequence_length, hidden_size).
training: Boolean indicating whether the model is in training mode or inference mode.
Returns:
Updated hidden state after applying feed-forward transformation.
"""
hidden_state = self.fc1(hidden_state)
hidden_state = self.dropout(hidden_state, training=training)
hidden_state = self.fc2(hidden_state)
return hidden_state
def get_config(self):
"""
Returns the configuration of the feed-forward layer.
Returns:
Configuration dictionary.
"""
config = super().get_config()
config.update({
"fc1": self.fc1,
"fc2": self.fc2,
"dropout": self.dropout,
})
return config