深度学习RNN实现股票预测填坑笔记

本文主要参考以下网址,本文是对该网址所列出的步骤的实践笔记,如有侵权,请与我联系:

https://blog.csdn.net/buptgshengod/article/details/78880941

1.代码下载

根据作者最后列出的网址,在GIT-cmd下使用命令下载文件:

git clone https://github.com/jimenbian/stock-rnn

如果你没有更改配置,这个stock-rnn就下载在你的git根目录下。

2.运行

1.用python3第一次运行时你会出现这个错误:
  File "D:\Pro2\Anaconda3\lib\random.py", line 272, in shuffle
    x[i], x[j] = x[j], x[i]
TypeError: 'range' object does not support item assignment

参照这个网址的意见:

http://cache.baiducontent.com/c?m=9d78d513d9850aed0ffa950e5a56973b594381132bd6a3027fa58448e2735a310735bfee70351075c4c50d7000df140cb2a76734200351e1c69fc95dddcace75699f2745315bc4034f935fb8cb377e8777cf4de9de4aa6fbab67cff1858f9940148a0d0a20c6b6c91b1715b972ed5174a6b1993f481e41e0b66c36e8590075c8655df500aea174355addebd75747c72cc7666593af30b535&p=987ec64ad48111a05bec94255b4096&newp=9a72c64ad4934eac59eddc374e5c86231610db2151d7da166b82c825d7331b001c3bbfb423261507d3c676610bac4f57ecf63d71340225a3dda5c91d9fb4c57479&user=baidu&fm=sc&query=TypeError%3A+%27range%27+object+does+not+support+item+assignment&qid=cb6367ba00010053&p1=4

由于这个程序是在python2.7版本的tensorflow下运行的,但window下的python2.7无法安装tensorflow,
所以为了运行,要把这个python2.7版本的程序改成python3.5版本的。所以这样修改:

data_model.py文件第63行:
batch_indices = range(num_batches)
改为:
batch_indices = list(range(num_batches))


2.model_rnn.py文件第191行:
for sample_sym, indices in sample_indices.iteritems():
改为:
for sample_sym, indices in sample_indices.items():

3.之后程序能运行了,但是在stock/image下的图片由于背景是透明的,这些图片在win10自带的图片查看软件下的透明背景是黑的,很难看,所以我修改了背景为白色:
stock-rnn.py文件第269行:
        plt.savefig(figname, format='png', bbox_inches='tight', transparent=True, )
改为:
        plt.savefig(figname, format='png', bbox_inches='tight', transparent=True, facecolor='white', edgecolor='black')  

# facecolor='white'在这里修改背景颜色

 

然后anaconda prompt下使用命令安装依赖的库:

pip install numpy==1.13.1 pandas==0.16.2 scikit-learn==0.16.1 scipy==0.19.1 tensorflow==1.2.1

anaconda prompt转到stock-rnn目录下输入命令开始训练并预测:

python main.py --stock_symbol=SP500 --train --input_size=1 --lstm_size=128 --max_epoch=50


4.那么怎么用tensorboard看tensorflow训练的模型的结果呢?
我从本程序的源网站:https://github.com/lilianweng/stock-rnn
找到这行指令,先在anaconda prompt中输入:

tensorboard --logdir ./logs --port 1234 --debug
然后这个黑窗口会输出:Starting TensorBoard b'54' at http://DESKTOP-RID98CM:1234
你只要在浏览器里输入:

http://DESKTOP-RID98CM:1234就可以了。
如果你没有开始训练,tensorboard会出错:No scalar data was found.

 

 3.查看结果

在stock-rnn/images/目录下的文件夹就是此次你训练后测试生成的图片。

如果使用更大的数据量,经过更多次数的迭代,预测结果会更准确。 

 

 

4.我的环境变化后再次使用此程序Debug过程:

 

***********以下2019.5.14再次更新
1.有的时候你更新了pandas再次运行此程序可能会出错:
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    import pandas as pd
  File "D:\Pro2\Anaconda3\envs\tensorflow\lib\site-packages\pandas\__init__.py", line 42, in <module>
    from pandas.core.api import *
  File "D:\Pro2\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\api.py", line 10, in <module>
    from pandas.core.groupby import Grouper
  File "D:\Pro2\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\groupby\__init__.py", line 1, in <module>
    from pandas.core.groupby.groupby import GroupBy  # noqa: F401
  File "D:\Pro2\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\groupby\groupby.py", line 23, in <module>
    from pandas.errors import AbstractMethodError
ImportError: cannot import name 'AbstractMethodError'
;
需要你卸载pandas:pip uninstall pandas
然后安装特定的版本:pip install pandas==0.16.2

安装时又报错:
error: command 'D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
于是我置之不理,看一下pip list,里面的pandas退回了0.20

