2016-12-13 10 views
0

私はテンソルフローでMLPを学習しています。テンソルフローinitialize_all_variables()場所

それの機能は、ReadFile関数、パラメータ、電車、テストを構築し、AOC

import tensorflow as tf 

##trn.txt start 
num_lines_trn = sum(1 for line in open('./trn.txt')) 
bat_x=[] 
bat_y=[] 
import os 
statinfo = os.stat("./trn.txt") 
f = open("./trn.txt", 'r') 
for i in range(num_lines_trn): 
    temp_x=[] 
    input_iter=0; 
    line = f.readline() 
    line = line.strip() 
    for number in line.split(): 
     if (input_iter==13): 
      bat_x.insert(i,temp_x) 
      #bat_y[i][float(number)]=1 
      if(float(number)==0): 
       bat_y.insert(i,[1,0]) 
      else: 
       bat_y.insert(i,[0,1]) 
     else : 
      temp_x.insert(input_iter,float(number)) 
      input_iter=input_iter+1 
f.close() 
##trn.txt end 

##tst.txt start 
num_lines_tst = sum(1 for line in open('./tst.txt')) 
tst_x=[] 
tst_y=[] 
import os 
statinfo = os.stat("./tst.txt") 
f = open("./tst.txt", 'r') 
for i in range(num_lines_tst): 
    temp_x=[] 
    input_iter=0; 
    line = f.readline() 
    line = line.strip() 
    for number in line.split(): 
     if (input_iter==13): 
      tst_x.insert(i,temp_x) 
      if(float(number)==0): 
       tst_y.insert(i,[1,0]) 
      else: 
       tst_y.insert(i,[0,1]) 
     else : 
      temp_x.insert(input_iter,float(number)) 
      input_iter=input_iter+1 
f.close() 
##tst.txt end 

# Parameters 
learning_rate = 0.001 
training_epochs = 1 
batch_size = 100 
display_step = 1 

n_hidden_1 = 512 # 1st layer number of features 
n_hidden_2 = 512 # 2nd layer number of features 
n_input=13 
n_classes=2 
x = tf.placeholder("float", [None, n_input]) 
y = tf.placeholder("float", [None, n_classes]) 
weights = { 
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes])) 
} 
biases = { 
    'b1': tf.Variable(tf.random_normal([n_hidden_1])), 
    'b2': tf.Variable(tf.random_normal([n_hidden_2])), 
    'out': tf.Variable(tf.random_normal([n_classes])) 
} 
# Construct model 
pred = multilayer_perceptron(x, weights, biases) 

# Define loss and optimizer 
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) 
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 

# Initializing the variables 

label_tensor=tf.Variable(tst_y, name="label") 
init = tf.initialize_all_variables() 
num_lines_tst = sum(1 for line in open('./tst.txt')) 

with tf.Session() as sess: 
    sess.run(init) 

    # Training cycle 
    for epoch in range(training_epochs): 
     avg_cost = 0. 
     total_batch = int(num_lines_trn/batch_size) 
     # Loop over all batches 
     for i in range(total_batch): 
      batch_x, batch_y = bat_x[i*batch_size:(i+1)*batch_size],bat_y[i*batch_size:(i+1)*batch_size]#mnist.train.next_batch(batch_size) 
      # Run optimization op (backprop) and cost op (to get loss value) 
      _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, 
                  y: batch_y}) 
      # Compute average loss 
      avg_cost += c/total_batch 
     # Display logs per epoch step 
     if epoch % display_step == 0: 
      print ("Epoch:", '%04d' % (epoch+1), "cost=", \ 
       "{:.9f}".format(avg_cost)) 

    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
    pred_array=sess.run(pred, feed_dict={x: tst_x}) 
    pred_tensor=tf.Variable(pred_array, name="pred") 
    print(sess.run(accuracy, feed_dict={x: tst_x, y: tst_y})) 
    auc = tf.contrib.metrics.streaming_auc(pred_tensor,label_tensor) 
    init2 = tf.initialize_local_variables() 
    sess.run(init2) 
    train_auc = sess.run(auc) 
    #print(train_auc) 
    #recall,update_op = tf.contrib.metrics.streaming_recall(pred_tensor,label_tensor) 
    #print(sess.run(recall)) 

を作成しているが、この場所は、私はこのエラーを回避するにはどうすればよいのエラー

train_auc = sess.run(auc) 

FailedPreconditionError: Attempting to use uninitialized value pred_49

のでしょうか?

+1

私たちのコード – BlueMoon93

+0

の実行中の例を与える、私は完全なコードに簡単なコードを編集した –

+0

入力ファイルの1行は13のパラメータと1つのクラスデータです –

答えて

1

コードにはファイルが必要なため、実行することはできません。ここでAUC実行する方法の小さな例です。

import tensorflow as tf 
import numpy as np 

# Data examples 
tst_y = [1, 1, 0] 
tst_x = [[1, 1], [1, 0], [0, 0]] 

# Simple linear neural network (Ax+b=y) 
x = tf.placeholder('float32', [None, 2], name='state_tensor') 
m = tf.get_variable('Matrix', [x.get_shape().as_list()[1], 1], tf.float32, tf.contrib.layers.xavier_initializer(uniform=False)) 
b = tf.get_variable('bias', [1], initializer=tf.contrib.layers.xavier_initializer(uniform=False)) 
pred = tf.nn.bias_add(tf.matmul(x, m), b) 

# The ops to calculate AUC 
label_tensor = tf.Variable(tst_y, name="label") 
predictions2 = tf.placeholder('float32', [None, 1], name='predictions2') 
auc, update_op = tf.contrib.metrics.streaming_auc(predictions2, label_tensor) 

# In a session 
with tf.Session() as sess: 
    # Initialize all and local variables 
    tf.initialize_all_variables().run() 
    tf.initialize_local_variables().run() 

    # Run the prediction and AUC ops 
    nn_predictions = sess.run(pred, feed_dict={x: tst_x}) 
    train_auc = sess.run(update_op, feed_dict={predictions2: nn_predictions}) 

    # Tensor and current value 
    print(auc, train_auc) 

があなたのコードにこの例を適応し、あなたの結果を報告

+0

返信ありがとう –