1、摘要
本文主要講解:使用鯨魚算法優(yōu)化LSTM超參數(shù)-神經(jīng)元個(gè)數(shù)-dropout-batch_size
主要思路:
- 鯨魚算法 Parameters : 迭代次數(shù)、鯨魚的維度、鯨魚的數(shù)量, 參數(shù)的上限,參數(shù)的下限
- LSTM Parameters 神經(jīng)網(wǎng)絡(luò)第一層神經(jīng)元個(gè)數(shù)、神經(jīng)網(wǎng)絡(luò)第二層神經(jīng)元個(gè)數(shù)、dropout比率、batch_size
- 開始搜索:初始化所鯨魚的位置、迭代尋優(yōu)、返回超出搜索空間邊界的搜索代理、計(jì)算每個(gè)搜索代理的目標(biāo)函數(shù)、更新 Alpha, Beta, and Delta
- 訓(xùn)練模型,使用鯨魚算法找到的最好的全局最優(yōu)參數(shù)
- plt.show()
2、數(shù)據(jù)介紹
zgpa_train.csv
DIANCHI.csv
需要數(shù)據(jù)的話去我其他文章的評(píng)論區(qū)
可接受定制
3、相關(guān)技術(shù)
WOA算法設(shè)計(jì)的既精妙又富有特色,它源于對(duì)自然界中座頭鯨群體狩獵行為的模擬, 通過鯨魚群體搜索、包圍、追捕和攻擊獵物等過程實(shí)現(xiàn)優(yōu)時(shí)化搜索的目的。在原始的WOA中,提供了包圍獵物,螺旋氣泡、尋找獵物的數(shù)學(xué)模型。
PS:如陷入局部最優(yōu)建議修改參數(shù)的上下限或者修改鯨魚尋優(yōu)的速度
4、完整代碼和步驟
代碼輸出如下:
此程序運(yùn)行代碼版本為:
tensorflow==2.5.0
numpy==1.19.5
keras==2.6.0
matplotlib==3.5.2
文章來源:http://www.zghlxwxcb.cn/news/detail-415276.html
主運(yùn)行程序入口文章來源地址http://www.zghlxwxcb.cn/news/detail-415276.html
import math
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from tensorflow.python.keras.callbacks import EarlyStopping
from tensorflow.python.keras.layers import Dense, Dropout, LSTM
from tensorflow.python.keras.layers.core import Activation
from tensorflow.python.keras.models import Sequential
os.chdir(r'D:\項(xiàng)目\PSO-LSTM\具體需求')
'''
灰狼算法優(yōu)化LSTM
'''
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用來正常顯示中文標(biāo)簽
plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負(fù)號(hào)
def creat_dataset(dataset, look_back):
dataX, dataY = [], []
for i in range(len(dataset) - look_back - 1):
a = dataset[i: (i + look_back)]
dataX.append(a)
dataY.append(dataset[i + look_back])
return np.array(dataX), np.array(dataY)
dataframe = pd.read_csv('zgpa_train.csv', header=0, parse_dates=[0], index_col=0, usecols=[0, 5], squeeze=True)
dataset = dataframe.values
data = pd.read_csv('DIANCHI.csv', header=0)
z = data['fazhi']
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset.reshape(-1, 1))
train_size = int(len(dataset) * 0.8)
test_size = len(dataset) - train_size
train, test = dataset[0: train_size], dataset[train_size: len(dataset)]
look_back = 10
trainX, trainY = creat_dataset(train, look_back)
testX, testY = creat_dataset(test, look_back)
def build_model(neurons1, neurons2, dropout):
X_train, y_train = trainX, trainY
X_test, y_test = testX, testY
model = Sequential()
model.add(LSTM(units=neurons1, return_sequences=True, input_shape=(10, 1)))
model.add(LSTM(units=neurons2, return_sequences=True))
model.add(LSTM(111, return_sequences=False))
model.add(Dropout(dropout))
model.add(Dense(55))
model.add(Dense(units=1))
model.add(Activation('relu'))
model.compile(loss='mean_squared_error', optimizer='Adam')
return model, X_train, y_train, X_test, y_test
def training(X):
neurons1 = int(X[0])
neurons2 = int(X[1])
dropout = round(X[2], 6)
batch_size = int(X[3])
print([neurons1,neurons2,dropout,batch_size])
model, X_train, y_train, X_test, y_test = build_model(neurons1, neurons2, dropout)
model.fit(
X_train,
y_train,
batch_size=batch_size,
epochs=10,
validation_split=0.1,
verbose=0,
callbacks=[EarlyStopping(monitor='val_loss', patience=22, restore_best_weights=True)])
pred = model.predict(X_test)
temp_mse = mean_squared_error(y_test, pred)
print(temp_mse)
return temp_mse
class woa():
# 初始化
def __init__(self, LB, UB, dim=4, b=1, whale_num=20, max_iter=500):
self.LB = LB
self.UB = UB
self.dim = dim
self.whale_num = whale_num
self.max_iter = max_iter
self.b = b
# Initialize the locations of whale
self.X = np.random.uniform(0, 1, (whale_num, dim)) * (UB - LB) + LB
self.gBest_score = np.inf
self.gBest_curve = np.zeros(max_iter)
self.gBest_X = np.zeros(dim)
# 適應(yīng)度函數(shù) max_depth,min_samples_split,min_samples_leaf,max_leaf_nodes
def fitFunc(self, para):
# 建立模型
mse = training(para)
return mse
# 優(yōu)化模塊
def opt(self):
t = 0
while t < self.max_iter:
print('At iteration: ' + str(t))
for i in range(self.whale_num):
# 防止X溢出
self.X[i, :] = np.clip(self.X[i, :], self.LB, self.UB) # Check boundries
fitness = self.fitFunc(self.X[i, :])
# Update the gBest_score and gBest_X
if fitness <= self.gBest_score:
self.gBest_score = fitness
self.gBest_X = self.X[i, :].copy()
print('self.gBest_score: ', self.gBest_score)
print('self.gBest_X: ', self.gBest_X)
a = 2 * (self.max_iter - t) / self.max_iter
# Update the location of whales
for i in range(self.whale_num):
p = np.random.uniform()
R1 = np.random.uniform()
R2 = np.random.uniform()
A = 2 * a * R1 - a
C = 2 * R2
l = 2 * np.random.uniform() - 1
# 如果隨機(jī)值大于0.5 就按以下算法更新X
if p >= 0.5:
D = abs(self.gBest_X - self.X[i, :])
self.X[i, :] = D * np.exp(self.b * l) * np.cos(2 * np.pi * l) + self.gBest_X
else:
# 如果隨機(jī)值小于0.5 就按以下算法更新X
if abs(A) < 1:
D = abs(C * self.gBest_X - self.X[i, :])
self.X[i, :] = self.gBest_X - A * D
else:
rand_index = np.random.randint(low=0, high=self.whale_num)
X_rand = self.X[rand_index, :]
D = abs(C * X_rand - self.X[i, :])
self.X[i, :] = X_rand - A * D
self.gBest_curve[t] = self.gBest_score
t += 1
return self.gBest_curve, self.gBest_X
if __name__ == '__main__':
'''
神經(jīng)網(wǎng)絡(luò)第一層神經(jīng)元個(gè)數(shù)
神經(jīng)網(wǎng)絡(luò)第二層神經(jīng)元個(gè)數(shù)
dropout比率
batch_size
'''
# ===========主程序================
Max_iter = 3 # 迭代次數(shù)
dim = 4 # 鯨魚的維度
SearchAgents_no = 2 # 尋值的鯨魚的數(shù)量
# 參數(shù)的上限
UB = np.array([20, 100, 0.01, 36])
# 參數(shù)的下限
LB = np.array([5, 20, 0.00001, 5])
# best_params is [2.e+02 3.e+02 1.e-03 1.e+00]
fitnessCurve, para = woa(LB, UB, dim=dim, whale_num=SearchAgents_no, max_iter=Max_iter).opt()
print('best_params is ', para)
# 訓(xùn)練模型 使用PSO找到的最好的神經(jīng)元個(gè)數(shù)
neurons1 = int(para[0])
neurons2 = int(para[1])
dropout = para[2]
batch_size = int(para[3])
model, X_train, y_train, X_test, y_test = build_model(neurons1, neurons2, dropout)
history = model.fit(X_train, y_train, epochs=100, batch_size=batch_size, validation_split=0.2, verbose=1,
callbacks=[EarlyStopping(monitor='val_loss', patience=29, restore_best_weights=True)])
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(trainY)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(testY)
trainScore = math.sqrt(mean_squared_error(trainY, trainPredict[:, 0]))
# print('Train Score %.2f RMSE' %(trainScore))
testScore = math.sqrt(mean_squared_error(testY, testPredict[:, 0]))
# print('Test Score %.2f RMSE' %(trainScore))
trainPredictPlot = np.empty_like(dataset)
trainPredictPlot[:] = np.nan
trainPredictPlot = np.reshape(trainPredictPlot, (dataset.shape[0], 1))
trainPredictPlot[look_back: len(trainPredict) + look_back, :] = trainPredict
testPredictPlot = np.empty_like(dataset)
testPredictPlot[:] = np.nan
testPredictPlot = np.reshape(testPredictPlot, (dataset.shape[0], 1))
testPredictPlot[len(trainPredict) + (look_back * 2) + 1: len(dataset) - 1, :] = testPredict
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()
fig2 = plt.figure(figsize=(20, 15))
plt.rcParams['font.family'] = ['STFangsong']
ax = plt.subplot(222)
plt.plot(scaler.inverse_transform(dataset), 'b-', label='實(shí)驗(yàn)數(shù)據(jù)')
plt.plot(trainPredictPlot, 'r', label='訓(xùn)練數(shù)據(jù)')
plt.plot(testPredictPlot, 'g', label='預(yù)測(cè)數(shù)據(jù)')
plt.plot(z, 'k-', label='壽命閥值RUL')
plt.ylabel('capacity', fontsize=20)
plt.xlabel('cycle', fontsize=20)
plt.legend()
name = 'neurons1_' + str(neurons1) + 'neurons2_' + str(neurons2) + '_dropout' + str(
dropout) + '_batch_size' + str(batch_size)
plt.savefig('D:\項(xiàng)目\PSO-LSTM\具體需求\photo\\' + name + '.png')
plt.show()
到了這里,關(guān)于鯨魚算法優(yōu)化LSTM超參數(shù)-神經(jīng)元個(gè)數(shù)-dropout-batch_size的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!