Repository ini wajib menyertakan file-file berikut agar proses inferensi berjalan lancar:
1import tensorflow as tf
2from tensorflow.keras import layers
3import joblib
4import numpy as np
5
6# Custom Layer: Simple Attention Mechanism
7@tf.keras.utils.register_keras_serializable()
8class SimpleAttention(layers.Layer):
9 def __init__(self, **kwargs):
10 super(SimpleAttention, self).__init__(**kwargs)
11
12 def build(self, input_shape):
13 self.W = self.add_weight(name="att_weight", shape=(input_shape[-1], 1),
14 initializer="glorot_uniform", trainable=True)
15 self.b = self.add_weight(name="att_bias", shape=(input_shape[1], 1),
16 initializer="zeros", trainable=True)
17 super(SimpleAttention, self).build(input_shape)
18
19 def call(self, inputs):
20 e = tf.tanh(tf.tensordot(inputs, self.W, axes=[2, 0]) + self.b)
21 alpha = tf.nn.softmax(e, axis=1)
22 return tf.reduce_sum(inputs * alpha, axis=1)
23
24# Custom Loss Function: Asymmetric MSE
25@tf.keras.utils.register_keras_serializable()
26def asymmetric_mse(y_true, y_pred):
27 residual = y_true - y_pred
28 loss = tf.where(residual > 0, 2.5 * tf.square(residual), tf.square(residual))
29 return tf.reduce_mean(loss)