2016-08-22 102 views
0

当我运行下面简单的代码,我得到这个错误:Tensorflow MomentumOptimizer问题

tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_5/Momentum 

该代码可以使用GradientDescentOptimizer,但我有错误与MomentumOptimizer。请指导我解决它。

这里是我的代码:

import tensorflow as tf 
import numpy as np 
import scipy.io as sio 
import h5py 
from tensorflow.python.training import queue_runner 

maxiter = 200000 
display = 1 
sess = tf.InteractiveSession() 
decay_rate = 0.00005 
starter_learning_rate = 0.000009 
alpha = 0.00005 
init_momentum = 0.9 

nnodes1 = 350 
nnodes2 = 100 
batch_size = 50 

train_mat = h5py.File('Basket_train_data_binary.mat') 
test_mat = h5py.File('Basket_test_data_binary.mat') 

train_mat = train_mat["binary_train"].value 
test_mat = test_mat["binary_test"].value 

Train = np.transpose(train_mat) 
Test = np.transpose(test_mat) 

# import the data                                 
#from tensorflow.examples.tutorials.mnist import input_data                      
# placeholders, which are the training data                          
x = tf.placeholder(tf.float32, shape=[None,43]) 
y_ = tf.placeholder(tf.float32, shape=[None]) 

# define the variables                                                                   
W1 = tf.Variable(tf.zeros([43,nnodes1])) 

b1 = tf.Variable(tf.zeros([nnodes1])) 

W2 = tf.Variable(tf.zeros([nnodes1,nnodes2])) 
b2 = tf.Variable(tf.zeros([nnodes2])) 

W3 = tf.Variable(tf.zeros([nnodes2,1])) 
b3 = tf.Variable(tf.zeros([1])) 

# Passing global_step to minimize() will increment it at each step.                    
global_step = tf.Variable(0, trainable=False) 
momentum = tf.Variable(init_momentum, trainable=False) 


# initilize the variables                              
sess.run(tf.initialize_all_variables()) 

# prediction function (just one layer)                           

layer1 = tf.nn.sigmoid(tf.matmul(x,W1) + b1) 
layer2 = tf.nn.sigmoid(tf.matmul(layer1,W2) + b2) 
y = tf.matmul(layer2,W3) + b3 

# cost function                                 
cost_function = tf.reduce_sum(tf.square(y_ - y)) 

l2regularization = tf.reduce_sum(tf.square(W1)) + tf.reduce_sum(tf.square(b1)) + tf.reduce_sum(tf.square(W2)) + tf.reduce_sum(tf.square(b2)) + tf.reduce_sum(tf.square(W3)) + tf.reduce_sum(tf.square(b3)) 
loss = cost_function + alpha*l2regularization 

# define the learning_rate and its decaying procedure.                       
learning_rate = tf.train.exponential_decay(starter_learning_rate,  global_step,10000, decay_rate, staircase=True) 
# define the training paramters and model, gradient model and feeding the function                
#train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) 
train_step = tf.train.MomentumOptimizer(learning_rate,0.9).minimize(loss, global_step=global_step)            


# evaluation                                  
# it returns 1, if both y and y_ are equal.                          
correct_prediction = tf.reduce_sum(tf.square(y_ - y)) 

# calculate the accuracy                               
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 


# Train the Model for 1000 times. by defining the batch number we  determine that it is sgd              
for i in range(maxiter): 
    batch = np.random.randint(0,len(Train),size=batch_size) 
    train_step.run(feed_dict={x:Train[batch,0:43], y_:Train[batch,43]}) 
    if np.mod(i,display) == 0: 
    # print tset loss                                
    print "Test", accuracy.eval(feed_dict={x: Test[:,0:43], y_: Test[:,43]}) 
    # print training loss                               
    print "Train" , sess.run(cost_function,feed_dict={x:  Train[:,0:43], y_: Train[:,43]}) 

请指导我怎样才能解决这个问题。 由于提前, 阿夫欣

+0

凡在代码你创建'Optimizer'和定义'train_step'后? – mrry

+0

@mrry我加了train_step,它在复制过程中错过了。 –

+0

您应该在整个图形定义之后初始化变量,特别是在创建MomentumOptimizer之后。 –

回答

1

在生产线

# initilize the variables                              
sess.run(tf.initialize_all_variables()) 

你该行之前初始化宣布的所有变量。

您在该行后面声明了优化程序(以及其他变量),因此优化程序使用的变量不受初始化的影响。

在完成图形声明之后移动初始化(例如:在声明每个变量和op之后)以进行修复。

TL; DR: 移动

# initilize the variables                              
sess.run(tf.initialize_all_variables()) 

# calculate the accuracy                               
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))