我有1212个样本的数据集,每个值都是股票价格。我创建了RNN模型来预测它们,但是当我生成预测和原始图时,我看到该预测图看起来很奇怪,所以我在这里。而且我不知道如何做出更像原始情节的预测
配置:
n_inputs=1
n_outputs=1
n_neurons=48,
n_iterations=1212
n_time_steps=12
learning_rate=.03
LSTM:
lstm_cell = tf.contrib.rnn.OutputProjectionWrapper(tf.contrib.rnn.BasicLSTMCell(num_units=self.n_neurons, activation=tf.nn.sigmoid), output_size=self.n_outputs)
outputs, sates = tf.nn.dynamic_rnn(lstm_cell, self.x, dtype=tf.float32)
损失和优化器:
loss = tf.reduce_mean(tf.square(outputs - self.y))
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss)
批处理系统:
def next_batch(self, data):
rand_start = np.random.randint(len(data) - self.n_time_steps)
y_batch = np.array(data[rand_start:rand_start + self.n_time_steps + 1]).reshape(1, self.n_time_steps + 1)
return y_batch[: ,:-1].reshape(-1, self.n_time_steps, 1), y_batch[:, 1:].reshape(-1, self.n_time_steps, 1)
培训:
for i in range(self.n_iterations):
x_batch, y_batch = self.next_batch(data)
sess.run(optimizer, feed_dict={
self.x:x_batch,
self.y:y_batch
})
这是我预测值的方式-恢复保存的模型
graph = tf.get_default_graph()
x = graph.get_tensor_by_name('x_input:0')
outputs_tensor = graph.get_tensor_by_name('outputs:0')
n_time_steps = graph.get_tensor_by_name('n_time_steps:0')
n_time_steps = sess.run(n_time_steps)
data = data.reshape(-1, n_time_steps, 1)
outputs_values = sess.run(outputs_tensor, feed_dict={
x: data
})
在其他文件中:
selected = list(data[-48:])
n_time_steps = 48
for _ in range(48):
part = selected[-n_time_steps:]
val = predictor.predict(part)
selected.append(val[0, -1, 0])