2016-08-22 8 views
0

は、私はこのエラーを取得します。それを解決するために私を案内してください。ここでTensorflow 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]}) 

は、私は、この問題を解決することができますどのように私を導いてください。あなたはすべての変数を初期化している事前に おかげで、ライン

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

で アフシン・

+0

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

? – mrry

+0

@mrry train_stepを追加しましたが、コピー中に逃しました。 –

+0

特にMomentumOptimizerの作成後に、グラフ定義全体の後に変数を初期化する必要があります。 –

答えて

1

はその行の前を宣言しました。

オプティマイザ(および他の変数)は、その行の後に宣言しているため、オプティマイザによって使用される変数は初期化の影響を受けません。

完全なグラフ宣言の後に初期化を移動します(たとえば、すべての変数とopの宣言の後)。

TLは、DR: 動くコードにあなたが `Optimizer`を作成し、` train_step`を定義します

# calculate the accuracy                               
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
関連する問題