Tansformer时间序列预测模型代码
时间: 2023-09-11 08:11:55 浏览: 157
模型预测代码
5星 · 资源好评率100%
以下是使用 Transformer 模型进行时间序列预测的 Python 代码示例:
```python
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
# 加载数据
data = pd.read_csv('data.csv', header=None)
dataset = data.values.astype('float32')
# 数据归一化
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# 定义函数获取输入输出数据
def create_dataset(dataset, look_back=1):
X, Y = [], []
for i in range(len(dataset) - look_back - 1):
a = dataset[i:(i + look_back), 0]
X.append(a)
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)
# 创建训练和测试数据集
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# 转换数据为张量
trainX = tf.convert_to_tensor(trainX)
trainY = tf.convert_to_tensor(trainY)
testX = tf.convert_to_tensor(testX)
testY = tf.convert_to_tensor(testY)
# 定义 Transformer 模型
class Transformer(tf.keras.Model):
def __init__(self, num_layers, d_model, num_heads, dff, input_vocab_size, maximum_position_encoding, rate=0.1):
super(Transformer, self).__init__()
self.d_model = d_model
self.embedding = tf.keras.layers.Dense(d_model, activation='relu')
self.pos_encoding = positional_encoding(maximum_position_encoding, d_model)
self.encoder = Encoder(num_layers, d_model, num_heads, dff, rate)
self.final_layer = tf.keras.layers.Dense(1)
def call(self, x, training):
seq_len = tf.shape(x)[1]
x = self.embedding(x)
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
x += self.pos_encoding[:, :seq_len, :]
x = self.encoder(x, training)
x = self.final_layer(x)
return x
def positional_encoding(position, d_model):
angle_rads = get_angles(np.arange(position)[:, np.newaxis], np.arange(d_model)[np.newaxis, :], d_model)
# 对数组中的偶数元素使用 sin 函数
sines = np.sin(angle_rads[:, 0::2])
# 对数组中的奇数元素使用 cos 函数
cosines = np.cos(angle_rads[:, 1::2])
pos_encoding = np.concatenate([sines, cosines], axis=-1)
pos_encoding = pos_encoding[np.newaxis, ...]
return tf.cast(pos_encoding, dtype=tf.float32)
def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * (i // 2)) / np.float32(d_model))
return pos * angle_rates
class MultiHeadAttention(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads):
super(MultiHeadAttention, self).__init__()
self.num_heads = num_heads
self.d_model = d_model
assert d_model % self.num_heads == 0
self.depth = d_model // self.num_heads
self.wq = tf.keras.layers.Dense(d_model)
self.wk = tf.keras.layers.Dense(d_model)
self.wv = tf.keras.layers.Dense(d_model)
self.dense = tf.keras.layers.Dense(d_model)
def split_heads(self, x, batch_size):
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth))
return tf.transpose(x, perm=[0, 2, 1, 3])
def call(self, v, k, q, mask):
batch_size = tf.shape(q)[0]
q = self.wq(q)
k = self.wk(k)
v = self.wv(v)
q = self.split_heads(q, batch_size)
k = self.split_heads(k, batch_size)
v = self.split_heads(v, batch_size)
scaled_attention, attention_weights = scaled_dot_product_attention(q, k, v, mask)
scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3])
concat_attention = tf.reshape(scaled_attention, (batch_size, -1, self.d_model))
output = self.dense(concat_attention)
return output, attention_weights
def scaled_dot_product_attention(q, k, v, mask):
matmul_qk = tf.matmul(q, k, transpose_b=True)
dk = tf.cast(tf.shape(k)[-1], tf.float32)
scaled_attention_logits = matmul_qk / tf.math.sqrt(dk)
if mask is not None:
scaled_attention_logits += (mask * -1e9)
attention_weights = tf.nn.softmax(scaled_attention_logits, axis=-1)
output = tf.matmul(attention_weights, v)
return output, attention_weights
class PointWiseFeedForwardNetwork(tf.keras.layers.Layer):
def __init__(self, d_model, dff):
super(PointWiseFeedForwardNetwork, self).__init__()
self.dense1 = tf.keras.layers.Dense(dff, activation='relu')
self.dense2 = tf.keras.layers.Dense(d_model)
def call(self, x):
x = self.dense1(x)
x = self.dense2(x)
return x
class EncoderLayer(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads, dff, rate=0.1):
super(EncoderLayer, self).__init__()
self.mha = MultiHeadAttention(d_model, num_heads)
self.ffn = PointWiseFeedForwardNetwork(d_model, dff)
self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dropout1 = tf.keras.layers.Dropout(rate)
self.dropout2 = tf.keras.layers.Dropout(rate)
def call(self, x, training, mask=None):
attn_output, _ = self.mha(x, x, x, mask)
attn_output = self.dropout1(attn_output, training=training)
out1 = self.layernorm1(x + attn_output)
ffn_output = self.ffn(out1)
ffn_output = self.dropout2(ffn_output, training=training)
out2 = self.layernorm2(out1 + ffn_output)
return out2
class Encoder(tf.keras.layers.Layer):
def __init__(self, num_layers, d_model, num_heads, dff, maximum_position_encoding, rate=0.1):
super(Encoder, self).__init__()
self.d_model = d_model
self.num_layers = num_layers
self.embedding = tf.keras.layers.Dense(d_model, activation='relu')
self.pos_encoding = positional_encoding(maximum_position_encoding, d_model)
self.enc_layers = [EncoderLayer(d_model, num_heads, dff, rate) for _ in range(num_layers)]
self.dropout = tf.keras.layers.Dropout(rate)
def call(self, x, training, mask=None):
seq_len = tf.shape(x)[1]
x = self.embedding(x)
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
x += self.pos_encoding[:, :seq_len, :]
x = self.dropout(x, training=training)
for i in range(self.num_layers):
x = self.enc_layers[i](x, training, mask)
return x
# 定义模型参数
num_layers = 4
d_model = 128
dff = 512
num_heads = 8
input_vocab_size = len(trainX)
dropout_rate = 0.1
maximum_position_encoding = len(trainX)
# 创建 Transformer 模型
model = Transformer(num_layers, d_model, num_heads, dff, input_vocab_size, maximum_position_encoding, dropout_rate)
# 定义优化器和损失函数
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
loss_object = tf.keras.losses.MeanSquaredError()
# 定义评估指标
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.MeanAbsoluteError(name='train_mae')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.MeanAbsoluteError(name='test_mae')
# 定义训练函数
@tf.function
def train_step(inputs, targets):
with tf.GradientTape() as tape:
predictions = model(inputs, True)
loss = loss_object(targets, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(targets, predictions)
# 定义测试函数
@tf.function
def test_step(inputs, targets):
predictions = model(inputs, False)
t_loss = loss_object(targets, predictions)
test_loss(t_loss)
test_accuracy(targets, predictions)
# 训练模型
EPOCHS = 100
for epoch in range(EPOCHS):
for i in range(len(trainX)):
train_step(trainX[i], trainY[i])
for i in range(len(testX)):
test_step(testX[i], testY[i])
template = 'Epoch {}, Loss: {}, MAE: {}, Test Loss: {}, Test MAE: {}'
print(template.format(epoch + 1, train_loss.result(), train_accuracy.result(), test_loss.result(), test_accuracy.result()))
# 使用模型进行预测
predictions = []
for i in range(len(testX)):
prediction = model(testX[i], False)
predictions.append(prediction.numpy()[0][0])
# 反归一化预测结果
predictions = scaler.inverse_transform(np.array(predictions).reshape(-1, 1))
# 反归一化真实结果
testY = scaler.inverse_transform(testY.reshape(-1, 1))
# 输出预测结果和真实结果
for i in range(len(predictions)):
print('Predicted:', predictions[i][0], 'Actual:', testY[i][0])
```
在上述代码中,我们首先加载了时间序列数据,然后对数据进行归一化处理。接着,我们定义了一个函数来生成输入输出数据,用于训练模型。然后,我们将数据转换为张量,并定义了一个 Transformer 模型。接着,我们定义了训练和测试函数,使用 Adam 优化器和均方误差损失函数进行模型训练。在训练完成后,我们使用模型对测试数据进行预测,并将预测结果反归一化,以得到真实的预测结果。最后,我们输出了预测结果和真实结果,以便进行比较和评估。
阅读全文