继续运行:
python main.py --stock_symbol=0000001 --train --input_size=1 --lstm_size=128 --max_epoch=50
ImportError: numpy.core.multiarray failed to import

于是卸载numpy,按照教程安装了numpy==1.13.1
继续报错:

    integral_types = (_numbers.Integral, _np.integer)
AttributeError: module 'numpy' has no attribute 'integer'
根据网站:
https://blog.csdn.net/zhaoshuling1109/article/details/80361391
卸载numpy后用此命令正确安装numpy:pip install -U numpy==1.13.1
没用:
RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
ImportError: numpy.core.multiarray failed to import

于是只好运行:pip install numpy --upgrade
然后特么居然过了这个错误,但是遇到新的错误:
File "D:\UryWu\股票预测\stock-rnn\data_model.py", line 26, in __init__
    raw_df = pd.read_csv(os.path.join("data", "%s.csv" % stock_sym))
AttributeError: module 'pandas' has no attribute 'read_csv'


然后卸载了pandas,安装了0.24版也就是最新版的出错:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte
安装0.20版的pandas也是报这个错,但是0.16.2版本的pandas又安装不上,是报这个错:
 pandas\lib.c(72775): warning C4244: “函数”: 从“npy_int64”转换到“int”,可能丢失数据
    pandas\lib.c(92829): warning C4047: “return”:“int”与“void *”的间接级别不同
    pandas\lib.c(92986): warning C4047: “return”:“int”与“void *”的间接级别不同
    error: command 'D:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2

问题很麻烦,于是我回到上一个问题,就是read_csv的那个问题:
根据这个网址的指示:
https://blog.csdn.net/moledyzhang/article/details/78978312
把你的数据csv文件另存为utf-8编码就可以了,它原来是ANSI编码

接着遇到错误:
File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Close'

然后我觉得是数据格式的问题,果然,看了一下我的csv数据:
实际上这个程序读取数据只读取开盘价(Open)和收盘价(Close),就是程序只读Open和Close那两列,所以只需让csv数据文件有那两列就可以了。
***********以上2019.5.14再次更新

 

***********以下2019.7.17再次更新

ERROR:FileNotFoundError: [Errno 2] File b'data\\0000001.csv' does not exist: b'data\\0000001.csv'

我想了想,我基本上没有变动过什么东西,所以应该是环境不对,当前是我名为tensorflow的虚拟环境,于是我把环境改为base,就可以了。

附录:

1.关于使用anaconda出现CondaHTTPError错误的解决
https://blog.csdn.net/ling_xiobai/article/details/78659981
按照上面配置了清华大学的镜像


2.安装tensorflow==1.2.1时遇到错误:

Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'd:\\pro2\\anaconda3\\lib\\site-packages\\tensorflow_gpu-1.1.0.dist-info\\METADATA'

去Anaconda3\Lib\site-packages下搜索tensorflow_gpu-1.1.0.dist-info并删除这个文件夹与搜索到的文件

3.怎么理解神经网络的迭代:iteration、epoch
https://zhidao.baidu.com/question/716300338908227765.html
atchsize:中文翻译为批大小(批尺寸)。在深度学习中,一般采用SGD训练,即每次训练在训练集中取batchsize个样本训练;
iteration:中文翻译为迭代,1个iteration等于使用batchsize个样本训练一次;一个迭代 = 一个正向通过+一个反向通过
epoch:迭代次数,1个epoch等于使用训练集中的全部样本训练一次;一个epoch = 所有训练样本的一个正向传递和一个反向传递
举个例子,训练集有1000个样本,batchsize=10,那么:训练完整个样本集需要:100次iteration,1次epoch。

4.股市中VOLUME是什么意思
https://zhidao.baidu.com/question/1576619087572264900.html
Volume 表示股票成交量的意思,成交量=成交股数*成交价格 再加权求和;成交量指标(VOL)是指一个时间单位内撮合成交的股数,用柱形实体表示。里面绿柱,红柱是随当日K线的变化而变化,也就是说日K线收红则成交量柱也为红,反之是绿。但并不是说涨了就为红,而是收盘价高于开盘价则K线为红,收盘价低于开盘价则K线为绿。

给自己看的其他事项:

1.虚拟环境为base

2.训练参数记录1:--stock_symbol=0000001 --train --input_size=1 --lstm_size=64 --max_epoch=5000

对上证指数0000001的5000步迭代eproch,从20:46到20:58

Step:24901 [Epoch:4980] [Learning rate: 0.000000] train_loss:0.000183 test_loss:0.000090

 

训练参数记录2:--stock_symbol=0000001 --train --input_size=1 --lstm_size=32 --max_epoch=5000

从21:00到21:25

Step:24901 [Epoch:4980] [Learning rate: 0.000000] train_loss:0.000199 test_loss:0.000051

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值