• 大小: 12KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 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

评论

共有 条评论