我对 TensorFlow 很陌生,过去几周我一直在学习,但这对我来说是第一个 RNN。
我的意图是预测一个非常简单的函数的下一个值,在这种情况下是一行。
我预计损失会很快降至 0,并且预测会变得准确。但是,无论如何,损失似乎没有意义,它基本上跟随任何数据波动(在这种情况下,一条线,它向上和向上)。
我正在发布整个代码(尽管它是简短和基本的),因为我认为我要么错过了一些非常微不足道的东西,要么我在这里误解了一些关键概念。
import numpy as np
import tensorflow as tf
num_steps = 6
state_size = 4
batch_size = 5
data_width = 600
def get_next_batch():
def f(x): return x*2.867
for k in range(0,data_width,num_steps):
rx,ry = np.zeros([batch_size,num_steps]),np.zeros([batch_size,num_steps])
for t in range(batch_size):
rx[t] = [(k+i)+(t*data_width) for i in range(num_steps)]
ry[t] = [f(h) for h in rx[t]]
yield rx,ry
X = tf.placeholder(tf.float32,[batch_size,num_steps])
Y_= tf.placeholder(tf.float32,[batch_size,num_steps])
rnn_inputs = tf.unstack(tf.reshape(X,[batch_size,-1,1]),axis=1)
cell = tf.contrib.rnn.BasicRNNCell(state_size)
init_state = tf.zeros([batch_size,state_size])
rnn_outputs,final_state = tf.contrib.rnn.static_rnn(cell,rnn_inputs,initial_state=init_state)
W = tf.Variable(tf.truncated_normal([state_size,1],stddev=0.1))
b = tf.Variable(tf.zeros(1))
Y = [tf.matmul(p,W)+b for p in rnn_outputs]
loss = tf.losses.mean_squared_error(labels=tf.unstack(tf.reshape(Y_,[batch_size,-1,1]),axis=1),predictions=Y)
train_step = tf.train.AdagradOptimizer(0.3).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
f = np.zeros([batch_size,state_size])
for batch_X,batch_Y in get_next_batch():
e,f,_ = sess.run([loss,final_state,train_step],feed_dict = { X:batch_X,Y_:batch_Y,init_state:f })
print(e)