您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

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

1、摘要

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

2、包版本

pip install tensorflow-gpu==2.3.0
pip install keras==2.4.3

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()

5、学习链接

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


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进