_________________________________________________________________
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
window = 5
input_size = 10
lstm_units = 16
dropout = 0.01
#建立LSTM模型 训练
inputs=Input(shape=(window, input_size))
model=Conv1D(filters = lstm_units, kernel_size = 1, activation = 'sigmoid')(inputs)#卷积层
model=MaxPooling1D(pool_size = window)(model)#池化层
model=Dropout(dropout)(model)#droupout层
model=Bidirectional(LSTM(lstm_units, activation='tanh'), name='bilstm')(model)#双向LSTM层
attention=Dense(lstm_units*2, activation='sigmoid', name='attention_vec')(model)#求解Attention权重
model=Multiply()([model, attention])#attention与LSTM对应数值相乘
outputs = Dense(1, activation='tanh')(model)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='mse',optimizer='adam',metrics=['accuracy'])
model.summary()#展示模型结构
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 20, 13)] 0
_________________________________________________________________
conv1d (Conv1D) (None, 20, 64) 896
_________________________________________________________________
dropout (Dropout) (None, 20, 64) 0
_________________________________________________________________
bidirectional (Bidirectional (None, 20, 128) 66048
_________________________________________________________________
dropout_1 (Dropout) (None, 20, 128) 0
_________________________________________________________________
attention_vec (Dense) (None, 20, 128) 16512
_________________________________________________________________
flatten (Flatten) (None, 2560) 0
_________________________________________________________________
dense (Dense) (None, 1) 2561
=================================================================
Total params: 86,017
Trainable params: 86,017
Non-trainable params: 0
_________________________________________________________________
定义函数的方法
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
# def attention_model(INPUT_DIMS = 13,TIME_STEPS = 20,lstm_units = 64):
INPUT_DIMS = 13
TIME_STEPS = 20
lstm_units = 64
inputs = Input(shape=(TIME_STEPS, INPUT_DIMS))
x = Conv1D(filters=64, kernel_size=1, activation='relu')(inputs) # padding = 'same'
x = Dropout(0.3)(x)
# lstm_out = Bidirectional(LSTM(lstm_units, activation='relu'), name='bilstm')(x)
lstm_out = Bidirectional(LSTM(lstm_units, return_sequences=True))(x)
lstm_out = Dropout(0.3)(lstm_out)
# attention_mul = attention_3d_block(lstm_out)
attention_mul = Dense(lstm_units * 2, activation='sigmoid', name='attention_vec')(lstm_out)
attention_mul = Flatten()(attention_mul)
output = Dense(1, activation='sigmoid')(attention_mul)
model = Model(inputs=[inputs], outputs=output)
# return model
model.summary() # 展示模型结构
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 20, 13)] 0
_________________________________________________________________
conv1d (Conv1D) (None, 20, 64) 896
_________________________________________________________________
dropout (Dropout) (None, 20, 64) 0
_________________________________________________________________
bidirectional (Bidirectional (None, 20, 128) 66048
_________________________________________________________________
dropout_1 (Dropout) (None, 20, 128) 0
_________________________________________________________________
attention_vec (Dense) (None, 20, 128) 16512
_________________________________________________________________
flatten (Flatten) (None, 2560) 0
_________________________________________________________________
dense (Dense) (None, 1) 2561
=================================================================
Total params: 86,017
Trainable params: 86,017
Non-trainable params: 0
_________________________________________________________________