LSTM程序练习

本文介绍了如何定义RNN网络的关键部分,特别是针对LSTM细胞的处理。首先,将输入reshape适应模型要求,然后通过LSTM细胞进行处理,利用tf.nn.dynamic_rnn得到隐层维度的数据。最终,注意到使用final_state[1]作为RNN的输出,而outputs包含了所有时间步的输出。
摘要由CSDN通过智能技术生成

# coding: utf-8

# In[1]:

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


# In[2]:

path = "F:/Anaconda_program/股票预测/data_stocks.csv"
data = pd.read_csv(filepath_or_buffer=path)
data_use = data.drop(['DATE'],axis=1)


# In[3]:

data_use =data_use.iloc[:,0].tolist()


# In[4]:

data_use[:3]


# In[5]:
##对数据标准化处理,对减小loss有极大作用
scaler = MinMaxScaler(feature_range=(-1,1))
scaler.fit(data_use)
data_use = scaler.transform(data_use)


# In[6]:

data.head()


# In[7]:

max_time = 5


# In[8]:生成输入数据

data_x = []
data_y = []

for i in range(len(data_use)-max_time):
    data_x.append(data_use[i:i+max_time])
    data_y.append(data_use[i+max_time])


# In[9]:

def shuffle_data(data,labels):
    assert len(data) == len(labels)
    idx = np.arange(len(labels))
    np.random.shuffle(idx)
    return data[idx],labels[idx]


# In[10]:

data_x = np.array(data_x)
data_y = np.array(data_y)
data_x = data_x[:,:,np.newaxis]    ###此处需要加1维度,2维变3维,[-1,5]变为[-1,5,1]
data_y = data_y[:,np.newaxis]
data_shuffle_x,data_shuffle_y = shuffle_data(data_x,data_y)


# ratio = 0.8
# len_data = data_shuffle_x.shape[0]
# index_train = np.int(len_data*ratio)
# train_x = data_shuffle_x[:index_train]
# train_y = data_shuffle_y[:index_train]
# 
# test_x = data_shuffle_x[index_train:]
# test_y = data_shuffle_y[index_train:]
# 

# In[11]:

ratio = 0.8
len_data = data_shuffle_x.shape[0]
index_train = np.int(len_data*ratio)
train_x = data_x[:index_train]
train_y = data_y[:index_train]

test_x = data_x[index_train:]
test_y = data_y[index_train:]


# In[12]:

print(train_x.sha
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值