TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人。
上一篇的内容,太简单了,做了一个详细的补充: t/383620
LSTM ( Long Short-Term Memory )是长短期记忆网络,是一种时间递归神经网络,适合于处理和预测时间序列中间隔和延迟相对较长的重要事件。LSTM 已经在科技领域有了多种应用。基于 LSTM 的系统可以学习翻译语言、控制机器人、图像分析、文档摘要、语音识别图像识别、手写识别、控制聊天机器人、预测疾病、点击率和股票、合成音乐等等任务。比特币的成交记录就是事件序列上的加个数据,可以基于过去的成交记录序列来对未来的价格作出预测。
原始数据来自 btctrade,用 requests 爬取,它包含比特币的 50 个交易记录。
get_trades.py 会获取这些交易记录,重新转化为 json ,并且用图片的方式展示出来,供下一步数据分析使用。
使用 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/
模型持久化,训练数据集持久化,测试数据集。