我使用一个自定义的预定义函数trainDNN运行RNN和LSTM模型import tensorflow as tf
from tensorflow.contrib.layers import fully_connected
import h5py
import time
from sklearn.utils import shuffle
def trainDNN(path, n_days, n_features, n_neurons,
train_sequences, train_lengths, train_y,
test_sequences, test_y, test_lengths,
lstm=False, n_epochs=50, batch_size=256,
learning_rate=0.0003, TRAIN_REC=8, TEST_REC=8):
# we're doing binary classification
n_outputs = 2
# this is the initial learning rate
# adam optimzer decays the learning rate automatically
# learning_rate = 0.0001
#learning rate decay is determined by epsilon
epsilon = 0.001
# setup the graph
tf.reset_default_graph()
# inputs to the network
X = tf.placeholder(tf.float32, [None, n_days, n_features])
y = tf.placeholder(tf.int32, [None])
seq_length = tf.placeholder(tf.int32, [None])
# the network itself
cell = tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons) if lstm else tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32, sequence_length=seq_length)
logits = fully_connected(states[-1] if lstm else states, n_outputs)