File size: 2,055 Bytes
25e7dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
from tensorflow.keras.models import Model, Input
from tensorflow.keras.layers import Dense, LSTM, GRU, Dropout
from tensorflow.keras.optimizers import Adam
import tensorflow as tf

def create_lstm_reg(input_shape: tuple[int, int],

                    lstm_units_1: int,

                    lstm_units_2: int,

                    # dense_units,

                    eta: float = 0.001,

                    dropout_rate: float = 0.1):
    inputs = Input(shape=input_shape)
    x = LSTM(units=lstm_units_1, return_sequences=True)(inputs)
    x = Dropout(rate=dropout_rate)(x)
    x = LSTM(units=lstm_units_2)(x)
    #     x = Dropout(rate=dropout_rate)(x)
    #     x = Dense(units=dense_units, activation='relu')(x)
    outputs = Dense(units=1)(x)

    model = Model(inputs=inputs, outputs=outputs)

    model.compile(optimizer=Adam(eta),
                  loss='mean_squared_error',
                  metrics=[
                      tf.keras.metrics.RootMeanSquaredError(),
                      tf.keras.metrics.MeanAbsolutePercentageError(name='mape'),
                      tf.keras.metrics.MeanAbsoluteError()
                  ]
                  )
    return model


def create_gru_reg(input_shape, gru_units_1, gru_units_2, dense_units, eta, dropout_rate=0.1):
    inputs = Input(shape=input_shape)
    x = GRU(units=gru_units_1, return_sequences=True)(inputs)
    x = Dropout(rate=dropout_rate)(x)
    x = GRU(units=gru_units_2)(x)
    #     x = Dropout(rate=dropout_rate)(x)
    #     x = Dense(units=dense_units, activation='relu')(x)
    outputs = Dense(units=1)(x)

    model = Model(inputs=inputs, outputs=outputs)

    model.compile(optimizer=Adam(eta),
                  loss='mean_squared_error',
                  metrics=[
                      tf.keras.metrics.RootMeanSquaredError(),
                      tf.keras.metrics.MeanAbsolutePercentageError(name='mape'),
                      tf.keras.metrics.MeanAbsoluteError()
                  ]
                  )
    return model