tcn、lstm、attention结合的时序预测的完整的tensorflow的代码
时间: 2024-02-18 18:06:03 浏览: 25
以下是一个结合了TCN、LSTM和Attention的时序预测模型的完整TensorFlow代码示例:
```python
import numpy as np
import tensorflow as tf
# Generate sample data
def generate_data(n):
x = np.arange(n)
y = np.sin(x*0.1) + np.random.normal(0, 0.1, n)
return x, y
# Split data into train and test sets
def split_data(x, y, train_ratio):
n_train = int(len(x) * train_ratio)
x_train, y_train = x[:n_train], y[:n_train]
x_test, y_test = x[n_train:], y[n_train:]
return x_train, y_train, x_test, y_test
# Generate training and test sets
n = 1000
x, y = generate_data(n=n)
x_train, y_train, x_test, y_test = split_data(x, y, train_ratio=0.8)
# Normalize data
mean = np.mean(y_train)
std = np.std(y_train)
y_train = (y_train - mean) / std
y_test = (y_test - mean) / std
# Create input sequences and labels
def create_sequences(x, y, sequence_length):
sequences = []
labels = []
for i in range(len(x) - sequence_length):
sequences.append(y[i:i+sequence_length])
labels.append(y[i+sequence_length])
return np.array(sequences), np.array(labels)
sequence_length = 30
x_train_seq, y_train_seq = create_sequences(x_train, y_train, sequence_length)
x_test_seq, y_test_seq = create_sequences(x_test, y_test, sequence_length)
# Create TensorFlow dataset
batch_size = 32
train_dataset = tf.data.Dataset.from_tensor_slices((x_train_seq, y_train_seq)).batch(batch_size)
test_dataset = tf.data.Dataset.from_tensor_slices((x_test_seq, y_test_seq)).batch(batch_size)
# Define TCN-Attention-LSTM model
class TCN_Attention_LSTM(tf.keras.Model):
def __init__(self, tcn_layers, lstm_units, attention_units, input_shape):
super(TCN_Attention_LSTM, self).__init__()
self.tcn_layers = tcn_layers
self.lstm_units = lstm_units
self.attention_units = attention_units
self.input_shape = input_shape
self.tcn_layer = []
for i in range(self.tcn_layers):
self.tcn_layer.append(tf.keras.layers.Conv1D(filters=64, kernel_size=3, dilation_rate=2**i, padding='same', activation=tf.nn.relu))
self.attention_layer = tf.keras.layers.Dense(units=self.attention_units, activation=tf.nn.tanh)
self.lstm_layer = tf.keras.layers.LSTM(units=self.lstm_units, return_sequences=True)
self.dense_layer = tf.keras.layers.Dense(units=1)
def call(self, inputs):
# TCN
tcn_input = inputs
for i in range(self.tcn_layers):
tcn_output = self.tcn_layer[i](tcn_input)
tcn_input = tcn_output + tcn_input
# Attention
attention_output = self.attention_layer(tcn_output)
attention_weights = tf.nn.softmax(attention_output, axis=1)
attention_output = tf.reduce_sum(tf.multiply(tcn_output, attention_weights), axis=1)
# LSTM
lstm_output = self.lstm_layer(tcn_output)
# Concatenate LSTM and attention output
lstm_attention_output = tf.concat([lstm_output, attention_output[:, tf.newaxis, :]], axis=1)
# Dense layer
output = self.dense_layer(lstm_attention_output)
return output
# Define loss function
def loss_fn(y_true, y_pred):
loss = tf.reduce_mean(tf.square(y_true - y_pred))
return loss
# Define optimizer
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
# Define training loop
@tf.function
def train_step(model, x, y, loss_fn, optimizer):
with tf.GradientTape() as tape:
y_pred = model(x)
loss = loss_fn(y, y_pred)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
return loss
# Define evaluation loop
@tf.function
def eval_step(model, x, y, loss_fn):
y_pred = model(x)
loss = loss_fn(y, y_pred)
return loss
# Train model
epochs = 100
tcn_layers = 4
lstm_units = 64
attention_units = 64
input_shape = (sequence_length, 1)
model = TCN_Attention_LSTM(tcn_layers=tcn_layers, lstm_units=lstm_units, attention_units=attention_units, input_shape=input_shape)
for epoch in range(epochs):
epoch_loss = 0.0
for x, y in train_dataset:
loss = train_step(model, x, y, loss_fn, optimizer)
epoch_loss += loss
epoch_loss /= len(train_dataset)
val_loss = 0.0
for x, y in test_dataset:
loss = eval_step(model, x, y, loss_fn)
val_loss += loss
val_loss /= len(test_dataset)
print('Epoch {}/{}: loss={:.4f}, val_loss={:.4f}'.format(epoch+1, epochs, epoch_loss, val_loss))
# Evaluate model on test set
test_loss = 0.0
for x, y in test_dataset:
loss = eval_step(model, x, y, loss_fn)
test_loss += loss
test_loss /= len(test_dataset)
print('Test loss: {:.4f}'.format(test_loss))
# Make predictions on test set
y_pred = []
for x, y in test_dataset:
pred = model(x)
y_pred.append(pred.numpy().flatten())
y_pred = np.concatenate(y_pred)
# Plot predictions vs actual values
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6))
plt.plot(x_test[sequence_length:], y_test[sequence_length:], label='Actual')
plt.plot(x_test[sequence_length:], y_pred, label='Predicted')
plt.legend()
plt.show()
```
在这个示例中,我们首先生成了一个正弦函数的样本数据,并将其分为训练集和测试集。然后,我们对数据进行了标准化,并创建了输入序列和标签。接下来,我们使用这些数据创建了TensorFlow数据集,并定义了TCN-Attention-LSTM模型。我们还定义了损失函数和优化器,并编写了训练和评估循环。最后,我们在测试集上评估模型,并绘制了预测值与实际值的图形。