资源简介
使用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
- 上一篇:2020深圳杯C题.docx
- 下一篇:Python3简明教程(中文版)
相关资源
- Thompson Sampling 汤普森选样 最优路线问
- 15.时间序列预测LSTM模型python代码实现
- LSTM预测股价代码
- 机器学习从入门到精通50天python源码网
- 基于python实现房价预测回归问题
- 多层感知器实现,机器学习,神经网
- Python-基于OpenCVKerasTensorFlow实现深度换
- Python-在PyTorch中关注神经机器翻译的最
- Python-PointSIFT一种类似SIFT的网络模块用
- Python-O2O优惠券使用预测的第一名解决
- Python-TensorFlow神经机翻译seq2seq教程
- Python-FastDTW的一个Python实现
- Python-人体姿势估计和跟踪的简单基线
- Python-Pytorch实现的CRAFT文本检测器
- Python-2016CCF大数据精准营销中搜狗用户
- Python-YOLOv3的PyTorch完整实现
- Python-人群计数相关资源列表
- Python-那些值得读的深度学习论文集合
- Python-利用深度学习预测比特币价格
- Python-基于卷积神经网络的Keras音频分
- Python-PySpark编程最佳实践指南
- Python深度学习部分
- Softmax regression逻辑回归解决多线性分
- Python项目案例开发从入门到实战源代
- Xie-Beni评价m=2
- kNN分类器和两个-Python
- LSTM股价预测(python).zip
- Logistics回归
- Multiboost的Python实现
- 决策树回归算法
评论
共有 条评论