当前位置:   article > 正文

CNN+LSTM多通道特征组合模型_cnn_lstm多头模型

cnn_lstm多头模型

1、摘要

本文主要讲解:CNN+LSTM多通道特征组合模型

2、包版本

pip install tensorflow-gpu==2.3.0
pip install keras==2.4.3
  • 1
  • 2

3、相关技术

多通道CNN模型,应用于文本分类任务

4、完整代码和步骤

主运行程序入口

# coding:utf-8
import os

import numpy as np
import tensorflow as tf
from keras import Input, Model
from keras.initializers import he_normal
from keras.layers import Dense, Conv1D, LeakyReLU, Concatenate
from keras.optimizers import Adam
from sklearn.metrics import mean_absolute_error
from tensorflow.python.keras.layers import CuDNNLSTM

from my_utils.read_write import pdReadCsv

os.chdir(os.getcwd())
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
tf.config.experimental.set_memory_growth(physical_devices[0], True)

# 定义多通道特征组合模型
def build_multi_cr_lstm_model(ts, fea_dim):
    # 定义输入
    inputs = Input(shape=(ts, fea_dim))

    # filters:卷积核的数目(即输出的维度)  kernel_size:卷积核的空域或时域窗长度
    # strides:为卷积的步长  kernel_initializer 权值矩阵的初始化器
    cnn_left_out1 = Conv1D(filters=50, kernel_size=6, strides=3, kernel_initializer=he_normal(seed=3))(inputs)
    act_left_out1 = LeakyReLU()(cnn_left_out1)
    #  输出维度 return_sequences:若为True则返回整个序列,否则仅返回输出序列的最后一个输出
    lstm_left_out1 = CuDNNLSTM(64, return_sequences=False)(act_left_out1)

    # cnn层&lstm层2
    cnn_right_out1 = Conv1D(filters=50, kernel_size=12, strides=3, kernel_initializer=he_normal(seed=3))(inputs)
    act_right_out1 = LeakyReLU()(cnn_right_out1)

    lstm_right_out1 = CuDNNLSTM(64, return_sequences=False)(act_right_out1)

    # cnn层&lstm层3
    cnn_mid_out1 = Conv1D(filters=50, kernel_size=6, strides=2, kernel_initializer=he_normal(seed=3))(inputs)
    act_mid_out1 = LeakyReLU()(cnn_mid_out1)

    lstm_mid_out1 = CuDNNLSTM(64, return_sequences=False)(act_mid_out1)
    # Concatenate 连接三个数组
    concat_output = Concatenate(axis=1)([lstm_left_out1, lstm_mid_out1, lstm_right_out1])
    # 上层叠加新的dense层  units:代表该层的输出维度
    outputs = Dense(units=1)(concat_output)
    model_func = Model(inputs=inputs, outputs=outputs)
    model_func.compile(loss='mae', optimizer=Adam(lr=0.002, decay=0.01), metrics=['mae'])
    return model_func


# 构建训练集、预测集,训练和预测分别transform
def make_train_test_data():
    train_x, train_y, train_y_MinMax = fit_size(X_train, y_train)
    ts = 12
    test_x = train_x
    test_y = train_y

    ts_train_x = np.array([])
    ts_train_y = np.array([])

    ts_test_x = np.array([])
    ts_test_y = np.array([])

    # 构建训练数据集
    print('训练数据的原始shape:', train_x.shape)
    for i in range(train_x.shape[0]):
        if i + ts == train_x.shape[0]:
            break

        ts_train_x = np.append(ts_train_x, train_x[i: i + ts, :])

        ts_train_y = np.append(ts_train_y, train_y[i + ts])

    # 构建预测数据集
    print('预测数据的原始shape:', test_x.shape)
    for i in range(test_x.shape[0]):
        if i + ts == test_x.shape[0]:
            break

        ts_test_x = np.append(ts_test_x, test_x[i: i + ts, :])

        ts_test_y = np.append(ts_test_y, test_y[i + ts])
    train_x = ts_train_x.reshape((train_x.shape[0] - ts, ts, train_x.shape[1]))
    test_x = ts_test_x.reshape((test_x.shape[0] - ts, ts, test_x.shape[1]))
    return train_x, ts_train_y, test_x, ts_test_y, train_y_MinMax


def start():
    # 构建model
    lstm_model = build_multi_cr_lstm_model(12, data_dim)

    lstm_model.fit(train_x, train_y, epochs=128, batch_size=32)
    pred_y = lstm_model.predict(test_x)
    # 转换为真实值
    inv_pred_y = train_y_MinMax.inverse_transform(pred_y)
    inv_test_Y = train_y_MinMax.inverse_transform(test_y.reshape(len(test_y), 1))
    mae = int(mean_absolute_error(inv_test_Y, inv_pred_y))
    print('test_mae : ', mae)
    mae = str(mae)

    model_path = r'mepre\\'
    lstm_model.save(
        model_path + name[0] + '_' + name[1] + '_' + name[2] + '_' + mae + '.h5')


def fit_size(x, y):
    from sklearn import preprocessing
    x_MinMax = preprocessing.MinMaxScaler()
    y_MinMax = preprocessing.MinMaxScaler()
    x = x_MinMax.fit_transform(x)
    y = y_MinMax.fit_transform(y)
    return x, y, y_MinMax


def load_data(df_train):
    X_train = df_train.drop(['CreateTime'], axis=1)
    y_train = df_train['wap'].values.reshape(-1, 1)
    return X_train, y_train, X_train, y_train


if __name__ == '__main__':
    os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
    # 超参设置
    data_dim = 43
    src = r'E:\\'
    train_path = src + r'mee_del.csv'
    df = pdReadCsv(train_path, ',')
    df = df.replace("--", '0')
    df.fillna(0, inplace=True)
    groups = df.groupby(['Papr'])
    for name, group in groups:
        print(name)
        X_train, y_train, X_test, y_test = load_data(group)
        train_x, train_y, test_x, test_y, train_y_MinMax = make_train_test_data()
        start()

  • 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
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138

5、学习链接

基于多通道CNN-BiGRU与多特征融合方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/168036?site
推荐阅读
相关标签
  

闽ICP备14008679号