今天是第十天我想要跑一個Lstm模型訓練,以下是程式碼跟資料集
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, BatchNormalization,Conv1D, MaxPooling1D
from tensorflow.keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import Huber
from tensorflow.keras.regularizers import l1,l2
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import torch
import pandas as pd
import matplotlib.pyplot as plt
def create_dataset(dataset,size):
question,ans = [],[]
for i in range(len(dataset) - size):
q_array = dataset[i:i + size]
a_array = dataset[i + size]
ans.append(a_array)
question.append(q_array)
return np.array(question), np.array(ans)
lookback = 32
model = Sequential([
LSTM(200, activation = 'relu', return_sequences=True,input_shape=(lookback, 2),kernel_regularizer=l1(0.1)),
Dropout(0.1),
LSTM(200, activation='relu', return_sequences=False,kernel_regularizer=l2(0.1)),
Dense(2, kernel_regularizer=l1(0.1))])
model.compile(optimizer=Adam(learning_rate=0.0002), loss=Huber())
input_data = pd.read_csv("1fish_11000.csv")
input_data = input_data[["x","y"]].values.astype('float32')
train,test = create_dataset(input_data,lookback)
print(train.shape)
print(test.shape)
early_stopping = EarlyStopping(monitor='val_loss', patience=50, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-6)
checkpoint = ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True, mode='min')
history = model.fit(train, test, epochs=6000, batch_size=lookback, callbacks=[checkpoint, early_stopping, reduce_lr])
plt.plot(history.history['loss'], label='Loss',c = "g")
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(history.history['accuracy'], label='accuracy',c = "b")
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy.')
plt.legend()
plt.show()
model.save('new lstm.h5')
這段程式碼建立並訓練了一個基於LSTM(Long Short-Term Memory)層的神經網絡模型,來預測某個時間序列資料的未來值。以下是程式碼的詳細解釋:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import Huber
from tensorflow.keras.regularizers import l1, l2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
這段程式碼匯入了建構與訓練深度學習模型所需的模組。這些模組包括:
def create_dataset(dataset, size):
question, ans = [], []
for i in range(len(dataset) - size):
q_array = dataset[i:i + size]
a_array = dataset[i + size]
ans.append(a_array)
question.append(q_array)
return np.array(question), np.array(ans)
這個函式將數據集轉換成LSTM模型可接受的格式。具體來說,它會創建一系列的輸入(question
)和對應的標籤(ans
),用來預測下一步的數值。
model = Sequential([
LSTM(200, activation='relu', return_sequences=True, input_shape=(lookback, 2), kernel_regularizer=l1(0.1)),
Dropout(0.1),
LSTM(200, activation='relu', return_sequences=False, kernel_regularizer=l2(0.1)),
Dense(2, kernel_regularizer=l1(0.1))
])
這個模型包含兩個LSTM層和一個全連接層(Dense layer)。具體來說:
x
和y
。此外,這些層都使用了L1和L2正則化來控制模型的複雜度,防止過擬合。
model.compile(optimizer=Adam(learning_rate=0.0002), loss=Huber())
這段程式碼編譯模型,設定使用Adam優化器和Huber損失函數。
input_data = pd.read_csv("1fish_11000.csv")
input_data = input_data[["x","y"]].values.astype('float32')
train, test = create_dataset(input_data, lookback)
print(train.shape)
print(test.shape)
這裡從CSV檔案讀取數據,並使用前面定義的create_dataset
函式來準備訓練和測試數據。lookback
是序列的長度,設定為32。
early_stopping = EarlyStopping(monitor='val_loss', patience=50, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=10, min_lr=1e-6)
checkpoint = ModelCheckpoint('best_model.keras', monitor='val_loss', save_best_only=True, mode='min')
這裡定義了三個回調函數:
val_loss
,如果50個epoch內沒有改善,則停止訓練,並恢復至最佳模型權重。val_loss
停止改善時,將學習率降低一半。history = model.fit(train, test, epochs=6000, batch_size=lookback, callbacks=[checkpoint, early_stopping, reduce_lr])
這段程式碼以指定的epochs(6000)和batch_size(32)訓練模型,並使用前述的回調函數來控制訓練過程。
plt.plot(history.history['loss'], label='Loss', c="g")
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(history.history['accuracy'], label='Accuracy', c="b")
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
這部分代碼可視化了模型訓練過程中的損失(Loss)和準確率(Accuracy)。不過,這裡的history.history
裡並沒有準確率的資料,所以在這裡的Accuracy
可視化會報錯。模型的損失則會成功顯示。
model.save('new lstm.h5')
最後,將訓練好的模型保存為H5格式的檔案,以便將來載入和使用。
因為訓練要跑6000次訓練可能要跑快一天,注意不要關掉程式就好。