2017-08-11 54 views
0

我得到這個錯誤:試圖使用未初始化值RNN/output_projection_wrapper /偏置

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value rnn/output_projection_wrapper/bias 
     [[Node: rnn/output_projection_wrapper/bias/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](rnn/output_projection_wrapper/bias)]] 

這是我的代碼:

n_steps = 20 
n_inputs = 1 
n_neurons = 100 
n_outputs = 1 

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) 
y = tf.placeholder(tf.float32, [None, n_steps, n_outputs]) 

cell = tf.contrib.rnn.OutputProjectionWrapper(
    tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.relu), 
    output_size=n_outputs) 


outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) 



learning_rate = 0.001 

loss = tf.reduce_mean(tf.square(outputs - y)) # MSE 
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 
training_op = optimizer.minimize(loss) 

init = tf.global_variables_initializer() 

saver = tf.train.Saver() 


n_iterations = 1500 
batch_size = 50 

with tf.Session() as sess: 
    init.run() 
    for iteration in range(n_iterations): 
     X_batch, y_batch = next_batch(batch_size, n_steps) 
     sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) 
     if iteration % 100 == 0: 
      mse = loss.eval(feed_dict={X: X_batch, y: y_batch}) 
      print(iteration, "\tMSE:", mse) 

saver.save(sess, "./my_time_series_model") # not shown in the book 

with tf.Session() as sess: 
    X_new = time_series(np.array(t_instance[:-1].reshape(-1, n_steps, n_inputs))) 
    y_pred = sess.run(outputs, feed_dict={X: X_new}) 

我該如何解決這個問題?

回答

1

這裏,第二個會話出現問題,因爲您沒有使用該會話初始化變量。因此,最好只爲一個圖定義一個會話(因爲重新初始化會覆蓋已訓練的變量)。

sess_config = tf.ConfigProto(allow_soft_placement=True, 
            log_device_placement=True) 
sess = tf.Session(config=sess_config) 
sess.run(init) 
# use this session for all computations 
for iteration in range(n_iterations): 
    X_batch, y_batch = next_batch(batch_size, n_steps) 
    sess.run(training_op, feed_dict={X: X_batch, y: y_batch}) 
    if iteration % 100 == 0: 
     mse = loss.eval(feed_dict={X: X_batch, y: y_batch}) 
     print(iteration, "\tMSE:", mse) 

saver.save(sess, "./my_time_series_model") # not shown in the book 

X_new = time_series(np.array(t_instance[:-1].reshape(-1, n_steps, n_inputs))) 
y_pred = sess.run(outputs, feed_dict={X: X_new}) 
+0

我仍然收到相同的錯誤。 –

+0

看到我的編輯,我添加了更多細節 –