V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fendouai_com
V2EX  ›  Python

TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人[补上篇]

  •  
  •   fendouai_com · 2017-08-17 15:38:32 +08:00 · 3038 次点击
    这是一个创建于 2441 天前的主题,其中的信息可能已经有所发展或是发生改变。

    简介

    TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人。

    上一篇的内容,太简单了,做了一个详细的补充: https://www.v2ex.com/t/383620

    LSTM ( Long Short-Term Memory )是长短期记忆网络,是一种时间递归神经网络,适合于处理和预测时间序列中间隔和延迟相对较长的重要事件。LSTM 已经在科技领域有了多种应用。基于 LSTM 的系统可以学习翻译语言、控制机器人、图像分析、文档摘要、语音识别图像识别、手写识别、控制聊天机器人、预测疾病、点击率和股票、合成音乐等等任务。比特币的成交记录就是事件序列上的加个数据,可以基于过去的成交记录序列来对未来的价格作出预测。

    数据集

    原始数据来自 btctrade,用 requests 爬取,它包含比特币的 50 个交易记录。

    get_trades.py 会获取这些交易记录,重新转化为 json ,并且用图片的方式展示出来,供下一步数据分析使用。

    模型

    rnn_predicter.py

    使用 LSMT 模型。截取 10 个交易记录作为输入,如果 第 11 个价格比第 10 个高,就把输出设置为 [1,0,0],如果低就设置为 [0,0,1] ,如果相同 [0,1,0]。

    for i in range(0,20):
        #print(price)
        one_predictor=np.array(price[i:i+20],dtype=float)
        #print(one_predictor)
        train_x.append(one_predictor)
        if(int(price[i+20])>int(price[i+21])):
            train_y.append(np.array([1,0,0]))
        elif (int(price[i + 20]) == int(price[i + 21])):
            train_y.append(np.array([0,1,0]))
        elif(int(price[i+20])<int(price[i+21])):
            train_y.append(np.array([0,0,1]))
    

    下一步定义模型:

    def RNN(x, weights, biases):
        #首先把数据拆分为 n 个序列,每一个的维度 (batch_size, n_input)
        x = tf.unstack(x, n_steps, 1)
    
        # 定一个 lstm cell
        lstm_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    
        # 获得 lstm 的输出
        outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
        # 加个线性激活
        return tf.matmul(outputs[-1], weights['out']) + biases['out']
    

    获得结果,定义损失函数和优化函数

    pred = RNN(x, weights, biases)
    # Define loss and optimizer
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
    
    # Evaluate model
    correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    

    项目开源地址和训练结果

    https://github.com/TensorFlowNews/TensorFlow-Bitcoin-Robot/

    后续更新发布

    http://www.tensorflownews.com/

    更新计划

    模型持久化,训练数据集持久化,测试数据集。

    1 条回复    2017-08-18 09:31:32 +08:00
    aosp
        1
    aosp  
       2017-08-18 09:31:32 +08:00
    楼主靠他赚了钱没
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1000 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:02 · PVG 04:02 · LAX 13:02 · JFK 16:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.