用sklearn和tensorflow做boston房价的回归计算的比较(3)--RNN之递归神经网路LSTM

在tensorflow里RNN才是做回归计算的正规军,其中LSTM更是让人工智能有了记忆,如果cnn最适合做的是图像识别,那么LSTM就是视频识别。网上的教程多是用正余弦数据在做预测,输入输出都是一维,我这用波士顿房价,输入是13个特征!

注意与前面两个模型不同的是,没有用train_test_split把训练数据分割,而是用的时序数据。

代码中注释比较少,不明白的可以看周莫烦的视频!

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-09-RNN3/

# 参考https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-09-RNN3/
from sklearn.datasets import load_boston

from sklearn import preprocessing
import tensorflow as tf
import numpy as np

# 波士顿房价数据
boston = load_boston()
x = boston.data
y = boston.target

print('波士顿数据X:',x.shape)# (506, 13)
# print(x[::100])
print('波士顿房价Y:',y.shape)
# print(y[::100])
# 数据标准化
ss_x = preprocessing.StandardScaler()
train_x = ss_x.fit_transform(x)
ss_y = preprocessing.StandardScaler()
train_y = ss_y.fit_transform(y.reshape(-1, 1))

BATCH_START = 0     # 建立 batch data 时候的 index
TIME_STEPS = 10     # backpropagation through time 的 time_steps
BATCH_SIZE = 30
INPUT_SIZE = 13      # sin 数据输入 size
OUTPUT_SIZE = 1     # cos 数据输出 size
CELL_SIZE = 10      # RNN 的 hidden unit size
LR = 0.006          # learning rate

def get_batch_boston():
    global train_x, train_y,BATCH_START, TIME_STEPS
    x_part1 = train_x[BATCH_START : BATCH_START+TIME_STEPS*BATCH_SIZE]
    y_part1 = train_y[BATCH_START : BATCH_START+TIME_STEPS*BATCH_SIZE]
    print('时间段=', BATCH_START, BATCH_START + TIME_STEPS * BATCH_SIZE)


    seq =x_part1.reshape((BATCH_SIZE, TIME_STEPS ,INPUT_SIZE))
    res =y_part1.reshape((BATCH_SIZE, TIME_STEPS ,1))

    BATCH_START += TIME_STEPS

    # returned seq, res and xs: shape (batch, step, input)
    #np.newaxis 用来增加一个维度 变为三个维度,第三个维度将用来存上一批样本
  • 18
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 30
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值