keras_models.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:UK_Imbalance_Price_Forecasting 作者: ADGEfficiency 项目源码 文件源码
def make_lstm(timestep,
              input_length,
              layer_nodes,
              dropout=0.35,
              optimizer='Adam',
              loss='mse'):
    """
    Creates a Long Short Term Memory (LSTM) neural network Keras model

    args
        timestep (int) : the length of the sequence
        input_length (int) : used to define input shape
        layer_nodes (list) : number of nodes in each of the layers input & hidden
        dropout (float) : the dropout rate to for the layer to layer connections
        optimizer (str) : reference to the Keras optimizer we want to use
        loss (str) : reference to the Keras loss function we want to use

    returns
        model (object) : the Keras LSTM neural network model
    """

    model = Sequential()

    #  first we add the input layer
    model.add(LSTM(units=layer_nodes[0],
                   input_shape=(timestep, input_length),
                   return_sequences=True))
    #  batch norm to normalize data going into the actvation functions
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    #  dropout some connections into the first hidden layer
    model.add(Dropout(dropout))

    #  now add hideen layers using the same strucutre
    for nodes in layer_nodes[1:]:
        model.add(LSTM(units=nodes, return_sequences=True))
        model.add(BatchNormalization())
        model.add(Activation('relu'))
        model.add(Dropout(dropout))

    #  add the output layer with a linear activation function
    #  we use a node size of 1 hard coded because we make one prediction
    #  per time step
    model.add(TimeDistributed(Dense(1)))
    model.add(Activation('linear'))

    #  compile model using user defined loss function and optimizer
    model.compile(loss=loss, optimizer=optimizer)
    print(model.summary())

    return model
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号