我尝试使用此代码段将其训练为使用替代1和0的时间序列:
error = tf.reduce_mean((labels - predict)**2)
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(error)
time_series = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
lbsx = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
for i in range(number_of_epochs):
for j in range(len(time_series)):
curr_X = time_series[j]
lbs = lbsx[j]
sess.run(train, feed_dict={X: [[curr_X]], labels: [[lbs]]})
不幸的是,我无法通过它做出正确的预测。我没有使用过tf.contrib rnn,而是自己创造了一个。
编辑:根据要求,这是我的模型实施:
# hyper parameters
learning_rate = 0.001
number_of_epochs = 10000
# Computation Graph
W1 = tf.Variable(tf.truncated_normal(shape=[1, 4]), dtype=tf.float32, name = 'W1')
W2 = tf.Variable(tf.truncated_normal(shape=[4, 1]), dtype=tf.float32, name = 'W2')
WR = tf.Variable(tf.truncated_normal(shape=[1, 4]), dtype=tf.float32, name = 'WR')
prev_val = tf.Variable([[0.0]], trainable=False)
X = tf.placeholder(tf.float32, [None, None], name = 'X')
labels = tf.placeholder(tf.float32, [None, 1], name = 'labels')
sess = tf.Session()
sess.run(tf.initialize_all_variables())
z = tf.matmul(X, W1) + tf.matmul(prev_val, WR)
prev_val = z
predict = tf.matmul(z, W2)