91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 12KB
    文件類型: .py
    金幣: 2
    下載: 9 次
    發布日期: 2021-06-19
  • 語言: Python
  • 標簽: 機器學習??LSTM??

資源簡介

使用python實現了PSO算法優化LSTM參數,包括time_step,batch_size,learning rate 和 cell_size等

資源截圖

代碼片段和文件信息

import?tensorflow?as?tf
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?pandas?as?pd
from?sklearn.preprocessing?import?MinMaxScaler
import?time
import?random

minMax1?=?MinMaxScaler()??#?minmax
tpot_data?=?pd.read_excel(‘S2_date_6-6_new.xlsx‘)??#?data?load
tpot_data?=?tpot_data.dropna()??#?delete?NAN
tpot_data?=?tpot_data[:5000]??#?delete?NAN
print(len(tpot_data))
targcts?=?tpot_data[‘S2[MW]‘].values
tpot_data[“DATE“]?=?tpot_data[“DATE“].astype(np.int64)/np.max(tpot_data[“DATE“].astype(np.int64))*10
features?=?minMax1.fit_transform(tpot_data.drop(‘S2[MW]‘?axis=1))??#?feature

#?num?=?18202
num?=?4000
training_features?testing_features?training_target?testing_target?=?\
????????features[:num]?features[num:-1]?targcts[:num]?targcts[num:-1]
batch_start?=?0
NN?=?0
print(len(training_target))
print(len(testing_target))


def?get_batch(input_size?batch_size?time_step?target?feature):
????global?batch_start
????print(“start:“?batch_start)
????sxs?=?np.arange(batch_start?batch_start?+?time_step?*?batch_size).reshape((batch_size?time_step))
????#?構造數組?batch?*?steps?=?100?*?10?=?1000行數據
????#?sxs?reshape?(100batch?10steps)
????#?循環過程每次輸入10行數據,輸入100次

????xs?=?feature[batch_start:?batch_start+time_step*batch_size]
????ys?=?target[batch_start:?batch_start+time_step*batch_size]
????#?構造數組?batch?*?1steps?=?10?*?100?=?1000行數據

????#?print(‘時間段?=‘?batch_start?batch_start?+?time_step?*?batch_size)
????#?輸出當前狀態(起點位置?終點位置)

????seq?=?xs.reshape((batch_size?time_step?input_size))
????#?xs?reshape?(100batch?10steps?len(input))
????res?=?ys.reshape((batch_size?time_step?1))
????#?ys?reshape?(100batch?10steps?len(output))
????batch_start?+=?time_step

????return?[seq?res?sxs]
????#?feature(batch?step?input)?target(batch?step?output)?aggregated?data(batch?step?input?+?output)


def?function(ps?test?le):
????ss?=?100?-?np.sum(np.abs(test?-?ps))/le
????return?ss


class?LSTMRNN(object):
????def?__init__(self?n_steps?input_size?output_size?cell_size?batch_size?LR):
????????self.n_steps?=?int(n_steps)
????????self.input_size?=?int(input_size)
????????self.output_size?=?int(output_size)
????????self.cell_size?=?int(cell_size)??#?記憶單元維度
????????self.batch_size?=?int(batch_size)
????????if?NN?!=?0:
????????????tf.reset_default_graph()
????????with?tf.name_scope(‘inputs‘):
????????????self.xs?=?tf.placeholder(tf.float32?[None?n_steps?input_size]?name=‘xs‘)
????????????self.ys?=?tf.placeholder(tf.float32?[None?n_steps?output_size]?name=‘ys‘)
????????with?tf.variable_scope(‘in_hidden‘):
????????????self.add_input_layer()
????????with?tf.variable_scope(‘LSTM_cell‘):
????????????self.add_cell()
????????with?tf.variable_scope(‘out_hidden‘):
????????????self.add_output_layer()
????????with?tf.name_scope(‘cost‘):
????????????self.compute_cost()
????????with?tf.name_scope(‘train‘):
????????????self.train_op?=?tf.train.AdamOptimizer(LR).minimize(sel

評論

共有 條評論