直接上代碼
#######
# coding=utf-8
importpandasaspd
importnumpyasnp
importmatplotlib.pyplotasplt
importtensorflowastf
rnn_unit =10# 隱層數(shù)量
input_size =7
output_size =1
lr =0.0006# 學(xué)習(xí)率
# ——————————————————導(dǎo)入數(shù)據(jù)——————————————————————
f =open('/Users/zhangmenren/Desktop/機(jī)器學(xué)習(xí)/dataset_2.csv')
df = pd.read_csv(f)# 讀入股票數(shù)據(jù)
data = df.iloc[:,2:10].values# 取第3-10列
# 獲取訓(xùn)練集
defget_train_data(batch_size=1, time_step=20, train_begin=0, train_end=5800):
batch_index = []
data_train = data[train_begin:train_end]
normalized_train_data = (data_train - np.mean(data_train,axis=0)) / np.std(data_train,axis=0)# 標(biāo)準(zhǔn)化
train_x, train_y = [], []# 訓(xùn)練集
foriinrange(len(normalized_train_data) - time_step):
ifi % batch_size ==0:
batch_index.append(i)
x = normalized_train_data[i:i + time_step, :7]
y = normalized_train_data[i:i + time_step,7, np.newaxis]
train_x.append(x.tolist())
train_y.append(y.tolist())
batch_index.append((len(normalized_train_data) - time_step))
returnbatch_index, train_x, train_y
# 獲取測(cè)試集
defget_test_data(time_step=1, test_begin=5800):
data_test = data[test_begin:]
mean = np.mean(data_test,axis=0)
std = np.std(data_test,axis=0)
normalized_test_data = (data_test - mean) / std# 標(biāo)準(zhǔn)化
size = (len(normalized_test_data) + time_step -1) // time_step# 有size個(gè)sample
test_x, test_y = [], []
foriinrange(size -1):
x = normalized_test_data[i * time_step:(i +1) * time_step, :7]
y = normalized_test_data[i * time_step:(i +1) * time_step,7]
test_x.append(x.tolist())
test_y.extend(y)
test_x.append((normalized_test_data[(i +1) * time_step:, :7]).tolist())
test_y.extend((normalized_test_data[(i +1) * time_step:,7]).tolist())
returnmean, std, test_x, test_y
# ——————————————————定義神經(jīng)網(wǎng)絡(luò)變量——————————————————
# 輸入層、輸出層權(quán)重、偏置
weights = {
'in': tf.Variable(tf.random_normal([input_size, rnn_unit])),
'out': tf.Variable(tf.random_normal([rnn_unit,1]))
}
biases = {
'in': tf.Variable(tf.constant(0.1,shape=[rnn_unit, ])),
'out': tf.Variable(tf.constant(0.1,shape=[1, ]))
}
# ——————————————————定義神經(jīng)網(wǎng)絡(luò)變量——————————————————
deflstm(X):
batch_size = tf.shape(X)[0]
time_step = tf.shape(X)[1]
w_in = weights['in']
b_in = biases['in']
input = tf.reshape(X, [-1, input_size])# 需要將tensor轉(zhuǎn)成2維進(jìn)行計(jì)算,計(jì)算后的結(jié)果作為隱藏層的輸入
input_rnn = tf.matmul(input, w_in) + b_in
input_rnn = tf.reshape(input_rnn, [-1, time_step, rnn_unit])# 將tensor轉(zhuǎn)成3維牵辣,作為lstm cell的輸入
cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_unit)
init_state = cell.zero_state(batch_size,dtype=tf.float32)
output_rnn, final_states = tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state,dtype=tf.float32)
output = tf.reshape(output_rnn, [-1, rnn_unit])
w_out = weights['out']
b_out = biases['out']
pred = tf.matmul(output, w_out) + b_out
returnpred, final_states
# ————————————————訓(xùn)練模型————————————————————
deftrain_lstm(batch_size=10, time_step=20, train_begin=2000, train_end=5800):
X = tf.placeholder(tf.float32,shape=[None, time_step, input_size])
Y = tf.placeholder(tf.float32,shape=[None, time_step, output_size])
batch_index, train_x, train_y = get_train_data(batch_size, time_step, train_begin, train_end)
withtf.variable_scope("sec_lstm"):
pred, _ = lstm(X)
loss = tf.reduce_mean(tf.square(tf.reshape(pred, [-1]) - tf.reshape(Y, [-1])))
train_op = tf.train.AdamOptimizer(lr).minimize(loss)
saver = tf.train.Saver(tf.global_variables(),max_to_keep=15)
withtf.Session()assess:
sess.run(tf.global_variables_initializer())
foriinrange(100000):# 這個(gè)迭代次數(shù),可以更改违施,越大預(yù)測(cè)效果會(huì)更好谋竖,但需要更長(zhǎng)時(shí)間
forstepinrange(len(batch_index) -1):
_, loss_ = sess.run([train_op, loss],feed_dict={X: train_x[batch_index[step]:batch_index[step +1]],
Y: train_y[batch_index[step]:batch_index[step +1]]})
print("Number of iterations:", i," loss:", loss_)
#print("model_save: ", saver.save(sess, 'model_save2/modle.ckpt'))
saver.save(sess,"save/model.ckpt")
# 我是在window下跑的,這個(gè)地址是存放模型的地方遭庶,模型參數(shù)文件名為modle.ckpt
# 在Linux下面用 'model_save2/modle.ckpt'
print("The train has finished")
train_lstm()
# ————————————————預(yù)測(cè)模型————————————————————
defprediction(time_step=20):
X = tf.placeholder(tf.float32,shape=[None, time_step, input_size])
mean, std, test_x, test_y = get_test_data(time_step)
withtf.variable_scope("sec_lstm",reuse=True):
pred, _ = lstm(X)
saver = tf.train.Saver(tf.global_variables())
withtf.Session()assess:
# 參數(shù)恢復(fù)
module_file = tf.train.latest_checkpoint('save/')
#saver.restore(sess, module_file)
print("1111111",module_file)
saver.restore(sess, module_file)
test_predict = []
forstepinrange(len(test_x) -1):
prob = sess.run(pred,feed_dict={X: [test_x[step]]})
predict = prob.reshape((-1))
test_predict.extend(predict)
test_y = np.array(test_y) * std[7] + mean[7]
test_predict = np.array(test_predict) * std[7] + mean[7]
acc = np.average(np.abs(test_predict - test_y[:len(test_predict)]) / test_y[:len(test_predict)])# 偏差程度
print("The accuracy of this predict:", acc)
# 以折線圖表示結(jié)果
plt.figure()
plt.plot(list(range(len(test_predict))), test_predict,color='b', )
plt.plot(list(range(len(test_y))), test_y,color='r')
plt.show()
prediction()
原文參考鏈接:https://www.2cto.com/kf/201702/604190.html
###########預(yù)測(cè)結(jié)果###########