# 导入包
from math import sqrt
from keras.activations import sigmoid
from keras.metrics import accuracy
from keras.optimizer_v1 import SGD
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import LSTM
import pandas as pd
import numpy as np
import openpyxl
from sklearn import metrics
from sklearn.metrics import r2_score
import tensorflow as tf
from sklearn.metrics import accuracy_score
from sklearn import metrics
# import eli5
# from eli5.sklearn import PermutationImportance
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
from keras import backend as K
# 1、数据预处理
# 1.1 定义有监督数据
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = [], []
# i: n_in, n_in-1, ..., 1,为滞后期数
# 分别代表t-n_in, ... ,t-1期
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
# i: 0, 1, ..., n_out-1,为超前预测的期数
# 分别代表t,t+1, ... ,t+n_out-1期
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
agg = concat(cols, axis=1)
agg.columns = names
if dropnan:
agg.dropna(inplace=True)
return agg
# 1.2 定义前瞻数,准备数据
def prepare_data(dataset, n_in, n_out, n_vars=15, train_proportion=0.8):
# 读取数据集
values = dataset.values
# 保证所有数据都是float32类型
values = values.astype('float32')
# 变量归一化
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
print('缩放')
print(scaled[0:5])
# 将时间序列问题转化为监督学习问题
reframed = series_to_supervised(scaled, n_in, n_out)
print('有监督')
print(reframed[0:5])
# 取出保留的变量,t之前的有4个变量,t(包括t)之后的只有第一列,也就是预测列
contain_vars = []
for i in range(1, n_in + 1):
contain_vars += [('var%d(t-%d)' % (j, i)) for j in range(1, n_vars + 1)]
data = reframed[contain_vars + ['var1(t)'] + [('var1(t+%d)' % (j)) for j in range(1, n_out)]]
print(data[0:5])
# 修改列名
'''
col_names = ['Y', 'X1', 'X2', 'X3']
contain_vars = []
for i in range(n_vars):
contain_vars += [('%s(t-%d)' % (col_names[i], j)) for j in range(1, n_in + 1)]
'''
col_names = ['Y', 'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12', 'X13', 'X14']
contain_vars = []
for i in range(1, n_in + 1):
contain_vars += [('%s(t-%d)' % (col_names[j], i)) for j in range(n_vars)]
data.columns = contain_vars + ['Y(t)'] + [('Y(t+%d)' % (j)) for j in range(1, n_out)]
print('改列名后\n', data[0:5])
# 分隔数据集,分为训练集和测试集
values = data.values
n_train = round(data.shape[0] * train_proportion)
train = values[:n_train, :]
test = values[n_train:, :]
# 分隔输入X和输出y
train_X, train_y = train[:, :n_in * n_vars], train[:, n_in * n_vars:]
test_X, test_y = test[:, :n_in * n_vars], test[:, n_in * n_vars:]
print(train_X.shape)
print(train_y.shape)
print(test_X.shape)
print(test_y.shape)
# 将输入X改造为LSTM的输入格式,即[samples,timesteps,features]
train_X = train_X.reshape((train_X.shape[0], n_in, n_vars)) # 原来是n_vars
test_X = test_X.reshape((test_X.shape[0], n_in, n_vars))
print(train_X.shape)
return scaler, data, train_X, train_y, test_X, test_y, dataset
# 1.4 训练模型
def fit_lstm(data_prepare, n_neurons, n_batch, n_epoch=500, lr=0.0001, repeats=1):
train_X = data_prepare[2]
train_y = data_prepare[3]
test_X = data_prepare[4]
test_y = data_prepare[5]
scaler = data_prepare[0]
model_list = []
for i in range(repeats):
# 设计神经网络
model = Sequential()
model.add(LSTM(n_neurons, input_shape=(train_X.shape[1], train_X.shape[2]))) # (时间步长,原始变量数)
# model.add(LSTM(n_neurons, return_sequences=True, input_shape=(train_X.shape[1], train_X.shape[2])))# (时间步长,原始变量数)
# model.add(LSTM(n_neuron_2))
model.add(Dense(train_y.shape[1], activation='sigmoid')) # 全连接层预测的列数
# model.add(Activation(sigmoid))
# loss = tf.keras.losses.BinaryCrossentropy(from_logits=False, label_smoothing=0.0, axis=-1, reduction="auto", name="binary_crossentropy",)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr)
BCELoss = tf.keras.losses.BinaryCrossentropy()
# model.compile(optimizer=optimizer, loss=myloss, metrics=[accuracy])
# 拟合神经网络
losses = []
for epoch in range(n_epoch):
index = np.random.choice(len(train_X), n_batch, replace=False)
x = train_X[index]
y = train_y[index]
e = 0.1
'''with tf.GradientTape() as tape:
hat_ys = model(x)
z = y*(4292 + 2686)-2686 #将y进行反归一化
q = tf.clip_by_value(1/(tf.abs(z)-30), 0.001, 1) #在(-30,30)之间增加权重
mse = tf.reduce_mean(tf.square(y - hat_ys)*q)/2 #增加权重后的mseloss
# mse = tf.reduce_mean(
# tf.square(y - hat_ys)) / 2 #没有权重的mse
# bce = tf.abs(hat_zs-zs)
# bce = tf.reduce_mean(bce)#原来是交叉熵公式,但由于张量问题无法运行,该公式要改
zs = tf.cast(y*(4292 + 2686)-2686>-30, dtype=tf.float32)
hat_zs = tf.cast(hat_zs,dtype=tf.float32)
#print(f'zs:{zs}')
#print(f'hat_zs:{hat_zs}')
# print(zs.shape(),hat_zs.shape())
celoss = CELoss(zs,hat_zs)
'''
hat_ys = model(x)
# z = y * (4292 + 2686) - 2686 # 将y进行反归一化
# q = tf.clip_by_value(1/(tf.abs(z)-30), 0.001, 1) #在(-30,30)之间增加权重
# mse = tf.reduce_mean(
# tf.square(y - hat_ys)*q)/2 #增加权重后的mseloss
mse = tf.reduce_mean(
tf.square(y - hat_ys)) / 2 # 没有权重的mse
# 定义转换标签函数
def exchange(hat_ys):
hat_zs = []
for hat_y in hat_ys:
if hat_y > -30 and hat_y < 30:
hat_z = 1
else:
hat_z = -1
hat_zs.append(hat_z)
return hat_zs
zs = exchange(y * (4292 + 2686) - 2686) # 设置真实标签
print(zs)
hat_ys = tf.reshape(hat_ys, [1, 128])[0]
print(hat_ys)
hat_zs = exchange(hat_ys * (4292 + 2686) - 2686)
# zs = tf.where(
# tf.logical_and(
# tf.greater(y, (-30 + 2686) / (4292 + 2686)),
# tf.less(y, (30 + 2686) / (4292 + 2686))
# )
# , 1, 0) # 设置预测值标签
# print(zs)
# hat_zs = tf.where(
# tf.logical_and(
没有合适的资源?快使用搜索试试~ 我知道了~
自定义loss_lstm.zip
共13个文件
ipynb:8个
xlsx:2个
py:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 54 浏览量
2023-08-25
11:45:41
上传
评论
收藏 3.99MB ZIP 举报
温馨提示
基于Python的项目实现
资源推荐
资源详情
资源评论
收起资源包目录
自定义loss_lstm.zip (13个子文件)
msebceloss.py 18KB
lstm-classify.ipynb 65KB
Untitled1.ipynb 62KB
hat_zs.xlsx 26KB
alldata.csv 13.05MB
Untitled.ipynb 39KB
lstm_reg.ipynb 65KB
data.xlsx 21KB
.ipynb_checkpoints
Untitled-checkpoint.ipynb 34KB
lstm-classify-checkpoint.ipynb 65KB
lstm_reg-checkpoint.ipynb 223KB
Untitled1-checkpoint.ipynb 72B
预测二进制事件bceloss.py 13KB
共 13 条
- 1
资源评论
sjx_alo
- 粉丝: 1w+
- 资源: 1226
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功