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

2017-08-17 15:38:32 +08:00
 fendouai_com

简介

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/

更新计划

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

3045 次点击
所在节点    Python
1 条回复
aosp
2017-08-18 09:31:32 +08:00
楼主靠他赚了钱没

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/383704

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX