Ceemdan-lstm代码
时间: 2023-10-05 12:11:10 浏览: 125
CEEMDAN-VMD-CNN-LSTM-Attention多变量时序预测(Matlab完整源码和数据)
这里是基于Python和TensorFlow的Ceemdan-LSTM的代码示例:
```python
import numpy as np
import tensorflow as tf
from tensorflow.contrib.rnn import LSTMCell
# 定义CEEMDAN函数
def ceemdan(x, max_imf):
# 定义一些常量和变量
x = np.array(x)
N = len(x)
H = x
SD = 1.0
t = 0
imf = []
# 进行CEEMDAN分解
while (SD > 0.3 or t < 4) and t < max_imf:
t += 1
H1 = H
SD1 = 2 * SD
while SD1 > SD:
H1 = H
d = H1
for i in range(t):
# 计算均值
mean = np.mean(d)
# 构造上下限
uppers = []
lowers = []
for j in range(len(d)):
if j == 0:
uppers.append((d[j] + d[j + 1]) / 2)
lowers.append((d[j] + d[N - 1]) / 2)
elif j == N - 1:
uppers.append((d[j] + d[0]) / 2)
lowers.append((d[j] + d[j - 1]) / 2)
else:
uppers.append((d[j] + d[j + 1]) / 2)
lowers.append((d[j] + d[j - 1]) / 2)
# 构造均值为中心线的包络线
upper_env = np.maximum(mean, uppers)
lower_env = np.minimum(mean, lowers)
# 计算平均包络线
mean_env = (upper_env + lower_env) / 2
# 计算d的振幅与平均包络线之差
d = d - mean_env
imf.append(d)
H1 = H1 - d
SD1 = np.std(H1)
H = H1
SD = np.std(H)
imf.append(H)
return imf
# 定义LSTM模型
class LSTMModel(object):
def __init__(self, input_size, output_size, hidden_size, num_layers):
self.input_size = input_size
self.output_size = output_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.build_model()
def build_model(self):
self.x = tf.placeholder(tf.float32, [None, None, self.input_size])
self.y = tf.placeholder(tf.float32, [None, self.output_size])
cell = LSTMCell(self.hidden_size, state_is_tuple=True)
self.cell = tf.contrib.rnn.MultiRNNCell([cell] * self.num_layers, state_is_tuple=True)
self.init_state = self.cell.zero_state(tf.shape(self.x)[0], tf.float32)
self.outputs, self.final_state = tf.nn.dynamic_rnn(self.cell, self.x, initial_state=self.init_state, time_major=False)
self.outputs = self.outputs[:, -1, :]
self.W = tf.Variable(tf.truncated_normal([self.hidden_size, self.output_size], stddev=0.1))
self.b = tf.Variable(tf.constant(0.1, shape=[self.output_size]))
self.pred = tf.matmul(self.outputs, self.W) + self.b
self.loss = tf.reduce_mean(tf.square(self.pred - self.y))
self.train_op = tf.train.AdamOptimizer().minimize(self.loss)
def train(self, train_x, train_y, test_x, test_y, epoch, batch_size):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(epoch):
start = 0
while start < len(train_x):
end = start + batch_size
batch_x = train_x[start:end]
batch_y = train_y[start:end]
_, loss = sess.run([self.train_op, self.loss], feed_dict={self.x: batch_x, self.y: batch_y})
start += batch_size
if i % 100 == 0:
print('Epoch ', i, ' loss: ', loss)
test_loss = sess.run(self.loss, feed_dict={self.x: test_x, self.y: test_y})
print('Test loss: ', test_loss)
pred = sess.run(self.pred, feed_dict={self.x: test_x})
return pred
# 加载数据
data = np.load('data.npy')
data = np.transpose(data)
data = np.reshape(data, [-1, 1])
data = np.array(ceemdan(data, 5))
data = np.transpose(data, [1, 2, 0])
train_x = data[:2000, :-1, :]
train_y = data[:2000, -1, :]
test_x = data[2000:, :-1, :]
test_y = data[2000:, -1, :]
# 训练模型并进行预测
model = LSTMModel(5, 1, 10, 2)
pred = model.train(train_x, train_y, test_x, test_y, 1000, 50)
```
其中,CEEMDAN函数实现了CEEMDAN分解,LSTMModel类实现了LSTM模型的定义和训练。
阅读全文