TimeseriesGenerator
import numpy as np
from keras.preprocessing.sequence import TimeseriesGenerator
y = np.array(range(5))
tg = TimeseriesGenerator(y, y, length=3, sampling_rate=1)
for i in zip(*tg[0]):
print(*i)
"""
[0 1 2] 3
[1 2 3] 4
"""
y = np.array(range(6))
tg = TimeseriesGenerator(y, y, length=3, sampling_rate=1, stride=2)
for i in zip(*tg[0]):
print(*i)
"""
[0 1 2] 3
[2 3 4] 5
"""
y = np.array([[i] for i in range(5)])
tg = TimeseriesGenerator(y, y, length=3, sampling_rate=1)
for i in zip(*tg[0]):
print(*i)
"""
[[0] [1] [2]] [3]
[[1] [2] [3]] [4]
"""
y = np.array([[i] for i in range(6)])
tg = TimeseriesGenerator(y, y, length=3, sampling_rate=1, stride=2)
for i in zip(*tg[0]):
print(*i)
"""
[[0] [1] [2]] [3]
[[2] [3] [4]] [5]
"""
y = np.array([(i, i) for i in range(5)])
tg = TimeseriesGenerator(y, y, length=3, sampling_rate=1)
for i in zip(*tg[0]):
print(*i)
"""
[[0 0] [1 1] [2 2]] [3 3]
[[1 1] [2 2] [3 3]] [4 4]
"""
示例1:余弦曲线预测
RNN版
import numpy as np, matplotlib.pyplot as mp
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.models import Sequential
from keras.layers import Dense, LSTM
"""创建样本"""
x_len = 1075
x = np.linspace(0, np.pi * 10.75, x_len, endpoint=False)
y = np.cos(x).reshape(-1, 1)
window = 75
batch_size = 256
tg = TimeseriesGenerator(y, y, length=window, batch_size=batch_size)
print(tg[0][0].shape)
print(tg[0][1].shape)
print(tg[3][0].shape)
print(tg[3][1].shape)
"""建模"""
model = Sequential()
model.add(LSTM(units=50, input_shape=(window, 1),
return_sequences=True))
model.add(LSTM(units=100,
return_sequences=False))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit_generator(generator=tg, epochs=30, verbose=2)
"""预测"""
pred_len = 200
for i in range(4):
x_pred = x[i * batch_size + window: i * batch_size + window + pred_len]
y_pred = []
X_pred = tg[i][0][0]
for i in range(pred_len):
Y_pred = model.predict(X_pred.reshape(-1, window, 1))
y_pred.append(Y_pred[0])
X_pred = np.concatenate((X_pred, Y_pred))[1:]
mp.scatter(x_pred[0], y_pred[0], c='r', s=9)
mp.plot(x_pred, y_pred, 'r')
mp.plot(x, y, 'y', linewidth=5, alpha=0.3)
mp.show()
CNN版
import numpy as np, matplotlib.pyplot as mp
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.models import Sequential
from keras.layers import Dense, Conv1D, MaxPool1D, GlobalMaxPool1D
"""配置"""
x_len = 1075
window = 75
filters = 64
kernel_size = 9
batch_size = 256
epochs = 99
"""创建样本"""
x = np.linspace(0, np.pi * 10.75, x_len, endpoint=False)
y = np.cos(x).reshape(-1, 1)
tg = TimeseriesGenerator(y, y, length=window, batch_size=batch_size)
"""建模"""
model = Sequential()
model.add(Conv1D(filters, kernel_size, activation='relu', input_shape=(window, 1)))
model.add(MaxPool1D())
model.add(Conv1D(filters, kernel_size, activation='relu'))
model.add(GlobalMaxPool1D())
model.add(Dense(1))
model.compile('adam', 'mse')
model.fit_generator(tg, epochs=epochs, verbose=2)
"""预测"""
pred_len = 200
for i in range(4):
x_pred = x[i * batch_size + window: i * batch_size + window + pred_len]
y_pred = []
X_pred = tg[i][0][0]
for i in range(pred_len):
Y_pred = model.predict(X_pred.reshape(-1, window, 1))
y_pred.append(Y_pred[0])
X_pred = np.concatenate((X_pred, Y_pred))[1:]
mp.scatter(x_pred[0], y_pred[0], c='r', s=9)
mp.plot(x_pred, y_pred, 'r')
mp.plot(x, y, 'y', linewidth=5, alpha=0.3)
mp.show()
示例2:RNN+CNN
import numpy as np, matplotlib.pyplot as mp
from keras.preprocessing.sequence import TimeseriesGenerator
from keras.models import Sequential
from keras.layers import Dense, Conv1D, MaxPool1D, GRU
"""配置"""
x_len = 1075
window = 75
filters = 64
kernel_size = 9
units = 64
batch_size = 256
epochs = 50
"""创建样本"""
x = np.linspace(0, np.pi * 10.75, x_len, endpoint=False)
y = (np.cos(x) + np.cos(x * 10) * .1).reshape(-1, 1)
tg = TimeseriesGenerator(y, y, length=window, batch_size=batch_size)
"""建模"""
model = Sequential()
model.add(Conv1D(filters, kernel_size, activation='relu', input_shape=(window, 1)))
model.add(MaxPool1D())
model.add(Conv1D(filters, kernel_size, activation='relu'))
model.add(GRU(units))
model.add(Dense(1))
model.compile('adam', 'mse')
model.fit_generator(tg, epochs=epochs, verbose=2)
"""预测"""
pred_len = 200
for i in range(4):
x_pred = x[i * batch_size + window: i * batch_size + window + pred_len]
y_pred = []
X_pred = tg[i][0][0]
for i in range(pred_len):
Y_pred = model.predict(X_pred.reshape(-1, window, 1))
y_pred.append(Y_pred[0])
X_pred = np.concatenate((X_pred, Y_pred))[1:]
mp.scatter(x_pred[0], y_pred[0], c='r', s=9)
mp.plot(x_pred, y_pred, 'r')
mp.plot(x, y, 'y', linewidth=5, alpha=0.3)
mp.show()