添加了Stock-Prediction-Models项目的多个文件,包括数据集、模型代码、README文档和CSS样式文件。这些文件用于股票预测模型的训练和展示,涵盖了LSTM、GRU等深度学习模型的应用。
19 lines
1.2 KiB
Python
19 lines
1.2 KiB
Python
import tensorflow as tf
|
|
import numpy as np
|
|
|
|
class Model:
|
|
def __init__(self, learning_rate, num_layers, size, size_layer, output_size, forget_bias = 0.1):
|
|
|
|
def lstm_cell(size_layer):
|
|
return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)
|
|
rnn_cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell(size_layer) for _ in range(num_layers)], state_is_tuple = False)
|
|
self.X = tf.placeholder(tf.float32, (None, None, size))
|
|
self.Y = tf.placeholder(tf.float32, (None, output_size))
|
|
drop = tf.contrib.rnn.DropoutWrapper(rnn_cells, output_keep_prob = forget_bias)
|
|
self.hidden_layer = tf.placeholder(tf.float32, (None, num_layers * 2 * size_layer))
|
|
self.outputs, self.last_state = tf.nn.dynamic_rnn(drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32)
|
|
rnn_W = tf.Variable(tf.random_normal((size_layer, output_size)))
|
|
rnn_B = tf.Variable(tf.random_normal([output_size]))
|
|
self.logits = tf.matmul(self.outputs[-1], rnn_W) + rnn_B
|
|
self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))
|
|
self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost) |