资源简介
使用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简明教程(中文版)
相关资源
- python机器学习Sebastian Raschka中文最新完
- Python-DeepMoji模型的pyTorch实现
- 《机器学习实战》源代码Python3
- Python-使用DeepFakes实现YouTube视频自动换
- Introduction to machine learning with python (
- python新浪微博爬虫,爬取微博和用户
- Python-一系列高品质的动漫人脸数据集
- Python-Insightface人脸检测识别的最小化
- 非线性回归Python代码
- 093 2018北风网人工智能视频(完结)转
- 运用LSTM对CPI数据进行预测.py
- python的色情图片识别
- 贝叶斯网络程序
- 《机器学习实战》Python3代码
- Python-自然场景文本检测PSENet的一个
- Python-在特征金字塔网络FPN的Pytorch实现
- Python-PyTorch实时多人姿态估计项目的实
- Python-用PyTorch10实现FasterRCNN和MaskRCNN比
- Python-心脏核磁共振MRI图像分割
- Python-基于YOLOv3的行人检测
- Python-RLSeq2Seq用于SequencetoSequence模型的
- Python-PyTorch对卷积CRF的参考实现
- Python-高效准确的EAST文本检测器的一个
- Python-pytorch实现的人脸检测和人脸识别
- Python-UNet用于医学图像分割的嵌套UN
- Python-TensorFlow弱监督图像分割
- Python-基于tensorflow实现的用textcnn方法
- Python-Keras实现Inceptionv4InceptionResnetv1和
- Python-pytorch中文手册
- Python-FastSCNN的PyTorch实现快速语义分割
评论
共有 条评论