<jupyter_start><jupyter_text>处理时间序列数据<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow.keras as keras<jupyter_output><empty_output><jupyter_text>抽取数据<jupyter_code>data = pd.read_csv('jena_climate_2009_2016.csv')
data
data.loc[:, 'T (degC)'].plot(figsize=(24,6))
data.loc[:1440, 'T (degC)'].plot(figsize=(14,6))<jupyter_output><empty_output><jupyter_text>数据说明1. 每10分钟采集一次 2. 24小时内会有144条数据<jupyter_code>### 数据标准化
float_data = data.iloc[:,1:].copy().values
mean = float_data[:20000].mean(axis=0)
float_data -= mean
std = float_data[:20000].std(axis=0)
float_data /= std
### 参数
lookback = 720   # 5天的观察数据
steps=6          #观察数据采样平率是每个小时一个数据点
delay=144        # 目标是未来24小时之后的数据
batch_size = 128
def generate(data, lookback, delay, min_index, max_index, shuffle=False, batch_size = 128, step=6):
    if max_index is None:
        max_index = len(data) - delay - 1
    i = min_index + lookback
    while 1:
        if shuffle:
            rows = np.random.randint(min_index + lookback, max_index, size=batch_size)
        else:
            if i + batch_size >= max_index:
                i = min_index+lookback
            rows = np.arange(i, min(i+ batch_size, max_index))
        
        samples = np.zeros((len(rows), lookback//step, data.shape[-1]))
        targets = np.zeros((len(rows),))
        for j, row in enumerate(rows):
            indices = range(rows[j]-lookback, rows[j], step)
            samples[j] = data[indices]
            targets[j] = data[rows[j] + delay][1]
        yield samples, targets
train_gen = generate(float_data,
                    lookback=lookback,
                    delay=delay,
                    min_index = 0,
                    max_index=200000,
                    shuffle=True,
                    step = steps,
                    batch_size = batch_size)
val_gen = generate(float_data,
                    lookback=lookback,
                    delay=delay,
                    min_index = 200001,
                    max_index=300000,
                    shuffle=True,
                    step = steps,
                    batch_size = batch_size)
test_gen = generate(float_data,
                    lookback=lookback,
                    delay=delay,
                    min_index = 300001,
                    max_index=None,
                    shuffle=True,
                    step = steps,
                    batch_size = batch_size)
val_steps = (300000 - 200001 - lookback) // batch_size
test_steps = (len(float_data) - 300001 - lookback) // batch_size<jupyter_output><empty_output><jupyter_text>基准<jupyter_code>def evaluate_naive_method():
    batch_maes = []
    for step in range(val_steps):
        samples, targets = next(val_gen)
        preds = samples[:, -1, 1]
        mae = np.mean(np.abs(preds - targets))
        batch_maes.append(mae)
    return np.mean(batch_maes)
mae_stand = evaluate_naive_method()
print(f"mae T : {mae_stand * std[1]}")<jupyter_output>mae T : 2.564456149193549<jupyter_text>1. 使用密集链接模型<jupyter_code>model = keras.Sequential([
    keras.layers.Flatten(input_shape=(lookback//steps, float_data.shape[-1])),
    keras.layers.Dense(32, activation ='relu'),
    keras.layers.Dense(1)
])
model.compile(optimizer=keras.optimizers.RMSprop(),
             loss='mae')
model.summary()
history = model.fit(train_gen,
                   steps_per_epoch=500,
                   epochs=20,
                   validation_data = val_gen,
                   validation_steps = val_steps)
def show_history(history, epochs):
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    epochs_range = range(epochs-1)

    plt.figure(figsize=(12, 8))
    ax1 = plt.subplot(1, 1, 1)
    ax1.plot(epochs_range, loss[1:],'bo', label='Training Loss')
    ax1.plot(epochs_range, val_loss[1:],'r', label='Validation Loss')
    ax1.legend(loc='upper right')
    ax1.set_title('Training and Validation Loss')

    plt.show()
history.history.keys()
show_history(history, 20)<jupyter_output><empty_output><jupyter_text>2. GRU循环网络<jupyter_code>model_GRU = keras.Sequential([
    keras.layers.GRU(32, input_shape=(None, float_data.shape[-1])),
    keras.layers.Dense(1)
])
model_GRU.compile(optimizer=keras.optimizers.RMSprop(),
             loss='mae')
history = model_GRU.fit(train_gen,
                   steps_per_epoch=500,
                   epochs=20,
                   validation_data = val_gen,
                   validation_steps = val_steps)
show_history(history, 20)<jupyter_output><empty_output><jupyter_text>3.使用dropout<jupyter_code>model_GRU = keras.Sequential([
    keras.layers.GRU(32, dropout=0.2, recurrent_dropout = 0.2,
                     input_shape=(None, float_data.shape[-1])),
    keras.layers.Dense(1)
])
model_GRU.compile(optimizer=keras.optimizers.RMSprop(),
             loss='mae')
history = model_GRU.fit(train_gen,
                   steps_per_epoch=500,
                   epochs=40,
                   validation_data = val_gen,
                   validation_steps = val_steps)
show_history(history, 40)<jupyter_output><empty_output><jupyter_text>4.增加容量<jupyter_code>model_GRU = keras.Sequential([
    keras.layers.GRU(32, dropout=0.1, recurrent_dropout = 0.5,return_sequences=True,
                     input_shape=(None, float_data.shape[-1])),
    keras.layers.GRU(64, dropout=0.1, recurrent_dropout = 0.5,
                     activation='relu'),
    keras.layers.Dense(1)
])
model_GRU.compile(optimizer=keras.optimizers.RMSprop(),
             loss='mae')
history = model_GRU.fit(train_gen,
                   steps_per_epoch=500,
                   epochs=40,
                   validation_data = val_gen,
                   validation_steps = val_steps)
show_history(history, 40)<jupyter_output><empty_output><jupyter_text>5.使用双向GRU<jupyter_code>model_BiGRU = keras.Sequential([
    keras.layers.Bidirectional(keras.layers.GRU(32),
                     input_shape=(None, float_data.shape[-1])),
    keras.layers.Dense(1)
])
model_BiGRU.compile(optimizer=keras.optimizers.RMSprop(),
             loss='mae')
history = model_GRU.fit(train_gen,
                   steps_per_epoch=500,
                   epochs=40,
                   validation_data = val_gen,
                   validation_steps = val_steps)
show_history(history, 40)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12038497948879773, 'fraction_numerical': 0.03329125907226255, 'mean_word_length': 2.5080243497509684, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '653043', 'license': 'MIT', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2194, 'n_words': 318, 'path': 'tensorflowTutorial/jena_climate.ipynb', 'repo_name': 'LeonKennedy/jupyter_space', 'size': '6338'}